|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2019-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +import Foundation |
| 8 | +// swiftlint:disable duplicate_imports |
| 9 | +#if COCOAPODS |
| 10 | +import KSCrash |
| 11 | +#elseif swift(>=6.0) |
| 12 | +internal import KSCrashRecording |
| 13 | +#else |
| 14 | +@_implementationOnly import KSCrashRecording |
| 15 | +#endif |
| 16 | +// swiftlint:enable duplicate_imports |
| 17 | + |
| 18 | +internal final class DatadogMinifyFilter: NSObject, CrashReportFilter { |
| 19 | + struct Constants { |
| 20 | + /// The maximum number of stack frames in each stack trace. |
| 21 | + /// When stack trace exceeds this limit, it will be reduced by dropping less important frames. |
| 22 | + static let maxNumberOfStackFrames = 200 |
| 23 | + } |
| 24 | + |
| 25 | + /// The maximum number of stack frames in each stack trace. |
| 26 | + let stackFramesLimit: Int |
| 27 | + |
| 28 | + required init(stackFramesLimit: Int = Constants.maxNumberOfStackFrames) { |
| 29 | + self.stackFramesLimit = stackFramesLimit |
| 30 | + super.init() |
| 31 | + } |
| 32 | + |
| 33 | + /// Filter the specified reports. |
| 34 | + /// |
| 35 | + /// - Parameters: |
| 36 | + /// - reports: The reports to process. |
| 37 | + /// - onCompletion: Block to call when processing is complete. |
| 38 | + func filterReports( |
| 39 | + _ reports: [KSCrashRecording.CrashReport], |
| 40 | + onCompletion: (([KSCrashRecording.CrashReport]?, (Error)?) -> Void)? |
| 41 | + ) { |
| 42 | + do { |
| 43 | + let reports = try reports.map { report in |
| 44 | + // Validate and extract the type-safe report dictionary |
| 45 | + guard var dict = report.untypedValue as? CrashFieldDictionary else { |
| 46 | + throw CrashReportException(description: "KSCrash report untypedValue is not a CrashDictionary") |
| 47 | + } |
| 48 | + |
| 49 | + return try AnyCrashReport(minify(report: dict)) |
| 50 | + } |
| 51 | + |
| 52 | + onCompletion?(reports, nil) |
| 53 | + } catch { |
| 54 | + onCompletion?(reports, error) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Minifies a crash report by removing unused binary images. |
| 59 | + /// |
| 60 | + /// This method reduces the crash report size by filtering out binary images (libraries/frameworks) |
| 61 | + /// that don't appear in any thread's backtrace. Only binary images that are actually referenced |
| 62 | + /// in stack frames are kept. |
| 63 | + /// |
| 64 | + /// ## Algorithm |
| 65 | + /// |
| 66 | + /// 1. Collects all `object_addr` values from all thread backtraces into a set |
| 67 | + /// 2. Filters `binary_images` to keep only those whose `image_addr` appears in the set |
| 68 | + /// |
| 69 | + /// - Parameter report: The crash report to minify |
| 70 | + /// - Returns: A minified crash report with only referenced binary images |
| 71 | + /// - Throws: `CrashReportException` if report structure is invalid |
| 72 | + func minify(report: CrashFieldDictionary) throws -> CrashFieldDictionary { |
| 73 | + var minifiedReport = report |
| 74 | + var objectAddresses: Set<Int64> = [] |
| 75 | + |
| 76 | + if var threads: [CrashFieldDictionary] = try report.valueIfPresent(forKey: .crash, .threads) { |
| 77 | + threads = try threads.map { try self.minify(thread: $0, objectAddresses: &objectAddresses) } |
| 78 | + minifiedReport.setValue(forKey: .crash, .threads, value: threads) |
| 79 | + } |
| 80 | + |
| 81 | + if var threads: [CrashFieldDictionary] = try report.valueIfPresent(forKey: .recrashReport, .crash, .threads) { |
| 82 | + threads = try threads.map { try self.minify(thread: $0, objectAddresses: &objectAddresses) } |
| 83 | + minifiedReport.setValue(forKey: .recrashReport, .crash, .threads, value: threads) |
| 84 | + } |
| 85 | + |
| 86 | + // Filter binary images to keep only those referenced in backtraces |
| 87 | + let binaryImages: [CrashFieldDictionary] = try report.value(forKey: .binaryImages) |
| 88 | + minifiedReport[.binaryImages] = try binaryImages.filter { image in |
| 89 | + try image.valueIfPresent(forKey: .imageAddress).map { objectAddresses.contains($0) } ?? false |
| 90 | + } |
| 91 | + |
| 92 | + return minifiedReport |
| 93 | + } |
| 94 | + |
| 95 | + private func minify(thread: CrashFieldDictionary, objectAddresses: inout Set<Int64>) throws -> CrashFieldDictionary { |
| 96 | + guard var backtrace: [CrashFieldDictionary] = try thread.valueIfPresent(forKey: .backtrace, .contents) else { |
| 97 | + return thread |
| 98 | + } |
| 99 | + |
| 100 | + let truncated = limit(backtrace: &backtrace) |
| 101 | + try objectAddresses.formUnion(backtrace.compactMap { try $0.valueIfPresent(forKey: .objectAddr) }) |
| 102 | + |
| 103 | + guard truncated else { |
| 104 | + return thread |
| 105 | + } |
| 106 | + |
| 107 | + var thread = thread |
| 108 | + thread.setValue(forKey: .backtrace, .contents, value: backtrace) |
| 109 | + thread.setValue(forKey: .backtrace, .truncated, value: true) |
| 110 | + return thread |
| 111 | + } |
| 112 | + |
| 113 | + /// Removes less important stack frames to ensure that their count is equal or below `stackFramesLimit`. |
| 114 | + /// Frames are removed at the middle of stack trace, which preserves the most important upper and bottom frames. |
| 115 | + private func limit(backtrace frames: inout [CrashFieldDictionary]) -> Bool { |
| 116 | + guard frames.count > stackFramesLimit else { |
| 117 | + return false |
| 118 | + } |
| 119 | + |
| 120 | + let toRemove = frames.count - stackFramesLimit |
| 121 | + let middleFrameIndex = frames.count / 2 |
| 122 | + let lowerBound = middleFrameIndex - toRemove / 2 |
| 123 | + let upperBound = lowerBound + toRemove |
| 124 | + frames.removeSubrange(lowerBound..<upperBound) |
| 125 | + |
| 126 | + return true |
| 127 | + } |
| 128 | +} |
0 commit comments