int*_*dro 184
StackI希望这有助于:
NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
// Example: 1 UIKit 0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]];
[array removeObject:@""];
NSLog(@"Stack = %@", [array objectAtIndex:0]);
NSLog(@"Framework = %@", [array objectAtIndex:1]);
NSLog(@"Memory address = %@", [array objectAtIndex:2]);
NSLog(@"Class caller = %@", [array objectAtIndex:3]);
NSLog(@"Function caller = %@", [array objectAtIndex:4]);
Run Code Online (Sandbox Code Playgroud)
bbu*_*bum 50
在完全优化的代码中,没有100%可靠的方法来确定某个方法的调用者.编译器可以使用尾调用优化,而编译器有效地重新使用调用者的堆栈帧用于被调用者.
要查看此示例,请使用gdb在任何给定方法上设置断点,然后查看回溯.请注意,在每次方法调用之前都没有看到objc_msgSend().这是因为objc_msgSend()对每个方法的实现进行尾调用.
虽然您可以编译未优化的应用程序,但您需要所有系统库的非优化版本才能避免这一问题.
这只是一个问题; 实际上,你问"我如何重新发明CrashTracer或gdb?".一个非常难以解决的职业问题.除非你想要"调试工具"成为你的职业,否则我建议不要走这条路.
你真的想回答什么问题?
Gun*_*nds 11
使用intropedro提供的答案,我想出了这个:
#define CALL_ORIGIN NSLog(@"Origin: [%@]", [[[[NSThread callStackSymbols] objectAtIndex:1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]] objectAtIndex:1])
Run Code Online (Sandbox Code Playgroud)
这将简单地返回原始类和功能:
2014-02-04 16:49:25.384 testApp[29042:70b] Origin: [LCallView addDataToMapView]
Run Code Online (Sandbox Code Playgroud)
ps - 如果使用performSelector调用函数,结果将是:
Origin: [NSObject performSelector:withObject:]
Run Code Online (Sandbox Code Playgroud)
刚写了一个方法来为你做这个:
- (NSString *)getCallerStackSymbol {
NSString *callerStackSymbol = @"Could not track caller stack symbol";
NSArray *stackSymbols = [NSThread callStackSymbols];
if(stackSymbols.count >= 2) {
callerStackSymbol = [stackSymbols objectAtIndex:2];
if(callerStackSymbol) {
NSMutableArray *callerStackSymbolDetailsArr = [[NSMutableArray alloc] initWithArray:[callerStackSymbol componentsSeparatedByString:@" "]];
NSUInteger callerStackSymbolIndex = callerStackSymbolDetailsArr.count - 3;
if (callerStackSymbolDetailsArr.count > callerStackSymbolIndex && [callerStackSymbolDetailsArr objectAtIndex:callerStackSymbolIndex]) {
callerStackSymbol = [callerStackSymbolDetailsArr objectAtIndex:callerStackSymbolIndex];
callerStackSymbol = [callerStackSymbol stringByReplacingOccurrencesOfString:@"]" withString:@""];
}
}
}
return callerStackSymbol;
}
Run Code Online (Sandbox Code Playgroud)
Swift 2.0版@Intropedro的答案供参考;
let sourceString: String = NSThread.callStackSymbols()[1]
let separatorSet :NSCharacterSet = NSCharacterSet(charactersInString: " -[]+?.,")
let array = NSMutableArray(array: sourceString.componentsSeparatedByCharactersInSet(separatorSet))
array.removeObject("")
print("Stack: \(array[0])")
print("Framework:\(array[1])")
print("Memory Address:\(array[2])")
print("Class Caller:\(array[3])")
print("Method Caller:\(array[4])")
Run Code Online (Sandbox Code Playgroud)
如果是为了补偿缘故,那就养成放弃的习惯 NSLog(@"%s", __FUNCTION__);
作为类中每个方法的第一行.然后,您始终可以通过查看调试器来了解方法调用的顺序.
归档时间: |
|
查看次数: |
32555 次 |
最近记录: |