iOS 获取调用栈信息

参考文献:https://www.jianshu.com/p/a027fdfd6e4f

有两种方法:

1. void* callstack[128];
    int frames = backtrace(callstack, 128);
    char **strs = backtrace_symbols(callstack, frames);

2.NSArray *csss = [NSThread callStackSymbols];

 

import "WTRunloopVC.h"
#import "WTRunloopTest.h"
#include <libkern/OSAtomic.h>
#include <execinfo.h>

@interface WTRunloopVC () 

@end

@implementation WTRunloopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self performSelector:@selector(testVC) onThread:[WTRunloopTest shareThread] withObject:nil waitUntilDone:NO];
    
}

-(void)testVC {
    NSLog(@"%@ - %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    
    //方法1:方法调用的堆栈信息
    void* callstack[128];
    int frames = backtrace(callstack, 128);
    char **strs = backtrace_symbols(callstack, frames);
    int i;
    NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
    for (i = 0;i < 4;i++){
        [backtrace addObject:[NSString stringWithUTF8String:strs[i]]];
    }
    free(strs);
    NSLog(@"=====>>>>>堆栈<<<<<=====\n%@",backtrace);

    //方法2:方法调用的堆栈信息
    NSArray *csss = [NSThread callStackSymbols];
    NSLog(@"=====>>>>>堆栈<<<<<=====\n%@",csss);
}

打印结果:

//方法1.打印的堆栈信息
(
    "0   TotalDemo                           0x00000001063254bc -[WTRunloopVC testVC] + 188",
    "1   Foundation                          0x000000010748f7bc __NSThreadPerformPerform + 331",
    "2   CoreFoundation                      0x0000000108828db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17",
    "3   CoreFoundation                      0x0000000108828633 __CFRunLoopDoSources0 + 243"
)
//方法2.打印的堆栈信息
Printing description of csss:
<_NSCallStackArray 0x600003b492c0>(
0   TotalDemo                           0x00000001063255f2 -[WTRunloopVC testVC] + 498,
1   Foundation                          0x000000010748f7bc __NSThreadPerformPerform + 331,
2   CoreFoundation                      0x0000000108828db1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17,
3   CoreFoundation                      0x0000000108828633 __CFRunLoopDoSources0 + 243,
4   CoreFoundation                      0x0000000108822cef __CFRunLoopRun + 1231,
5   CoreFoundation                      0x00000001088224d2 CFRunLoopRunSpecific + 626,
6   Foundation                          0x0000000107479f88 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 277,
7   TotalDemo                           0x000000010635ea27 +[WTRunloopTest testShareThread] + 375,
8   Foundation                          0x000000010748f24b __NSThread__start__ + 1197,
9   libsystem_pthread.dylib             0x000000010a9522eb _pthread_body + 126,
10  libsystem_pthread.dylib             0x000000010a955249 _pthread_start + 66,
11  libsystem_pthread.dylib             0x000000010a95140d thread_start + 13
)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值