首页 > 其他 > 详细

打印方法调用者的信息(方法反向追踪)

时间:2015-06-18 21:38:28      阅读:209      评论:0      收藏:0      [点我收藏+]

转:http://stackoverflow.com/questions/4046833/print-the-name-of-the-calling-function-to-the-debug-log

http://stackoverflow.com/questions/1451342/objective-c-find-caller-of-method

http://ios-blog.co.uk/tutorials/quick-tips/identify-calling-method-in-ios/

#include <execinfo.h>

void *addr[2];
int nframes = backtrace(addr, sizeof(addr)/sizeof(*addr));
if (nframes > 1) {
    char **syms = backtrace_symbols(addr, nframes);
    NSLog(@"%s: caller: %s", __func__, syms[1]);
    free(syms);
} else {
    NSLog(@"%s: *** Failed to generate backtrace.", __func__);
}

NSArray *syms = [NSThread  callStackSymbols]; 
if ([syms count] > 1) { 
    NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:1]);
} else {
     NSLog(@"<%@ %p> %@", [self class], self, NSStringFromSelector(_cmd)); 
}

 或

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]);
    NSLog(@"Line caller = %@", [array objectAtIndex:5]);

 



打印方法调用者的信息(方法反向追踪)

原文:http://www.cnblogs.com/ygm900/p/4586725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!