Skip to content

Commit

Permalink
增加 OCRunnerFrameStackHistory 全局函数,用于在DEBUG模式下查看调用栈
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFruity committed Dec 2, 2020
1 parent 21fa12a commit 4cf195e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions OCRunner/RunnerClasses+Execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#import <ORPatchFile/ORPatchFile.h>
//#import <oc2mangoLib/oc2mangoLib.h>
#import <objc/runtime.h>
#if DEBUG
extern NSString *OCRunnerFrameStackHistory(void);
void ORDebugFrameStackPush(id value, NSObject *node);
void ORDebugFrameStackPop(void);
#else
#define ORDebugFrameStackPop(value)
#define ORDebugFrameStackPush(value1, value2)
#endif

@class MFValue;
NS_ASSUME_NONNULL_BEGIN
@class MFScopeChain;
Expand Down
49 changes: 49 additions & 0 deletions OCRunner/RunnerClasses+Execute.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,51 @@
#import "ORCoreImp.h"
#import "ORSearchedFunction.h"

#if DEBUG
NSMutableArray *ORDebugMainFrameStack(void){
NSMutableDictionary *threadInfo = [[NSThread mainThread] threadDictionary];
return threadInfo[@"ORDebugStack"];
}
NSMutableArray *ORDebugFrameStack(void){
NSMutableDictionary *threadInfo = [[NSThread currentThread] threadDictionary];
NSMutableArray *stack = threadInfo[@"ORDebugStack"];
if (!stack) {
stack = [NSMutableArray array];
threadInfo[@"ORDebugStack"] = stack;
}
return stack;
}
void ORDebugFrameStackPush(MFValue *value, NSObject *node){
if (value) {
[ORDebugFrameStack() addObject:@[value, node]];
}else{
[ORDebugFrameStack() addObject:@[node]];
}
}
void ORDebugFrameStackPop(void){
[ORDebugFrameStack() removeLastObject];
}
NSString *OCRunnerFrameStackHistory(void){
NSMutableArray *frames = ORDebugMainFrameStack();
NSMutableString *log = [@"OCRunner Frames:\n\n" mutableCopy];
for (NSArray *frame in frames) {
if (frame.count == 2) {
MFValue *target = frame.firstObject;
ORMethodImplementation *imp = frame.lastObject;
[log appendFormat:@"%@ %@ %@\n", imp.declare.isClassMethod ? @"+" : @"-", target.objectValue, imp.declare.selectorName];
}else{
ORFunctionImp *imp = frame.firstObject;
if (imp.declare.funVar.varname == nil){
[log appendString:@" Block Call \n"];
}else{
[log appendFormat:@" CFunction: %@\n", imp.declare.funVar.varname];
}
}
}
return log;
}
#endif

static MFValue * invoke_MFBlockValue(MFValue *blockValue, NSArray *args){
const char *blockTypeEncoding = [MFBlock typeEncodingForBlock:blockValue.objectValue];
NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:blockTypeEncoding];
Expand Down Expand Up @@ -598,8 +643,10 @@ - ( MFValue *)execute:(MFScopeChain *)scope{
[self.declare execute:current];
}
}
ORDebugFrameStackPush(nil, self);
MFValue *value = [self.scopeImp execute:current];
value.resultType = MFStatementResultTypeNormal;
ORDebugFrameStackPop();
return value;
}
@end
Expand Down Expand Up @@ -1209,9 +1256,11 @@ - (nullable MFValue *)execute:(MFScopeChain *)scope {
@implementation ORMethodImplementation(Execute)
- (nullable MFValue *)execute:(MFScopeChain *)scope {
MFScopeChain *current = [MFScopeChain scopeChainWithNext:scope];
ORDebugFrameStackPush(scope.instance, self);
[self.declare execute:current];
MFValue *result = [self.scopeImp execute:current];
result.resultType = MFStatementResultTypeNormal;
ORDebugFrameStackPop();
return result;
}
@end
Expand Down

0 comments on commit 4cf195e

Please sign in to comment.