iOSiOS plus

PLCrashReporter使用实录

2017-12-15  本文已影响199人  金风细细

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提供的工具解析出来.具体步骤为:
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使用实录暂时记录完毕.

上一篇 下一篇

猜你喜欢

热点阅读