@app-swiss-knife/react-native-sdk 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +465 -0
- package/LICENSE +21 -0
- package/README.md +229 -42
- package/SwissKnifePurchases.podspec +1 -1
- package/android/build.gradle +24 -2
- package/android/consumer-rules.pro +12 -0
- package/android/src/main/AndroidManifest.xml +15 -1
- package/android/src/main/java/com/swissknife/crashreporting/SKCrashReportingModule.kt +5 -4
- package/android/src/main/java/com/swissknife/purchases/SwissKnifeJson.kt +7 -2
- package/android/src/main/java/com/swissknife/purchases/SwissKnifePurchasesModule.kt +168 -6
- package/android/src/play/java/com/swissknife/purchases/StoreIntentHandler.kt +7 -0
- package/android/src/rustore/java/com/swissknife/purchases/StoreIntentHandler.kt +9 -0
- package/android/vendor/java/com/appswissknife/SwissKnife.kt +107 -38
- package/android/vendor/java/com/appswissknife/analytics/SKAnalytics.kt +180 -115
- package/android/vendor/java/com/appswissknife/attributes/AttributesClient.kt +16 -18
- package/android/vendor/java/com/appswissknife/attributes/SetAttributesWorker.kt +13 -13
- package/android/vendor/java/com/appswissknife/attribution/Attribution.kt +56 -25
- package/android/vendor/java/com/appswissknife/context/DeviceContext.kt +1 -1
- package/android/vendor/java/com/appswissknife/crashreporting/SKCrashReporting.kt +641 -208
- package/android/vendor/java/com/appswissknife/entitlements/EntitlementsRepository.kt +195 -26
- package/android/vendor/java/com/appswissknife/identity/Identity.kt +31 -6
- package/android/vendor/java/com/appswissknife/models/PurchasesError.kt +5 -0
- package/android/vendor/java/com/appswissknife/net/BackendHttpClient.kt +69 -35
- package/android/vendor/java/com/appswissknife/net/IngestClient.kt +756 -98
- package/android/vendor/java/com/appswissknife/net/OfflineQueueStore.kt +214 -30
- package/android/vendor/java/com/appswissknife/notifications/NotificationRenderer.kt +195 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionRequestActivity.kt +60 -0
- package/android/vendor/java/com/appswissknife/notifications/PermissionStatus.kt +41 -0
- package/android/vendor/java/com/appswissknife/notifications/PushEventQueue.kt +143 -0
- package/android/vendor/java/com/appswissknife/notifications/PushPayload.kt +72 -0
- package/android/vendor/java/com/appswissknife/notifications/PushRegistration.kt +89 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeMessagingService.kt +57 -0
- package/android/vendor/java/com/appswissknife/notifications/SwissKnifeNotifications.kt +438 -0
- package/android/vendor/java/com/appswissknife/offerings/OfferingsRepository.kt +266 -64
- package/android/vendor/java/com/appswissknife/performance/SKPerformance.kt +1 -0
- package/android/vendor/java/com/appswissknife/purchases/BillingClientGateway.kt +545 -88
- package/android/vendor/java/com/appswissknife/purchases/PlayPriceEnrichmentProvider.kt +55 -24
- package/android/vendor/java/com/appswissknife/purchases/ProductKindHint.kt +21 -0
- package/android/vendor/java/com/appswissknife/purchases/PurchasesGateway.kt +14 -0
- package/android/vendor-rustore/AndroidManifest.xml +59 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreGateway.kt +428 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStoreInitProvider.kt +43 -0
- package/android/vendor-rustore/java/com/appswissknife/rustore/RuStorePriceEnrichmentProvider.kt +85 -0
- package/app.plugin.js +287 -0
- package/ios/SwissKnifePurchases.m +20 -0
- package/ios/SwissKnifePurchases.swift +97 -3
- package/ios/SwissKnifePushBootstrap.m +34 -0
- package/ios/vendor/SwissKnifeiOS/Analytics/SKAnalytics.swift +138 -89
- package/ios/vendor/SwissKnifeiOS/Context/DeviceContext.swift +1 -1
- package/ios/vendor/SwissKnifeiOS/CrashReporting/SKCrashReporting.swift +531 -100
- package/ios/vendor/SwissKnifeiOS/Net/BackendBackoffGate.swift +43 -2
- package/ios/vendor/SwissKnifeiOS/Net/IngestClient.swift +495 -16
- package/ios/vendor/SwissKnifeiOS/Net/OfflineQueueStore.swift +231 -23
- package/ios/vendor/SwissKnifeiOS/Notifications/PushPayload.swift +87 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/PushRegistrar.swift +322 -0
- package/ios/vendor/SwissKnifeiOS/Notifications/SwissKnife+Notifications.swift +403 -0
- package/ios/vendor/SwissKnifeiOS/Performance/SKPerformance.swift +1 -0
- package/ios/vendor/SwissKnifeiOS/Services/EntitlementsService.swift +11 -7
- package/ios/vendor/SwissKnifeiOS/Services/PurchaseService.swift +38 -7
- package/ios/vendor/SwissKnifeiOS/SwissKnife.swift +34 -6
- package/lib/commonjs/attribution.js +4 -3
- package/lib/commonjs/attribution.js.map +1 -1
- package/lib/commonjs/identity.js +12 -11
- package/lib/commonjs/identity.js.map +1 -1
- package/lib/commonjs/index.js +16 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/internal/config.js +105 -8
- package/lib/commonjs/internal/config.js.map +1 -1
- package/lib/commonjs/internal/telemetry-context.js +70 -0
- package/lib/commonjs/internal/telemetry-context.js.map +1 -0
- package/lib/commonjs/modules/analytics.js +18 -25
- package/lib/commonjs/modules/analytics.js.map +1 -1
- package/lib/commonjs/modules/crash-reporting.js +353 -31
- package/lib/commonjs/modules/crash-reporting.js.map +1 -1
- package/lib/commonjs/modules/log.js +1 -1
- package/lib/commonjs/modules/notifications.js +500 -0
- package/lib/commonjs/modules/notifications.js.map +1 -0
- package/lib/commonjs/modules/performance.js +57 -8
- package/lib/commonjs/modules/performance.js.map +1 -1
- package/lib/commonjs/native/module.js +6 -0
- package/lib/commonjs/native/module.js.map +1 -1
- package/lib/commonjs/native/spec.js +12 -5
- package/lib/commonjs/native/spec.js.map +1 -1
- package/lib/commonjs/purchases.js +4 -4
- package/lib/commonjs/types.js +12 -15
- package/lib/commonjs/types.js.map +1 -1
- package/lib/module/attribution.js +4 -3
- package/lib/module/attribution.js.map +1 -1
- package/lib/module/identity.js +12 -11
- package/lib/module/identity.js.map +1 -1
- package/lib/module/index.js +8 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/internal/config.js +103 -8
- package/lib/module/internal/config.js.map +1 -1
- package/lib/module/internal/telemetry-context.js +63 -0
- package/lib/module/internal/telemetry-context.js.map +1 -0
- package/lib/module/modules/analytics.js +18 -25
- package/lib/module/modules/analytics.js.map +1 -1
- package/lib/module/modules/crash-reporting.js +353 -31
- package/lib/module/modules/crash-reporting.js.map +1 -1
- package/lib/module/modules/log.js +1 -1
- package/lib/module/modules/notifications.js +491 -0
- package/lib/module/modules/notifications.js.map +1 -0
- package/lib/module/modules/performance.js +57 -8
- package/lib/module/modules/performance.js.map +1 -1
- package/lib/module/native/module.js +6 -1
- package/lib/module/native/module.js.map +1 -1
- package/lib/module/native/spec.js +11 -4
- package/lib/module/native/spec.js.map +1 -1
- package/lib/module/purchases.js +4 -4
- package/lib/module/types.js +12 -15
- package/lib/module/types.js.map +1 -1
- package/lib/typescript/commonjs/attribution.d.ts +3 -2
- package/lib/typescript/commonjs/attribution.d.ts.map +1 -1
- package/lib/typescript/commonjs/identity.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.d.ts +3 -1
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/config.d.ts +16 -2
- package/lib/typescript/commonjs/internal/config.d.ts.map +1 -1
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/commonjs/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/analytics.d.ts +8 -11
- package/lib/typescript/commonjs/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts +50 -12
- package/lib/typescript/commonjs/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/commonjs/modules/notifications.d.ts +110 -0
- package/lib/typescript/commonjs/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/commonjs/modules/performance.d.ts +13 -0
- package/lib/typescript/commonjs/modules/performance.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/module.d.ts +2 -0
- package/lib/typescript/commonjs/native/module.d.ts.map +1 -1
- package/lib/typescript/commonjs/native/spec.d.ts +33 -4
- package/lib/typescript/commonjs/native/spec.d.ts.map +1 -1
- package/lib/typescript/commonjs/purchases.d.ts +4 -4
- package/lib/typescript/commonjs/types.d.ts +4 -5
- package/lib/typescript/commonjs/types.d.ts.map +1 -1
- package/lib/typescript/module/attribution.d.ts +3 -2
- package/lib/typescript/module/attribution.d.ts.map +1 -1
- package/lib/typescript/module/identity.d.ts.map +1 -1
- package/lib/typescript/module/index.d.ts +3 -1
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/internal/config.d.ts +16 -2
- package/lib/typescript/module/internal/config.d.ts.map +1 -1
- package/lib/typescript/module/internal/telemetry-context.d.ts +22 -0
- package/lib/typescript/module/internal/telemetry-context.d.ts.map +1 -0
- package/lib/typescript/module/modules/analytics.d.ts +8 -11
- package/lib/typescript/module/modules/analytics.d.ts.map +1 -1
- package/lib/typescript/module/modules/crash-reporting.d.ts +50 -12
- package/lib/typescript/module/modules/crash-reporting.d.ts.map +1 -1
- package/lib/typescript/module/modules/notifications.d.ts +110 -0
- package/lib/typescript/module/modules/notifications.d.ts.map +1 -0
- package/lib/typescript/module/modules/performance.d.ts +13 -0
- package/lib/typescript/module/modules/performance.d.ts.map +1 -1
- package/lib/typescript/module/native/module.d.ts +2 -0
- package/lib/typescript/module/native/module.d.ts.map +1 -1
- package/lib/typescript/module/native/spec.d.ts +33 -4
- package/lib/typescript/module/native/spec.d.ts.map +1 -1
- package/lib/typescript/module/purchases.d.ts +4 -4
- package/lib/typescript/module/types.d.ts +4 -5
- package/lib/typescript/module/types.d.ts.map +1 -1
- package/package.json +13 -4
- package/src/attribution.ts +4 -3
- package/src/identity.ts +12 -11
- package/src/index.ts +18 -4
- package/src/internal/config.ts +113 -8
- package/src/internal/telemetry-context.ts +76 -0
- package/src/modules/analytics.ts +24 -33
- package/src/modules/crash-reporting.ts +476 -67
- package/src/modules/log.ts +1 -1
- package/src/modules/notifications.ts +557 -0
- package/src/modules/performance.ts +94 -14
- package/src/native/module.ts +6 -0
- package/src/native/spec.ts +45 -10
- package/src/purchases.ts +4 -4
- package/src/types.ts +12 -15
- package/android/vendor/java/com/appswissknife/analytics/EventQueueStore.kt +0 -326
- package/android/vendor/java/com/appswissknife/analytics/FlushEventsWorker.kt +0 -296
|
@@ -110,20 +110,63 @@ public struct SKErrorEvent: Codable {
|
|
|
110
110
|
// MARK: - Scope
|
|
111
111
|
|
|
112
112
|
public final class SKErrorScope {
|
|
113
|
-
internal
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
internal struct Snapshot {
|
|
114
|
+
var tags: [String: String] = [:]
|
|
115
|
+
var extra: [String: String] = [:]
|
|
116
|
+
var user: SKUser?
|
|
117
|
+
var identityUserId: String?
|
|
118
|
+
|
|
119
|
+
var effectiveUser: SKUser? {
|
|
120
|
+
let id = user?.id ?? identityUserId
|
|
121
|
+
guard id != nil || user?.email != nil || user?.username != nil else { return nil }
|
|
122
|
+
return SKUser(id: id, email: user?.email, username: user?.username)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private let lock = NSLock()
|
|
127
|
+
private var state = Snapshot()
|
|
128
|
+
|
|
129
|
+
// Copy under the same lock as mutations: Swift value semantics alone do
|
|
130
|
+
// not make a dictionary safe to copy while another thread changes it.
|
|
131
|
+
internal func snapshot() -> Snapshot {
|
|
132
|
+
lock.lock()
|
|
133
|
+
defer { lock.unlock() }
|
|
134
|
+
return state
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Fatal exception handling must not wait for the interrupted writer.
|
|
138
|
+
internal func snapshotIfAvailable() -> Snapshot? {
|
|
139
|
+
guard lock.try() else { return nil }
|
|
140
|
+
defer { lock.unlock() }
|
|
141
|
+
return state
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
internal var tags: [String: String] { snapshot().tags }
|
|
145
|
+
internal var extra: [String: String] { snapshot().extra }
|
|
146
|
+
internal var user: SKUser? { snapshot().user }
|
|
147
|
+
internal var identityUserId: String? {
|
|
148
|
+
get { snapshot().identityUserId }
|
|
149
|
+
set { lock.lock(); defer { lock.unlock() }; state.identityUserId = newValue }
|
|
150
|
+
}
|
|
116
151
|
|
|
117
152
|
public func setTag(_ key: String, value: String) {
|
|
118
|
-
|
|
153
|
+
lock.lock()
|
|
154
|
+
defer { lock.unlock() }
|
|
155
|
+
state.tags[key] = value
|
|
119
156
|
}
|
|
120
157
|
|
|
121
158
|
public func setExtra(_ key: String, value: Any) {
|
|
122
|
-
|
|
159
|
+
// User-defined descriptions may execute arbitrary code: evaluate outside the lock.
|
|
160
|
+
let rendered = "\(value)"
|
|
161
|
+
lock.lock()
|
|
162
|
+
defer { lock.unlock() }
|
|
163
|
+
state.extra[key] = rendered
|
|
123
164
|
}
|
|
124
165
|
|
|
125
166
|
public func setUser(_ user: SKUser?) {
|
|
126
|
-
|
|
167
|
+
lock.lock()
|
|
168
|
+
defer { lock.unlock() }
|
|
169
|
+
state.user = user
|
|
127
170
|
}
|
|
128
171
|
}
|
|
129
172
|
|
|
@@ -131,6 +174,7 @@ public final class SKErrorScope {
|
|
|
131
174
|
|
|
132
175
|
internal final class RingBuffer<T> {
|
|
133
176
|
private var buffer: [T] = []
|
|
177
|
+
private let lock = NSLock()
|
|
134
178
|
private let capacity: Int
|
|
135
179
|
|
|
136
180
|
init(capacity: Int) {
|
|
@@ -138,15 +182,235 @@ internal final class RingBuffer<T> {
|
|
|
138
182
|
}
|
|
139
183
|
|
|
140
184
|
func append(_ element: T) {
|
|
185
|
+
lock.lock()
|
|
186
|
+
defer { lock.unlock() }
|
|
141
187
|
if buffer.count >= capacity {
|
|
142
188
|
buffer.removeFirst()
|
|
143
189
|
}
|
|
144
190
|
buffer.append(element)
|
|
145
191
|
}
|
|
146
192
|
|
|
147
|
-
func snapshot() -> [T] {
|
|
193
|
+
func snapshot() -> [T] {
|
|
194
|
+
lock.lock()
|
|
195
|
+
defer { lock.unlock() }
|
|
196
|
+
return buffer
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
func snapshotIfAvailable() -> [T]? {
|
|
200
|
+
guard lock.try() else { return nil }
|
|
201
|
+
defer { lock.unlock() }
|
|
202
|
+
return buffer
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
func clear() {
|
|
206
|
+
lock.lock()
|
|
207
|
+
defer { lock.unlock() }
|
|
208
|
+
buffer = []
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// MARK: - ANR Watchdog
|
|
213
|
+
|
|
214
|
+
/// Detects a blocked main thread: post a ping to main, sleep `timeout`, and
|
|
215
|
+
/// report a hang if the ping has not run by then.
|
|
216
|
+
///
|
|
217
|
+
/// - **Latched.** One hang is one report. After reporting, no new ping is
|
|
218
|
+
/// posted until the SAME ping has run; only then does the watchdog rearm.
|
|
219
|
+
/// It used to report again every cycle, so a 30 s hang with the default
|
|
220
|
+
/// 5 s timeout produced six identical ANRs.
|
|
221
|
+
/// - **Paused in the background.** Nothing is checked between
|
|
222
|
+
/// `didEnterBackground` and `willEnterForeground`, and the first cycle
|
|
223
|
+
/// after resume is a grace cycle: the main queue of a process that was
|
|
224
|
+
/// just suspended has a backlog, which is not a hang.
|
|
225
|
+
/// - **Oversleep guard.** If the watchdog thread itself woke far later than
|
|
226
|
+
/// it asked (the whole process was frozen), an unanswered ping says
|
|
227
|
+
/// nothing about main, so that cycle does not report.
|
|
228
|
+
/// - The ping counters are written on main and read on the watchdog thread,
|
|
229
|
+
/// so they live under `lock`. (`var responded = false` used to be shared
|
|
230
|
+
/// between the two threads with no synchronisation at all.)
|
|
231
|
+
///
|
|
232
|
+
/// `Environment` holds the seams — how to reach main, how to sleep, and a
|
|
233
|
+
/// monotonic clock — so tests can drive `runCycle()` one cycle at a time.
|
|
234
|
+
internal final class ANRWatchdog: @unchecked Sendable {
|
|
235
|
+
|
|
236
|
+
struct Environment {
|
|
237
|
+
var postToMain: (@escaping () -> Void) -> Void
|
|
238
|
+
var sleep: (TimeInterval) -> Void
|
|
239
|
+
/// Monotonic seconds. Includes time the device slept, so a frozen
|
|
240
|
+
/// process shows up as an oversleep.
|
|
241
|
+
var now: () -> TimeInterval
|
|
242
|
+
|
|
243
|
+
static let live = Environment(
|
|
244
|
+
postToMain: { DispatchQueue.main.async(execute: $0) },
|
|
245
|
+
sleep: { Thread.sleep(forTimeInterval: $0) },
|
|
246
|
+
now: { TimeInterval(clock_gettime_nsec_np(CLOCK_MONOTONIC)) / 1_000_000_000 }
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
let timeout: TimeInterval
|
|
251
|
+
static let oversleepSlack: TimeInterval = 1
|
|
252
|
+
private let environment: Environment
|
|
253
|
+
private let onHang: () -> Void
|
|
254
|
+
|
|
255
|
+
// Everything below is guarded by `lock`. It is never held across a
|
|
256
|
+
// sleep, a post to main, or `onHang`.
|
|
257
|
+
private let lock = NSLock()
|
|
258
|
+
private var running = false
|
|
259
|
+
/// Bumped by every start and stop, so a loop from an earlier start
|
|
260
|
+
/// exits instead of running alongside a new one.
|
|
261
|
+
private var generation = 0
|
|
262
|
+
private var lastPingSent: UInt64 = 0
|
|
263
|
+
private var lastPingAnswered: UInt64 = 0
|
|
264
|
+
/// The ping a hang was reported on; `nil` when armed.
|
|
265
|
+
private var latchedPing: UInt64?
|
|
266
|
+
private var inBackground = false
|
|
267
|
+
private var graceCycles = 0
|
|
268
|
+
|
|
269
|
+
init(
|
|
270
|
+
timeout: TimeInterval,
|
|
271
|
+
environment: Environment = .live,
|
|
272
|
+
onHang: @escaping () -> Void
|
|
273
|
+
) {
|
|
274
|
+
self.timeout = timeout
|
|
275
|
+
self.environment = environment
|
|
276
|
+
self.onHang = onHang
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
var isRunning: Bool {
|
|
280
|
+
lock.lock(); defer { lock.unlock() }
|
|
281
|
+
return running
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/// Start the watchdog thread. Idempotent: a running watchdog keeps its
|
|
285
|
+
/// one thread.
|
|
286
|
+
func start() {
|
|
287
|
+
lock.lock()
|
|
288
|
+
if running {
|
|
289
|
+
lock.unlock()
|
|
290
|
+
return
|
|
291
|
+
}
|
|
292
|
+
running = true
|
|
293
|
+
generation += 1
|
|
294
|
+
let gen = generation
|
|
295
|
+
lock.unlock()
|
|
296
|
+
|
|
297
|
+
let thread = Thread { [weak self] in
|
|
298
|
+
while let watchdog = self, watchdog.runCycle(generation: gen) {}
|
|
299
|
+
}
|
|
300
|
+
thread.name = "com.swissknife.anr-watchdog"
|
|
301
|
+
thread.qualityOfService = .utility
|
|
302
|
+
thread.start()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/// Stop checking. The thread exits when its current sleep ends.
|
|
306
|
+
func stop() {
|
|
307
|
+
lock.lock()
|
|
308
|
+
running = false
|
|
309
|
+
generation += 1
|
|
310
|
+
lock.unlock()
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
func appDidEnterBackground() {
|
|
314
|
+
lock.lock()
|
|
315
|
+
inBackground = true
|
|
316
|
+
lock.unlock()
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
func appWillEnterForeground() {
|
|
320
|
+
lock.lock()
|
|
321
|
+
inBackground = false
|
|
322
|
+
graceCycles = 1
|
|
323
|
+
lock.unlock()
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/// Run one check on the calling thread: what the watchdog thread does in
|
|
327
|
+
/// a loop. Returns `false` once the watchdog has been stopped.
|
|
328
|
+
@discardableResult
|
|
329
|
+
func runCycle() -> Bool {
|
|
330
|
+
lock.lock()
|
|
331
|
+
let gen = generation
|
|
332
|
+
lock.unlock()
|
|
333
|
+
return runCycle(generation: gen)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/// Test seam — mark the watchdog running without spawning its thread,
|
|
337
|
+
/// so a test can drive `runCycle()` deterministically.
|
|
338
|
+
internal func _armWithoutThreadForTesting() {
|
|
339
|
+
lock.lock()
|
|
340
|
+
running = true
|
|
341
|
+
generation += 1
|
|
342
|
+
lock.unlock()
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private enum Step {
|
|
346
|
+
case stop
|
|
347
|
+
/// Nothing to check this cycle (background, or a hang still latched).
|
|
348
|
+
case idle
|
|
349
|
+
case ping(UInt64)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
private func runCycle(generation gen: Int) -> Bool {
|
|
353
|
+
lock.lock()
|
|
354
|
+
let step = beginCycleLocked(gen)
|
|
355
|
+
lock.unlock()
|
|
356
|
+
|
|
357
|
+
let ping: UInt64
|
|
358
|
+
switch step {
|
|
359
|
+
case .stop:
|
|
360
|
+
return false
|
|
361
|
+
case .idle:
|
|
362
|
+
ping = 0
|
|
363
|
+
case .ping(let id):
|
|
364
|
+
ping = id
|
|
365
|
+
environment.postToMain { [weak self] in self?.answer(id) }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
let sleptFrom = environment.now()
|
|
369
|
+
environment.sleep(timeout)
|
|
370
|
+
let slept = environment.now() - sleptFrom
|
|
371
|
+
|
|
372
|
+
lock.lock()
|
|
373
|
+
let report = endCycleLocked(gen, ping: ping, slept: slept)
|
|
374
|
+
let keepGoing = running && generation == gen
|
|
375
|
+
lock.unlock()
|
|
376
|
+
|
|
377
|
+
if report { onHang() }
|
|
378
|
+
return keepGoing
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private func beginCycleLocked(_ gen: Int) -> Step {
|
|
382
|
+
guard running, generation == gen else { return .stop }
|
|
383
|
+
if inBackground { return .idle }
|
|
384
|
+
if let latched = latchedPing {
|
|
385
|
+
// Still the same hang until the ping we reported on has run.
|
|
386
|
+
guard lastPingAnswered >= latched else { return .idle }
|
|
387
|
+
latchedPing = nil
|
|
388
|
+
}
|
|
389
|
+
lastPingSent += 1
|
|
390
|
+
return .ping(lastPingSent)
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private func endCycleLocked(_ gen: Int, ping: UInt64, slept: TimeInterval) -> Bool {
|
|
394
|
+
guard running, generation == gen, ping != 0 else { return false }
|
|
395
|
+
if inBackground { return false }
|
|
396
|
+
if graceCycles > 0 {
|
|
397
|
+
graceCycles -= 1
|
|
398
|
+
return false
|
|
399
|
+
}
|
|
400
|
+
// The watchdog thread was frozen too; the ping proves nothing. The
|
|
401
|
+
// second of slack absorbs timer coalescing, which stretches a
|
|
402
|
+
// utility-QoS sleep by up to ~150 ms whatever the timeout.
|
|
403
|
+
if slept > timeout * 2 + Self.oversleepSlack { return false }
|
|
404
|
+
guard latchedPing == nil, lastPingAnswered < ping else { return false }
|
|
405
|
+
latchedPing = ping
|
|
406
|
+
return true
|
|
407
|
+
}
|
|
148
408
|
|
|
149
|
-
func
|
|
409
|
+
private func answer(_ id: UInt64) {
|
|
410
|
+
lock.lock()
|
|
411
|
+
if id > lastPingAnswered { lastPingAnswered = id }
|
|
412
|
+
lock.unlock()
|
|
413
|
+
}
|
|
150
414
|
}
|
|
151
415
|
|
|
152
416
|
// MARK: - Main SDK
|
|
@@ -210,8 +474,18 @@ public final class SKCrashReporting {
|
|
|
210
474
|
// session_event payloads disagree with their own enclosing envelope.
|
|
211
475
|
private var sessionErrorCount: Int = 0
|
|
212
476
|
|
|
213
|
-
// ANR watchdog
|
|
214
|
-
|
|
477
|
+
// ANR watchdog — one per process; a repeated `start(...)` reuses it.
|
|
478
|
+
// Behind `anrLock` because `start(...)` runs on the host's thread and
|
|
479
|
+
// the lifecycle observer on main.
|
|
480
|
+
private let anrLock = NSLock()
|
|
481
|
+
private var _anrWatchdog: ANRWatchdog?
|
|
482
|
+
private var anrWatchdog: ANRWatchdog? {
|
|
483
|
+
get { anrLock.lock(); defer { anrLock.unlock() }; return _anrWatchdog }
|
|
484
|
+
set { anrLock.lock(); _anrWatchdog = newValue; anrLock.unlock() }
|
|
485
|
+
}
|
|
486
|
+
/// Main-actor only: the AppLifecycle subscription that pauses the
|
|
487
|
+
/// watchdog in the background.
|
|
488
|
+
private var anrLifecycleToken: UUID?
|
|
215
489
|
|
|
216
490
|
/// Session-rotation observer token — re-persists crash context on
|
|
217
491
|
/// rotation so signal-path recovery closes the right session.
|
|
@@ -254,10 +528,12 @@ public final class SKCrashReporting {
|
|
|
254
528
|
/// notifies observers — `SwissKnife.configure(appUserID:)` seeds the id
|
|
255
529
|
/// directly via `Identity._seedConfiguredUser`.
|
|
256
530
|
///
|
|
257
|
-
///
|
|
258
|
-
///
|
|
259
|
-
|
|
260
|
-
|
|
531
|
+
/// Stored under the scope lock; the NSException handler takes only a
|
|
532
|
+
/// non-blocking snapshot, and the signal handler never reads Swift state.
|
|
533
|
+
private var identityUserId: String? {
|
|
534
|
+
get { scope.identityUserId }
|
|
535
|
+
set { scope.identityUserId = newValue }
|
|
536
|
+
}
|
|
261
537
|
|
|
262
538
|
/// Identity-change observation token, so a repeated `start()` re-arms
|
|
263
539
|
/// the observer instead of stacking duplicates.
|
|
@@ -454,9 +730,13 @@ public final class SKCrashReporting {
|
|
|
454
730
|
// on disk for the whole next session. Schedule a launch-time flush
|
|
455
731
|
// BEHIND the recovery enqueues on the same serial queue, so the items
|
|
456
732
|
// are on the ingest queue before the drain starts.
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
733
|
+
//
|
|
734
|
+
// The NSException report is recovered FIRST: whether the signal report
|
|
735
|
+
// is a duplicate depends on that report having actually been read,
|
|
736
|
+
// decoded and enqueued (see `isNSExceptionAbortDuplicate`).
|
|
737
|
+
let recoveredNSExceptionAt = flushPendingCrash()
|
|
738
|
+
let recoveredSignalCrash = flushSignalMarker(recoveredNSExceptionAt: recoveredNSExceptionAt)
|
|
739
|
+
if recoveredSignalCrash || recoveredNSExceptionAt != nil {
|
|
460
740
|
queue.async { [ingest] in
|
|
461
741
|
ingest.scheduleImmediateFlush()
|
|
462
742
|
}
|
|
@@ -518,6 +798,8 @@ public final class SKCrashReporting {
|
|
|
518
798
|
// ANR watchdog
|
|
519
799
|
if configuration.anrTimeoutSeconds > 0 {
|
|
520
800
|
startANRWatchdog()
|
|
801
|
+
} else {
|
|
802
|
+
stopANRWatchdog()
|
|
521
803
|
}
|
|
522
804
|
}
|
|
523
805
|
|
|
@@ -728,16 +1010,9 @@ public final class SKCrashReporting {
|
|
|
728
1010
|
/// - All three nil ⇒ `nil`, preserving today's behaviour for apps that
|
|
729
1011
|
/// never log in and never call `setUser`.
|
|
730
1012
|
///
|
|
731
|
-
///
|
|
732
|
-
///
|
|
733
|
-
private var effectiveUser: SKUser? {
|
|
734
|
-
let explicit = scope.user
|
|
735
|
-
let id = explicit?.id ?? identityUserId
|
|
736
|
-
let email = explicit?.email
|
|
737
|
-
let username = explicit?.username
|
|
738
|
-
guard id != nil || email != nil || username != nil else { return nil }
|
|
739
|
-
return SKUser(id: id, email: email, username: username)
|
|
740
|
-
}
|
|
1013
|
+
/// Ordinary-thread readers use the synchronized cache. The NSException
|
|
1014
|
+
/// hook uses a non-blocking scope snapshot instead.
|
|
1015
|
+
private var effectiveUser: SKUser? { scope.snapshot().effectiveUser }
|
|
741
1016
|
|
|
742
1017
|
private func buildEvent(level: SKErrorLevel) -> SKErrorEvent {
|
|
743
1018
|
var event = SKErrorEvent(level: level)
|
|
@@ -867,11 +1142,12 @@ public final class SKCrashReporting {
|
|
|
867
1142
|
// this snapshot ahead of the crash.
|
|
868
1143
|
refreshIdentityUserId()
|
|
869
1144
|
let session = Session.shared.current
|
|
1145
|
+
let scopeSnapshot = scope.snapshot()
|
|
870
1146
|
let ctx = PersistedContext(
|
|
871
1147
|
breadcrumbs: breadcrumbs.snapshot(),
|
|
872
|
-
tags:
|
|
873
|
-
extra:
|
|
874
|
-
user: effectiveUser,
|
|
1148
|
+
tags: scopeSnapshot.tags,
|
|
1149
|
+
extra: scopeSnapshot.extra,
|
|
1150
|
+
user: scopeSnapshot.effectiveUser,
|
|
875
1151
|
environment: configuration?.environment,
|
|
876
1152
|
release: configuration?.release,
|
|
877
1153
|
sessionId: session.id,
|
|
@@ -886,27 +1162,66 @@ public final class SKCrashReporting {
|
|
|
886
1162
|
|
|
887
1163
|
// MARK: - Pending Crash (disk)
|
|
888
1164
|
|
|
889
|
-
/// Returns
|
|
890
|
-
///
|
|
891
|
-
|
|
1165
|
+
/// Returns the crash instant (the file's mtime) when a pending
|
|
1166
|
+
/// NSException crash was recovered and enqueued, `nil` otherwise. `start()`
|
|
1167
|
+
/// uses it to trigger the launch-time ingest flush and to pair the report
|
|
1168
|
+
/// with the SIGABRT the same crash left behind.
|
|
1169
|
+
private func flushPendingCrash() -> Date? {
|
|
892
1170
|
let url = SKCrashReporting.pendingCrashURL
|
|
893
1171
|
guard FileManager.default.fileExists(atPath: url.path),
|
|
894
|
-
let data = try? Data(contentsOf: url) else { return
|
|
1172
|
+
let data = try? Data(contentsOf: url) else { return nil }
|
|
1173
|
+
let crashedAt = (try? FileManager.default.attributesOfItem(atPath: url.path))?[.modificationDate] as? Date
|
|
895
1174
|
|
|
896
1175
|
// Delete before sending to avoid re-sending on failure (best-effort delivery)
|
|
897
1176
|
try? FileManager.default.removeItem(at: url)
|
|
898
1177
|
|
|
899
|
-
guard configuration != nil else { return
|
|
1178
|
+
guard configuration != nil else { return nil }
|
|
900
1179
|
// The crash JSON on disk is a single SKErrorEvent. Decode and
|
|
901
1180
|
// re-enqueue through IngestClient so retry / batching / context
|
|
902
1181
|
// stamping all happen uniformly with live errors.
|
|
903
1182
|
let decoder = JSONDecoder()
|
|
904
1183
|
decoder.dateDecodingStrategy = .iso8601
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
1184
|
+
guard let event = try? decoder.decode(SKErrorEvent.self, from: data) else { return nil }
|
|
1185
|
+
sendEvent(event)
|
|
1186
|
+
return crashedAt ?? Date.distantPast
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/// How long after the NSException report was written its `abort()` tail
|
|
1190
|
+
/// may land in the signal marker. The gap is the rest of the exception
|
|
1191
|
+
/// handler (session close) plus any chained incumbent handler — well under
|
|
1192
|
+
/// a second in practice. A relaunch plus a new crash cannot fit inside it.
|
|
1193
|
+
internal static let nsExceptionAbortPairingWindow: TimeInterval = 10
|
|
1194
|
+
|
|
1195
|
+
/// Whether a recovered signal report is the `abort()` tail of an uncaught
|
|
1196
|
+
/// NSException, and must therefore be dropped.
|
|
1197
|
+
///
|
|
1198
|
+
/// An uncaught NSException does not end the process by itself: the ObjC
|
|
1199
|
+
/// runtime calls our handler, which writes `pending-crash.json`, and then
|
|
1200
|
+
/// `abort()`s, which raises SIGABRT and runs our signal handler too. Both
|
|
1201
|
+
/// used to be reported, so every NSException crash arrived as TWO events —
|
|
1202
|
+
/// the real one and a "Signal 6" that grouped into its own issue.
|
|
1203
|
+
///
|
|
1204
|
+
/// The signal report is dropped only when ALL of these hold, so a crash is
|
|
1205
|
+
/// never left with zero events:
|
|
1206
|
+
/// - the signal is SIGABRT (any other signal is a separate fault);
|
|
1207
|
+
/// - the NSException report was actually recovered — read, decoded and
|
|
1208
|
+
/// enqueued (`recoveredNSExceptionAt != nil`). If the handler died before
|
|
1209
|
+
/// finishing the write, or the file no longer decodes, the signal report
|
|
1210
|
+
/// is the only record and is kept;
|
|
1211
|
+
/// - the two files belong to the same crash: the NSException report was
|
|
1212
|
+
/// written at most `nsExceptionAbortPairingWindow` before the signal
|
|
1213
|
+
/// marker. A stale report left by an earlier run (e.g. unreadable during
|
|
1214
|
+
/// a locked-device relaunch) cannot swallow a later, unrelated abort().
|
|
1215
|
+
internal static func isNSExceptionAbortDuplicate(
|
|
1216
|
+
signal: Int32?,
|
|
1217
|
+
recoveredNSExceptionAt: Date?,
|
|
1218
|
+
signalAt: Date
|
|
1219
|
+
) -> Bool {
|
|
1220
|
+
guard signal == SIGABRT, let nsExceptionAt = recoveredNSExceptionAt else { return false }
|
|
1221
|
+
let gap = signalAt.timeIntervalSince(nsExceptionAt)
|
|
1222
|
+
// Small negative slack: both mtimes come from the same clock, but
|
|
1223
|
+
// filesystem timestamp granularity can order them either way.
|
|
1224
|
+
return gap >= -1 && gap <= nsExceptionAbortPairingWindow
|
|
910
1225
|
}
|
|
911
1226
|
|
|
912
1227
|
/// On next launch, read the signal marker file + backtrace binary +
|
|
@@ -915,7 +1230,7 @@ public final class SKCrashReporting {
|
|
|
915
1230
|
/// marker and raw backtrace addresses to the backtrace-bin file.
|
|
916
1231
|
/// Returns `true` when a signal crash was recovered and enqueued —
|
|
917
1232
|
/// `start()` uses this to trigger the launch-time ingest flush.
|
|
918
|
-
private func flushSignalMarker() -> Bool {
|
|
1233
|
+
private func flushSignalMarker(recoveredNSExceptionAt: Date? = nil) -> Bool {
|
|
919
1234
|
let markerURL = SKCrashReporting.signalMarkerURL
|
|
920
1235
|
let contextURL = SKCrashReporting.persistedContextURL
|
|
921
1236
|
let backtraceURL = SKCrashReporting.backtraceFileURL
|
|
@@ -932,6 +1247,21 @@ public final class SKCrashReporting {
|
|
|
932
1247
|
|
|
933
1248
|
let signalString = String(data: markerData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "unknown"
|
|
934
1249
|
|
|
1250
|
+
// The SIGABRT that follows an uncaught NSException is the same crash
|
|
1251
|
+
// the NSException handler already recorded (pending-crash.json, just
|
|
1252
|
+
// sent by `flushPendingCrash`) — including its crashed-session close,
|
|
1253
|
+
// which that handler sends at crash time. Drop the whole signal report
|
|
1254
|
+
// and its artifacts rather than emitting a second, worse event.
|
|
1255
|
+
if SKCrashReporting.isNSExceptionAbortDuplicate(
|
|
1256
|
+
signal: Int32(signalString),
|
|
1257
|
+
recoveredNSExceptionAt: recoveredNSExceptionAt,
|
|
1258
|
+
signalAt: crashedAt
|
|
1259
|
+
) {
|
|
1260
|
+
try? FileManager.default.removeItem(at: backtraceURL)
|
|
1261
|
+
try? FileManager.default.removeItem(at: contextURL)
|
|
1262
|
+
return false
|
|
1263
|
+
}
|
|
1264
|
+
|
|
935
1265
|
// Build event from persisted context (if available)
|
|
936
1266
|
var event = SKErrorEvent(level: .fatal)
|
|
937
1267
|
event.exceptionType = "Signal"
|
|
@@ -1076,18 +1406,12 @@ public final class SKCrashReporting {
|
|
|
1076
1406
|
// stack of 35 real frames contributed NOTHING and every SIGSEGV in
|
|
1077
1407
|
// the app fell back to the constant "Signal|Signal 11" and landed
|
|
1078
1408
|
// in one undifferentiated issue.
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
} else {
|
|
1086
|
-
let img = imageName ?? ""
|
|
1087
|
-
isInApp = !img.hasPrefix("lib") && !img.hasPrefix("_")
|
|
1088
|
-
&& img != "UIKitCore" && img != "Foundation"
|
|
1089
|
-
&& img != "CoreFoundation" && img != "libdyld.dylib"
|
|
1090
|
-
}
|
|
1409
|
+
isInApp = SKCrashReporting.isInAppImage(
|
|
1410
|
+
imagePath: imagePath,
|
|
1411
|
+
imageName: imageName,
|
|
1412
|
+
appExecutable: appExecutable,
|
|
1413
|
+
appBundlePath: SKCrashReporting.appBundlePath
|
|
1414
|
+
)
|
|
1091
1415
|
} else {
|
|
1092
1416
|
symbolName = nil
|
|
1093
1417
|
imageName = nil
|
|
@@ -1105,6 +1429,47 @@ public final class SKCrashReporting {
|
|
|
1105
1429
|
)
|
|
1106
1430
|
}
|
|
1107
1431
|
|
|
1432
|
+
/// The ONE in-app rule, shared by `symbolicate(address:)` (signal / ANR
|
|
1433
|
+
/// frames) and `parseCallStackSymbols` (NSException / captured-error
|
|
1434
|
+
/// frames). Two rules used to exist; the symbol-line parser compared the
|
|
1435
|
+
/// image name against the main executable only, so an Xcode 16 debug
|
|
1436
|
+
/// build (whose code lives in `<App>.debug.dylib`) and every embedded
|
|
1437
|
+
/// framework came out not-in-app, and those stacks contributed nothing to
|
|
1438
|
+
/// grouping.
|
|
1439
|
+
///
|
|
1440
|
+
/// 1. A known image path under the app bundle is in-app — main executable,
|
|
1441
|
+
/// `.debug.dylib`, and everything under `Frameworks/`.
|
|
1442
|
+
/// 2. With no path, the main executable name or its `.debug.dylib` twin.
|
|
1443
|
+
/// 3. With no executable name either, a denylist of system image names.
|
|
1444
|
+
internal static func isInAppImage(
|
|
1445
|
+
imagePath: String?,
|
|
1446
|
+
imageName: String?,
|
|
1447
|
+
appExecutable: String?,
|
|
1448
|
+
appBundlePath: String
|
|
1449
|
+
) -> Bool {
|
|
1450
|
+
if let path = imagePath, !appBundlePath.isEmpty {
|
|
1451
|
+
let bundle = appBundlePath.hasSuffix("/") ? appBundlePath : appBundlePath + "/"
|
|
1452
|
+
if path.hasPrefix(bundle) { return true }
|
|
1453
|
+
}
|
|
1454
|
+
if let appExec = appExecutable, !appExec.isEmpty {
|
|
1455
|
+
return imageName == appExec || imageName == appExec + ".debug.dylib"
|
|
1456
|
+
}
|
|
1457
|
+
let img = imageName ?? ""
|
|
1458
|
+
return !img.isEmpty && !img.hasPrefix("lib") && !img.hasPrefix("_")
|
|
1459
|
+
&& img != "UIKitCore" && img != "Foundation"
|
|
1460
|
+
&& img != "CoreFoundation" && img != "libdyld.dylib"
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
/// Image path for a live in-process address, via `dladdr`. Ordinary
|
|
1464
|
+
/// threads only.
|
|
1465
|
+
private static func imagePath(forAddress address: UInt) -> String? {
|
|
1466
|
+
guard address != 0 else { return nil }
|
|
1467
|
+
var info = Dl_info()
|
|
1468
|
+
guard dladdr(UnsafeRawPointer(bitPattern: address), &info) != 0,
|
|
1469
|
+
let fname = info.dli_fname else { return nil }
|
|
1470
|
+
return String(cString: fname)
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1108
1473
|
internal func writeCrashToDisk(_ event: SKErrorEvent) {
|
|
1109
1474
|
let encoder = JSONEncoder()
|
|
1110
1475
|
encoder.dateEncodingStrategy = .iso8601
|
|
@@ -1133,12 +1498,12 @@ public final class SKCrashReporting {
|
|
|
1133
1498
|
event.exceptionValue = exception.reason
|
|
1134
1499
|
event.mechanism = "nsexception"
|
|
1135
1500
|
event.stacktrace = reporting.parseCallStackSymbols(exception.callStackSymbols)
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
event.user =
|
|
1501
|
+
// Exceptions can originate on this queue (including flush callbacks).
|
|
1502
|
+
// Never sync back onto it or wait for an interrupted breadcrumb writer.
|
|
1503
|
+
event.breadcrumbs = reporting.breadcrumbs.snapshotIfAvailable()
|
|
1504
|
+
let scopeSnapshot = reporting.scope.snapshotIfAvailable()
|
|
1505
|
+
event.tags = scopeSnapshot?.tags
|
|
1506
|
+
event.user = scopeSnapshot?.effectiveUser
|
|
1142
1507
|
event.environment = reporting.configuration?.environment
|
|
1143
1508
|
event.release = reporting.configuration?.release
|
|
1144
1509
|
reporting.applyDeviceContext(to: &event)
|
|
@@ -1262,38 +1627,81 @@ public final class SKCrashReporting {
|
|
|
1262
1627
|
|
|
1263
1628
|
// MARK: - ANR Watchdog
|
|
1264
1629
|
|
|
1630
|
+
/// Start the watchdog, or keep the one already running. Each call used
|
|
1631
|
+
/// to start ANOTHER polling loop, so every repeated `start(...)` (the RN
|
|
1632
|
+
/// bridge on a JS reload) added a watchdog.
|
|
1265
1633
|
private func startANRWatchdog() {
|
|
1266
1634
|
guard let timeout = configuration?.anrTimeoutSeconds, timeout > 0 else { return }
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
var responded = false
|
|
1271
|
-
DispatchQueue.main.async { responded = true }
|
|
1272
|
-
Thread.sleep(forTimeInterval: timeout)
|
|
1273
|
-
if self?.anrWatchdogRunning == true && !responded {
|
|
1274
|
-
self?.captureANR()
|
|
1275
|
-
}
|
|
1276
|
-
}
|
|
1635
|
+
if let existing = anrWatchdog, existing.timeout == timeout {
|
|
1636
|
+
existing.start() // idempotent; restarts one that was stopped
|
|
1637
|
+
return
|
|
1277
1638
|
}
|
|
1639
|
+
anrWatchdog?.stop()
|
|
1640
|
+
let watchdog = ANRWatchdog(timeout: timeout) { [weak self] in self?.captureANR() }
|
|
1641
|
+
anrWatchdog = watchdog
|
|
1642
|
+
watchdog.start()
|
|
1643
|
+
observeLifecycleForANRWatchdog()
|
|
1278
1644
|
}
|
|
1279
1645
|
|
|
1280
1646
|
private func stopANRWatchdog() {
|
|
1281
|
-
|
|
1647
|
+
anrWatchdog?.stop()
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/// Pause the watchdog between didEnterBackground and willEnterForeground.
|
|
1651
|
+
/// Subscribed once; the handler always talks to the current watchdog.
|
|
1652
|
+
private func observeLifecycleForANRWatchdog() {
|
|
1653
|
+
Task { @MainActor [weak self] in
|
|
1654
|
+
#if canImport(UIKit)
|
|
1655
|
+
// A background launch (silent push, background fetch) never sends
|
|
1656
|
+
// didEnterBackground: start this watchdog paused in that case.
|
|
1657
|
+
if UIApplication.shared.applicationState == .background {
|
|
1658
|
+
self?.anrWatchdog?.appDidEnterBackground()
|
|
1659
|
+
}
|
|
1660
|
+
#endif
|
|
1661
|
+
guard let self, self.anrLifecycleToken == nil else { return }
|
|
1662
|
+
self.anrLifecycleToken = AppLifecycle.subscribe { [weak self] event in
|
|
1663
|
+
switch event {
|
|
1664
|
+
case .didEnterBackground:
|
|
1665
|
+
self?.anrWatchdog?.appDidEnterBackground()
|
|
1666
|
+
case .willEnterForeground:
|
|
1667
|
+
self?.anrWatchdog?.appWillEnterForeground()
|
|
1668
|
+
default:
|
|
1669
|
+
break
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1282
1673
|
}
|
|
1283
1674
|
|
|
1284
1675
|
private func captureANR() {
|
|
1285
|
-
// Capture main thread stack
|
|
1286
|
-
//
|
|
1287
|
-
|
|
1288
|
-
|
|
1676
|
+
// Capture the main thread's stack via thread_get_state. When that is
|
|
1677
|
+
// impossible the event is sent WITHOUT a stack: the only other stack
|
|
1678
|
+
// available is the watchdog thread's own, which is identical for
|
|
1679
|
+
// every ANR and would group them all under the watchdog's frames.
|
|
1289
1680
|
var event = buildEvent(level: .error)
|
|
1290
1681
|
event.message = "Application Not Responding"
|
|
1291
1682
|
event.mechanism = "anr"
|
|
1292
|
-
|
|
1683
|
+
SKCrashReporting.applyANRStack(captureMainThreadStack(), to: &event)
|
|
1293
1684
|
guard let finalEvent = applyBeforeSend(event) else { return }
|
|
1294
1685
|
sendEvent(finalEvent)
|
|
1295
1686
|
}
|
|
1296
1687
|
|
|
1688
|
+
/// Extra key set on an ANR event whose main-thread stack could not be read.
|
|
1689
|
+
internal static let anrStackUnavailableExtraKey = "anr_stack"
|
|
1690
|
+
|
|
1691
|
+
/// Attach the main-thread stack to an ANR event, or — when it could not be
|
|
1692
|
+
/// captured — no stack at all plus an explicit marker, so the dashboard
|
|
1693
|
+
/// can say "stack unavailable" instead of showing the watchdog's frames.
|
|
1694
|
+
internal static func applyANRStack(_ frames: [SKStackFrame]?, to event: inout SKErrorEvent) {
|
|
1695
|
+
if let frames, !frames.isEmpty {
|
|
1696
|
+
event.stacktrace = frames
|
|
1697
|
+
return
|
|
1698
|
+
}
|
|
1699
|
+
event.stacktrace = nil
|
|
1700
|
+
var extra = event.extra ?? [:]
|
|
1701
|
+
extra[anrStackUnavailableExtraKey] = "unavailable"
|
|
1702
|
+
event.extra = extra
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1297
1705
|
/// Capture the main thread's stack trace using Mach thread APIs.
|
|
1298
1706
|
///
|
|
1299
1707
|
/// The watchdog thread suspends the main thread, reads its register
|
|
@@ -1305,21 +1713,20 @@ public final class SKCrashReporting {
|
|
|
1305
1713
|
/// here would deadlock the watchdog against it. The address buffer is
|
|
1306
1714
|
/// therefore pre-allocated before the suspend.
|
|
1307
1715
|
///
|
|
1308
|
-
///
|
|
1309
|
-
///
|
|
1310
|
-
///
|
|
1311
|
-
|
|
1716
|
+
/// Returns `nil` if the main thread cannot be inspected. It deliberately
|
|
1717
|
+
/// does NOT fall back to the watchdog thread's own stack — that is the
|
|
1718
|
+
/// same few frames for every ANR, so it merged unrelated hangs into one
|
|
1719
|
+
/// issue while looking like real data.
|
|
1720
|
+
private func captureMainThreadStack() -> [SKStackFrame]? {
|
|
1312
1721
|
// Only ever inspect the main thread from OFF the main thread —
|
|
1313
1722
|
// suspending yourself never resumes.
|
|
1314
|
-
guard !Thread.isMainThread else {
|
|
1315
|
-
return parseCallStackSymbols(Thread.callStackSymbols)
|
|
1316
|
-
}
|
|
1723
|
+
guard !Thread.isMainThread else { return nil }
|
|
1317
1724
|
|
|
1318
1725
|
var threadList: thread_act_array_t?
|
|
1319
1726
|
var threadCount: mach_msg_type_number_t = 0
|
|
1320
1727
|
guard task_threads(mach_task_self_, &threadList, &threadCount) == KERN_SUCCESS,
|
|
1321
1728
|
let threads = threadList, threadCount > 0 else {
|
|
1322
|
-
return
|
|
1729
|
+
return nil
|
|
1323
1730
|
}
|
|
1324
1731
|
defer {
|
|
1325
1732
|
let ptr = UnsafeMutableRawPointer(threads)
|
|
@@ -1338,9 +1745,7 @@ public final class SKCrashReporting {
|
|
|
1338
1745
|
var addresses = [UInt](repeating: 0, count: maxFrames)
|
|
1339
1746
|
var addressCount = 0
|
|
1340
1747
|
|
|
1341
|
-
guard thread_suspend(mainThread) == KERN_SUCCESS else {
|
|
1342
|
-
return parseCallStackSymbols(Thread.callStackSymbols)
|
|
1343
|
-
}
|
|
1748
|
+
guard thread_suspend(mainThread) == KERN_SUCCESS else { return nil }
|
|
1344
1749
|
|
|
1345
1750
|
var pc: UInt = 0
|
|
1346
1751
|
var lr: UInt = 0
|
|
@@ -1419,11 +1824,8 @@ public final class SKCrashReporting {
|
|
|
1419
1824
|
|
|
1420
1825
|
thread_resume(mainThread)
|
|
1421
1826
|
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
// symbols as a last-resort hint that an ANR happened.
|
|
1425
|
-
return parseCallStackSymbols(Thread.callStackSymbols)
|
|
1426
|
-
}
|
|
1827
|
+
// State capture failed — no stack rather than the watchdog's own.
|
|
1828
|
+
guard addressCount > 0 else { return nil }
|
|
1427
1829
|
|
|
1428
1830
|
// Main thread is running again — safe to allocate and symbolicate.
|
|
1429
1831
|
let appExecutable = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String
|
|
@@ -1537,8 +1939,31 @@ public final class SKCrashReporting {
|
|
|
1537
1939
|
/// Each line has the format:
|
|
1538
1940
|
/// `<frame_number> <image_name> <address> <symbol> + <offset>`
|
|
1539
1941
|
/// e.g. `0 MyApp 0x0000000104a8b1c4 $s5MyApp14ViewControllerC11viewDidLoadyyF + 84`
|
|
1942
|
+
///
|
|
1943
|
+
/// In-app classification goes through `isInAppImage` — the same rule as
|
|
1944
|
+
/// `symbolicate(address:)` — with the image PATH resolved by `dladdr` on
|
|
1945
|
+
/// the frame's address. Symbol lines carry only the image's basename,
|
|
1946
|
+
/// which cannot tell an embedded framework from a system one. This parser
|
|
1947
|
+
/// only ever sees stacks of the current process, so the addresses are
|
|
1948
|
+
/// live.
|
|
1540
1949
|
internal func parseCallStackSymbols(_ symbols: [String], returnAddresses: [Int] = []) -> [SKStackFrame] {
|
|
1541
|
-
|
|
1950
|
+
parseCallStackSymbols(
|
|
1951
|
+
symbols,
|
|
1952
|
+
returnAddresses: returnAddresses,
|
|
1953
|
+
appExecutable: Bundle.main.infoDictionary?["CFBundleExecutable"] as? String,
|
|
1954
|
+
appBundlePath: SKCrashReporting.appBundlePath
|
|
1955
|
+
)
|
|
1956
|
+
}
|
|
1957
|
+
|
|
1958
|
+
/// Test seam — the parser with the app identity injected, so in-app
|
|
1959
|
+
/// classification is assertable under `swift test` (where the main
|
|
1960
|
+
/// bundle is the test runner's).
|
|
1961
|
+
internal func parseCallStackSymbols(
|
|
1962
|
+
_ symbols: [String],
|
|
1963
|
+
returnAddresses: [Int] = [],
|
|
1964
|
+
appExecutable: String?,
|
|
1965
|
+
appBundlePath: String
|
|
1966
|
+
) -> [SKStackFrame] {
|
|
1542
1967
|
|
|
1543
1968
|
return symbols.enumerated().map { index, symbol in
|
|
1544
1969
|
let trimmed = symbol.trimmingCharacters(in: .whitespaces)
|
|
@@ -1570,14 +1995,17 @@ public final class SKCrashReporting {
|
|
|
1570
1995
|
rawAddress = addressStr.hasPrefix("0x") ? addressStr : nil
|
|
1571
1996
|
}
|
|
1572
1997
|
|
|
1573
|
-
let
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1998
|
+
let addressValue: UInt? = {
|
|
1999
|
+
if index < returnAddresses.count { return UInt(bitPattern: returnAddresses[index]) }
|
|
2000
|
+
guard addressStr.hasPrefix("0x") else { return nil }
|
|
2001
|
+
return UInt(addressStr.dropFirst(2), radix: 16)
|
|
2002
|
+
}()
|
|
2003
|
+
let isInApp = SKCrashReporting.isInAppImage(
|
|
2004
|
+
imagePath: addressValue.flatMap { SKCrashReporting.imagePath(forAddress: $0) },
|
|
2005
|
+
imageName: imageName,
|
|
2006
|
+
appExecutable: appExecutable,
|
|
2007
|
+
appBundlePath: appBundlePath
|
|
2008
|
+
)
|
|
1581
2009
|
|
|
1582
2010
|
return SKStackFrame(
|
|
1583
2011
|
filename: nil,
|
|
@@ -1631,6 +2059,9 @@ public final class SKCrashReporting {
|
|
|
1631
2059
|
|
|
1632
2060
|
internal var _pendingCrashURL: URL { SKCrashReporting.pendingCrashURL }
|
|
1633
2061
|
|
|
2062
|
+
/// Test seam — the process's ANR watchdog, if one was started.
|
|
2063
|
+
internal var _anrWatchdogForTesting: ANRWatchdog? { anrWatchdog }
|
|
2064
|
+
|
|
1634
2065
|
/// Test seam — the on-disk crash-context snapshot the signal path
|
|
1635
2066
|
/// recovers from. Used to assert the subject is baked in AHEAD of the
|
|
1636
2067
|
/// crash, which is the whole reason the handler needs no Identity call.
|