Skip to content

Commit 3e2e204

Browse files
Merge pull request #1 from justin-doordash/justinlennox/mergeRepos
Squash and merge commit
2 parents 061c982 + 4e9ead0 commit 3e2e204

File tree

10 files changed

+124
-1
lines changed

10 files changed

+124
-1
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
033082AA2AE0D6A1003D29A1 /* SentryBeforeCrashIntegration.h in Headers */ = {isa = PBXBuildFile; fileRef = 033082A92AE0D6A1003D29A1 /* SentryBeforeCrashIntegration.h */; };
11+
033082AC2AE0D6B1003D29A1 /* SentryBeforeCrashIntegration.m in Sources */ = {isa = PBXBuildFile; fileRef = 033082AB2AE0D6B1003D29A1 /* SentryBeforeCrashIntegration.m */; };
1012
0354A22B2A134D9C003C3A04 /* SentryProfilerState.h in Headers */ = {isa = PBXBuildFile; fileRef = 0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */; };
1113
03BCC38A27E1BF49003232C7 /* SentryTime.h in Headers */ = {isa = PBXBuildFile; fileRef = 03BCC38927E1BF49003232C7 /* SentryTime.h */; };
1214
03BCC38C27E1C01A003232C7 /* SentryTime.mm in Sources */ = {isa = PBXBuildFile; fileRef = 03BCC38B27E1C01A003232C7 /* SentryTime.mm */; };
@@ -973,6 +975,8 @@
973975
/* End PBXCopyFilesBuildPhase section */
974976

975977
/* Begin PBXFileReference section */
978+
033082A92AE0D6A1003D29A1 /* SentryBeforeCrashIntegration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryBeforeCrashIntegration.h; path = include/SentryBeforeCrashIntegration.h; sourceTree = "<group>"; };
979+
033082AB2AE0D6B1003D29A1 /* SentryBeforeCrashIntegration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SentryBeforeCrashIntegration.m; sourceTree = "<group>"; };
976980
0354A22A2A134D9C003C3A04 /* SentryProfilerState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryProfilerState.h; path = Sources/Sentry/include/SentryProfilerState.h; sourceTree = SOURCE_ROOT; };
977981
035E73C727D56757005EEB11 /* SentryBacktraceTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryBacktraceTests.mm; sourceTree = "<group>"; };
978982
035E73C927D57398005EEB11 /* SentryThreadHandleTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentryThreadHandleTests.mm; sourceTree = "<group>"; };
@@ -3092,6 +3096,8 @@
30923096
7BCFBD6E2681D0EE00BC27D8 /* SentryCrashScopeObserver.m */,
30933097
7B96571F26830C9100C66E25 /* SentryScopeSyncC.h */,
30943098
7B96572126830D2400C66E25 /* SentryScopeSyncC.c */,
3099+
033082A92AE0D6A1003D29A1 /* SentryBeforeCrashIntegration.h */,
3100+
033082AB2AE0D6B1003D29A1 /* SentryBeforeCrashIntegration.m */,
30953101
D84F833B2A1CC401005828E0 /* SentrySwiftAsyncIntegration.h */,
30963102
D84F833C2A1CC401005828E0 /* SentrySwiftAsyncIntegration.m */,
30973103
);
@@ -3906,6 +3912,7 @@
39063912
D8BBD32728FD9FC00011F850 /* SentrySwift.h in Headers */,
39073913
8E4E7C7425DAAB49006AB9E2 /* SentrySpanProtocol.h in Headers */,
39083914
8EC4CF4A25C38DAA0093DEE9 /* SentrySpanStatus.h in Headers */,
3915+
033082AA2AE0D6A1003D29A1 /* SentryBeforeCrashIntegration.h in Headers */,
39093916
8ECC673D25C23996000E2BF6 /* SentrySpanId.h in Headers */,
39103917
03F84D2227DD414C008FE43F /* SentryStackFrame.hpp in Headers */,
39113918
8ECC673E25C23996000E2BF6 /* SentrySpanContext.h in Headers */,
@@ -4706,6 +4713,7 @@
47064713
D8F016B32B9622D6007B9AFB /* SentryId.swift in Sources */,
47074714
D865893029D6ECA7000BE151 /* SentryCrashBinaryImageCache.c in Sources */,
47084715
7BC9A20428F4166D001E7C4C /* SentryMeasurementValue.m in Sources */,
4716+
033082AC2AE0D6B1003D29A1 /* SentryBeforeCrashIntegration.m in Sources */,
47094717
D859696B27BECD8F0036A46E /* SentryCoreDataTrackingIntegration.m in Sources */,
47104718
7BD86EC7264A641D005439DB /* SentrySysctl.m in Sources */,
47114719
84281C432A578E5600EE88F2 /* SentryProfilerState.mm in Sources */,

Sources/Sentry/Public/SentryDefines.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ typedef id<SentrySpan> _Nullable (^SentryBeforeSendSpanCallback)(id<SentrySpan>
9999
*/
100100
typedef BOOL (^SentryBeforeCaptureScreenshotCallback)(SentryEvent *_Nonnull event);
101101

102+
/**
103+
* A callback to be notified right before the program terminated with a crash.
104+
*/
105+
typedef void (^SentryBeforeCrashCallback)(NSString *_Nonnull eventID);
106+
102107
/**
103108
* A callback to be notified when the last program execution terminated with a crash.
104109
*/

Sources/Sentry/Public/SentryEvent.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ NS_SWIFT_NAME(Event)
1616
*/
1717
@property (nonatomic, strong) SentryId *eventId;
1818

19+
/**
20+
* The identifier of the Sentry report.
21+
*/
22+
@property (nonatomic, copy) NSString *reportId;
23+
1924
/**
2025
* Message of the event.
2126
*/

Sources/Sentry/Public/SentryOptions.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ NS_SWIFT_NAME(Options)
9292

9393
#endif // !TARGET_OS_WATCH
9494

95+
/**
96+
* C Function to call before a crash to give the callee an opportunity to
97+
* run a function before we crash. NULL = ignore.
98+
*
99+
* @warning Only call async-safe functions from this function! DO NOT call
100+
* Objective-C methods!!!
101+
* @note Default value is nil.
102+
*/
103+
@property (nullable, nonatomic, copy) SentryBeforeCrashCallback beforeCrash;
104+
105+
106+
/**
107+
* When enabled, and when the beforeCrash handler is not null, this allows the callee
108+
* an opportunity to run a function before we crash.
109+
* @note Default value @c NO.
110+
*/
111+
@property (nonatomic, assign) BOOL enableBeforeCrashHandler;
112+
95113
/**
96114
* How many breadcrumbs do you want to keep in memory?
97115
* @note Default is @c 100 .
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// SentryBeforeCrashIntegration.m
3+
// Sentry
4+
//
5+
// Created by Filip Busic on 10/16/23.
6+
// Copyright © 2023 Sentry. All rights reserved.
7+
//
8+
9+
#import "SentryBeforeCrashIntegration.h"
10+
#import "SentryCrashC.h"
11+
12+
static void (^g_beforeCrashOptionsCallback)(NSString *) = nil;
13+
14+
// Define a callback function
15+
void beforeCrashCallback(const char *eventID) {
16+
if (g_beforeCrashOptionsCallback) {
17+
g_beforeCrashOptionsCallback([NSString stringWithUTF8String:eventID]);
18+
}
19+
}
20+
21+
@implementation SentryBeforeCrashIntegration
22+
23+
- (BOOL)installWithOptions:(nonnull SentryOptions *)options
24+
{
25+
if (![super installWithOptions:options]) {
26+
return NO;
27+
}
28+
29+
if (options.enableBeforeCrashHandler) {
30+
g_beforeCrashOptionsCallback = options.beforeCrash;
31+
sentrycrash_setBeforeCrashCallback(&beforeCrashCallback);
32+
}
33+
34+
return options.enableBeforeCrashHandler;
35+
}
36+
37+
- (void)uninstall
38+
{
39+
g_beforeCrashOptionsCallback = nil;
40+
sentrycrash_setBeforeCrashCallback(nil);
41+
}
42+
43+
- (SentryIntegrationOption)integrationOptions
44+
{
45+
return kIntegrationOptionDebuggerNotAttached;
46+
}
47+
48+
@end

Sources/Sentry/SentryCrashReportConverter.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ - (SentryEvent *_Nullable)convertReportToEvent
106106
} else {
107107
event.timestamp = sentry_fromIso8601String(self.report[@"report"][@"timestamp"]);
108108
}
109+
event.reportId = self.report[@"report"][@"id"];
109110
event.threads = [self convertThreads];
110111
event.debugMeta = [self debugMetaForThreads:event.threads];
111112
event.exceptions = [self convertExceptions];

Sources/Sentry/SentryOptions.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "SentrySwift.h"
2020
#import "SentrySwiftAsyncIntegration.h"
2121
#import "SentryTracer.h"
22+
#import "SentryBeforeCrashIntegration.h"
2223
#import <objc/runtime.h>
2324

2425
#if SENTRY_HAS_UIKIT
@@ -65,7 +66,8 @@ @implementation SentryOptions {
6566
NSStringFromClass([SentryCoreDataTrackingIntegration class]),
6667
NSStringFromClass([SentryFileIOTrackingIntegration class]),
6768
NSStringFromClass([SentryNetworkTrackingIntegration class]),
68-
NSStringFromClass([SentrySwiftAsyncIntegration class])
69+
NSStringFromClass([SentrySwiftAsyncIntegration class]),
70+
NSStringFromClass([SentryBeforeCrashIntegration class])
6971
]
7072
.mutableCopy;
7173

@@ -87,6 +89,7 @@ - (instancetype)init
8789
#if !TARGET_OS_WATCH
8890
self.enableSigtermReporting = NO;
8991
#endif // !TARGET_OS_WATCH
92+
self.enableBeforeCrashHandler = NO;
9093
self.diagnosticLevel = kSentryLevelDebug;
9194
self.debug = NO;
9295
self.maxBreadcrumbs = defaultMaxBreadcrumbs;
@@ -448,6 +451,14 @@ - (BOOL)validateOptions:(NSDictionary<NSString *, id> *)options
448451
if ([self isBlock:options[@"tracesSampler"]]) {
449452
self.tracesSampler = options[@"tracesSampler"];
450453
}
454+
455+
[self setBool:options[@"enableBeforeCrashHandler"]
456+
block:^(BOOL value) { self->_enableBeforeCrashHandler = value; }];
457+
458+
if ([self isBlock:options[@"beforeCrash"]]) {
459+
self.beforeCrash = options[@"beforeCrash"];
460+
}
461+
451462
#pragma clang diagnostic push
452463
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
453464
if ([options[@"enableTracing"] isKindOfClass:NSNumber.self]) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#import "SentryBaseIntegration.h"
2+
#import "SentryIntegrationProtocol.h"
3+
#import <Foundation/Foundation.h>
4+
5+
NS_ASSUME_NONNULL_BEGIN
6+
7+
@interface SentryBeforeCrashIntegration : SentryBaseIntegration <SentryIntegrationProtocol>
8+
9+
@end
10+
11+
NS_ASSUME_NONNULL_END

Sources/SentryCrash/Recording/SentryCrashC.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static SentryCrashMonitorType g_monitoring = SentryCrashMonitorTypeProductionSaf
5757
static char g_lastCrashReportFilePath[SentryCrashFU_MAX_PATH_LENGTH];
5858
static void (*g_saveScreenShot)(const char *) = 0;
5959
static void (*g_saveViewHierarchy)(const char *) = 0;
60+
static void (*g_beforeCrashCallback)(const char *) = 0;
6061

6162
// ============================================================================
6263
#pragma mark - Utility -
@@ -106,6 +107,10 @@ onCrash(struct SentryCrash_MonitorContext *monitorContext)
106107
}
107108
}
108109
}
110+
111+
if (g_beforeCrashCallback) {
112+
g_beforeCrashCallback(monitorContext->eventID);
113+
}
109114
}
110115

111116
// ============================================================================
@@ -193,6 +198,12 @@ sentrycrash_setSaveScreenshots(void (*callback)(const char *))
193198
g_saveScreenShot = callback;
194199
}
195200

201+
void
202+
sentrycrash_setBeforeCrashCallback(void (*callback)(const char *))
203+
{
204+
g_beforeCrashCallback = callback;
205+
}
206+
196207
void
197208
sentrycrash_setSaveViewHierarchy(void (*callback)(const char *))
198209
{

Sources/SentryCrash/Recording/SentryCrashC.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ void sentrycrash_setSaveScreenshots(SaveAttachmentCallback callback);
139139
*/
140140
void sentrycrash_setSaveViewHierarchy(SaveAttachmentCallback callback);
141141

142+
/**
143+
* Set the callback to be called at the end of a crash.
144+
*/
145+
void sentrycrash_setBeforeCrashCallback(void (*callback)(const char *eventID));
146+
142147
/** Report a custom, user defined exception.
143148
* This can be useful when dealing with scripting languages.
144149
*

0 commit comments

Comments
 (0)