大家好,我是OB!今天来聊聊crash!
前面说到 iOS利用runtime防崩溃的三大步,今天来看看万一程序奔溃了,怎么收集crash日志。
一、crash日志收集
NSException
是OC中关于异常的类,它包含了reason
异常原因,callStackSymbols
堆栈调用信息,因此可以在程序crash时,利用系统回调NSSetUncaughtExceptionHandler
可以获取crash日志。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSSetUncaughtExceptionHandler(&captureExceptionHandler);
NSMutableArray *mArray = [[[NSMutableArray alloc] init] copy];
[mArray addObject:@"12345"];
return YES;
}
void captureExceptionHandler(NSException *exception){
NSArray *allStackInfo = [exception callStackSymbols];
NSString *name = [exception name];
NSString *reason = [exception reason];
NSLog(@"===%@-%@-%@",allStackInfo,name,reason);
}
打印如下:
可以看到,堆栈信息,崩溃原因都可以收集到,然后将崩溃日志存储起来,下次打开在上传,利用符号文件,可以解析出有用信息
Test_07_crash[2933:12080973] ===(
0 CoreFoundation 0x000000010424e6fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x00000001037f2ac5 objc_exception_throw + 48
2 CoreFoundation 0x000000010426cab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000104253443 ___forwarding___ + 1443
4 CoreFoundation 0x0000000104255238 _CF_forwarding_prep_0 + 120
5 Test_07_crash 0x0000000102f1d65f -[AppDelegate application:didFinishLaunchingWithOptions:] + 191
6 UIKitCore 0x00000001081a2311 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 280
7 UIKitCore 0x00000001081a3cad -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3932
8 UIKitCore 0x00000001081a90c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617
9 UIKitCore 0x00000001079ee6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904
10 UIKitCore 0x00000001079f6fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
11 UIKitCore 0x00000001079ee2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236
12 UIKitCore 0x00000001079eec48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091
13 UIKitCore 0x00000001079ecfba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782
14 UIKitCore 0x00000001079ecc71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433
15 UIKitCore 0x00000001079f19b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576
16 UIKitCore 0x00000001079f2610 _performActionsWithDelayForTransitionContext + 100
17 UIKitCore 0x00000001079f171d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223
18 UIKitCore 0x00000001079f66d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
19 UIKitCore 0x00000001081a79a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514
20 UIKitCore 0x0000000107d5edfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
21 FrontBoardServices 0x000000010f8b5125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448
22 FrontBoardServices 0x000000010f8beed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283
23 FrontBoardServices 0x000000010f8be700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53
24 libdispatch.dylib 0x0000000105ab8db5 _dispatch_client_callout + 8
25 libdispatch.dylib 0x0000000105abc2ba _dispatch_block_invoke_direct + 300
26 FrontBoardServices 0x000000010f8f0146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
27 FrontBoardServices 0x000000010f8efdfe -[FBSSerialQueue _performNext] + 451
28 FrontBoardServices 0x000000010f8f0393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42
29 CoreFoundation 0x00000001041b5be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
30 CoreFoundation 0x00000001041b5463 __CFRunLoopDoSources0 + 243
31 CoreFoundation 0x00000001041afb1f __CFRunLoopRun + 1231
32 CoreFoundation 0x00000001041af302 CFRunLoopRunSpecific + 626
33 GraphicsServices 0x000000010c8192fe GSEventRunModal + 65
34 UIKitCore 0x00000001081aaba2 UIApplicationMain + 140
35 Test_07_crash 0x0000000102f1d570 main + 112
36 libdyld.dylib 0x0000000105b2d541 start + 1
)
-NSInvalidArgumentException
- -[__NSArray0 addObject:]: unrecognized selector sent to instance 0x600003ca40a0
未完待续