PLCrashReporter使用实录

1 原生抓崩溃API :NSSetUncaughtExceptionHandler

ios提供了原生的抓取崩溃的API: NSSetUncaughtExceptionHandler,具体用法如下:

  1. 在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中,写:
NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
  1. 在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);
}

就完成了.

  1. 测试
    你可以写点数组越界,不存在的方法,或者直接抛出异常,用单步断点调试查看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的用法

  1. 步骤一:下载

到PLCrashReporter官方下载最新版本,目前为1.2.下个zip包.
打开zip包.点击其说明:API Documentation.html

PLCrashReporter.png

  1. 步骤二:
    把iOS Framework中的framework直接拖入工程中.即可.
    或者, 用cocoapods导入:
    在Podfile中加入这一句,然后执行pod install.
    pod 'PLCrashReporter', '~> 1.2'
  1. 步骤三:
    点击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 延伸点

  1. 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
  1. 原始堆栈信息的解析
    原始堆栈信息也可以直接用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文件也放过来:


解析工具.png

在命令行输入:

./symbolicatecrash 1.crash 1.dSYM > 1.log

就可以得到1.log,也就是解析后的日志.

  1. 保证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使用实录暂时记录完毕.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,290评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,399评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,021评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,034评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,412评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,651评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,902评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,605评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,339评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,586评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,076评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,400评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,060评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,851评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,685评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,595评论 2 270