Skip to content

feat: Fishhook for C++ Exceptions #5128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Samples/macOS-Swift/Shared/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
SentrySDK.start { options in
options.dsn = "https://[email protected]/5428557"
options.debug = true
options.experimental.enableUnhandledCPPExceptionsV2 = true
options.tracesSampleRate = 1.0

let args = ProcessInfo.processInfo.arguments
Expand Down
6 changes: 6 additions & 0 deletions Samples/macOS-Swift/Shared/CppSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Sentry::CppSample::throwCPPException(void)
internalFunction();
}

void
Sentry::CppSample::noExceptCppException() noexcept
{
throw std::invalid_argument("Invalid Argument.");
}

void
Sentry::CppSample::rethrowNoActiveCPPException(void)
{
Expand Down
1 change: 1 addition & 0 deletions Samples/macOS-Swift/Shared/CppSample.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Sentry {
class CppSample {
public:
void throwCPPException();
void noExceptCppException() noexcept;
void rethrowNoActiveCPPException();
};
}
Expand Down
1 change: 1 addition & 0 deletions Samples/macOS-Swift/Shared/CppWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@interface CppWrapper : NSObject
- (void)throwCPPException;
- (void)noExceptCppException;
- (void)rethrowNoActiveCPPException;
- (void)throwNSRangeException;
@end
6 changes: 6 additions & 0 deletions Samples/macOS-Swift/Shared/CppWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ - (void)throwCPPException
cppTool.throwCPPException();
}

- (void)noExceptCppException
{
Sentry::CppSample cppTool;
cppTool.noExceptCppException();
}

- (void)rethrowNoActiveCPPException
{
Sentry::CppSample cppTool;
Expand Down
24 changes: 24 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
620467AC2D3FFD230025F06C /* SentryNSErrorCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 620467AB2D3FFD1C0025F06C /* SentryNSErrorCodable.swift */; };
6205B4A42CE73AA100744684 /* TestDebugImageProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D85790282976A69F00C6AC1F /* TestDebugImageProvider.swift */; };
6205CF262D549D8A001E6049 /* SentryDateCodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6205CF252D549D8A001E6049 /* SentryDateCodableTests.swift */; };
621655662DB12A8900810504 /* SentryCrashMach-OTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 621655652DB12A8900810504 /* SentryCrashMach-OTests.m */; };
621AE74B2C626C230012E730 /* SentryANRTrackerV2.h in Headers */ = {isa = PBXBuildFile; fileRef = 621AE74A2C626C230012E730 /* SentryANRTrackerV2.h */; };
621AE74D2C626C510012E730 /* SentryANRTrackerV2.m in Sources */ = {isa = PBXBuildFile; fileRef = 621AE74C2C626C510012E730 /* SentryANRTrackerV2.m */; };
621D22012DBB7E09006F9C48 /* SentryANRTrackerV1IntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 621D22002DBB7E09006F9C48 /* SentryANRTrackerV1IntegrationTests.swift */; };
Expand Down Expand Up @@ -119,9 +120,14 @@
62872B5F2BA1B7F300A4FA7D /* NSLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62872B5E2BA1B7F300A4FA7D /* NSLock.swift */; };
62872B632BA1B86100A4FA7D /* NSLockTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62872B622BA1B86100A4FA7D /* NSLockTests.swift */; };
62885DA729E946B100554F38 /* TestConncurrentModifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62885DA629E946B100554F38 /* TestConncurrentModifications.swift */; };
628B45342DB8D5E700934391 /* SentryCrashCxaThrowSwapper_Tests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 628B45332DB8D5E700934391 /* SentryCrashCxaThrowSwapper_Tests.mm */; };
628B89022D841D7F004B6F2A /* SentryDateUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 628B89012D841D7F004B6F2A /* SentryDateUtilsTests.swift */; };
629194A92D51F976000F7C6B /* SentryDebugMetaCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629194A82D51F976000F7C6B /* SentryDebugMetaCodable.swift */; };
629258552DAF91940049388F /* SentryCrashStackCursorSelfThreadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629258542DAF91940049388F /* SentryCrashStackCursorSelfThreadTests.swift */; };
629258592DAFA57F0049388F /* SentryCrashCxaThrowSwapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 629258582DAFA57F0049388F /* SentryCrashCxaThrowSwapper.h */; };
6292585B2DAFA5F70049388F /* SentryCrashCxaThrowSwapper.c in Sources */ = {isa = PBXBuildFile; fileRef = 6292585A2DAFA5F70049388F /* SentryCrashCxaThrowSwapper.c */; };
6292585E2DAFA8290049388F /* SentryCrashMach-O.h in Headers */ = {isa = PBXBuildFile; fileRef = 6292585C2DAFA8290049388F /* SentryCrashMach-O.h */; };
6292585F2DAFA8290049388F /* SentryCrashMach-O.c in Sources */ = {isa = PBXBuildFile; fileRef = 6292585D2DAFA8290049388F /* SentryCrashMach-O.c */; };
6293F5752D422A95002BC3BD /* SentryStacktraceCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6293F5742D422A8A002BC3BD /* SentryStacktraceCodable.swift */; };
629428802CB3BF69002C454C /* SwizzleClassNameExclude.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6294287F2CB3BF4E002C454C /* SwizzleClassNameExclude.swift */; };
6294774C2C6F255F00846CBC /* SentryANRTrackerV2Delegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6294774B2C6F255F00846CBC /* SentryANRTrackerV2Delegate.swift */; };
Expand Down Expand Up @@ -1171,6 +1177,7 @@
620379DC2AFE1432005AC0C1 /* SentryBuildAppStartSpans.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBuildAppStartSpans.m; sourceTree = "<group>"; };
620467AB2D3FFD1C0025F06C /* SentryNSErrorCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSErrorCodable.swift; sourceTree = "<group>"; };
6205CF252D549D8A001E6049 /* SentryDateCodableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDateCodableTests.swift; sourceTree = "<group>"; };
621655652DB12A8900810504 /* SentryCrashMach-OTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentryCrashMach-OTests.m"; sourceTree = "<group>"; };
621AE74A2C626C230012E730 /* SentryANRTrackerV2.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryANRTrackerV2.h; path = include/SentryANRTrackerV2.h; sourceTree = "<group>"; };
621AE74C2C626C510012E730 /* SentryANRTrackerV2.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryANRTrackerV2.m; sourceTree = "<group>"; };
621AE74E2C626CF70012E730 /* SentryANRTrackerV2Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryANRTrackerV2Tests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1205,9 +1212,14 @@
62872B5E2BA1B7F300A4FA7D /* NSLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLock.swift; sourceTree = "<group>"; };
62872B622BA1B86100A4FA7D /* NSLockTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSLockTests.swift; sourceTree = "<group>"; };
62885DA629E946B100554F38 /* TestConncurrentModifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestConncurrentModifications.swift; sourceTree = "<group>"; };
628B45332DB8D5E700934391 /* SentryCrashCxaThrowSwapper_Tests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryCrashCxaThrowSwapper_Tests.mm; sourceTree = "<group>"; };
628B89012D841D7F004B6F2A /* SentryDateUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDateUtilsTests.swift; sourceTree = "<group>"; };
629194A82D51F976000F7C6B /* SentryDebugMetaCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryDebugMetaCodable.swift; sourceTree = "<group>"; };
629258542DAF91940049388F /* SentryCrashStackCursorSelfThreadTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashStackCursorSelfThreadTests.swift; sourceTree = "<group>"; };
629258582DAFA57F0049388F /* SentryCrashCxaThrowSwapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryCrashCxaThrowSwapper.h; sourceTree = "<group>"; };
6292585A2DAFA5F70049388F /* SentryCrashCxaThrowSwapper.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = SentryCrashCxaThrowSwapper.c; sourceTree = "<group>"; };
6292585C2DAFA8290049388F /* SentryCrashMach-O.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentryCrashMach-O.h"; sourceTree = "<group>"; };
6292585D2DAFA8290049388F /* SentryCrashMach-O.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = "SentryCrashMach-O.c"; sourceTree = "<group>"; };
6293F5742D422A8A002BC3BD /* SentryStacktraceCodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryStacktraceCodable.swift; sourceTree = "<group>"; };
6294287F2CB3BF4E002C454C /* SwizzleClassNameExclude.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwizzleClassNameExclude.swift; sourceTree = "<group>"; };
6294774B2C6F255F00846CBC /* SentryANRTrackerV2Delegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryANRTrackerV2Delegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2992,6 +3004,10 @@
7B883F48253D714C00879E62 /* SentryCrashUUIDConversion.c */,
71F11CDEF5952DF5CC69AC74 /* SentryCrashUUIDConversion.h */,
7B31C290277B04A000337126 /* SentryCrashPlatformSpecificDefines.h */,
629258582DAFA57F0049388F /* SentryCrashCxaThrowSwapper.h */,
6292585A2DAFA5F70049388F /* SentryCrashCxaThrowSwapper.c */,
6292585C2DAFA8290049388F /* SentryCrashMach-O.h */,
6292585D2DAFA8290049388F /* SentryCrashMach-O.c */,
);
path = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -3060,6 +3076,8 @@
0ADC33EF28D9BE690078D980 /* TestSentryUIDeviceWrapper.swift */,
7B984A9E28E572AF001F4BEE /* CrashReport.swift */,
7BF69E062987D1FE002EBCA4 /* SentryCrashDoctorTests.swift */,
621655652DB12A8900810504 /* SentryCrashMach-OTests.m */,
628B45332DB8D5E700934391 /* SentryCrashCxaThrowSwapper_Tests.mm */,
629258542DAF91940049388F /* SentryCrashStackCursorSelfThreadTests.swift */,
);
path = SentryCrash;
Expand Down Expand Up @@ -4424,6 +4442,7 @@
7BFC169B2524995700FF6266 /* SentryMessage.h in Headers */,
7B18DE4028D9F748004845C6 /* SentryNSNotificationCenterWrapper.h in Headers */,
03F84D1E27DD414C008FE43F /* SentryBacktrace.hpp in Headers */,
629258592DAFA57F0049388F /* SentryCrashCxaThrowSwapper.h in Headers */,
63AA76991EB9C1C200D153DE /* SentryDefines.h in Headers */,
D86B6835294348A400B8B1FC /* SentryAttachment+Private.h in Headers */,
84AF45A629A7FFA500FBB177 /* SentryProfiledTracerConcurrency.h in Headers */,
Expand Down Expand Up @@ -4614,6 +4633,7 @@
62A456E32B0370AA003F19A1 /* SentryUIEventTrackerTransactionMode.h in Headers */,
63FE714B20DA4C1100CDBAE8 /* SentryCrashString.h in Headers */,
7BCFBD6D2681D0A900BC27D8 /* SentryCrashScopeObserver.h in Headers */,
6292585E2DAFA8290049388F /* SentryCrashMach-O.h in Headers */,
63FE715320DA4C1100CDBAE8 /* SentryCrashObjCApple.h in Headers */,
63FE710120DA4C1000CDBAE8 /* SentryCrashDate.h in Headers */,
7B4E24FC251C97B500060D68 /* SentrySession.h in Headers */,
Expand Down Expand Up @@ -4972,6 +4992,7 @@
62C97D3A2CC64E6B00DDA204 /* SentryUncaughtNSExceptions.m in Sources */,
03F84D3727DD4191008FE43F /* SentrySamplingProfiler.cpp in Sources */,
8453421628BE8A9500C22EEC /* SentrySpanStatus.m in Sources */,
6292585B2DAFA5F70049388F /* SentryCrashCxaThrowSwapper.c in Sources */,
7B08A3472924CF9C0059603A /* SentryMetricKitIntegration.m in Sources */,
623FD9022D3FA5E000803EDA /* SentryFrameCodable.swift in Sources */,
62BDDD122D51FD540024CCD1 /* SentryThreadCodable.swift in Sources */,
Expand Down Expand Up @@ -5263,6 +5284,7 @@
D820CDB72BB1895F00BA339D /* SentrySessionReplayIntegration.m in Sources */,
632F43521F581D5400A18A36 /* SentryCrashExceptionApplication.m in Sources */,
620379DD2AFE1432005AC0C1 /* SentryBuildAppStartSpans.m in Sources */,
6292585F2DAFA8290049388F /* SentryCrashMach-O.c in Sources */,
7B77BE3727EC8460003C9020 /* SentryDiscardReasonMapper.m in Sources */,
63FE712520DA4C1000CDBAE8 /* SentryCrashSignalInfo.c in Sources */,
63FE70F320DA4C1000CDBAE8 /* SentryCrashMonitor_Signal.c in Sources */,
Expand All @@ -5277,6 +5299,7 @@
8ED3D306264DFE700049393B /* SwiftDescriptorTests.swift in Sources */,
7BFC16AD2524BCE700FF6266 /* SentryMessageTests.swift in Sources */,
7BF6505F292B77EC00BBA5A8 /* SentryMetricKitIntegrationTests.swift in Sources */,
628B45342DB8D5E700934391 /* SentryCrashCxaThrowSwapper_Tests.mm in Sources */,
629690532AD3E060000185FA /* SentryReachabilitySwiftTests.swift in Sources */,
7BA61CC6247CFC5F00C130A8 /* SentryCrashDefaultBinaryImageProviderTests.swift in Sources */,
7BBC827925DFD7D7005F1ED8 /* SentryInAppLogicTests.swift in Sources */,
Expand Down Expand Up @@ -5330,6 +5353,7 @@
D8FFE50C2703DBB400607131 /* SwizzlingCallTests.swift in Sources */,
7BFAA6E7297AA16A00E7E02E /* SentryCrashMonitor_CppException_Tests.mm in Sources */,
9286059929A50BAB00F96038 /* SentryGeoTests.swift in Sources */,
621655662DB12A8900810504 /* SentryCrashMach-OTests.m in Sources */,
D8B76B0828081461000A58C4 /* TestSentryScreenShot.swift in Sources */,
A8AFFCD22907DA7600967CD7 /* SentryHttpStatusCodeRangeTests.swift in Sources */,
7BE2C7F8257000A4003B66C7 /* SentryTestIntegration.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Sentry/SentryAsyncSafeLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void sentry_asyncLogC(const char *level, const char *file, int line, const char
#define SENTRY_ASYNC_SAFE_LOG_LEVEL_DEBUG 40
#define SENTRY_ASYNC_SAFE_LOG_LEVEL_TRACE 50

#define SENTRY_ASYNC_SAFE_LOG_LEVEL SENTRY_ASYNC_SAFE_LOG_LEVEL_ERROR
#define SENTRY_ASYNC_SAFE_LOG_LEVEL SENTRY_ASYNC_SAFE_LOG_LEVEL_TRACE

#define a_SENTRY_ASYNC_SAFE_LOG(LEVEL, FMT, ...) \
i_SENTRY_ASYNC_SAFE_LOG(LEVEL, __FILE__, __LINE__, FMT, ##__VA_ARGS__)
Expand Down
9 changes: 8 additions & 1 deletion Sources/Sentry/SentryCrashIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#import "SentryCrashC.h"
#import "SentryCrashIntegrationSessionHandler.h"
#import "SentryCrashMonitor_CPPException.h"
#include "SentryCrashMonitor_Signal.h"
#import "SentryCrashWrapper.h"
#import "SentryDispatchQueueWrapper.h"
Expand Down Expand Up @@ -120,7 +121,8 @@ - (BOOL)installWithOptions:(nonnull SentryOptions *)options

[self startCrashHandler:options.cacheDirectoryPath
enableSigtermReporting:enableSigtermReporting
enableReportingUncaughtExceptions:enableUncaughtNSExceptionReporting];
enableReportingUncaughtExceptions:enableUncaughtNSExceptionReporting
enableCppExceptionsV2:options.experimental.enableUnhandledCPPExceptionsV2];

[self configureScope];

Expand All @@ -139,6 +141,7 @@ - (SentryIntegrationOption)integrationOptions
- (void)startCrashHandler:(NSString *)cacheDirectory
enableSigtermReporting:(BOOL)enableSigtermReporting
enableReportingUncaughtExceptions:(BOOL)enableReportingUncaughtExceptions
enableCppExceptionsV2:(BOOL)enableCppExceptionsV2
{
void (^block)(void) = ^{
BOOL canSendReports = NO;
Expand Down Expand Up @@ -166,6 +169,10 @@ - (void)startCrashHandler:(NSString *)cacheDirectory
}
#endif // TARGET_OS_OSX

if (enableCppExceptionsV2) {
sentrycrashcm_cppexception_enable_V2();
}

// We need to send the crashed event together with the crashed session in the same envelope
// to have proper statistics in release health. To achieve this we need both synchronously
// in the hub. The crashed event is converted from a SentryCrashReport to an event in
Expand Down
10 changes: 7 additions & 3 deletions Sources/Sentry/SentryCrashReportConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,13 @@ - (SentryDebugMeta *)debugMetaFromBinaryImageDictionary:(NSDictionary *)sourceIm
if ([exceptionType isEqualToString:@"nsexception"]) {
exception = [self parseNSException];
} else if ([exceptionType isEqualToString:@"cpp_exception"]) {
exception =
[[SentryException alloc] initWithValue:self.exceptionContext[@"cpp_exception"][@"name"]
type:@"C++ Exception"];
NSString *cppExceptionName = self.exceptionContext[@"cpp_exception"][@"name"];
NSString *cppExceptionReason = self.exceptionContext[@"reason"];

NSString *exceptionValue =
[NSString stringWithFormat:@"%@: %@", cppExceptionName, cppExceptionReason];

exception = [[SentryException alloc] initWithValue:exceptionValue type:@"C++ Exception"];
} else if ([exceptionType isEqualToString:@"mach"]) {
exception = [[SentryException alloc]
initWithValue:[NSString stringWithFormat:@"Exception %@, Code %@, Subcode %@",
Expand Down
5 changes: 5 additions & 0 deletions Sources/Sentry/include/SentryCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@
#define NEVER_INLINE
#endif

/* KEEP_FUNCTION_IN_STACKTRACE */
#define KEEP_FUNCTION_IN_STACKTRACE __attribute__((disable_tail_calls))

#define THWART_TAIL_CALL_OPTIMISATION __asm__ __volatile__("");

/* NO_RETURN */

#if !defined(NO_RETURN) && COMPILER(GCC_COMPATIBLE)
Expand Down
Loading
Loading