-
Notifications
You must be signed in to change notification settings - Fork 130
/
BugsnagCrashReport.m
358 lines (296 loc) · 11.5 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
//
// KSCrashReport.m
// Bugsnag
//
// Created by Simon Maynard on 11/26/14.
//
//
#import "Bugsnag.h"
#import "BugsnagCollections.h"
#import "BugsnagCrashReport.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:@"0x%lx", instructionAddress],
@"frameAddress");
BSGDictSetSafeObject(formatted,
[NSString stringWithFormat:@"0x%lx", symbolAddress],
@"symbolAddress");
BSGDictSetSafeObject(formatted,
[NSString stringWithFormat:@"0x%lx", imageAddress],
@"machoLoadAddress");
BSGDictInsertIfNotNil(formatted, frame[@"isPC"], @"isPC");
BSGDictInsertIfNotNil(formatted, frame[@"isLR"], @"isLR");
NSString *file = frame[@"object_name"];
NSString *method = frame[@"symbol_name"];
BSGDictInsertIfNotNil(formatted, file, @"machoFile");
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"], @"machoUUID");
BSGDictInsertIfNotNil(formatted, image[@"name"], @"machoFile");
BSGDictSetSafeObject(formatted,
[NSString stringWithFormat:@"0x%lx", imageSlide],
@"machoVMAddress");
return formatted;
}
}
return nil;
}
@implementation BugsnagCrashReport
- (id)initWithKSReport:(NSDictionary *)report {
if ((self = [super init])) {
_ksReport = report;
}
return self;
}
- (NSString *)releaseStage {
return self.config[@"releaseStage"];
}
- (NSArray *)notifyReleaseStages {
return self.config[@"notifyReleaseStages"];
}
- (NSString *)context {
if ([self.config[@"context"] isKindOfClass:[NSString class]]) {
return self.config[@"context"];
}
// TODO:SM Get other contexts if possible
return nil;
}
- (NSString *)appVersion {
if ([self.config[@"appVersion"] isKindOfClass:[NSString class]]) {
return self.config[@"appVersion"];
}
return nil;
}
- (NSArray *)binaryImages {
return self.ksReport[@"binary_images"];
}
- (NSArray *)threads {
return self.crash[@"threads"];
}
- (NSDictionary *)error {
return self.crash[@"error"];
}
- (NSString *)errorType {
return self.error[@"type"];
}
- (NSString *)errorClass {
if ([self.errorType isEqualToString:@"cpp_exception"]) {
return self.error[@"cpp_exception"][@"name"];
} else if ([self.errorType isEqualToString:@"mach"]) {
return self.error[@"mach"][@"exception_name"];
} else if ([self.errorType isEqualToString:@"signal"]) {
return self.error[@"signal"][@"name"];
} else if ([self.errorType isEqualToString:@"nsexception"]) {
return self.error[@"nsexception"][@"name"];
} else if ([self.errorType isEqualToString:@"user"]) {
return self.error[@"user_reported"][@"name"];
}
return @"Exception";
}
- (NSString *)errorMessage {
if ([self.errorType isEqualToString:@"mach"]) {
NSString *diagnosis = self.crash[@"diagnosis"];
if (diagnosis && ![diagnosis hasPrefix:@"No diagnosis"]) {
return [[diagnosis componentsSeparatedByString:@"\n"] firstObject];
}
}
return self.error[@"reason"];
}
- (NSArray *)breadcrumbs {
return self.state[@"crash"][@"breadcrumbs"];
}
- (NSString *)severity {
return self.state[@"crash"][@"severity"];
}
- (NSString *)dsymUUID {
return self.system[@"app_uuid"];
}
- (NSString *)deviceAppHash {
return self.system[@"device_app_hash"];
}
- (NSUInteger)depth {
return [self.state[@"crash"][@"depth"] unsignedIntegerValue];
}
- (NSDictionary *)metaData {
return self.ksReport[@"user"][@"metaData"];
}
- (NSDictionary *)appStats {
return self.system[@"application_stats"];
}
// PRIVATE
- (NSDictionary *)system {
return self.ksReport[@"system"];
}
- (NSDictionary *)state {
return self.ksReport[@"user"][@"state"];
}
- (NSDictionary *)config {
return self.ksReport[@"user"][@"config"];
}
- (NSDictionary *)crash {
return self.ksReport[@"crash"];
}
- (NSDictionary *)unityExceptionReport {
return self.metaData[@"_bugsnag_unity_exception"];
}
- (NSDictionary *)serializableValueWithTopLevelData:
(NSMutableDictionary *)data {
NSMutableDictionary *event = [NSMutableDictionary dictionary];
NSMutableDictionary *exception = [NSMutableDictionary dictionary];
NSMutableDictionary *metaData = [[self metaData] mutableCopy];
NSString *severity =
[self severity].length > 0 ? [self severity] : BugsnagSeverityError;
// Build Event
BSGDictSetSafeObject(event, @[ exception ], @"exceptions");
BSGDictInsertIfNotNil(event, [self dsymUUID], @"dsymUUID");
BSGDictSetSafeObject(event, severity, @"severity");
BSGDictSetSafeObject(event, [self breadcrumbs], @"breadcrumbs");
BSGDictSetSafeObject(event, @"2", @"payloadVersion");
BSGDictSetSafeObject(event, metaData, @"metaData");
BSGDictSetSafeObject(event, [self deviceState], @"deviceState");
BSGDictSetSafeObject(event, [self device], @"device");
BSGDictSetSafeObject(event, [self appState], @"appState");
BSGDictSetSafeObject(event, [self app], @"app");
if ([metaData[@"context"] isKindOfClass:[NSString class]]) {
BSGDictSetSafeObject(event, metaData[@"context"], @"context");
[metaData removeObjectForKey:@"context"];
} else {
BSGDictSetSafeObject(event, [self context], @"context");
}
// Build MetaData
BSGDictSetSafeObject(metaData, [self error], @"error");
// Make user mutable and set the id if the user hasn't already
NSMutableDictionary *user = [metaData[@"user"] mutableCopy];
if (user == nil)
user = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(metaData, user, @"user");
if (!user[@"id"]) {
BSGDictSetSafeObject(user, [self deviceAppHash], @"id");
}
// Build Exception
BSGDictSetSafeObject(exception, [self errorClass], @"errorClass");
BSGDictInsertIfNotNil(exception, [self errorMessage], @"message");
// HACK: For the Unity Notifier. We don't include ObjectiveC exceptions or
// threads
// if this is an exception from Unity-land.
NSDictionary *unityReport = metaData[@"_bugsnag_unity_exception"];
if (unityReport) {
BSGDictSetSafeObject(data, unityReport[@"notifier"], @"notifier");
BSGDictSetSafeObject(exception, unityReport[@"stacktrace"], @"stacktrace");
[metaData removeObjectForKey:@"_bugsnag_unity_exception"];
return event;
}
BSGDictSetSafeObject(event, [self serializeThreadsWithException:exception],
@"threads");
return event;
}
// 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];
if ([thread[@"crashed"] boolValue]) {
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, @"isPC");
}
if (seen == 2 && !stackOverflow &&
[@[ @"signal", @"deadlock", @"mach" ]
containsObject:[self errorType]]) {
BSGDictSetSafeObject(mutableFrame, @YES, @"isLR");
}
BSGArrayInsertIfNotNil(
stacktrace, BSGFormatFrame(mutableFrame, [self binaryImages]));
}
}
BSGDictSetSafeObject(exception, stacktrace, @"stacktrace");
} else {
NSMutableArray *threadStack = [NSMutableArray array];
for (NSDictionary *frame in backtrace) {
BSGArrayInsertIfNotNil(threadStack,
BSGFormatFrame(frame, [self binaryImages]));
}
NSMutableDictionary *threadDict = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(threadDict, thread[@"index"], @"id");
BSGDictSetSafeObject(threadDict, threadStack, @"stacktrace");
// only if this is enabled in KSCrash.
if (thread[@"name"]) {
BSGDictSetSafeObject(threadDict, thread[@"name"], @"name");
}
BSGArrayAddSafeObject(bugsnagThreads, threadDict);
}
}
return bugsnagThreads;
}
// Generates the deviceState section of the payload
- (NSDictionary *)deviceState {
NSMutableDictionary *deviceState = [[self state][@"deviceState"] mutableCopy];
BSGDictSetSafeObject(deviceState, [self system][@"memory"][@"free"],
@"freeMemory");
return deviceState;
}
// Generates the device section of the payload
- (NSDictionary *)device {
NSMutableDictionary *device = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(device, @"Apple", @"manufacturer");
BSGDictSetSafeObject(device, [[NSLocale currentLocale] localeIdentifier],
@"locale");
BSGDictSetSafeObject(device, [self system][@"device_app_hash"], @"id");
BSGDictSetSafeObject(device, [self system][@"time_zone"], @"timezone");
BSGDictSetSafeObject(device, [self system][@"model"], @"modelNumber");
BSGDictSetSafeObject(device, [self system][@"machine"], @"model");
BSGDictSetSafeObject(device, [self system][@"system_name"], @"osName");
BSGDictSetSafeObject(device, [self system][@"system_version"], @"osVersion");
BSGDictSetSafeObject(device, [self system][@"memory"][@"usable"],
@"totalMemory");
return device;
}
// Generates the appState section of the payload
- (NSDictionary *)appState {
NSMutableDictionary *appState = [NSMutableDictionary dictionary];
NSInteger activeTimeSinceLaunch =
[[self appStats][@"active_time_since_launch"] doubleValue] * 1000.0;
NSInteger backgroundTimeSinceLaunch =
[[self appStats][@"background_time_since_launch"] doubleValue] * 1000.0;
BSGDictSetSafeObject(appState, @(activeTimeSinceLaunch),
@"durationInForeground");
BSGDictSetSafeObject(appState,
@(activeTimeSinceLaunch + backgroundTimeSinceLaunch),
@"duration");
BSGDictSetSafeObject(appState, [self appStats][@"application_in_foreground"],
@"inForeground");
BSGDictSetSafeObject(appState, [self appStats], @"stats");
return appState;
}
// Generates the app section of the payload
- (NSDictionary *)app {
NSMutableDictionary *app = [NSMutableDictionary dictionary];
BSGDictSetSafeObject(app, [self system][@"CFBundleVersion"],
@"bundleVersion");
BSGDictSetSafeObject(app, [self system][@"CFBundleIdentifier"], @"id");
BSGDictSetSafeObject(app, [self system][@"CFBundleExecutable"], @"name");
BSGDictSetSafeObject(app, [Bugsnag configuration].releaseStage,
@"releaseStage");
if ([self appVersion]) {
BSGDictSetSafeObject(app, [self appVersion], @"version");
} else {
BSGDictSetSafeObject(app, [self system][@"CFBundleShortVersionString"],
@"version");
}
return app;
}
@end