Xcode debug时如何看crash的call stack

debug的时候,会经常碰到crash之后xcode如下显示:

*** First throw call stack:

(0x330f188f 0x35096259 0x3303a9db 0x96801 0x8ebbb 0xa03d9 0x9c2e3 0x49f63 0x66a8f 0x687ad 0x3258e86f 0x3258e7c5 0x3317c001 0x3541160d 0x330baf13 0x330c5523 0x330c54c5 0x330c4313 0x330474a5 0x3304736d 0x33194439 0x35626cd5 0x883a3 0x21f0)

terminate called throwing an exception(lldb) 

像是说:我生气了,但是就不告诉你为什么.

网上找到一个好办法:

在AppDelegate.m中添加函数:

void uncaughtExceptionHandler(NSException*exception){
    NSLog(@"CRASH: %@", exception);
    NSLog(@"Stack Trace: %@",[exception callStackSymbols]);
    // Internal error reporting
}

这里会打印出异常时的call stack, 也是发生crash时候, 最有价值的信息.

然后在程序入口处向下面这样调用它:

-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{   
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
    // Normal launch stuff
}
NSSetUncaughtExceptionHandler 就是告诉系统, 当发生异常时, 使用这个函数作为回调.
这样之后, 发生crash时, 就可以看到打印出的call stack, 就可以很方便的定位到发生crash的那行代码.



原文地址:https://www.cnblogs.com/dqshll/p/2661933.html