macOS: Reporting NSException via NSApplication.reportException has wrong stacktrace
Description
When reporting an NSException via the following code
let userInfo: [String: String] = ["user-info-key-1": "user-info-value-1", "user-info-key-2": "user-info-value-2"]
let exception = NSException(name: NSExceptionName("NSException via NSApplication report"), reason: "It doesn't work", userInfo: userInfo)
NSApplication.shared.reportException(exception)
the stacktrace points to the wrong code location.
The Xcode Crash report points to the correct location. Our macOS-Swift and macOS-SwiftUI sample apps have a button called reportNSException to reproduce this easily. When tackling this, we need to be aware of potential impact on grouping.
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000018f3411fc
Termination Reason: Namespace SIGNAL, Code 5 Trace/BPT trap: 5
Terminating Process: exc handler [30000]
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 AppKit 0x18f3411fc -[NSApplication _crashOnException:] + 256
1 AppKit 0x18f340fbc -[NSApplication reportException:] + 460
2 Sentry 0x1047df1c8 __65+[SentryUncaughtNSExceptions swizzleNSApplicationReportException]_block_invoke_2 + 124 (SentryUncaughtNSExceptions.m:26)
3 AppKit 0x18f5a2990 NSApplicationUncaughtExceptionHandler + 152
4 Sentry 0x10483f4ac handleException + 2088 (SentryCrashMonitor_NSException.m:105)
5 Sentry 0x10483ec6c handleUncaughtException + 48 (SentryCrashMonitor_NSException.m:113)
6 Sentry 0x1047df2a4 +[SentryUncaughtNSExceptions capture:] + 196 (SentryUncaughtNSExceptions.m:47)
7 Sentry 0x1047df1a0 __65+[SentryUncaughtNSExceptions swizzleNSApplicationReportException]_block_invoke_2 + 84 (SentryUncaughtNSExceptions.m:26)
8 macOS-SwiftUI.debug.dylib 0x1044a3eb0 ContentView.reportNSException() + 584 (ContentView.swift:40)
9 macOS-SwiftUI.debug.dylib 0x1044a3c60 implicit closure #6 in implicit closure #5 in closure #1 in ContentView.body.getter + 12 (ContentView.swift:15)
10 SwiftUI 0x1ba87c394 closure #1 in ButtonAction.callAsFunction() + 32
11 SwiftUI 0x1b9598280 partial apply for closure #1 in ButtonAction.callAsFunction() + 20
12 SwiftUI 0x1b9ca52d8 partial apply for specialized thunk for @callee_guaranteed () -> (@out A, @error @owned Error) + 28
13 SwiftUI 0x1b9ca2ce8 specialized static MainActor.assumeIsolated<A>(_:file:line:) + 132
14 SwiftUI 0x1ba214978 implicit closure #2 in implicit closure #1 in AppKitButtonStyle.Content.body(environment:) + 468
15 SwiftUI 0x1ba21f4e8 partial apply for closure #2 in SwiftUIAppKitButton.didReleaseButton(_:) + 28
16 SwiftUICore 0x22665a540 partial apply for thunk for @escaping @callee_guaranteed () -> () + 28
17 SwiftUICore 0x2268fc280 static Update.dispatchActions() + 1236
18 SwiftUICore 0x2268fb764 static Update.end() + 212
19 SwiftUI 0x1ba21f124 specialized SwiftUIAppKitButton.didReleaseButton(_:) + 320
20 SwiftUI 0x1ba21d2c4 @objc SwiftUIAppKitButton.didReleaseButton(_:) + 52
21 AppKit 0x18f20da50 -[NSApplication(NSResponder) sendAction:to:from:] + 460
22 AppKit 0x18f6924e4 __40-[NSControl _sendActionsForEvents:view:]_block_invoke + 216
23 AppKit 0x18f6923bc -[NSControl _sendActionsForEvents:view:] + 216
24 AppKit 0x18f20abcc NSControlTrackMouse + 1616
25 AppKit 0x18f20a550 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 144
26 AppKit 0x18f20a3c8 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 580
27 AppKit 0x18f20984c -[NSControl mouseDown:] + 448
28 AppKit 0x18f2086ec -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 3672
29 AppKit 0x18f194044 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 384
30 AppKit 0x18f193cf4 -[NSWindow(NSEventRouting) sendEvent:] + 284
31 AppKit 0x18f99159c -[NSApplication(NSEventRouting) sendEvent:] + 1656
32 AppKit 0x18f5a4984 -[NSApplication _handleEvent:] + 60
33 AppKit 0x18f05fba4 -[NSApplication run] + 520
34 AppKit 0x18f03644c NSApplicationMain + 888
35 SwiftUI 0x1b952c784 specialized runApp(_:) + 160
36 SwiftUI 0x1b99b030c runApp<A>(_:) + 84
37 SwiftUI 0x1b9cbfbec static App.main() + 224
38 macOS-SwiftUI.debug.dylib 0x1044a572c static MacOSSwiftUIApp.$main() + 40
39 macOS-SwiftUI.debug.dylib 0x1044a5d94 __debug_main_executa
Related to ZD ticket: any ETA on when this issue might be addressed?
Sorry no, we don't have an ETA for it yet. I bumped the prio in Linear, but can't make any promises yet.
Just to chime in on this: the inability to get the details really hinders our ability to fix certain classes of crashes. One example is an exception thrown deep inside Apple's UI AppKit framework, even though the code that triggers it lives in application code. Without details of the real source of the exception, we're unable to determine the proper fix.