forked from nst/RuntimeBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTBMethod.m
192 lines (150 loc) · 6.1 KB
/
RTBMethod.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
//
// RTBMethod.m
// OCRuntime
//
// Created by Nicolas Seriot on 06/05/15.
// Copyright (c) 2015 Nicolas Seriot. All rights reserved.
//
#import "RTBMethod.h"
#import "RTBRuntimeHeader.h"
#include "dlfcn.h"
#if USE_NEW_DECODER
#import "RTBTypeDecoder2.h"
@compatibility_alias RTBTypeDecoder RTBTypeDecoder2;
#else
#import "RTBTypeDecoder.h"
#endif
@interface RTBMethod ()
@property (nonatomic) Method method;
@property (nonatomic) BOOL isClassMethod;
@property (nonatomic, strong) NSNumber *cachedHasArguments;
@property (nonatomic, strong) NSArray *cachedArgumentsTypesDecoded;
@property (nonatomic, strong) NSDictionary *cachedDyldInfoDictionary;
@end
@implementation RTBMethod
+ (instancetype)methodObjectWithMethod:(Method)method isClassMethod:(BOOL)isClassMethod {
RTBMethod *m = [[RTBMethod alloc] init];
m.method = method;
m.isClassMethod = isClassMethod;
return m;
}
- (NSString *)description {
NSString *superDescription = [super description];
return [NSString stringWithFormat:@"%@ %@", superDescription, [self selectorString]];
}
- (Method)method {
return _method;
}
- (NSDictionary *)dyldInfo {
IMP imp = method_getImplementation(_method);
Dl_info info;
int rc = dladdr(imp, &info);
if (!rc) {
return nil;
}
// printf("-- function %s\n", info.dli_sname);
// printf("-- program %s\n", info.dli_fname);
// printf("-- fbase %p\n", info.dli_fbase);
// printf("-- saddr %p\n", info.dli_saddr);
NSString *filePath = [NSString stringWithFormat:@"%s", info.dli_fname];
#if TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
NSString *symbolName = @""; // info.dli_sname is unreliable on the device, most of time "<redacted>"
#else
NSString *symbolName = [NSString stringWithFormat:@"%s", info.dli_sname];
#endif
NSString *categoryName = nil;
NSUInteger startIndex = [symbolName rangeOfString:@"("].location;
NSUInteger stopIndex = [symbolName rangeOfString:@")"].location;
if(startIndex != NSNotFound && stopIndex != NSNotFound && startIndex < stopIndex) {
categoryName = [symbolName substringWithRange:NSMakeRange(startIndex+1, (stopIndex - startIndex)-1)];
}
NSMutableDictionary *md = [NSMutableDictionary dictionaryWithCapacity:2];
if(filePath) md[@"filePath"] = filePath;
if(symbolName) md[@"symbolName"] = symbolName;
if(categoryName) md[@"categoryName"] = categoryName;
return md;
}
- (NSString *)categoryName {
if(_cachedDyldInfoDictionary == nil) {
self.cachedDyldInfoDictionary = [self dyldInfo];
}
return _cachedDyldInfoDictionary[@"categoryName"];
}
- (NSString *)symbolName {
if(_cachedDyldInfoDictionary == nil) {
self.cachedDyldInfoDictionary = [self dyldInfo];
}
return _cachedDyldInfoDictionary[@"symbolName"];
}
- (NSString *)filePath {
if(_cachedDyldInfoDictionary == nil) {
self.cachedDyldInfoDictionary = [self dyldInfo];
}
return _cachedDyldInfoDictionary[@"filePath"];
}
- (BOOL)hasArguments {
if(_cachedHasArguments == nil) {
self.cachedHasArguments = [NSNumber numberWithBool:[[self argumentsTypesDecoded] count] > 2]; // id, SEL, ...
}
return [_cachedHasArguments boolValue];
}
- (NSString *)returnTypeEncoded {
static int BUFFER_SIZE = 255;
char* buffer = malloc(BUFFER_SIZE * sizeof(char));
method_getReturnType(_method, buffer, BUFFER_SIZE);
NSString *s = [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
free(buffer);
return s;
}
- (NSString *)returnTypeDecoded {
NSString *s = [self returnTypeEncoded];
return [RTBTypeDecoder decodeType:s flat:YES];
}
- (NSArray *)argumentsTypesDecoded {
if(_cachedArgumentsTypesDecoded == nil) {
/*
NSString *methodName = [NSString stringWithCString:method_getName(_method) encoding:NSUTF8StringEncoding];
if([self.filePath isEqualToString:@"/System/Library/PrivateFrameworks/CoreKnowledge.framework/CoreKnowledge"] && [methodName isEqualToString:@"identifier"]) {
return _cachedArgumentsTypesDecoded;
}
*/
unsigned int numberOfArguments = method_getNumberOfArguments(_method);
NSMutableArray *ma = [NSMutableArray array];
for(unsigned int i = 0; i < numberOfArguments; i++) {
char *argType = method_copyArgumentType(_method, i);
NSAssert(argType != NULL, @"");
NSString *encodedType = [NSString stringWithCString:argType encoding:NSASCIIStringEncoding];
free(argType);
NSString *decodedType = [RTBTypeDecoder decodeType:encodedType flat:YES];
[ma addObject:decodedType];
}
self.cachedArgumentsTypesDecoded = ma;
}
return _cachedArgumentsTypesDecoded;
}
- (NSString *)headerDescriptionWithNewlineAfterArgs:(BOOL)newlineAfterArgs {
char* returnTypeCString = method_copyReturnType(_method);
if(returnTypeCString == NULL) return @"";
NSString *returnTypeEncoded = [NSString stringWithCString:returnTypeCString encoding:NSASCIIStringEncoding];
free(returnTypeCString);
NSString *returnType = [RTBTypeDecoder decodeType:returnTypeEncoded flat:YES];
NSString *methodName = NSStringFromSelector(method_getName(_method));
NSArray *argumentTypes = [self argumentsTypesDecoded];
return [RTBRuntimeHeader descriptionForMethodName:methodName
returnType:returnType
argumentTypes:argumentTypes
newlineAfterArgs:newlineAfterArgs
isClassMethod:_isClassMethod];
}
- (NSString *)selectorString {
return NSStringFromSelector(method_getName(_method));
}
- (SEL)selector {
return method_getName(_method);
}
- (NSComparisonResult)compare:(RTBMethod *)otherMethod {
if(self.isClassMethod && otherMethod.isClassMethod == NO) return NSOrderedAscending;
if(self.isClassMethod == NO && otherMethod.isClassMethod) return NSOrderedDescending;
return [[self selectorString] compare:[otherMethod selectorString]];
}
@end