/
Tweak.xm
277 lines (247 loc) · 7.35 KB
/
Tweak.xm
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
@import Foundation;
#import <MRYIPCCenter.h>
#import <sharedutils.h>
#import "symbolication.h"
#import <mach-o/dyld.h>
#import <mach/mach.h>
#import <dlfcn.h>
@interface Cr4shedServer : NSObject
+ (id)sharedInstance;
-(NSDictionary*)sendNotification:(NSDictionary*)userInfo;
-(NSDictionary*)writeString:(NSDictionary*)userInfo;
@end
static NSString* writeStringToFile(NSString* str, NSString* filename)
{
NSDictionary* reply;
if (%c(Cr4shedServer))
{
reply = [[%c(Cr4shedServer) sharedInstance] writeString:@{@"string" : str, @"filename" : filename}];
}
else
{
MRYIPCCenter* ipcCenter = [MRYIPCCenter centerNamed:@"com.muirey03.cr4sheddserver"];
reply = [ipcCenter callExternalMethod:@selector(writeString:) withArguments:@{@"string" : str, @"filename" : filename}];
}
return reply[@"path"];
}
static NSString* getCallStack(NSException* e)
{
NSArray* symbols = symbolicatedException(e);
NSString* symbolStr = [symbols componentsJoinedByString:@"\n"];
return symbolStr;
}
static void sendNotification(NSString* content, NSDictionary* userInfo)
{
if (%c(Cr4shedServer))
{
[[%c(Cr4shedServer) sharedInstance] sendNotification:@{@"content" : content}];
}
else
{
MRYIPCCenter* ipcCenter = [MRYIPCCenter centerNamed:@"com.muirey03.cr4sheddserver"];
[ipcCenter callExternalMethod:@selector(sendNotification:) withArguments:@{@"content" : content, @"userInfo" : userInfo}];
}
}
static unsigned long getImageVersion(uint32_t img)
{
if (img < _dyld_image_count())
{
const struct mach_header* header = _dyld_get_image_header(img);
if (header)
{
BOOL is64bit = header->magic == MH_MAGIC_64 || header->magic == MH_CIGAM_64;
uintptr_t cursor = (uintptr_t)header + (is64bit ? sizeof(struct mach_header_64) : sizeof(struct mach_header));
const struct segment_command* segmentCommand = NULL;
for (uint32_t i = 0; i < header->ncmds; i++, cursor += segmentCommand->cmdsize)
{
segmentCommand = (struct segment_command *)cursor;
if (segmentCommand->cmd == LC_ID_DYLIB)
{
const struct dylib_command* dylibCommand = (const struct dylib_command*)segmentCommand;
return dylibCommand->dylib.current_version;
}
}
}
}
return 0;
}
static void createCrashLog(NSString* specialisedInfo, NSMutableDictionary* extraInfo)
{
if (isBlacklisted()) return;
markProcessAsHandled();
// Format the contents of the new crash log:
NSDate* now = [NSDate date];
NSString* dateString = stringFromDate(now, CR4DateFormatPretty);
NSString* processID = [NSBundle mainBundle].bundleIdentifier;
NSString* processName = [[NSProcessInfo processInfo] processName];
NSString* device = [NSString stringWithFormat:@"%@, iOS %@", deviceName(), deviceVersion()];
NSBundle* bundle = [NSBundle mainBundle];
NSString* versionString = bundle.infoDictionary[@"CFBundleShortVersionString"];
if (!versionString)
versionString = bundle.infoDictionary[@"CFBundleVersion"];
NSMutableString* errorMessage = [NSMutableString stringWithFormat: @"Date: %@\n"
@"Process: %@\n"
@"Bundle id: %@\n"
@"Device: %@\n",
dateString,
processName,
processID,
device];
if (versionString.length)
[errorMessage appendFormat:@"Bundle version: %@\n", versionString];
[errorMessage appendFormat: @"\n%@\n\n"
@"Loaded images:\n",
specialisedInfo];
//add image infos:
uint32_t image_cnt = _dyld_image_count();
for (unsigned int i = 0; i < image_cnt; i++)
{
[errorMessage appendFormat:@"%u: %s (Version: %lu)\n", i, _dyld_get_image_name(i), getImageVersion(i)];
}
//extra info for the GUI to parse easily:
if (!extraInfo) extraInfo = [NSMutableDictionary new];
[extraInfo addEntriesFromDictionary:@{
@"ProcessName" : processName ?: @"",
@"ProcessBundleID" : processID ?: @""
}];
errorMessage = [addInfoToLog(errorMessage, [extraInfo copy]) mutableCopy];
// Get the date to use for the filename:
NSString* filenameDateStr = stringFromDate(now, CR4DateFormatFilename);
// Get the filename for the new crash log:
NSString* filename = [NSString stringWithFormat:@"%@@%@", processName, filenameDateStr];
// Create the crash log
NSString* path = writeStringToFile(errorMessage, filename);
if (!path)
return;
//show notification:
NSDictionary* notifUserInfo = @{@"logPath" : path};
sendNotification([NSString stringWithFormat:@"%@ crashed at %@", processName, dateString], notifUserInfo);
}
/* add the exception handler: */
static NSUncaughtExceptionHandler* oldHandler = NULL;
void createNSExceptionLog(NSException* e)
{
/* Remove false positives: */
if ([e.reason containsString:@"optimistic locking failure"])
return;
if ([e.reason containsString:@"This NSPersistentStoreCoordinator has no persistent stores"])
return;
NSString* culprit = determineCulprit(e.callStackSymbols);
NSString* stackSymbols = getCallStack(e);
NSMutableString* info = [NSMutableString stringWithFormat: @"Exception type: %@\n"
@"Reason: %@\n"
@"Culprit: %@\n\n",
e.name,
e.reason,
culprit];
//user info (for Supercharge):
NSDictionary* excUserInfo = e.userInfo;
if (excUserInfo.allKeys.count)
{
NSMutableString* userInfoStr = [@"User info:\n" mutableCopy];
for (NSString* key in excUserInfo.allKeys)
{
NSString* objStr = [excUserInfo[key] description];
//if it is multi-line, insert a '\n' at the start
if ([objStr componentsSeparatedByString:@"\n"].count > 1)
objStr = [@"\n" stringByAppendingString:objStr];
if (!objStr.length) objStr = @"N/A";
[userInfoStr appendFormat:@"%@: %@\n", key, objStr];
}
[userInfoStr appendString:@"\n"];
[info appendString:userInfoStr];
}
[info appendFormat:@"Call stack:\n%@", stackSymbols];
NSMutableDictionary* extraInfo = [@{
@"Culprit" : culprit ?: @"Unknown",
@"NSExceptionReason" : e.reason ?: @""
} mutableCopy];
createCrashLog([info copy], extraInfo);
}
void unhandledExceptionHandler(NSException* e)
{
@autoreleasepool
{
static BOOL hasCrashed = NO;
if (hasCrashed)
exit(EXIT_FAILURE);
else
hasCrashed = YES;
@try
{
createNSExceptionLog(e);
}
@catch (NSException* e)
{
exit(EXIT_FAILURE);
}
if (oldHandler)
oldHandler(e);
}
}
%group Tweak
%hookf (void, NSSetUncaughtExceptionHandler, NSUncaughtExceptionHandler* handler)
{
@autoreleasepool
{
if (handler != &unhandledExceptionHandler)
{
oldHandler = handler;
return;
}
%orig;
}
}
%hookf (NSUncaughtExceptionHandler*, NSGetUncaughtExceptionHandler)
{
@autoreleasepool
{
return oldHandler;
}
}
%end
inline BOOL isHardBlacklisted(NSString* procName)
{
if (!procName)
return YES;
NSArray<NSString*>* blacklisted = @[
@"ProtectedCloudKeySyncing",
@"gssc",
@"awdd",
@"biometrickitd",
@"spindump",
@"keybagd",
@"ReportMemoryException",
@"nsurlsessiond",
@"locationd",
@"coreduetd",
@"mDNSResponder",
@"hangreporter",
@"nanoregistrylaunchd",
@"nanoregistryd",
@"mobilewatchdog",
@"misd",
@"dasd",
@"passd",
@"CircleJoinRequested",
@"suggestd"
];
for (NSString* bannedProc in blacklisted)
{
if (bannedProc && [procName isEqualToString:bannedProc])
return YES;
}
return NO;
}
%ctor
{
@autoreleasepool
{
if (!isHardBlacklisted([[NSProcessInfo processInfo] processName]))
{
oldHandler = NSGetUncaughtExceptionHandler();
NSSetUncaughtExceptionHandler(&unhandledExceptionHandler);
%init(Tweak);
}
}
}