getsentry / sentry-cocoa

The official Sentry SDK for iOS, tvOS, macOS, watchOS.
https://sentry.io/for/cocoa/
MIT License
803 stars 316 forks source link

Capture NSException without SentryCrashExceptionApplication #2854

Closed brustolin closed 1 week ago

brustolin commented 1 year ago

Description

In order to capture unhandled NSException in a MacOS application, is necessary to change the Principal class to SentryCrashExceptionApplication (see docs).

It would be better if we could find a solution that don't require changing Principal class.

┆Issue is synchronized with this Jira Improvement by Unito

github-actions[bot] commented 1 year ago

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

kdrag0n commented 1 year ago

It'd be great if this is implemented by sending the "Application Specific Information" or "Application Specific Backtrace" section seen in macOS crash reports:

Application Specific Backtrace 0:
0   CoreFoundation                      0x0000000191b53154 __exceptionPreprocess + 176
1   libobjc.A.dylib                     0x00000001916724d4 objc_exception_throw + 60
2   Foundation                          0x0000000192b06a98 -[NSCalendarDate initWithCoder:] + 0
3   AppKit                              0x0000000194f1d8c8 -[NSToolbar _itemAtIndex:] + 168
4   AppKit                              0x0000000194f1d75c -[NSToolbar _removeItemAtIndex:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:] + 52
5   AppKit                              0x000000019546f32c -[NSToolbar _syncToChangedToolbar:itemRemoved:] + 100
6   AppKit                              0x0000000194eea260 _sendToolbarChangeSyncSEL + 276
7   AppKit                              0x0000000194f1db08 -[NSToolbar _notifyFamily_DidRemoveItemAtIndex:] + 140
8   AppKit                              0x0000000194f1d818 -[NSToolbar _removeItemAtIndex:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:] + 240
9   SwiftUI                             0x00000001bb079758 OUTLINED_FUNCTION_0 + 38100
10  SwiftUI                             0x00000001bab5bac4 OUTLINED_FUNCTION_114 + 764
11  SwiftUI                             0x00000001bb1dbcec OUTLINED_FUNCTION_0 + 996
12  SwiftUI                             0x00000001b9f02170 OUTLINED_FUNCTION_1 + 7648
13  SwiftUI                             0x00000001bb078fb0 OUTLINED_FUNCTION_0 + 36140
14  SwiftUI                             0x00000001ba810a24 __swift_memcpy409_8 + 7456
15  SwiftUI                             0x00000001ba815f00 __swift_memcpy409_8 + 29180
16  SwiftUI                             0x00000001baf21f24 OUTLINED_FUNCTION_0 + 5152
17  SwiftUI                             0x00000001ba810884 __swift_memcpy409_8 + 7040
18  SwiftUI                             0x00000001ba809b6c OUTLINED_FUNCTION_1 + 64040
19  SwiftUI                             0x00000001ba808858 OUTLINED_FUNCTION_1 + 59156
20  SwiftUI                             0x00000001ba808e10 OUTLINED_FUNCTION_1 + 60620
21  SwiftUI                             0x00000001baf35d9c OUTLINED_FUNCTION_0 + 86680
22  SwiftUI                             0x00000001ba38c62c OUTLINED_FUNCTION_2 + 21228
23  SwiftUI                             0x00000001ba38c288 OUTLINED_FUNCTION_2 + 20296
24  SwiftUI                             0x00000001baec8924 OUTLINED_FUNCTION_23 + 88840
25  SwiftUI                             0x00000001baeb32b4 OUTLINED_FUNCTION_23 + 1176
26  SwiftUI                             0x00000001baf2e238 OUTLINED_FUNCTION_0 + 55092
27  SwiftUI                             0x00000001baf39ec4 OUTLINED_FUNCTION_0 + 103360
28  SwiftUI                             0x00000001baf22a40 OUTLINED_FUNCTION_0 + 7996
29  AppKit                              0x0000000194d11790 +[NSAnimationContext runAnimationGroup:] + 56
30  SwiftUI                             0x00000001baf2e11c OUTLINED_FUNCTION_0 + 54808
31  SwiftUI                             0x00000001baf2e68c OUTLINED_FUNCTION_0 + 56200
32  AppKit                              0x00000001954c632c ___NSViewLayout_block_invoke + 592
33  AppKit                              0x0000000194d3d5f8 NSPerformVisuallyAtomicChange + 108
34  AppKit                              0x0000000194d42790 _NSViewLayout + 96
35  AppKit                              0x00000001954bcc14 __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 364
36  AppKit                              0x0000000194d3d5f8 NSPerformVisuallyAtomicChange + 108
37  AppKit                              0x0000000194d42724 -[NSView _layoutSubtreeWithOldSize:] + 100
38  AppKit                              0x00000001954bcd58 __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 688
39  AppKit                              0x0000000194d3d5f8 NSPerformVisuallyAtomicChange + 108
40  AppKit                              0x0000000194d42724 -[NSView _layoutSubtreeWithOldSize:] + 100
41  AppKit                              0x00000001954bd718 __56-[NSView _layoutSubtreeIfNeededAndAllowTemporaryEngine:]_block_invoke + 800
42  AppKit                              0x0000000194d3d5f8 NSPerformVisuallyAtomicChange + 108
43  AppKit                              0x0000000194d42274 -[NSView _layoutSubtreeIfNeededAndAllowTemporaryEngine:] + 100
44  AppKit                              0x0000000194d3d5f8 NSPerformVisuallyAtomicChange + 108
45  AppKit                              0x0000000194d42204 -[NSView layoutSubtreeIfNeeded] + 96
46  AppKit                              0x000000019570e3bc -[NSWindow(NSConstraintBasedLayoutInternal) _layoutViewTree] + 104
47  AppKit                              0x000000019570e544 -[NSWindow(NSConstraintBasedLayoutInternal) layoutIfNeeded] + 240
48  AppKit                              0x0000000194da1694 __NSWindowGetDisplayCycleObserverForLayout_block_invoke + 364
49  AppKit                              0x0000000194da0c30 NSDisplayCycleObserverInvoke + 168
50  AppKit                              0x0000000194da088c NSDisplayCycleFlush + 644
51  QuartzCore                          0x0000000198fbdce4 _ZN2CA11Transaction19run_commit_handlersE18CATransactionPhase + 120
52  QuartzCore                          0x0000000198fbcaa0 _ZN2CA11Transaction6commitEv + 320
53  AppKit                              0x0000000194e22c7c __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 272
54  AppKit                              0x00000001954fef7c ___NSRunLoopObserverCreateWithHandler_block_invoke + 64
55  CoreFoundation                      0x0000000191ad99f0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
56  CoreFoundation                      0x0000000191ad98dc __CFRunLoopDoObservers + 532
57  CoreFoundation                      0x0000000191ad8f14 __CFRunLoopRun + 776
58  CoreFoundation                      0x0000000191ad84b8 CFRunLoopRunSpecific + 612
59  HIToolbox                           0x000000019b322c40 RunCurrentEventLoopInMode + 292
60  HIToolbox                           0x000000019b3228d0 ReceiveNextEventCommon + 220
61  HIToolbox                           0x000000019b3227d4 _BlockUntilNextEventMatchingListInModeWithFilter + 76
62  AppKit                              0x0000000194cf9d44 _DPSNextEvent + 636
63  AppKit                              0x0000000194cf8ee0 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716
64  AppKit                              0x0000000194ced344 -[NSApplication run] + 464
65  AppKit                              0x0000000194cc4794 NSApplicationMain + 880
66  SwiftUI                             0x00000001b9dbf6b8 OUTLINED_FUNCTION_8 + 8272
67  SwiftUI                             0x00000001baf205ac OUTLINED_FUNCTION_14 + 188
68  SwiftUI                             0x00000001ba7a0c48 OUTLINED_FUNCTION_1 + 136
69  OrbStack                            0x00000001043dbe94 OrbStack + 32404
70  dyld                                0x00000001916a3f28 start + 2236

As compared to the main thread's backtrace:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   AppKit                                 0x194fdaaa4 -[NSApplication _crashOnException:] + 240
1   AppKit                                 0x194e22df0 __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 644
2   AppKit                                 0x1954fef7c ___NSRunLoopObserverCreateWithHandler_block_invoke + 64
3   CoreFoundation                         0x191ad99f0 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
4   CoreFoundation                         0x191ad98dc __CFRunLoopDoObservers + 532
5   CoreFoundation                         0x191ad8f14 __CFRunLoopRun + 776
6   CoreFoundation                         0x191ad84b8 CFRunLoopRunSpecific + 612
7   HIToolbox                              0x19b322c40 RunCurrentEventLoopInMode + 292
8   HIToolbox                              0x19b3228d0 ReceiveNextEventCommon + 220
9   HIToolbox                              0x19b3227d4 _BlockUntilNextEventMatchingListInModeWithFilter + 76
10  AppKit                                 0x194cf9d44 _DPSNextEvent + 636
11  AppKit                                 0x194cf8ee0 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716
12  AppKit                                 0x194ced344 -[NSApplication run] + 464
13  AppKit                                 0x194cc4794 NSApplicationMain + 880
14  SwiftUI                                0x1b9dbf6b8 0x1b9d24000 + 636600
15  SwiftUI                                0x1baf205ac 0x1b9d24000 + 18859436
16  SwiftUI                                0x1ba7a0c48 0x1b9d24000 + 10996808
17  OrbStack                               0x1043dbe94 0x1043d4000 + 32404
18  dyld                                   0x1916a3f28 start + 2236

References: https://github.com/microsoft/appcenter-sdk-apple/issues/1944#issuecomment-701378592, https://github.com/microsoft/appcenter/issues/857

rodolfoBee commented 4 months ago

@brustolin This is still an issue in v8.26.0 of the SDK. Even after changing the principal class to SentryCrashExceptionApplication some exception are still reported only as NSApplication _crashOnException from AppKit without information on the original exception (or the Application Specific Backtrace mentioned in the comment above)

Information on the most recent event is added to the shadow Jira ticket as it might contain PII.

brustolin commented 4 months ago

Thanks for the update @rodolfoBee. We still dont have a solution for this.

philipphofmann commented 1 week ago

@brustolin, we actually have a different solution for this; see docs

You only need to set this flag.

UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])

We also use this in the macOS sample app https://github.com/getsentry/sentry-cocoa/blob/22339689aae7f92b52444952a76d5cab40b5efa3/Samples/macOS-Swift/macOS-Swift/AppDelegate.swift#L8

and the NSPrincipalClass is NSApplication. https://github.com/getsentry/sentry-cocoa/blob/22339689aae7f92b52444952a76d5cab40b5efa3/Samples/macOS-Swift/macOS-Swift/Info.plist#L35-L36

We can close this. Please reopen if I missed something.