Skip to content

Commit 20d5eaa

Browse files
itaybreantonis
andauthored
Fix build error from sentry-cocoa swift migration (#5109)
* Fix build error from sentry-cocoa swift migration * Apply suggestions from code review Co-authored-by: Antonis Lilis <[email protected]> --------- Co-authored-by: Antonis Lilis <[email protected]>
1 parent bd2462e commit 20d5eaa

File tree

3 files changed

+107
-80
lines changed

3 files changed

+107
-80
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#import "RNSentry.h"
2+
#import "RNSentryBreadcrumb.h"
3+
#import "RNSentryId.h"
4+
#import <Sentry/PrivateSentrySDKOnly.h>
5+
#import <Sentry/SentryAppStartMeasurement.h>
6+
#import <Sentry/SentryBreadcrumb.h>
7+
#import <Sentry/SentryDebugImageProvider+HybridSDKs.h>
8+
#import <Sentry/SentryDebugMeta.h>
9+
#import <Sentry/SentryDependencyContainer.h>
10+
#import <Sentry/SentryEvent.h>
11+
#import <Sentry/SentryException.h>
12+
#import <Sentry/SentryFormatter.h>
13+
#import <Sentry/SentryOptions.h>
14+
#import <Sentry/SentryUser.h>
15+
@import Sentry;
16+
17+
// This method was moved to a new category so we can use `@import Sentry` to use Sentry's Swift
18+
// classes
19+
@implementation
20+
RNSentry (fetchNativeStack)
21+
22+
- (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAddr
23+
symbolicate:(SymbolicateCallbackType)symbolicate
24+
{
25+
#if CROSS_PLATFORM_TEST
26+
BOOL shouldSymbolicateLocally = [SentrySDKInternal.options debug];
27+
#else
28+
BOOL shouldSymbolicateLocally = [SentrySDK.options debug];
29+
#endif
30+
NSString *appPackageName = [[NSBundle mainBundle] executablePath];
31+
32+
NSMutableSet<NSString *> *_Nonnull imagesAddrToRetrieveDebugMetaImages =
33+
[[NSMutableSet alloc] init];
34+
NSMutableArray<NSDictionary<NSString *, id> *> *_Nonnull serializedFrames =
35+
[[NSMutableArray alloc] init];
36+
37+
for (NSNumber *addr in instructionsAddr) {
38+
SentryBinaryImageInfo *_Nullable image = [[[SentryDependencyContainer sharedInstance]
39+
binaryImageCache] imageByAddress:[addr unsignedLongLongValue]];
40+
if (image != nil) {
41+
NSString *imageAddr = sentry_formatHexAddressUInt64([image address]);
42+
[imagesAddrToRetrieveDebugMetaImages addObject:imageAddr];
43+
44+
NSDictionary<NSString *, id> *_Nonnull nativeFrame = @{
45+
@"platform" : @"cocoa",
46+
@"instruction_addr" : sentry_formatHexAddress(addr),
47+
@"package" : [image name],
48+
@"image_addr" : imageAddr,
49+
@"in_app" : [NSNumber numberWithBool:[appPackageName isEqualToString:[image name]]],
50+
};
51+
52+
if (shouldSymbolicateLocally) {
53+
Dl_info symbolsBuffer;
54+
bool symbols_succeed = false;
55+
symbols_succeed
56+
= symbolicate((void *)[addr unsignedLongLongValue], &symbolsBuffer) != 0;
57+
if (symbols_succeed) {
58+
NSMutableDictionary<NSString *, id> *_Nonnull symbolicated
59+
= nativeFrame.mutableCopy;
60+
symbolicated[@"symbol_addr"]
61+
= sentry_formatHexAddressUInt64((uintptr_t)symbolsBuffer.dli_saddr);
62+
symbolicated[@"function"] = [NSString stringWithCString:symbolsBuffer.dli_sname
63+
encoding:NSUTF8StringEncoding];
64+
65+
nativeFrame = symbolicated;
66+
}
67+
}
68+
69+
[serializedFrames addObject:nativeFrame];
70+
} else {
71+
[serializedFrames addObject:@{
72+
@"platform" : @"cocoa",
73+
@"instruction_addr" : sentry_formatHexAddress(addr),
74+
}];
75+
}
76+
}
77+
78+
if (shouldSymbolicateLocally) {
79+
return @{
80+
@"frames" : serializedFrames,
81+
};
82+
} else {
83+
NSMutableArray<NSDictionary<NSString *, id> *> *_Nonnull serializedDebugMetaImages =
84+
[[NSMutableArray alloc] init];
85+
86+
NSArray<SentryDebugMeta *> *debugMetaImages =
87+
[[[SentryDependencyContainer sharedInstance] debugImageProvider]
88+
getDebugImagesForImageAddressesFromCache:imagesAddrToRetrieveDebugMetaImages];
89+
90+
for (SentryDebugMeta *debugImage in debugMetaImages) {
91+
[serializedDebugMetaImages addObject:[debugImage serialize]];
92+
}
93+
94+
return @{
95+
@"frames" : serializedFrames,
96+
@"debugMetaImages" : serializedDebugMetaImages,
97+
};
98+
}
99+
}
100+
101+
@end

packages/core/ios/RNSentry.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ SentrySDK (Private)
3131

3232
- (void)setEventOriginTag:(SentryEvent *)event;
3333

34+
@end
35+
36+
@interface
37+
RNSentry (fetchNativeStack)
38+
3439
- (NSDictionary *_Nonnull)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAddr
3540
symbolicate:(SymbolicateCallbackType)symbolicate;
3641

packages/core/ios/RNSentry.mm

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#import "RNSentryId.h"
2121
#import <Sentry/PrivateSentrySDKOnly.h>
2222
#import <Sentry/SentryAppStartMeasurement.h>
23-
#import <Sentry/SentryBinaryImageCache.h>
2423
#import <Sentry/SentryBreadcrumb.h>
2524
#import <Sentry/SentryDebugImageProvider+HybridSDKs.h>
2625
#import <Sentry/SentryDebugMeta.h>
@@ -30,6 +29,7 @@
3029
#import <Sentry/SentryFormatter.h>
3130
#import <Sentry/SentryOptions.h>
3231
#import <Sentry/SentryUser.h>
32+
3333
#if __has_include(<Sentry/SentryOptions+HybridSDKs.h>)
3434
# define USE_SENTRY_OPTIONS 1
3535
# import <Sentry/SentryOptions+HybridSDKs.h>
@@ -358,85 +358,6 @@ - (void)stopObserving
358358
return packageName;
359359
}
360360

361-
- (NSDictionary *)fetchNativeStackFramesBy:(NSArray<NSNumber *> *)instructionsAddr
362-
symbolicate:(SymbolicateCallbackType)symbolicate
363-
{
364-
#if CROSS_PLATFORM_TEST
365-
BOOL shouldSymbolicateLocally = [SentrySDKInternal.options debug];
366-
#else
367-
BOOL shouldSymbolicateLocally = [SentrySDK.options debug];
368-
#endif
369-
NSString *appPackageName = [[NSBundle mainBundle] executablePath];
370-
371-
NSMutableSet<NSString *> *_Nonnull imagesAddrToRetrieveDebugMetaImages =
372-
[[NSMutableSet alloc] init];
373-
NSMutableArray<NSDictionary<NSString *, id> *> *_Nonnull serializedFrames =
374-
[[NSMutableArray alloc] init];
375-
376-
for (NSNumber *addr in instructionsAddr) {
377-
SentryBinaryImageInfo *_Nullable image = [[[SentryDependencyContainer sharedInstance]
378-
binaryImageCache] imageByAddress:[addr unsignedLongLongValue]];
379-
if (image != nil) {
380-
NSString *imageAddr = sentry_formatHexAddressUInt64([image address]);
381-
[imagesAddrToRetrieveDebugMetaImages addObject:imageAddr];
382-
383-
NSDictionary<NSString *, id> *_Nonnull nativeFrame = @{
384-
@"platform" : @"cocoa",
385-
@"instruction_addr" : sentry_formatHexAddress(addr),
386-
@"package" : [image name],
387-
@"image_addr" : imageAddr,
388-
@"in_app" : [NSNumber numberWithBool:[appPackageName isEqualToString:[image name]]],
389-
};
390-
391-
if (shouldSymbolicateLocally) {
392-
Dl_info symbolsBuffer;
393-
bool symbols_succeed = false;
394-
symbols_succeed
395-
= symbolicate((void *)[addr unsignedLongLongValue], &symbolsBuffer) != 0;
396-
if (symbols_succeed) {
397-
NSMutableDictionary<NSString *, id> *_Nonnull symbolicated
398-
= nativeFrame.mutableCopy;
399-
symbolicated[@"symbol_addr"]
400-
= sentry_formatHexAddressUInt64((uintptr_t)symbolsBuffer.dli_saddr);
401-
symbolicated[@"function"] = [NSString stringWithCString:symbolsBuffer.dli_sname
402-
encoding:NSUTF8StringEncoding];
403-
404-
nativeFrame = symbolicated;
405-
}
406-
}
407-
408-
[serializedFrames addObject:nativeFrame];
409-
} else {
410-
[serializedFrames addObject:@{
411-
@"platform" : @"cocoa",
412-
@"instruction_addr" : sentry_formatHexAddress(addr),
413-
}];
414-
}
415-
}
416-
417-
if (shouldSymbolicateLocally) {
418-
return @{
419-
@"frames" : serializedFrames,
420-
};
421-
} else {
422-
NSMutableArray<NSDictionary<NSString *, id> *> *_Nonnull serializedDebugMetaImages =
423-
[[NSMutableArray alloc] init];
424-
425-
NSArray<SentryDebugMeta *> *debugMetaImages =
426-
[[[SentryDependencyContainer sharedInstance] debugImageProvider]
427-
getDebugImagesForImageAddressesFromCache:imagesAddrToRetrieveDebugMetaImages];
428-
429-
for (SentryDebugMeta *debugImage in debugMetaImages) {
430-
[serializedDebugMetaImages addObject:[debugImage serialize]];
431-
}
432-
433-
return @{
434-
@"frames" : serializedFrames,
435-
@"debugMetaImages" : serializedDebugMetaImages,
436-
};
437-
}
438-
}
439-
440361
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSDictionary *, fetchNativeStackFramesBy
441362
: (NSArray *)instructionsAddr)
442363
{

0 commit comments

Comments
 (0)