1 原生抓崩潰API :NSSetUncaughtExceptionHandler
ios提供了原生的抓取崩潰的API: NSSetUncaughtExceptionHandler,具體用法如下:
- 在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中,寫:
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
- 在AppDelegate.m的類定義的上方(@implementation AppDelegate),定義一個C函數:
void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols]; //得到當前調用棧信息
NSString *reason = [exception reason]; //非常重要,就是崩潰的原因
NSString *name = [exception name]; //異常類型
NSString *title = [NSString stringWithFormat:@"%@:%@", reason, name];
NSString *content = [arr componentsJoinedByString:@";"];
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:title forKey:EMCrashTitleIdentifier];
[userDefault setObject:content forKey:EMCrashContentIdentifier];
[userDefault synchronize];
DDLogError(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
DDLogVerbose(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr);
}
就完成了.
- 測試
你可以寫點數組越界,不存在的方法,或者直接拋出異常,用單步斷點調試查看UncaughtExceptionHandler是否抓取到了異常.如:
//1 索引異常
NSArray * arr = @[@(1), @(2), @(3),];
NSLog(@"arr 4: %@", arr[4]);
//2 不存在的方法
NSString * str = [NSNumber numberWithBool:YES];
[str stringByAppendingString:@"dd"];
//3 拋出異常
[NSException raise:@"crash !!" format:@"format"];
2. 利用PLCrashReporter
原生的API只能抓到OC層面的對象異常.但是內核和內存產生的異常抓不到.例如:
當你需要把一張照片存入系統相冊,代碼如下:
- (void)savePhoto:(UIImage *)image {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *) self);
}
回調為:
/*
保存照片的回調函數
*/
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (!error) {
[EMToastView showTipText:@"保存成功"];
return;
}
}
但是你并沒有在工程的info.plist中注冊相關權限:
NSPhotoLibraryAddUsageDescription (只含寫權限)
或者NSPhotoLibraryUsageDescription(讀寫權限). 如:
<key>NSPhotoLibraryAddUsageDescription</key>
<string>App需要您的同意,才能訪問相冊</string>
那么,在ios系統版本為11.1.2的時候,就會產生崩潰;(ios10和ios11的其他系統版本,以及ios12都不會產生崩潰)
多說一句: 這個NSPhotoLibraryAddUsageDescription權限的官方文檔說ios11后必須在info.plist中注冊該權限,但是實測發現除了11.1.2系統外,也可以不注冊:
官方說明.png
這個崩潰依靠NSSetUncaughtExceptionHandler是抓不到的.那么只有靠PLCrashReporter了,我還看到有靠信號量的,暫時還沒有去研究.
下面是PLCrashReporter的用法
- 步驟一:下載
到PLCrashReporter官方下載最新版本,目前為1.2.下個zip包.
打開zip包.點擊其說明:API Documentation.html
- 步驟二:
把iOS Framework中的framework直接拖入工程中.即可.
或者, 用cocoapods導入:
在Podfile中加入這一句,然后執行pod install.
pod 'PLCrashReporter', '~> 1.2'
- 步驟三:
點擊Example iPhone Usage.把它的例子代碼copy到你工程里面.我稍作修改如下:
在AppDelegate.m中寫2個函數:
先要引入2個頭文件:
#import <CrashReporter/CrashReporter.h>
#import <CrashReporter/PLCrashReportTextFormatter.h>
+ (void) handleCCrashReport:(PLCrashReporter * ) crashReporter{
NSData *crashData;
NSError *error;
// Try loading the crash report
crashData = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
if (crashData == nil) {
NSLog(@"Could not load crash report: %@", error);
[crashReporter purgePendingCrashReport];
return;
}
// We could send the report from here, but we'll just print out
// some debugging info instead
PLCrashReport *report = [[PLCrashReport alloc] initWithData: crashData error: &error] ;
if (report == nil) {
NSLog(@"Could not parse crash report");
[crashReporter purgePendingCrashReport];
return;
}
// NSLog(@"Crashed on %@", report.systemInfo.timestamp);
// NSLog(@"Crashed with signal %@ (code %@, address=0x%" PRIx64 ")", report.signalInfo.name,
// report.signalInfo.code, report.signalInfo.address);
// Purge the report
[crashReporter purgePendingCrashReport];
* //上傳--這后面的代碼是我加的.
* NSString *humanText = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];
*
* [self uploadCrashLog:@"c crash" crashContent:humanText withSuccessBlock:^{
* }];//uploadCrashLog這個函數是把log文件上傳服務器的,請自行補充哈;還可以寫入沙盒,供自己就地查看,下面是寫入沙盒的代碼
*
* NSString * documentDic = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).firstObject;
* NSString * fileName = [documentDic stringByAppendingPathComponent:@"1.crash"];
* NSError * err = nil;
* [humanText writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&err];
return;
}
這個handleCCrashReportWrap在applicationDidFinishLaunching函數中被調用
+(void) handleCCrashReportWrap{
//xcode單步調試下不能使用,否則xcode斷開
if (debugger_should_exit()) {
NSLog(@"The demo crash app should be run without a debugger present. Exiting ...");
return;
}
PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration:config];
NSError *error;
// Check if we previously crashed
if ([crashReporter hasPendingCrashReport])
[self handleCCrashReport:crashReporter ];
// Enable the Crash Reporter
if (![crashReporter enableCrashReporterAndReturnError: &error])
NSLog(@"Warning: Could not enable crash reporter: %@", error);
}
這樣就可以了,用xcode裝上app,但是不要啟用單步調試. 因為PLCrashReporter的作者貌似做了處理,如果發現是Xcode單步調試,它會斷開app的連接.
運行兩次app,第一次先觸發崩潰,第二次再打開app; 就可以在沙盒或者你的服務器上看到相關日志了.
經過實測,第二次打開app,是可以單步調試斷點進入的.斷點打在這種地方就可以了:
// Check if we previously crashed
* if ([crashReporter hasPendingCrashReport])
* [self handleCrashReport];
到此就結束了.但是還要說明幾個延伸的點:
3 延伸點
- PLCrashReporter的初始化:
例子中用的是
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
而我的代碼用成了:
PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
PLCrashReporter *crashReporter = [PLCrashReporter alloc] initWithConfiguration: config];
這個PLCrashReporterSymbolicationStrategyAll代表希望抓到的日志是被dysm解析過的日志,而不是原始堆棧信息.如果把PLCrashReporterSymbolicationStrategyAll換成PLCrashReporterSymbolicationStrategyNone,就會得到原始堆棧信息.
dysm解析過的日志大概長這樣,能看到崩潰產生的行數.函數等:
解析后.png
原始堆棧信息大概長這樣,不容易看懂:它就是設備的崩潰日志.可以在Xcode->Devices and Simulators->View Device Logs->中找到:
原始堆棧信息.png
- 原始堆棧信息的解析
原始堆棧信息也可以直接用xcode提供的工具解析出來.具體步驟為:
- 找到xcode的symbolicatecrash工具. 在命令行中輸入shell查找命令:
find /Applications/Xcode.app -name symbolicatecrash -type f
會輸出symbolicatecrash工具的地址:例如:
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/Library/PrivateFrameworks/DVTFoundation.framework/symbolicatecrash
把它copy出來放到單獨的文件夾里,copy出來的目的是放一起好用嘛.
把你得到的1.crash文件也放過來,把項目的dSYM文件也放過來:
在命令行輸入:
./symbolicatecrash 1.crash 1.dSYM > 1.log
就可以得到1.log,也就是解析后的日志.
- 保證xcode的單步調試不被斷開
在PLCrashReporter下載的那個包中:打開Srouce->plcrashreporter-1.2->Source->Crash Demo->main.m.參考它的debugger_should_exit寫法.
這個函數能獲取到Xcode是否在單步調試模式下,如果是,我們就不用這個功能;如果否,我們就用這個功能,所以在AppDelegate的基礎上這么改進:
- 引入頭文件
#import <sys/types.h>
#import <sys/sysctl.h>
還是在AppDelegate的類定義上方(@implementation AppDelegate),copy這個debugger_should_exit函數:
static bool debugger_should_exit (void) {
struct kinfo_proc info;
size_t info_size = sizeof(info);
int name[4];
name[0] = CTL_KERN;
name[1] = KERN_PROC;
name[2] = KERN_PROC_PID;
name[3] = getpid();
if (sysctl(name, 4, &info, &info_size, NULL, 0) == -1) {
NSLog(@"sysctl() failed: %s", strerror(errno));
return false;
}
if ((info.kp_proc.p_flag & P_TRACED) != 0)
return true;
return false;
}
在函數handleCCrashReportWrap開頭,加入判斷:
+(void) handleCCrashReportWrap{
//xcode單步調試下不能使用,否則xcode斷開
if (debugger_should_exit()) {
NSLog(@"The demo crash app should be run without a debugger present. Exiting ...");
return;
}
PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration:config];
NSError *error;
// Check if we previously crashed
if ([crashReporter hasPendingCrashReport])
[self handleCCrashReport:crashReporter ];
// Enable the Crash Reporter
if (![crashReporter enableCrashReporterAndReturnError: &error])
NSLog(@"Warning: Could not enable crash reporter: %@", error);
}
PLCrashReporter使用實錄暫時記錄完畢.