-
Notifications
You must be signed in to change notification settings - Fork 130
/
BugsnagCrashReport.m
772 lines (664 loc) · 27.7 KB
/
BugsnagCrashReport.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//
// BugsnagCrashReport.m
// Bugsnag
//
// Created by Simon Maynard on 11/26/14.
//
//
#if TARGET_OS_MAC || TARGET_OS_TV
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#include <sys/utsname.h>
#endif
#import "BSGSerialization.h"
#import "Bugsnag.h"
#import "BugsnagCollections.h"
#import "BugsnagHandledState.h"
#import "BugsnagLogger.h"
#import "BugsnagKeys.h"
#import "BugsnagKSCrashSysInfoParser.h"
#import "BugsnagSession.h"
#import "BSG_RFC3339DateTool.h"
NSMutableDictionary *BSGFormatFrame(NSDictionary *frame,
NSArray *binaryImages) {
NSMutableDictionary *formatted = [NSMutableDictionary dictionary];
unsigned long instructionAddress =
[frame[@"instruction_addr"] unsignedLongValue];
unsigned long symbolAddress = [frame[@"symbol_addr"] unsignedLongValue];
unsigned long imageAddress = [frame[@"object_addr"] unsignedLongValue];
BSGDictSetSafeObject(
formatted, [NSString stringWithFormat:BSGKeyFrameAddrFormat, instructionAddress],
@"frameAddress");
BSGDictSetSafeObject(formatted,
[NSString stringWithFormat:BSGKeyFrameAddrFormat, symbolAddress],
BSGKeySymbolAddr);
BSGDictSetSafeObject(formatted,
[NSString stringWithFormat:BSGKeyFrameAddrFormat, imageAddress],
BSGKeyMachoLoadAddr);
BSGDictInsertIfNotNil(formatted, frame[BSGKeyIsPC], BSGKeyIsPC);
BSGDictInsertIfNotNil(formatted, frame[BSGKeyIsLR], BSGKeyIsLR);
NSString *file = frame[@"object_name"];
NSString *method = frame[@"symbol_name"];
BSGDictInsertIfNotNil(formatted, file, BSGKeyMachoFile);
BSGDictInsertIfNotNil(formatted, method, @"method");
for (NSDictionary *image in binaryImages) {
if ([(NSNumber *)image[@"image_addr"] unsignedLongValue] ==
imageAddress) {
unsigned long imageSlide =
[image[@"image_vmaddr"] unsignedLongValue];
BSGDictInsertIfNotNil(formatted, image[@"uuid"], BSGKeyMachoUUID);
BSGDictInsertIfNotNil(formatted, image[BSGKeyName], BSGKeyMachoFile);
BSGDictSetSafeObject(
formatted, [NSString stringWithFormat:BSGKeyFrameAddrFormat, imageSlide],
BSGKeyMachoVMAddress);
return formatted;
}
}
return nil;
}
NSString *_Nonnull BSGParseErrorClass(NSDictionary *error,
NSString *errorType,
NSString *fallbackValue) {
NSString *errorClass;
if ([errorType isEqualToString:BSGKeyCppException]) {
errorClass = error[BSGKeyCppException][BSGKeyName];
} else if ([errorType isEqualToString:BSGKeyMach]) {
errorClass = error[BSGKeyMach][BSGKeyExceptionName];
} else if ([errorType isEqualToString:BSGKeySignal]) {
errorClass = error[BSGKeySignal][BSGKeyName];
} else if ([errorType isEqualToString:@"nsexception"]) {
errorClass = error[@"nsexception"][BSGKeyName];
} else if ([errorType isEqualToString:BSGKeyUser]) {
errorClass = error[@"user_reported"][BSGKeyName];
}
if (!errorClass) { // use a default value
errorClass = fallbackValue.length > 0 ? fallbackValue : @"Exception";
}
return errorClass;
}
NSString *BSGParseErrorMessage(NSDictionary *report, NSDictionary *error,
NSString *errorType) {
if ([errorType isEqualToString:BSGKeyMach] || error[BSGKeyReason] == nil) {
NSString *diagnosis = [report valueForKeyPath:@"crash.diagnosis"];
if (diagnosis && ![diagnosis hasPrefix:@"No diagnosis"]) {
return [[diagnosis componentsSeparatedByString:@"\n"] firstObject];
}
}
return error[BSGKeyReason] ?: @"";
}
NSString *BSGParseContext(NSDictionary *report, NSDictionary *metaData) {
id context = [report valueForKeyPath:@"user.overrides.context"];
if ([context isKindOfClass:[NSString class]])
return context;
context = metaData[BSGKeyContext];
if ([context isKindOfClass:[NSString class]])
return context;
context = [report valueForKeyPath:@"user.config.context"];
if ([context isKindOfClass:[NSString class]])
return context;
return nil;
}
NSString *BSGParseGroupingHash(NSDictionary *report, NSDictionary *metaData) {
id groupingHash = [report valueForKeyPath:@"user.overrides.groupingHash"];
if (groupingHash)
return groupingHash;
groupingHash = metaData[BSGKeyGroupingHash];
if ([groupingHash isKindOfClass:[NSString class]])
return groupingHash;
return nil;
}
NSArray *BSGParseBreadcrumbs(NSDictionary *report) {
return [report valueForKeyPath:@"user.overrides.breadcrumbs"]
?: [report valueForKeyPath:@"user.state.crash.breadcrumbs"];
}
NSString *BSGParseReleaseStage(NSDictionary *report) {
return [report valueForKeyPath:@"user.overrides.releaseStage"]
?: [report valueForKeyPath:@"user.config.releaseStage"];
}
BSGSeverity BSGParseSeverity(NSString *severity) {
if ([severity isEqualToString:BSGKeyInfo])
return BSGSeverityInfo;
else if ([severity isEqualToString:BSGKeyWarning])
return BSGSeverityWarning;
return BSGSeverityError;
}
NSString *BSGFormatSeverity(BSGSeverity severity) {
switch (severity) {
case BSGSeverityInfo:
return BSGKeyInfo;
case BSGSeverityError:
return BSGKeyError;
case BSGSeverityWarning:
return BSGKeyWarning;
}
}
NSDictionary *BSGParseCustomException(NSDictionary *report,
NSString *errorClass, NSString *message) {
id frames =
[report valueForKeyPath:@"user.overrides.customStacktraceFrames"];
id type = [report valueForKeyPath:@"user.overrides.customStacktraceType"];
if (type && frames) {
return @{
BSGKeyStacktrace : frames,
BSGKeyType : type,
BSGKeyErrorClass : errorClass,
BSGKeyMessage : message
};
}
return nil;
}
static NSString *const DEFAULT_EXCEPTION_TYPE = @"cocoa";
@interface NSDictionary (BSGKSMerge)
- (NSDictionary *)BSG_mergedInto:(NSDictionary *)dest;
@end
@interface RegisterErrorData : NSObject
@property (nonatomic, strong) NSString *errorClass;
@property (nonatomic, strong) NSString *errorMessage;
+ (instancetype)errorDataFromThreads:(NSArray *)threads;
- (instancetype)initWithClass:(NSString *_Nonnull)errorClass message:(NSString *_Nonnull)errorMessage NS_DESIGNATED_INITIALIZER;
@end
@interface FallbackReportData : NSObject
@property (nonatomic, strong) NSString *errorClass;
@property (nonatomic, getter=isUnhandled) BOOL unhandled;
@property (nonatomic) BSGSeverity severity;
- (instancetype)initWithMetadata:(NSString *)metadata;
@end
@interface BugsnagCrashReport ()
/**
* The type of the error, such as `mach` or `user`
*/
@property(nonatomic, readwrite, copy, nullable) NSString *errorType;
/**
* The UUID of the dSYM file
*/
@property(nonatomic, readonly, copy, nullable) NSString *dsymUUID;
/**
* A unique hash identifying this device for the application or vendor
*/
@property(nonatomic, readonly, copy, nullable) NSString *deviceAppHash;
/**
* Binary images used to identify application symbols
*/
@property(nonatomic, readonly, copy, nullable) NSArray *binaryImages;
/**
* Thread information captured at the time of the error
*/
@property(nonatomic, readonly, copy, nullable) NSArray *threads;
/**
* User-provided exception metadata
*/
@property(nonatomic, readwrite, copy, nullable) NSDictionary *customException;
@property(nonatomic, strong) BugsnagSession *session;
@property (nonatomic, readwrite, getter=isIncomplete) BOOL incomplete;
@end
@implementation BugsnagCrashReport
- (instancetype)initWithKSReport:(NSDictionary *)report {
return [self initWithKSReport:report fileMetadata:@""];
}
- (instancetype)initWithKSReport:(NSDictionary *)report
fileMetadata:(NSString *)metadata {
if (self = [super init]) {
_error = [report valueForKeyPath:@"crash.error"];
_errorType = _error[BSGKeyType];
if ([[report valueForKeyPath:@"user.state.didOOM"] boolValue]) {
_errorClass = BSGParseErrorClass(_error, _errorType, nil);
_errorMessage = BSGParseErrorMessage(report, _error, _errorType);
_breadcrumbs = [report valueForKeyPath:@"user.state.oom.breadcrumbs"];
_app = [report valueForKeyPath:@"user.state.oom.app"];
_device = [report valueForKeyPath:@"user.state.oom.device"];
_releaseStage = [report valueForKeyPath:@"user.state.oom.app.releaseStage"];
_handledState = [BugsnagHandledState handledStateWithSeverityReason:LikelyOutOfMemory];
_deviceAppHash = [report valueForKeyPath:@"user.state.oom.device.id"];
NSDictionary *sessionData = [report valueForKeyPath:@"user.state.oom.session"];
if (sessionData) {
_session = [[BugsnagSession alloc] initWithDictionary:sessionData];
_session.unhandledCount += 1; // include own event
if (_session.user) {
_metaData = @{@"user": [_session.user toJson]};
}
}
} else {
FallbackReportData *fallback = [[FallbackReportData alloc] initWithMetadata:metadata];
_notifyReleaseStages =
[report valueForKeyPath:@"user.config.notifyReleaseStages"];
_releaseStage = BSGParseReleaseStage(report);
_incomplete = report.count == 0;
_threads = [report valueForKeyPath:@"crash.threads"];
RegisterErrorData *data = [RegisterErrorData errorDataFromThreads:_threads];
if (data) {
_errorClass = data.errorClass ?: fallback.errorClass;
_errorMessage = data.errorMessage;
} else {
_errorClass = BSGParseErrorClass(_error, _errorType, fallback.errorClass);
_errorMessage = BSGParseErrorMessage(report, _error, _errorType);
}
_binaryImages = report[@"binary_images"];
_breadcrumbs = BSGParseBreadcrumbs(report);
_dsymUUID = [report valueForKeyPath:@"system.app_uuid"];
_deviceAppHash = [report valueForKeyPath:@"system.device_app_hash"];
_metaData =
[report valueForKeyPath:@"user.metaData"] ?: [NSDictionary new];
_context = BSGParseContext(report, _metaData);
_deviceState = BSGParseDeviceState(report);
_device = BSGParseDevice(report);
_app = BSGParseApp(report);
_appState = BSGParseAppState(report[BSGKeySystem], [report valueForKeyPath:@"user.config.appVersion"]);
_groupingHash = BSGParseGroupingHash(report, _metaData);
_overrides = [report valueForKeyPath:@"user.overrides"];
_customException = BSGParseCustomException(report, [_errorClass copy],
[_errorMessage copy]);
NSDictionary *recordedState =
[report valueForKeyPath:@"user.handledState"];
if (recordedState) {
_handledState =
[[BugsnagHandledState alloc] initWithDictionary:recordedState];
// only makes sense to use serialised value for handled exceptions
_depth = [[report valueForKeyPath:@"user.depth"]
unsignedIntegerValue];
} else if (_errorType != nil) { // the event was unhandled.
BOOL isSignal = [BSGKeySignal isEqualToString:_errorType];
SeverityReasonType severityReason =
isSignal ? Signal : UnhandledException;
_handledState = [BugsnagHandledState
handledStateWithSeverityReason:severityReason
severity:BSGSeverityError
attrValue:_errorClass];
_depth = 0;
} else { // Incomplete report
SeverityReasonType severityReason = [fallback isUnhandled] ? UnhandledException : HandledError;
_handledState = [BugsnagHandledState handledStateWithSeverityReason:severityReason
severity:fallback.severity
attrValue:nil];
}
_severity = _handledState.currentSeverity;
if (report[@"user"][@"id"]) {
_session = [[BugsnagSession alloc] initWithDictionary:report[@"user"]];
}
}
}
return self;
}
- (instancetype _Nonnull)
initWithErrorName:(NSString *_Nonnull)name
errorMessage:(NSString *_Nonnull)message
configuration:(BugsnagConfiguration *_Nonnull)config
metaData:(NSDictionary *_Nonnull)metaData
handledState:(BugsnagHandledState *_Nonnull)handledState
session:(BugsnagSession *_Nullable)session {
if (self = [super init]) {
_errorClass = name;
_errorMessage = message;
_metaData = metaData ?: [NSDictionary new];
_releaseStage = config.releaseStage;
_notifyReleaseStages = config.notifyReleaseStages;
_context = BSGParseContext(nil, metaData);
_breadcrumbs = [config.breadcrumbs arrayValue];
_overrides = [NSDictionary new];
_handledState = handledState;
_severity = handledState.currentSeverity;
_session = session;
}
return self;
}
@synthesize metaData = _metaData;
- (NSDictionary *)metaData {
@synchronized (self) {
return _metaData;
}
}
- (void)setMetaData:(NSDictionary *)metaData {
@synchronized (self) {
_metaData = BSGSanitizeDict(metaData);
}
}
- (void)addMetadata:(NSDictionary *_Nonnull)tabData
toTabWithName:(NSString *_Nonnull)tabName {
NSDictionary *cleanedData = BSGSanitizeDict(tabData);
if ([cleanedData count] == 0) {
bsg_log_err(@"Failed to add metadata: Values not convertible to JSON");
return;
}
NSMutableDictionary *allMetadata = [self.metaData mutableCopy];
NSMutableDictionary *allTabData =
allMetadata[tabName] ?: [NSMutableDictionary new];
allMetadata[tabName] = [cleanedData BSG_mergedInto:allTabData];
self.metaData = allMetadata;
}
- (void)addAttribute:(NSString *)attributeName
withValue:(id)value
toTabWithName:(NSString *)tabName {
NSMutableDictionary *allMetadata = [self.metaData mutableCopy];
NSMutableDictionary *allTabData =
allMetadata[tabName] ?: [NSMutableDictionary new];
if (value) {
id cleanedValue = BSGSanitizeObject(value);
if (!cleanedValue) {
bsg_log_err(@"Failed to add metadata: Value of type %@ is not "
@"convertible to JSON",
[value class]);
return;
}
allTabData[attributeName] = cleanedValue;
} else {
[allTabData removeObjectForKey:attributeName];
}
allMetadata[tabName] = allTabData;
self.metaData = allMetadata;
}
- (BOOL)shouldBeSent {
return [self.notifyReleaseStages containsObject:self.releaseStage] ||
(self.notifyReleaseStages.count == 0 &&
[[Bugsnag configuration] shouldSendReports]);
}
@synthesize context = _context;
- (NSString *)context {
@synchronized (self) {
return _context;
}
}
- (void)setContext:(NSString *)context {
[self setOverrideProperty:BSGKeyContext value:context];
@synchronized (self) {
_context = context;
}
}
@synthesize groupingHash = _groupingHash;
- (NSString *)groupingHash {
@synchronized (self) {
return _groupingHash;
}
}
- (void)setGroupingHash:(NSString *)groupingHash {
[self setOverrideProperty:BSGKeyGroupingHash value:groupingHash];
@synchronized (self) {
_groupingHash = groupingHash;
}
}
@synthesize breadcrumbs = _breadcrumbs;
- (NSArray *)breadcrumbs {
@synchronized (self) {
return _breadcrumbs;
}
}
- (void)setBreadcrumbs:(NSArray *)breadcrumbs {
[self setOverrideProperty:BSGKeyBreadcrumbs value:breadcrumbs];
@synchronized (self) {
_breadcrumbs = breadcrumbs;
}
}
@synthesize releaseStage = _releaseStage;
- (NSString *)releaseStage {
@synchronized (self) {
return _releaseStage;
}
}
- (void)setReleaseStage:(NSString *)releaseStage {
[self setOverrideProperty:BSGKeyReleaseStage value:releaseStage];
@synchronized (self) {
_releaseStage = releaseStage;
}
}
- (void)attachCustomStacktrace:(NSArray *)frames withType:(NSString *)type {
[self setOverrideProperty:@"customStacktraceFrames" value:frames];
[self setOverrideProperty:@"customStacktraceType" value:type];
}
@synthesize severity = _severity;
- (BSGSeverity)severity {
@synchronized (self) {
return _severity;
}
}
- (void)setSeverity:(BSGSeverity)severity {
@synchronized (self) {
_severity = severity;
_handledState.currentSeverity = severity;
}
}
- (void)setOverrideProperty:(NSString *)key value:(id)value {
@synchronized (self) {
NSMutableDictionary *metadata = [self.overrides mutableCopy];
if (value) {
metadata[key] = value;
} else {
[metadata removeObjectForKey:key];
}
_overrides = metadata;
}
}
- (NSDictionary *)serializableValueWithTopLevelData:
(NSMutableDictionary *)data {
return [self toJson];
}
- (NSDictionary *)toJson {
NSMutableDictionary *event = [NSMutableDictionary dictionary];
NSMutableDictionary *metaData = [[self metaData] mutableCopy];
if (self.customException) {
BSGDictSetSafeObject(event, @[ self.customException ], BSGKeyExceptions);
BSGDictSetSafeObject(event, [self serializeThreadsWithException:nil],
BSGKeyThreads);
} else {
NSMutableDictionary *exception = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(exception, [self errorClass], BSGKeyErrorClass);
BSGDictInsertIfNotNil(exception, [self errorMessage], BSGKeyMessage);
BSGDictInsertIfNotNil(exception, DEFAULT_EXCEPTION_TYPE, BSGKeyType);
BSGDictSetSafeObject(event, @[ exception ], BSGKeyExceptions);
BSGDictSetSafeObject(
event, [self serializeThreadsWithException:exception], BSGKeyThreads);
}
// Build Event
BSGDictSetSafeObject(event, BSGFormatSeverity(self.severity), BSGKeySeverity);
BSGDictSetSafeObject(event, [self breadcrumbs], BSGKeyBreadcrumbs);
BSGDictSetSafeObject(event, metaData, BSGKeyMetaData);
if ([self isIncomplete]) {
BSGDictSetSafeObject(event, @YES, BSGKeyIncomplete);
}
NSDictionary *device = BSGDictMerge(self.device, self.deviceState);
BSGDictSetSafeObject(event, device, BSGKeyDevice);
NSMutableDictionary *appObj = [NSMutableDictionary new];
[appObj addEntriesFromDictionary:self.app];
for (NSString *key in self.appState) {
BSGDictInsertIfNotNil(appObj, self.appState[key], key);
}
if (self.dsymUUID) {
BSGDictInsertIfNotNil(appObj, @[self.dsymUUID], @"dsymUUIDs");
}
BSGDictSetSafeObject(event, appObj, BSGKeyApp);
BSGDictSetSafeObject(event, [self context], BSGKeyContext);
BSGDictInsertIfNotNil(event, self.groupingHash, BSGKeyGroupingHash);
BSGDictSetSafeObject(event, @(self.handledState.unhandled), BSGKeyUnhandled);
// serialize handled/unhandled into payload
NSMutableDictionary *severityReason = [NSMutableDictionary new];
NSString *reasonType = [BugsnagHandledState
stringFromSeverityReason:self.handledState.calculateSeverityReasonType];
severityReason[BSGKeyType] = reasonType;
if (self.handledState.attrKey && self.handledState.attrValue) {
severityReason[BSGKeyAttributes] =
@{self.handledState.attrKey : self.handledState.attrValue};
}
BSGDictSetSafeObject(event, severityReason, BSGKeySeverityReason);
// Inserted into `context` property
[metaData removeObjectForKey:BSGKeyContext];
// Build metadata
BSGDictSetSafeObject(metaData, [self error], BSGKeyError);
// Make user mutable and set the id if the user hasn't already
NSMutableDictionary *user = [metaData[BSGKeyUser] mutableCopy];
if (user == nil) {
user = [NSMutableDictionary dictionary];
}
BSGDictInsertIfNotNil(event, user, BSGKeyUser);
if (!user[BSGKeyId] && self.device[BSGKeyId]) { // if device id is null, don't set user id to default
BSGDictSetSafeObject(user, [self deviceAppHash], BSGKeyId);
}
if (self.session) {
BSGDictSetSafeObject(event, [self generateSessionDict], BSGKeySession);
}
return event;
}
- (NSDictionary *)generateSessionDict {
NSDictionary *events = @{
@"handled": @(self.session.handledCount),
@"unhandled": @(self.session.unhandledCount)
};
NSDictionary *sessionJson = @{
BSGKeyId: self.session.sessionId,
@"startedAt": [BSG_RFC3339DateTool stringFromDate:self.session.startedAt],
@"events": events
};
return sessionJson;
}
// Build all stacktraces for threads and the error
- (NSArray *)serializeThreadsWithException:(NSMutableDictionary *)exception {
NSMutableArray *bugsnagThreads = [NSMutableArray array];
for (NSDictionary *thread in self.threads) {
NSArray *backtrace = thread[@"backtrace"][@"contents"];
BOOL stackOverflow = [thread[@"stack"][@"overflow"] boolValue];
BOOL isReportingThread = [thread[@"crashed"] boolValue];
if (isReportingThread) {
NSUInteger seen = 0;
NSMutableArray *stacktrace = [NSMutableArray array];
for (NSDictionary *frame in backtrace) {
NSMutableDictionary *mutableFrame = [frame mutableCopy];
if (seen++ >= [self depth]) {
// Mark the frame so we know where it came from
if (seen == 1 && !stackOverflow) {
BSGDictSetSafeObject(mutableFrame, @YES, BSGKeyIsPC);
}
if (seen == 2 && !stackOverflow &&
[@[ BSGKeySignal, @"deadlock", BSGKeyMach ]
containsObject:[self errorType]]) {
BSGDictSetSafeObject(mutableFrame, @YES, BSGKeyIsLR);
}
BSGArrayInsertIfNotNil(
stacktrace,
BSGFormatFrame(mutableFrame, [self binaryImages]));
}
}
BSGDictSetSafeObject(exception, stacktrace, BSGKeyStacktrace);
}
[self serialiseThread:bugsnagThreads thread:thread backtrace:backtrace reportingThread:isReportingThread];
}
return bugsnagThreads;
}
- (void)serialiseThread:(NSMutableArray *)bugsnagThreads
thread:(NSDictionary *)thread
backtrace:(NSArray *)backtrace
reportingThread:(BOOL)isReportingThread {
NSMutableArray *threadStack = [NSMutableArray array];
for (NSDictionary *frame in backtrace) {
BSGArrayInsertIfNotNil(
threadStack, BSGFormatFrame(frame, [self binaryImages]));
}
NSMutableDictionary *threadDict = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(threadDict, thread[@"index"], BSGKeyId);
BSGDictSetSafeObject(threadDict, threadStack, BSGKeyStacktrace);
BSGDictSetSafeObject(threadDict, DEFAULT_EXCEPTION_TYPE, BSGKeyType);
// only if this is enabled in BSG_KSCrash.
if (thread[BSGKeyName]) {
BSGDictSetSafeObject(threadDict, thread[BSGKeyName], BSGKeyName);
}
if (isReportingThread) {
BSGDictSetSafeObject(threadDict, @YES, @"errorReportingThread");
}
BSGArrayAddSafeObject(bugsnagThreads, threadDict);
}
- (NSString *_Nullable)enhancedErrorMessageForThread:(NSDictionary *_Nullable)thread {
return [self errorMessage];
}
@end
@implementation FallbackReportData
- (instancetype)initWithMetadata:(NSString *)metadata {
if (self = [super init]) {
NSString *separator = @"-";
NSString *location = metadata;
NSRange range = [location rangeOfString:separator options:NSBackwardsSearch];
if (range.location != NSNotFound) {
_errorClass = [location substringFromIndex:range.location + 1];
location = [location substringToIndex:range.location];
}
range = [location rangeOfString:separator options:NSBackwardsSearch];
if (range.location != NSNotFound) {
NSString *value = [location substringFromIndex:range.location + 1];
_unhandled = ![value isEqualToString:@"h"];
location = [location substringToIndex:range.location + 1];
} else {
_unhandled = YES;
}
range = [location rangeOfString:separator options:NSBackwardsSearch];
if (range.location != NSNotFound) {
NSString *value = [location substringFromIndex:range.location];
if ([value isEqualToString:@"w"]) {
_severity = BSGSeverityWarning;
} else if ([value isEqualToString:@"i"]) {
_severity = BSGSeverityInfo;
} else {
_severity = BSGSeverityError;
}
}
}
return self;
}
@end
@implementation RegisterErrorData
+ (instancetype)errorDataFromThreads:(NSArray *)threads {
for (NSDictionary *thread in threads) {
if (![thread[@"crashed"] boolValue]) {
continue;
}
NSDictionary *notableAddresses = thread[@"notable_addresses"];
NSMutableArray *interestingValues = [NSMutableArray new];
NSString *reservedWord = nil;
for (NSString *key in notableAddresses) {
if ([key hasPrefix:@"stack"]) { // skip stack frames, only use register values
continue;
}
NSDictionary *data = notableAddresses[key];
if (![@"string" isEqualToString:data[BSGKeyType]]) {
continue;
}
NSString *contentValue = data[@"value"];
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCDFAInspection"
if (contentValue == nil || ![contentValue isKindOfClass:[NSString class]]) {
continue;
}
#pragma clang diagnostic pop
if ([self isReservedWord:contentValue]) {
reservedWord = contentValue;
} else if ([[contentValue componentsSeparatedByString:@"/"] count] <= 2) {
// must be a string that isn't a reserved word and isn't a filepath
[interestingValues addObject:contentValue];
}
}
[interestingValues sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *message = [interestingValues componentsJoinedByString:@" | "];
return [[RegisterErrorData alloc] initWithClass:reservedWord
message:message];
}
return nil;
}
/**
* Determines whether a string is a "reserved word" that identifies it as a known value.
*
* For fatalError, preconditionFailure, and assertionFailure, "fatal error" will be in one of the registers.
*
* For assert, "assertion failed" will be in one of the registers.
*/
+ (BOOL)isReservedWord:(NSString *)contentValue {
return [@"assertion failed" caseInsensitiveCompare:contentValue] == NSOrderedSame
|| [@"fatal error" caseInsensitiveCompare:contentValue] == NSOrderedSame
|| [@"precondition failed" caseInsensitiveCompare:contentValue] == NSOrderedSame;
}
- (instancetype)init {
return [self initWithClass:@"Unknown" message:@"<unset>"];
}
- (instancetype)initWithClass:(NSString *)errorClass message:(NSString *)errorMessage {
if (errorClass.length == 0) {
return nil;
}
if (self = [super init]) {
_errorClass = errorClass;
_errorMessage = errorMessage;
}
return self;
}
@end