forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyCrashReporter.m
241 lines (192 loc) · 7.91 KB
/
CountlyCrashReporter.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
// CountlyCrashReporter.m
//
// This code is provided under the MIT License.
//
// Please visit www.count.ly for more information.
#import "CountlyCommon.h"
@interface CountlyCrashReporter ()
@end
NSString* const kCountlyExceptionUserInfoBacktraceKey = @"kCountlyExceptionUserInfoBacktraceKey";
@implementation CountlyCrashReporter
static NSMutableArray *customCrashLogs = nil;
#if TARGET_OS_IOS
+ (instancetype)sharedInstance
{
static CountlyCrashReporter *s_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{s_sharedInstance = self.new;});
return s_sharedInstance;
}
- (instancetype)init
{
if (self = [super init])
{
self.crashSegmentation = nil;
}
return self;
}
- (void)startCrashReporting
{
NSSetUncaughtExceptionHandler(&CountlyUncaughtExceptionHandler);
signal(SIGABRT, CountlySignalHandler);
signal(SIGILL, CountlySignalHandler);
signal(SIGSEGV, CountlySignalHandler);
signal(SIGFPE, CountlySignalHandler);
signal(SIGBUS, CountlySignalHandler);
signal(SIGPIPE, CountlySignalHandler);
signal(SIGTRAP, CountlySignalHandler);
}
- (void)recordHandledException:(NSException *)exception
{
CountlyExceptionHandler(exception, true);
}
void CountlyUncaughtExceptionHandler(NSException *exception)
{
CountlyExceptionHandler(exception, false);
}
void CountlyExceptionHandler(NSException *exception, bool nonfatal)
{
NSMutableDictionary* crashReport = NSMutableDictionary.dictionary;
crashReport[@"_os"] = CountlyDeviceInfo.osName;
crashReport[@"_os_version"] = CountlyDeviceInfo.osVersion;
crashReport[@"_device"] = CountlyDeviceInfo.device;
crashReport[@"_resolution"] = CountlyDeviceInfo.resolution;
crashReport[@"_app_version"] = CountlyDeviceInfo.appVersion;
crashReport[@"_app_build"] = CountlyDeviceInfo.appBuild;
crashReport[@"_build_uuid"] = CountlyDeviceInfo.buildUUID;
crashReport[@"_name"] = exception.debugDescription;
crashReport[@"_nonfatal"] = @(nonfatal);
crashReport[@"_ram_current"] = @((CountlyDeviceInfo.totalRAM-CountlyDeviceInfo.freeRAM)/1048576);
crashReport[@"_ram_total"] = @(CountlyDeviceInfo.totalRAM/1048576);
crashReport[@"_disk_current"] = @((CountlyDeviceInfo.totalDisk-CountlyDeviceInfo.freeDisk)/1048576);
crashReport[@"_disk_total"] = @(CountlyDeviceInfo.totalDisk/1048576);
crashReport[@"_bat"] = @(CountlyDeviceInfo.batteryLevel);
crashReport[@"_orientation"] = CountlyDeviceInfo.orientation;
crashReport[@"_online"] = @((CountlyDeviceInfo.connectionType)? 1 : 0 );
crashReport[@"_opengl"] = @(CountlyDeviceInfo.OpenGLESversion);
crashReport[@"_root"] = @(CountlyDeviceInfo.isJailbroken);
crashReport[@"_background"] = @(CountlyDeviceInfo.isInBackground);
crashReport[@"_run"] = @(CountlyCommon.sharedInstance.timeSinceLaunch);
if(CountlyCrashReporter.sharedInstance.crashSegmentation)
crashReport[@"_custom"] = CountlyCrashReporter.sharedInstance.crashSegmentation;
if(customCrashLogs)
crashReport[@"_logs"] = [customCrashLogs componentsJoinedByString:@"\n"];
NSArray* stackArray = exception.userInfo[kCountlyExceptionUserInfoBacktraceKey];
if(!stackArray) stackArray = exception.callStackSymbols;
NSMutableString* stackString = NSMutableString.string;
for (NSString* line in stackArray)
{
[stackString appendString:line];
[stackString appendString:@"\n"];
}
crashReport[@"_error"] = stackString;
if(nonfatal)
{
[CountlyConnectionManager.sharedInstance sendCrashReportLater:[crashReport JSONify]];
}
else
{
if(!CountlyConnectionManager.sharedInstance.connection)
CountlyConnectionManager.sharedInstance.connection = NSURLSessionDataTask.new;
[Countly.sharedInstance suspend];
//NOTE: suspend method adds 'event' and 'end_session' requests to queue and starts them.
// a dummy connection object is created to prevent these requests when app is about to terminate due to crash
NSString *urlString = [NSString stringWithFormat:@"%@/i", CountlyConnectionManager.sharedInstance.appHost];
NSString *queryString = [[CountlyConnectionManager.sharedInstance queryEssentials] stringByAppendingFormat:@"&crash=%@", [crashReport JSONify]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
request.HTTPMethod = @"POST";
request.HTTPBody = [queryString dataUTF8];
COUNTLY_LOG(@"Crash report request started: %@ \n%@", urlString, queryString);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[[NSURLSession.sharedSession dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
{
if(error || ![CountlyConnectionManager.sharedInstance isRequestSuccessful:response])
{
COUNTLY_LOG(@"Crash report request failed! Report stored to try again later. \n%@", error);
[CountlyConnectionManager.sharedInstance sendCrashReportLater:[crashReport JSONify]];
}
else
{
COUNTLY_LOG(@"Crash report request successfully completed.");
}
dispatch_semaphore_signal(semaphore);
}] resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSSetUncaughtExceptionHandler(NULL);
signal(SIGABRT, SIG_DFL);
signal(SIGILL, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGTRAP, SIG_DFL);
}
}
void CountlySignalHandler(int signalCode)
{
void* callstack[128];
NSInteger frames = backtrace(callstack, 128);
char **lines = backtrace_symbols(callstack, (int)frames);
const NSInteger startOffset = 1;
NSMutableArray *backtrace = [NSMutableArray arrayWithCapacity:frames];
for (NSInteger i = startOffset; i < frames; i++)
[backtrace addObject:[NSString stringWithUTF8String:lines[i]]];
free(lines);
NSMutableDictionary *userInfo = @{@"signal_code":@(signalCode)}.mutableCopy;
userInfo[kCountlyExceptionUserInfoBacktraceKey] = backtrace;
NSString *reason = [NSString stringWithFormat:@"App terminated by SIG%@", [NSString stringWithUTF8String:sys_signame[signalCode]].uppercaseString];
NSException *e = [NSException exceptionWithName:@"Fatal Signal" reason:reason userInfo:userInfo];
CountlyUncaughtExceptionHandler(e);
}
- (void)logWithFormat:(NSString *)format andArguments:(va_list)args
{
static NSDateFormatter* df = nil;
if( customCrashLogs == nil )
{
customCrashLogs = NSMutableArray.new;
df = NSDateFormatter.new;
df.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
}
NSString* logFormat = [NSString stringWithFormat:@"<%@> %@",[df stringFromDate:NSDate.date], format];
[customCrashLogs addObject:[NSString.alloc initWithFormat:logFormat arguments:args]];
}
#pragma mark ---
- (void)crashTest
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
[self performSelector:@selector(thisIsTheUnrecognizedSelectorCausingTheCrash)];
#pragma clang diagnostic pop
}
- (void)crashTest2
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
NSArray* anArray = @[@"one",@"two",@"three"];
NSString* myCrashingString = anArray[5];
#pragma clang diagnostic pop
}
- (void)crashTest3
{
int *nullPointer = NULL;
*nullPointer = 2015;
}
- (void)crashTest4
{
CGRect aRect = (CGRect){0.0/0.0, 0.0, 100.0, 100.0};
UIView *crashView = UIView.new;
crashView.frame = aRect;
}
- (void)crashTest5
{
kill(getpid(), SIGABRT);
}
- (void)crashTest6
{
__builtin_trap();
}
#endif
@end