-
Notifications
You must be signed in to change notification settings - Fork 6
/
SwizzleKit.m
379 lines (323 loc) · 13.3 KB
/
SwizzleKit.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
// SwizzleKit
// Created by Scott Morrison on 12/01/09.
// ------------------------------------------------------------------------
// Copyright (c) 2009, Scott Morrison All rights reserved.
//
//
// ------------------------------------------------------------------------
#import "SwizzleKit.m"
#define SWIZZLE_PREFIX @"UNIQUE_PREFIX"
#define PROVIDER_SUFFIX @"UNIQUE_PREFIX"
@implementation NSObject (UNIQUE_PREFIXSwizzleKit)
-(BOOL)UNIQUE_PREFIXrespondsDirectlyToSelector:(SEL)aSelector{
BOOL responds = NO;
unsigned int methodCount = 0;
Method * methods = nil;
// extend instance Methods
methods = class_copyMethodList([self class], &methodCount);
int ci= methodCount;
while (methods && ci--){
if (method_getName(methods[ci]) == aSelector){
responds = YES;
break;
}
}
free(methods);
return responds;
}
@end
id UNIQUE_PREFIXobject_getMapTableVariable(id anObject, const char* variableName){
static NSMapTable * mapTable = nil;
if (!mapTable){
if ([[anObject class] respondsToSelector:@selector(mapTable)]){
mapTable = [[anObject class] mapTable];
}
}
id theValue = nil;
if (mapTable){
@synchronized(mapTable){
NSMutableDictionary *aDict;
aDict = NSMapGet(mapTable, anObject);
if (nil == aDict){
aDict = [NSMutableDictionary dictionary];
NSMapInsert(mapTable, anObject, aDict);
}
theValue = [aDict objectForKey:[NSString stringWithFormat:@"%s",variableName] ] ;
}
}
return theValue;
}
void UNIQUE_PREFIXobject_setMapTableVariable(id anObject, const char* variableName,id value){
static NSMapTable * mapTable = nil;
if (!mapTable){
if ([[anObject class] respondsToSelector:@selector(mapTable)]){
mapTable = [[anObject class] mapTable];
}
}
if (mapTable){
@synchronized(mapTable){
NSMutableDictionary *aDict;
aDict = NSMapGet(mapTable, anObject);
if (nil == aDict){
aDict = [NSMutableDictionary dictionary];
NSMapInsert(mapTable, anObject, aDict);
}
if (value){
[aDict setObject:value forKey:[NSString stringWithFormat:@"%s",variableName]];
}
else{
[aDict removeObjectForKey:[NSString stringWithFormat:@"%s",variableName]];
}
}
}
}
void UNIQUE_PREFIXdescribeClass(const char * clsName){
Class aClass = objc_getClass(clsName);
if (aClass){
NSMutableString * logString = [NSMutableString string];
Class superClass = class_getSuperclass(aClass);
const char * superClassName = class_getName(superClass);
[logString appendFormat:@"@interface %s : %s\n{",clsName,superClassName];
unsigned int ivarCount = 0;
NSUInteger ci =0;
Ivar * ivars = class_copyIvarList(aClass, &ivarCount);
for (ci=0;ci<ivarCount;ci++){
[logString appendFormat:@" %s %s; //%ld\n",ivar_getTypeEncoding(ivars[ci]), ivar_getName(ivars[ci]), ivar_getOffset(ivars[ci])];
}
[logString appendString:@"}\n"];
free(ivars);
unsigned int classMethodCount =0;
Method *classMethods = class_copyMethodList(object_getClass(aClass), &classMethodCount);
for(ci=0;ci<classMethodCount;ci++){
[logString appendFormat:@"+[%s %@]\n",class_getName(aClass),NSStringFromSelector(method_getName(classMethods[ci]))];
}
free(classMethods);
unsigned int instanceMethodCount =0;
Method *instanceMethods = class_copyMethodList(aClass, &instanceMethodCount);
for(ci=0;ci<instanceMethodCount;ci++){
[logString appendFormat:@"-[%s %@]\n",class_getName(aClass),NSStringFromSelector(method_getName(instanceMethods[ci]))];
}
NSLog(@"%@",logString);
}
}
@implementation UNIQUE_PREFIXSwizzler
+(Class)subclass:(Class)baseClass usingClassName:(NSString*)subclassName providerClass:(Class)providerClass{
Class subclass = objc_allocateClassPair(baseClass, [subclassName UTF8String], 0);
if (!subclass) return nil;
unsigned int ivarCount =0;
Ivar * ivars = class_copyIvarList(providerClass, &ivarCount);
int ci = 0;
for (ci=0 ;ci < ivarCount; ci++){
Ivar anIvar = ivars[ci];
NSUInteger ivarSize = 0;
NSUInteger ivarAlignment = 0;
const char * typeEncoding = ivar_getTypeEncoding(anIvar);
NSGetSizeAndAlignment(typeEncoding, &ivarSize, &ivarAlignment);
const char * ivarName = ivar_getName(anIvar);
NSString * ivarStringName = [NSString stringWithUTF8String:ivarName];
BOOL addIVarResult = class_addIvar(subclass, ivarName, ivarSize, ivarAlignment, typeEncoding );
if (!addIVarResult){
NSLog(@"could not add iVar %s", ivar_getName(anIvar));
return nil;
}
}
free(ivars);
objc_registerClassPair(subclass);
[self extendClass:subclass withMethodsFromClass:providerClass];
return subclass;
}
+(void)extendClass:(Class) targetClass withMethodsFromClass:(Class)providerClass{
unsigned int methodCount = 0;
Method * methods = nil;
// extend instance Methods
methods = class_copyMethodList(providerClass, &methodCount);
int ci= methodCount;
while (methods && ci--){
NSString * methodName = NSStringFromSelector(method_getName(methods[ci]));
[self addInstanceMethodName:methodName fromProviderClass:providerClass toClass:targetClass];
//NSLog(@"extending -[%s %@]",class_getName(targetClass),methodName);
}
free(methods);
// extend Class Methods
methods = class_copyMethodList(object_getClass(providerClass), &methodCount);
ci= methodCount;
while (methods && ci--){
NSString * methodName = NSStringFromSelector(method_getName(methods[ci]));
[self addClassMethodName:methodName fromProviderClass:providerClass toClass:targetClass];
//NSLog(@"extending +[%s %@]",class_getName(targetClass),methodName);
}
free(methods);
methods = 0;
}
+(BOOL)addClassMethodName:(NSString *)methodName fromProviderClass:(Class)providerClass toClass:(Class)targetClass{
Class metaClass = object_getClass(targetClass);// objc_getMetaClass(class_getName(targetClass));
if (!metaClass) {
return NO;
}
SEL selector = NSSelectorFromString(methodName);
Method originalMethod = class_getClassMethod(providerClass,selector);
if (!originalMethod) {
return NO;
}
IMP originalImplementation = method_getImplementation(originalMethod);
if (!originalImplementation){
return NO;
}
class_addMethod(metaClass, selector ,originalImplementation, method_getTypeEncoding(originalMethod));
return YES;
}
+(BOOL)addInstanceMethodName:(NSString *)methodName fromProviderClass:(Class)providerClass toClass:(Class)targetClass{
if (!targetClass) {
return NO;
}
SEL selector = NSSelectorFromString(methodName);
Method originalMethod = class_getInstanceMethod(providerClass,selector);
if (!originalMethod) {
return NO;
}
IMP originalImplementation = method_getImplementation(originalMethod);
if (!originalImplementation){
return NO;
}
class_addMethod(targetClass, selector ,originalImplementation, method_getTypeEncoding(originalMethod));
return YES;
}
+(IMP)swizzleClassMethod:(NSString*)methodName forClass:(Class)targetClass{
Method oldMethod, newMethod;
IMP newIMP = nil;
SEL oldSelector = NSSelectorFromString(methodName);
NSString * newMethodName = [SWIZZLE_PREFIX stringByAppendingString:methodName];
SEL newSelector = NSSelectorFromString(newMethodName);
oldMethod = class_getClassMethod(targetClass, oldSelector);
if (oldMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for +[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
//Debugger();
return NULL;
}
newMethod = class_getClassMethod(targetClass, newSelector);
if (newMethod==NULL) {
//look for a provider Class
NSString * providerClassName = [NSStringFromClass(targetClass) stringByAppendingString:PROVIDER_SUFFIX];
Class providerClass = NSClassFromString(providerClassName);
if (providerClass){
[self addClassMethodName: newMethodName fromProviderClass:providerClass toClass:targetClass];
newMethod = class_getClassMethod(targetClass, newSelector);
if (newMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for +[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}
}
else{
NSLog(@"SWIZZLE Error - Can't find existing method for +[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}
//Debugger();
}
if (NULL != oldMethod && NULL != newMethod) {
//newIMP =
newIMP= method_getImplementation(oldMethod);
method_exchangeImplementations(oldMethod, newMethod);
//newIMP = method_setImplementation(oldMethod,method_getImplementation(newMethod));
//method_setImplementation(newMethod, newIMP);
return newIMP;
}
return NULL;
}
+(IMP)swizzleInstanceMethod:(NSString*)methodName forClass:(Class)targetClass{
//NSLog (@"Trying to swizzle %@ method: %@",targetClass,methodName);
Method oldMethod, newMethod;
IMP oldIMP = nil;
SEL oldSelector = NSSelectorFromString(methodName);
NSString * newMethodName = [SWIZZLE_PREFIX stringByAppendingString:methodName];
SEL newSelector = NSSelectorFromString(newMethodName);
oldMethod = class_getInstanceMethod(targetClass, oldSelector);
if (oldMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for -[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
//Debugger();
return NULL;
}
newMethod = class_getInstanceMethod(targetClass, newSelector);
if (newMethod==NULL) {
//look for a provider Class
NSString * providerClassName = [NSStringFromClass(targetClass) stringByAppendingString:PROVIDER_SUFFIX];
Class providerClass = NSClassFromString(providerClassName);
if (providerClass){
[self addInstanceMethodName: newMethodName fromProviderClass:providerClass toClass:targetClass];
newMethod = class_getInstanceMethod(targetClass, newSelector);
if (newMethod==NULL) {
NSLog(@"SWIZZLE Error - Can't find existing method for -[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}
}
else{
NSLog(@"SWIZZLE Error - Can't find existing method for -[%@ %@]",NSStringFromClass(targetClass),NSStringFromSelector(oldSelector));
return NULL;
}
//Debugger();
}
if (NULL != oldMethod && NULL != newMethod) {
//newIMP = method_exchangeImplementations(, )
oldIMP = method_setImplementation(oldMethod,method_getImplementation(newMethod));
method_setImplementation(newMethod, oldIMP);
return oldIMP;
}
return NULL;
}
@end
@implementation NSThread(SwizzleKit)
+(NSArray*)abbreviatedCallStackSymbols{
// returns the backtrace as a NSArray of NSStrings, simplifying the addresses etc in the process.
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
NSMutableArray *callStack = [[NSMutableArray alloc] initWithCapacity:frames];
for (i = 1; i < frames; ++i) {
NSString * frameString = [[NSString alloc] initWithUTF8String:strs[i]];
NSScanner * scanner = [NSScanner scannerWithString:frameString];
NSString * dummy = nil;
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * frameNumber = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&frameNumber];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * module = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&module];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * address = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&address];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * method = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&method];
[callStack addObject:[NSString stringWithFormat:@"%3-s %18-s %@",[frameNumber UTF8String],[module UTF8String],method]];
}
free(strs);
return [callStack autorelease];
}
+(NSArray *)callStackSymbolsForFrameCount:(NSInteger) frameCount{
// will return the most recent <frameCount> stackFrames from the backtrace
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
NSMutableArray *callStack = [[NSMutableArray alloc] initWithCapacity:frames];
int maxFrame = MIN(frames,frameCount+1);
for (i = 1; i < maxFrame; ++i) {
NSString * frameString = [[NSString alloc] initWithUTF8String:strs[i]];
NSScanner * scanner = [NSScanner scannerWithString:frameString];
NSString * dummy = nil;
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * frameNumber = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&frameNumber];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * module = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&module];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * address = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&address];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &dummy];
NSString * method = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&method];
[callStack addObject:[NSString stringWithFormat:@"%3-s %18-s %@",[frameNumber UTF8String],[module UTF8String],method]];
}
free(strs);
return [callStack autorelease];
}
@end