forked from Countly/countly-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountlyCrashReporter.m
184 lines (145 loc) · 6.36 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
// 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[@"_architecture"] = CountlyDeviceInfo.architecture;
crashReport[@"_resolution"] = CountlyDeviceInfo.resolution;
crashReport[@"_app_version"] = CountlyDeviceInfo.appVersion;
crashReport[@"_app_build"] = CountlyDeviceInfo.appBuild;
crashReport[@"_build_uuid"] = CountlyDeviceInfo.buildUUID;
crashReport[@"_executable_name"] = CountlyDeviceInfo.executableName;
crashReport[@"_name"] = exception.description;
crashReport[@"_type"] = exception.name;
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;
UInt64 loadAddress = 0;
NSMutableString* stackString = NSMutableString.string;
for (NSString* line in stackArray)
{
[stackString appendString:line];
[stackString appendString:@"\n"];
if (loadAddress == 0)
{
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"\\s+\\s" options:0 error:nil];
NSString* trimmedLine = [regex stringByReplacingMatchesInString:line options:0 range:(NSRange){0,line.length} withTemplate:@" "];
NSArray* lineComponents = [trimmedLine componentsSeparatedByString:@" "];
if (lineComponents.count >= 3 && [lineComponents[1] isEqualToString:CountlyDeviceInfo.executableName])
{
NSString* address = lineComponents[2];
NSString* offset = lineComponents.lastObject;
UInt64 length = strtoull(address.UTF8String, NULL, 16);
loadAddress = length - offset.integerValue;
}
}
}
crashReport[@"_load_address"] = [NSString stringWithFormat:@"0x%llx", loadAddress];
crashReport[@"_error"] = stackString;
if (nonfatal)
{
[CountlyConnectionManager.sharedInstance sendCrashReport:[crashReport cly_JSONify] immediately:NO];
return;
}
[CountlyConnectionManager.sharedInstance sendCrashReport:[crashReport cly_JSONify] immediately:YES];
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]];
}
#endif
@end