-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlegerdemain.h
72 lines (54 loc) · 1.77 KB
/
legerdemain.h
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
/*To ensure RTLD_NEXT is defined*/
#ifndef __USE_GNU
#define __USE_GNU
#endif
/*For dynamic loading support*/
#include <dlfcn.h>//for RTLD_NEXT
/*For ucontext_t*/
#include <ucontext.h>
/*Define booleans...*/
#ifndef bool
#define bool int
#define true 1
#define false 0
#endif
#define INIT_CTX 0x1
/*Construct the name of the pointer to the original function*/
#define LDM_ORIG(f) __LDM ## f
/*Declare the pointer to the original function*/
#define LDM_ORIG_DECL(ret, f, args...) \
ret (* LDM_ORIG(f))(args)
/*Register a replacement version of function f*/
#define LDM_REG(f, args...) \
dlerror(); \
LDM_ORIG(f) = dlsym(RTLD_NEXT, #f); \
if(LDM_ORIG(f) == NULL){ \
fprintf(stderr,"Couldn't load function %s: %s\n",#f,dlerror()); \
}
/*Call the original version that was found by
* LDM_REG
*/
#define LDM_CALL_ORIGINAL(f, args...) \
LDM_ORIG(f)(args);
/*Print a message that is prepended with [LDM] -- for error monitoring, etc */
#define ldmmsg(...) fprintf(stderr,"[LDM] ");fprintf(__VA_ARGS__)
/*Get the current return address --
*faster than calling backtrace()*/
#define getReturnAddr(x) \
x = (void*)0xfeedf00d; \
ucontext_t ucontext; \
if( getcontext(&ucontext) != -1 ){ \
x = ((void**)ucontext.uc_mcontext.gregs[REG_RBP])[1]; \
}
/*Plugin constructor/destructor magic*/
#define LDM_PLUGIN_INIT _LDM_PLUGIN_INIT_
#define LDM_PLUGIN_THD_INIT _LDM_PLUGIN_THD_INIT_
#define LDM_STR(x) LDM_XSTR(x)
#define LDM_XSTR(x) #x
#define LDM_PLUGIN_DEINIT(x) atexit(x)
/*Thread destructor magic*/
#define LDM_THD_DTR_DECL(k,f) pthread_key_t k;
#define LDM_REGISTER_THD_DTR(k,f) pthread_key_create(&k,f)
#define LDM_INSTALL_THD_DTR(k) pthread_setspecific(k,(void*)0x1)
/*Get the value of a register as a void pointer*/
#define LDM_GET_REG(ctx,reg) (((ucontext_t*)ctx)->uc_mcontext.gregs[reg]);