崩溃日志的产生
iOS中运行App过程中如果发生程序崩溃,会生成一个崩溃日志文件。这个文件会保存的特定系统目录下,扩展名是crash。你可以通过系统设置中的“通用-关于本机-诊断与用量-诊断与用量数据”来查看崩溃日志文件。
当手机连接到iTunes时,也会将该文件同步到电脑上。
-
在Mac系统中这些文件会同步到“~/Library/Logs/CrashReporter/MobileDevice”下。
-
在Windows系统中会同步到“C:UsersAppDataRoamingApple computerLogsCrashReporter/MobileDevice”(Vista或以上)或“C:Documents and SettingsApplication DataApple computerLogsCrashReporter”(XP)。
崩溃日志的发送
通过手机发送崩溃日志文本
打开系统设置中的“通用-关于本机-诊断与用量-诊断与用量数据”,你会看到一个按App包名排序的崩溃日志列表,找到相应App的崩溃日志并打开,复制崩溃日志内容发送给相应的开发者。开发者可以复制收到的日志文本保存为扩展名是crash的文本文件。
通过日志文件发送
当手机连接到iTunes时,会将崩溃日志文件同步到电脑上。在Mac系统中,可以到“~/Library/Logs/CrashReporter/MobileDevice”目录中找到该文件,而Windows系统在“C:UsersAppDataRoamingApple computerLogsCrashReporter/MobileDevice”(Vista或以上)或“C:Documents and SettingsApplication DataApple computerLogsCrashReporter”(XP)。可以将这些文件发送给开发者。
自动上传崩溃日志到iTC(iTunes Connect)
iOS中崩溃日志是可以上传的iTC的服务器,并由开发者查看的。用户可以通过系统设置中的“通用-关于本机-诊断与用量”来设定是否上传崩溃日志。同时开发者也可以通过捕获异常信号自己定制异常上报。
崩溃日志的格式
一般崩溃日志头部有如下字段
- Exception Type: EXC_BAD_ACCESS (SIGSEGV)
- Exception Codes: KERN_INVALID_ADDRESS at 0x20000008
- Crashed Thread: 0
其中Exception Type是异常类型,Exception Codes是异常代码。Crashed Thread指示异常的线程编号。上面表示Crash的线程编号是0(主线程的线程编号是0)。
看崩溃日志时一般要分两种情况,一种是内存、CPU的系统错误,如内存访问错误、除零异常等。另外一种是程序通过写代码抛出的异常,即代码中通过throw关键字抛出的异常。程序抛出的异常是可以捕获并处理的,如果没有捕获,就会Crash,并生成崩溃日志。这两种在日志中有一些细微的差别。
一般内存等系统异常Crash线程信息如下:
- Thread 0 name: Dispatch queue: com.apple.main-thread
- Thread 0 Crashed:
- 0 libobjc.A.dylib 0x3624cf78 objc_msgSend + 16
- 1 QQYanChu 0x0000d92a -[QYServerInterface connectionDidFinishLoading:] (QYServerInterface.m:328)
- 2 Foundation 0x30d03c22 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke_0 + 10
- 3 Foundation 0x30c5b6d2 -[NSURLConnectionInternalConnection invokeForDelegate:] + 22
- 4 Foundation 0x30c5b69c -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 192
- 5 Foundation 0x30c5b5be -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 54
- 6 CFNetwork 0x338077ee URLConnectionClient::_clientDidFinishLoading(URLConnectionClient::ClientConnectionEventQueue*) + 186
- 7 CFNetwork 0x337fc49e URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long) + 418
- 8 CFNetwork 0x337fc592 URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long) + 662
- 9 CFNetwork 0x337fc19c URLConnectionClient::processEvents() + 100
- 10 CFNetwork 0x337fc0d2 MultiplexerSource::perform() + 150
- 11 CoreFoundation 0x34dffacc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 8
- 12 CoreFoundation 0x34dff298 __CFRunLoopDoSources0 + 208
- 13 CoreFoundation 0x34dfe03e __CFRunLoopRun + 646
- 14 CoreFoundation 0x34d8149e CFRunLoopRunSpecific + 294
- 15 CoreFoundation 0x34d81366 CFRunLoopRunInMode + 98
- 16 GraphicsServices 0x3607d432 GSEventRunModal + 130
- 17 UIKit 0x31e77e76 UIApplicationMain + 1074
- 18 QQYanChu 0x0000feea main (main.m:39)
- 19 QQYanChu 0x00007ed4 start + 32
第一列是编号,表示堆栈中函数调用的嵌套顺序。最上面就是崩溃发生的函数,往下面看就可以看到我们的代码。第二列是堆栈中函数定义的位置,有的是在动态库或Framework中定义的,有的是我们的App定义的,如QQYanChu就是我们的App可执行文件,另外你还可以看到其他的动态库等,一般我们关注自己的QQYanChu的就行了。 如上面的例子就是在-[QYServerInterface connectionDidFinishLoading:] 这个调用中挂的。一般这个信息还会包含代码文件和说对应的行号,如上面红色部分就是说崩溃在QYServerInterface.m的328行。这就可以方便我们快速定位问题。
下面是程序抛出的异常的日志。
- Last Exception Backtrace:
- 0 CoreFoundation 0x3425929e __exceptionPreprocess + 158
- 1 libobjc.A.dylib 0x33a2b97a objc_exception_throw + 26
- 2 CoreFoundation 0x3425ce02 -[NSObject(NSObject) doesNotRecognizeSelector:] + 166
- 3 CoreFoundation 0x3425b52c ___forwarding___ + 388
- 4 CoreFoundation 0x341b2f64 _CF_forwarding_prep_0 + 20
- 5 QQYanChu 0x000ef372 -[QYUser staticDataFetcher:fetchedData:] (QYUser.m:168)
- 6 QQYanChu 0x001ee9f2 -[QStaticDataFetcher successWithData:] + 94
- 7 QQYanChu 0x001efb44 -[QStaticDataFetcher sendDelegateMessageWithLocalData] + 220
- 8 Foundation 0x39256a6a __NSFireDelayedPerform + 446
- 9 CoreFoundation 0x3422e5da __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 10
- 10 CoreFoundation 0x3422e28c __CFRunLoopDoTimer + 268
- 11 CoreFoundation 0x3422cefc __CFRunLoopRun + 1228
- 12 CoreFoundation 0x3419feb8 CFRunLoopRunSpecific + 352
- 13 CoreFoundation 0x3419fd44 CFRunLoopRunInMode + 100
- 14 GraphicsServices 0x34f082e6 GSEventRunModal + 70
- 15 UIKit 0x36de52fc UIApplicationMain + 1116
- 16 QQYanChu 0x000fcfc0 main (main.m:39)
- 17 QQYanChu 0x000ee4d4 start + 36
-
-
- Thread 0 name: Dispatch queue: com.apple.main-thread
- Thread 0 Crashed:
- 0 libsystem_kernel.dylib 0x32f28350 __pthread_kill + 8
- 1 libsystem_c.dylib 0x3830311e pthread_kill + 54
- 2 libsystem_c.dylib 0x3833f96e abort + 90
- 3 libc++abi.dylib 0x38e35d4a abort_message + 70
- 4 libc++abi.dylib 0x38e32ff4 default_terminate() + 20
- 5 libobjc.A.dylib 0x33a2ba74 _objc_terminate() + 144
- 6 libc++abi.dylib 0x38e33078 safe_handler_caller(void (*)()) + 76
- 7 libc++abi.dylib 0x38e33110 std::terminate() + 16
- 8 libc++abi.dylib 0x38e34594 __cxa_rethrow + 84
- 9 libobjc.A.dylib 0x33a2b9cc objc_exception_rethrow + 8
- 10 CoreFoundation 0x3419ff1c CFRunLoopRunSpecific + 452
- 11 CoreFoundation 0x3419fd44 CFRunLoopRunInMode + 100
- 12 GraphicsServices 0x34f082e6 GSEventRunModal + 70
- 13 UIKit 0x36de52fc UIApplicationMain + 1116
- 14 QQYanChu 0x000fcfc0 main (main.m:39)
- 15 QQYanChu 0x000ee4d4 start + 36
可以看出相比系统异常日志中多出了Last Exception Backtrace字段。上面是线程0崩溃的,而线程0只调用的abort。这是我们要看Last Exception Backtrace这里的堆栈,这个堆栈是抛出异常时的线程当时的堆栈情况,这个信息才是有意义的。在我们程序抛出异常后,系统会执行一系列相关逻辑后调用abort异常结束程序。