当app出现崩溃, 研发阶段一般可以通过以下方式来跟踪crash信息
- 模拟器运行, 查看xcode错误日志
- 真机调试, 查看xcode错误日志
- 真机运行, 查看device系统日志
下面举例说明, 先写一段会Crash的代码crashdemo:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self performSelector:@selector(print) withObject:nil afterDelay:5];
}
- (void)print {
NSArray *array = @[];
NSLog(@"%@", array[1]);
}
Demo#1.模拟器运行, 查看xcode错误日志
程序执行后会立即崩溃, 打开xcode系统日志可以看到以下错误信息
2016-10-29 12:13:29.015 CrashDemo[37842:7436441] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'
*** First throw call stack:
(
CoreFoundation 0x00b7ba84 __exceptionPreprocess + 180
libobjc.A.dylib 0x00642e02 objc_exception_throw + 50
CoreFoundation 0x00b22390 __CFArrayGetTypeID_block_invoke + 0
CoreFoundation 0x00ac07f8 -[NSArray objectAtIndexedSubscript:] + 40
CrashDemo 0x000877b7 -[ViewController print] + 87
Foundation 0x00250d71 __NSFireDelayedPerform + 442
CoreFoundation 0x00acd576 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
CoreFoundation 0x00accf72 __CFRunLoopDoTimer + 1250
CoreFoundation 0x00a8b25a __CFRunLoopRun + 2202
CoreFoundation 0x00a8a706 CFRunLoopRunSpecific + 470
CoreFoundation 0x00a8a51b CFRunLoopRunInMode + 123
GraphicsServices 0x041e4664 GSEventRunModal + 192
GraphicsServices 0x041e44a1 GSEventRun + 104
UIKit 0x00f0c1eb UIApplicationMain + 160
CrashDemo 0x00087bba main + 138
libdyld.dylib 0x03189a21 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
通过xcode日志可以看到是数组访问越界, 发生越界的方式名为print
针对这个demo我们当然很清楚是刚才列的array[1]发生越界, 但对于一个完整的程序如何查看是在哪个地方发生越界的呢?
这个时候我们可以利用xcode的Show the breakpoint navigator功能, 点加号选择add exception breakpoint
这个时候我们在执行程序, xcode执行会自动停在要发生crash的代码段
Demo#2.真机调试, 查看xcode错误日志
如果有添加exeception point, 程序会自动停到打印array[1]那一行. 如果没有添加则程序会crash, xcode会出现以下错误日志
2016-10-29 12:15:53.561 CrashDemo[1062:316582] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'
*** First throw call stack:
(0x211b398b 0x2094ee17 0x211433e7 0xc5a3b 0x219d1ad5 0x211765ff 0x21176231 0x2117407d 0x210c32e9 0x210c30d5 0x226b3ac9 0x257880b9 0xc5c99 0x20d6b873)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
通过错误信息我们只能看到是有发生数组访问越界, 如果有添加exeception breakpoint则会自动停在发生error的代码行.
Demo#3. 真机运行, 查看device系统日志
xcode停止运行这个crashdemo, 选择xcode window - devices, 选择手机 - view device logs
然后在手机上运行crashdemo, 在device logs中按时间排序查看最新的log就能看到crashdemo的crash log
Incident Identifier: 9A4C52F0-B0D7-42C9-A7CB-D4D3321D00D5
CrashReporter Key: 90f4d3621773443794fa73f506fd6bdef49fc269
Hardware Model: iPhone4,1
Process: CrashDemo [1074]
Path: /private/var/containers/Bundle/Application/1307034E-9C2B-451F-ACD9-04C97DEC047B/CrashDemo.app/CrashDemo
Identifier: PEGA.CrashDemo
Version: 1 (1.0)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2016-10-29 12:21:49.49 +0800
Launch Time: 2016-10-29 12:21:43.43 +0800
OS Version: iOS 9.3.1 (13E238)
Report Version: 104
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Filtered syslog:
None found
Last Exception Backtrace:
CoreFoundation 0x211b3986 __exceptionPreprocess + 122
libobjc.A.dylib 0x2094ee12 objc_exception_throw + 34
CoreFoundation 0x211433e2 -[__NSArray0 objectAtIndex:] + 110
CrashDemo 0x000e6a36 0xe0000 + 27190
Foundation 0x219d1ad0 __NSFireDelayedPerform + 464
CoreFoundation 0x211765fa
这些在开发阶段都能很简便的实现, 但是当app发布出去后用户发生crash呢? 一般用户只能反馈在做什么的时候发生crash
然后我们在去做尝试是否能遇到, 不过这样效率不高而且一般很难复现到用户的crash
Bugly的出现解决的这个问题
Bugly SDK在当程序崩溃时, 会自动将错误信息发送到服务器方便开发人员查看分析
那么如何使用Bugly?
首先先到https://bugly.qq.com/v2/注册账号, 并注册app下载SDK包
将Bugly.framework拖拽到工程中, 记得勾选copy if needed.
然后添加libz.tbd / libstdc++.tbd / Security.framework / SystemConfiguration.framework到工程中
delegate.m中注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Bugly startWithAppId:@"此处替换为你的AppId"];
return YES;
}
这样当程序发生崩溃时, 崩溃信息会自动发送到服务器登录你的bugly账号就能查看到了