-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook_func.c
108 lines (87 loc) · 2.09 KB
/
hook_func.c
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
//-finstrument-functions
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PATH_MAX 256
static char path[PATH_MAX];
__attribute__((constructor no_instrument_function))
static void executable_path_init() {
char buf[PATH_MAX];
memset(buf, 0, sizeof(buf));
memset(path, 0, sizeof(path));
#ifdef _SOLARIS_TRACE
getcwd(buf, PATH_MAX);
sprintf(path, "%s/%s", buf, getexecname());
#elif _LINUX_TRACE
readlink("/proc/self/exe", path, PATH_MAX);
#else
#error "The OS has not been supported!"
#endif
}
__attribute__((no_instrument_function))
void __cyg_profile_func_enter(void *this_fn, void *call_site) {
char buf[PATH_MAX];
char cmd[PATH_MAX];
memset(buf, 0, sizeof(buf));
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "addr2line %p -e %s -f | head -1", this_fn, path);
FILE *ptr = NULL;
memset(buf, 0, sizeof(buf));
if ((ptr = popen(cmd, "r")) != NULL) {
fgets(buf, PATH_MAX, ptr);
printf("enter func => %p:%s", this_fn, buf);
}
(void) pclose(ptr);
}
__attribute__((no_instrument_function))
void __cyg_profile_func_exit(void *this_fn, void *call_site) {
char buf[PATH_MAX];
char cmd[PATH_MAX];
memset(buf, 0, sizeof(buf));
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "addr2line %p -e %s -f|head -1", this_fn, path);
FILE *ptr = NULL;
memset(buf, 0, sizeof(buf));
if ((ptr = popen(cmd, "r")) != NULL) {
fgets(buf, PATH_MAX, ptr);
printf("exit func <= %p:%s", this_fn, buf);
}
(void) pclose(ptr);
}
int func4()
{
printf("%s %d\n", __FUNCTION__, __LINE__);
return (0);
}
int func3()
{
printf("%s %d\n", __FUNCTION__, __LINE__);
func4();
printf("%s %d\n", __FUNCTION__, __LINE__);
return (0);
}
int func2()
{
printf("%s %d\n", __FUNCTION__, __LINE__);
func3();
printf("%s %d\n", __FUNCTION__, __LINE__);
func4();
printf("%s %d\n", __FUNCTION__, __LINE__);
return (0);
}
int func1()
{
int a;
int b;
printf("%s %d\n", __FUNCTION__, __LINE__);
func2();
printf("%s %d\n", __FUNCTION__, __LINE__);
return (0);
}
int main(int argc, char *argv[])
{
printf("%s %d\n", __FUNCTION__, __LINE__);
func1();
printf("%s %d\n", __FUNCTION__, __LINE__);
return (0);
}