-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
RNSentry.m
303 lines (267 loc) · 11.5 KB
/
RNSentry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#import "RNSentry.h"
#import "RNSentryEventEmitter.h"
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#else
#import "RCTConvert.h"
#endif
#import <Sentry/Sentry.h>
NSString *const RNSentryVersionString = @"0.39.0";
NSString *const RNSentrySdkName = @"sentry.javascript.react-native";
@interface RNSentry()
@property (nonatomic, strong) NSMutableDictionary *moduleMapping;
@end
@implementation RNSentry
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
+ (void)installWithBridge:(RCTBridge *)bridge {
// For now we don't need this anymore
}
+ (void)installWithRootView:(RCTRootView *)rootView {
// For now we don't need this anymore
}
- (NSInteger)indexOfReactNativeCallFrame:(NSArray<SentryFrame *> *)frames nativeCallAddress:(NSUInteger)nativeCallAddress {
NSInteger smallestDiff = NSIntegerMax;
NSInteger index = -1;
NSUInteger counter = 0;
for (SentryFrame *frame in frames) {
NSUInteger instructionAddress;
// We skip js frames because they don't have an instructionAddress
if (frame.instructionAddress == nil) {
continue;
}
[[NSScanner scannerWithString:frame.instructionAddress] scanHexLongLong:&instructionAddress];
if (instructionAddress < nativeCallAddress) {
continue;
}
NSInteger diff = instructionAddress - nativeCallAddress;
if (diff < smallestDiff) {
smallestDiff = diff;
index = counter;
}
counter++;
}
if (index > -1) {
return index + 1;
}
return index;
}
- (void)injectReactNativeFrames:(SentryEvent *)event {
NSString *address = [[NSUserDefaults standardUserDefaults] objectForKey:@"RNSentry.__sentry_address"];
if (nil == address) {
// We bail out here since __sentry_address is not set
return;
}
SentryThread *crashedThread = [event.exceptions objectAtIndex:0].thread;
NSArray<SentryFrame *> *frames = crashedThread.stacktrace.frames;
NSInteger indexOfReactFrames = [self indexOfReactNativeCallFrame:frames
nativeCallAddress:[address integerValue]];
if (indexOfReactFrames == -1) {
return;
}
NSMutableArray<SentryFrame *> *finalFrames = [NSMutableArray new];
NSString *stacktrace = [[NSUserDefaults standardUserDefaults] objectForKey:@"RNSentry.__sentry_stack"];
NSArray<SentryFrame *> *reactFrames = [SentryJavaScriptBridgeHelper convertReactNativeStacktrace:[SentryJavaScriptBridgeHelper parseJavaScriptStacktrace:stacktrace]];
for (NSInteger i = 0; i < frames.count; i++) {
[finalFrames addObject:[frames objectAtIndex:i]];
if (i == indexOfReactFrames) {
[finalFrames addObjectsFromArray:reactFrames];
}
}
crashedThread.stacktrace.frames = finalFrames;
}
- (void)setReleaseVersionDist:(SentryEvent *)event {
if (event.extra[@"__sentry_version"]) {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
event.releaseName = [NSString stringWithFormat:@"%@-%@", infoDict[@"CFBundleIdentifier"], event.extra[@"__sentry_version"]];
}
if (event.extra[@"__sentry_release"]) {
event.releaseName = [NSString stringWithFormat:@"%@", event.extra[@"__sentry_release"]];
}
if (event.extra[@"__sentry_dist"]) {
event.dist = [NSString stringWithFormat:@"%@", event.extra[@"__sentry_dist"]];
}
event.sdk = @{@"name": RNSentrySdkName,
@"version": RNSentryVersionString,
@"integrations": @[@"sentry-cocoa"]};
}
RCT_EXPORT_MODULE()
- (NSDictionary<NSString *, id> *)constantsToExport
{
return @{@"nativeClientAvailable": @YES};
}
RCT_EXPORT_METHOD(crashedLastLaunch:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSNumber *crashedLastLaunch = @NO;
if (SentryClient.sharedClient && [SentryClient.sharedClient crashedLastLaunch]) {
crashedLastLaunch = @YES;
}
resolve(crashedLastLaunch);
}
RCT_EXPORT_METHOD(startWithDsnString:(NSString * _Nonnull)dsnString
options:(NSDictionary *_Nonnull)options
resolve:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSError *error = nil;
self.moduleMapping = [[NSMutableDictionary alloc] init];
SentryClient *client = [[SentryClient alloc] initWithDsn:dsnString didFailWithError:&error];
client.beforeSerializeEvent = ^(SentryEvent * _Nonnull event) {
[self injectReactNativeFrames:event];
[self setReleaseVersionDist:event];
};
client.shouldSendEvent = ^BOOL(SentryEvent * _Nonnull event) {
// We don't want to send an event after startup that came from a Unhandled JS Exception of react native
// Because we sent it already before the app crashed.
if (nil != event.exceptions.firstObject.type &&
[event.exceptions.firstObject.type rangeOfString:@"Unhandled JS Exception"].location != NSNotFound) {
NSLog(@"Unhandled JS Exception");
return NO;
}
// Since we set shouldSendEvent for react-native we need to duplicate the code for sampling here
if (nil != options[@"sampleRate"]) {
return ([options[@"sampleRate"] floatValue] >= ((double)arc4random() / 0x100000000));
}
return YES;
};
[SentryClient setSharedClient:client];
[SentryClient.sharedClient startCrashHandlerWithError:&error];
if (error) {
reject(@"SentryReactNative", error.localizedDescription, error);
return;
}
resolve(@YES);
}
RCT_EXPORT_METHOD(activateStacktraceMerging:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
// React Native < 0.45
if (NSClassFromString(@"RCTBatchedBridge")) {
[self swizzleCallNativeModule:NSClassFromString(@"RCTBatchedBridge")];
} else {
[self swizzleInvokeWithBridge:NSClassFromString(@"RCTModuleMethod")];
}
resolve(@YES);
}
- (void)swizzleInvokeWithBridge:(Class)class {
static const void *key = &key;
SEL selector = @selector(invokeWithBridge:module:arguments:);
uintptr_t callNativeModuleAddress = [class instanceMethodForSelector:selector];
__block RNSentry *_self = self;
SentrySwizzleInstanceMethod(class,
selector,
SentrySWReturnType(id),
SentrySWArguments(RCTBridge *bridge, id module, NSArray *arguments),
SentrySWReplacement({
// TODO: refactor this block, its used twice
NSMutableArray *newParams = [NSMutableArray array];
if (arguments != nil && arguments.count > 0) {
for (id param in arguments) {
if ([param isKindOfClass:NSDictionary.class] && param[@"__sentry_stack"]) {
[_self.moduleMapping setValue:[NSString stringWithFormat:@"%@", [module class]] forKey:[NSString stringWithFormat:@"%@", param[@"__sentry_moduleID"]]];
[RNSentryEventEmitter emitModuleTableUpdate:_self.moduleMapping.mutableCopy];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%lu", callNativeModuleAddress] forKey:@"RNSentry.__sentry_address"];
[[NSUserDefaults standardUserDefaults] setObject:[RCTConvert NSString:param[@"__sentry_stack"]] forKey:@"RNSentry.__sentry_stack"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
if (param != nil) {
[newParams addObject:param];
}
}
}
}
return SentrySWCallOriginal(bridge, module, newParams);
}), SentrySwizzleModeOncePerClassAndSuperclasses, key);
}
- (void)swizzleCallNativeModule:(Class)class {
static const void *key = &key;
SEL selctor = @selector(callNativeModule:method:params:);
uintptr_t callNativeModuleAddress = [class instanceMethodForSelector:selctor];
SentrySwizzleInstanceMethod(class,
selctor,
SentrySWReturnType(id),
SentrySWArguments(NSUInteger moduleID, NSUInteger methodID, NSArray *params),
SentrySWReplacement({
// TODO: refactor this block, its used twice
NSMutableArray *newParams = [NSMutableArray array];
if (params != nil && params.count > 0) {
for (id param in params) {
if ([param isKindOfClass:NSDictionary.class] && param[@"__sentry_stack"]) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithUnsignedInteger:callNativeModuleAddress] forKey:@"RNSentry.__sentry_address"];
[[NSUserDefaults standardUserDefaults] setObject:[RCTConvert NSString:param[@"__sentry_stack"]] forKey:@"RNSentry.__sentry_stack"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
if (param != nil) {
[newParams addObject:param];
}
}
}
}
return SentrySWCallOriginal(moduleID, methodID, newParams);
}), SentrySwizzleModeOncePerClassAndSuperclasses, key);
}
RCT_EXPORT_METHOD(clearContext)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
[SentryClient.sharedClient clearContext];
});
}
RCT_EXPORT_METHOD(setLogLevel:(int)level)
{
[SentryClient setLogLevel:[SentryJavaScriptBridgeHelper sentryLogLevelFromJavaScriptLevel:level]];
}
RCT_EXPORT_METHOD(setTags:(NSDictionary *_Nonnull)tags)
{
SentryClient.sharedClient.tags = [SentryJavaScriptBridgeHelper sanitizeDictionary:tags];
}
RCT_EXPORT_METHOD(setExtra:(NSDictionary *_Nonnull)extra)
{
SentryClient.sharedClient.extra = extra;
}
RCT_EXPORT_METHOD(addExtra:(NSString *_Nonnull)key value:(id)value)
{
NSMutableDictionary *prevExtra = SentryClient.sharedClient.extra.mutableCopy;
[prevExtra setValue:value forKey:key];
SentryClient.sharedClient.extra = prevExtra;
}
RCT_EXPORT_METHOD(setUser:(NSDictionary *_Nonnull)user)
{
SentryUser *sentryUser = [SentryJavaScriptBridgeHelper createSentryUserFromJavaScriptUser:user];
if (sentryUser) {
SentryClient.sharedClient.user = sentryUser;
}
}
RCT_EXPORT_METHOD(captureBreadcrumb:(NSDictionary * _Nonnull)breadcrumb)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
[SentryClient.sharedClient.breadcrumbs addBreadcrumb:[SentryJavaScriptBridgeHelper createSentryBreadcrumbFromJavaScriptBreadcrumb:breadcrumb]];
});
}
RCT_EXPORT_METHOD(captureEvent:(NSDictionary * _Nonnull)event)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
SentryEvent *sentryEvent = [SentryJavaScriptBridgeHelper createSentryEventFromJavaScriptEvent:event];
if (sentryEvent.exceptions) {
#if DEBUG
// We want to send the exception instead of storing it because in debug
// the app does not crash it will restart
[SentryClient.sharedClient sendEvent:sentryEvent withCompletionHandler:NULL];
#else
[SentryClient.sharedClient storeEvent:sentryEvent];
#endif
} else {
[SentryClient.sharedClient sendEvent:sentryEvent withCompletionHandler:NULL];
}
[RNSentryEventEmitter emitStoredEvent];
});
}
RCT_EXPORT_METHOD(crash)
{
[SentryClient.sharedClient crash];
}
@end