forked from DavidGoldman/InspectiveC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInspCWrapper.m
More file actions
120 lines (101 loc) · 3.49 KB
/
InspCWrapper.m
File metadata and controls
120 lines (101 loc) · 3.49 KB
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
/*
If you link directly with libinspectivec, it will automatically load the dylib into any process
that you hook into (which is bad if you only want to hook into a single process). You can try
using this method instead.
*/
#include <dlfcn.h>
typedef void (*inspectiveC_IntFuncT)(int depth);
typedef void (*inspectiveC_ObjectFuncT)(id obj);
typedef void (*inspectiveC_ClassFuncT)(Class clazz);
typedef void (*inspectiveC_SelFuncT)(SEL _cmd);
typedef void (*inspectiveC_voidFuncT)(void);
static void *inspectiveC_Handle = NULL;
static inspectiveC_IntFuncT $setMaximumRelativeLoggingDepth;
static inspectiveC_ObjectFuncT $watchObject;
static inspectiveC_ObjectFuncT $unwatchObject;
static inspectiveC_ClassFuncT $watchClass;
static inspectiveC_ClassFuncT $unwatchClass;
static inspectiveC_SelFuncT $watchSelector;
static inspectiveC_SelFuncT $unwatchSelector;
static inspectiveC_voidFuncT $enableLogging;
static inspectiveC_voidFuncT $disableLogging;
static void * inspectiveC_loadFunctionNamed(const char *name) {
void *func = dlsym(inspectiveC_Handle, name);
if (!func) {
NSLog(@"[InspectiveC Wrapper] Unable to load function %s! Error: %s", name, dlerror());
}
return func;
}
static void inspectiveC_init() {
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
inspectiveC_Handle = dlopen("/usr/lib/libinspectivec.dylib", RTLD_NOW);
if (inspectiveC_Handle) {
$setMaximumRelativeLoggingDepth = (inspectiveC_IntFuncT)inspectiveC_loadFunctionNamed("InspectiveC_setMaximumRelativeLoggingDepth");
$watchObject = (inspectiveC_ObjectFuncT)inspectiveC_loadFunctionNamed("InspectiveC_watchObject");
$unwatchObject = (inspectiveC_ObjectFuncT)inspectiveC_loadFunctionNamed("InspectiveC_unwatchObject");
$watchClass = (inspectiveC_ClassFuncT)inspectiveC_loadFunctionNamed("InspectiveC_watchInstancesOfClass");
$unwatchClass = (inspectiveC_ClassFuncT)inspectiveC_loadFunctionNamed("InspectiveC_unwatchInstancesOfClass");
$watchSelector = (inspectiveC_SelFuncT)inspectiveC_loadFunctionNamed("InspectiveC_watchSelector");
$unwatchSelector = (inspectiveC_SelFuncT)inspectiveC_loadFunctionNamed("InspectiveC_unwatchSelector");
$enableLogging = (inspectiveC_voidFuncT)inspectiveC_loadFunctionNamed("InspectiveC_enableLogging");
$disableLogging = (inspectiveC_voidFuncT)inspectiveC_loadFunctionNamed("InspectiveC_disableLogging");
} else {
NSLog(@"[InspectiveC Wrapper] Unable to load libinspectivec! Error: %s", dlerror());
}
});
}
void setMaximumRelativeLoggingDepth(int depth) {
inspectiveC_init();
if($setMaximumRelativeLoggingDepth) {
$setMaximumRelativeLoggingDepth(depth);
}
}
void watchObject(id obj) {
inspectiveC_init();
if ($watchObject) {
$watchObject(obj);
}
}
void unwatchObject(id obj) {
inspectiveC_init();
if ($unwatchObject) {
$unwatchObject(obj);
}
}
void watchClass(Class clazz) {
inspectiveC_init();
if ($watchClass) {
$watchClass(clazz);
}
}
void unwatchClass(Class clazz) {
inspectiveC_init();
if ($unwatchClass) {
$unwatchClass(clazz);
}
}
void watchSelector(SEL _cmd) {
inspectiveC_init();
if ($watchSelector) {
$watchSelector(_cmd);
}
}
void unwatchSelector(SEL _cmd) {
inspectiveC_init();
if ($unwatchSelector) {
$unwatchSelector(_cmd);
}
}
void enableLogging() {
inspectiveC_init();
if ($enableLogging) {
$enableLogging();
}
}
void disableLogging() {
inspectiveC_init();
if ($disableLogging) {
$disableLogging();
}
}