PLCrashReporter

repository·master·Indexed 25 days ago

https://github.com/microsoft/plcrashreporter

An in-process live crash reporting framework for iOS, macOS, and tvOS. It provides detailed crash information, including stack traces, thread states, and register data using public APIs/ABIs. The library supports integration via CocoaPods, Swift Package Manager, Carthage, or binary copying, and includes the plcrashutil tool for decoding protobuf-encoded crash reports.

Tokens
1.9K
Snippets
6
Records
8
Agent score
35%

What's inside PLCrashReporter

  1. Install PLCrashReporter by copying binaries

    master
    1. Download the frameworks zip from the releases page.
    2. Unzip and drag PLCrashReporter.framework or PLCrashReporter.xcframework into your Xcode Project Navigator.
    3. Ensure your app target is checked in the dialog and click Finish.
    4. Embedding Settings (in Frameworks, Libraries and Embedded Content):
      • iOS and tvOS: Set Embed to Do not embed.
      • macOS: Set Embed to Embed and Sign.

    Note: PLCrashReporter-Static-{version}.zip contains static frameworks for all platforms.

  2. Install PLCrashReporter via Carthage

    master
    1. Add github "microsoft/plcrashreporter" to your Cartfile.
    2. Run carthage update --use-xcframeworks.
    3. In Xcode, drag CrashReporter.xcframework from the Carthage/Build folder into the Frameworks, Libraries and Embedded Content section of your target.
    4. Embedding Settings:
      • iOS and tvOS: Set Embed to Do not embed.
      • macOS: Set Embed to Embed and Sign.
    carthage update --use-xcframeworks
  3. Build PLCrashReporter from source

    master

    To build the project, ensure you have Xcode 11+ and optional tools like Doxygen, GraphViz, and protobuf-c. If you have modified .proto files, run the generation script first.

    1. Generate protobuf files (if modified):
      ./Dependencies/protobuf-c/generate-pb-c.sh
    2. Build using xcodebuild:
      xcodebuild -configuration Release -target 'CrashReporter'
    xcodebuild -configuration Release -target 'CrashReporter'
  4. Initialize and use PLCrashReporter in Objective-C

    master

    To use PLCrashReporter in Objective-C, import the module, configure the signal handler and symbolication strategy, and enable the reporter. Note that enabling in-process crash reporting may conflict with attached debuggers.

    @import CrashReporter;
    
    // Configure: Use PLCrashReporterSignalHandlerTypeMach and a symbolication strategy
    // Use PLCrashReporterSymbolicationStrategyNone for release versions.
    PLCrashReporterConfig *config = [[PLCrashReporterConfig alloc] initWithSignalHandlerType: PLCrashReporterSignalHandlerTypeMach
                                                                       symbolicationStrategy: PLCrashReporterSymbolicationStrategyAll];
    PLCrashReporter *crashReporter = [[PLCrashReporter alloc] initWithConfiguration: config];
    
    // Enable the Crash Reporter.
    NSError *error;
    if (![crashReporter enableCrashReporterAndReturnError: &error]) {
        NSLog(@"Warning: Could not enable crash reporter: %@", error);
    }
    
    // Checking for and processing a pending report
    if ([crashReporter hasPendingCrashReport]) {
        NSData *data = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
        if (data != nil) {
            PLCrashReport *report = [[PLCrashReport alloc] initWithData: data error: &error];
            if (report != nil) {
                NSString *text = [PLCrashReportTextFormatter stringValueForCrashReport: report withTextFormat: PLCrashReportTextFormatiOS];
                NSLog(@"%@", text);
            }
        }
        [crashReporter purgePendingCrashReport];
    }
  5. Initialize and use PLCrashReporter in Swift

    master

    To use PLCrashReporter in Swift, import the module, create a configuration, and initialize the reporter. It is recommended to use [] for symbolication strategy in release versions.

    import CrashReporter
    
    // Configure: Use .mach signal handler and .all symbolication strategy
    // Use [] for release versions.
    let config = PLCrashReporterConfig(signalHandlerType: .mach, symbolicationStrategy: .all)
    guard let crashReporter = PLCrashReporter(configuration: config) else {
        print("Could not create an instance of PLCrashReporter")
        return
    }
    
    // Enable the Crash Reporter.
    do {
        try crashReporter.enableAndReturnError()
    } catch let error {
        print("Warning: Could not enable crash reporter: \(error)")
    }
    
    // Checking for and processing a pending report
    if crashReporter.hasPendingCrashReport() {
        do {
            let data = try crashReporter.loadPendingCrashReportDataAndReturnError()
            let report = try PLCrashReport(data: data)
    
            if let text = PLCrashReportTextFormatter.stringValue(for: report, with: PLCrashReportTextFormatiOS) { 
                print(text)
            } else {
                print("CrashReporter: can't convert report to text")
            }
        } catch let error {
            print("CrashReporter failed to load and parse with error: \(error)")
        }
        crashReporter.purgePendingCrashReport()
    }
  6. Decode crash reports using plcrashutil

    master

    Crash reports are protobuf-encoded. You can use the included plcrashutil binary to convert them to Apple's standard iPhone text format for use with atos symbolication.

    plcrashutil convert --format=iphone example_report.plcrash