-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer_util.h
98 lines (79 loc) · 1.94 KB
/
tracer_util.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
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
#ifndef __TRACER_UTIL_H__
#define __TRACER_UTIL_H__
#include <cxxabi.h>
#include <dlfcn.h>
#include <stdlib.h>
// IsConst_Destructor(demangled)
// demangled -> XXX::YYY
// demangled_method -> address of "YYY"
// demangled_split -> XXX\0:YYY
// split_caller_object -> XXX
// split_caller_method -> YYY
// dladdrを用いて関数アドレスから関数シンボルへ変換
const char *addr2name(void *address) {
Dl_info dli;
if (0 != dladdr(address, &dli)) {
return dli.dli_sname;
}
return 0;
}
const char *addr2fname(void *address) {
Dl_info dli;
if (0 != dladdr(address, &dli)) {
return dli.dli_fname;
}
return 0;
}
char *demangle(const char *mangled_name) {
char *p = (char *)malloc(32);
size_t sz;
int st;
abi::__cxa_demangle((const char *)mangled_name, p, &sz, &st);
if (st < 0 || !p) {
free(p);
return 0;
}
return p;
}
void demangle_free(const char *p) { free((void *)p); }
// demangled_method -> address of "YYY"
char *demangled_method(const char *demangled) {
char *p = (char *)demangled;
while (*p)
if (*p == ':')
return p + 2;
else
p++;
return 0;
}
// demangled_split -> XXX\0:YYY
char *demangled_split(char *demangled) {
char *p = demangled_method(demangled);
if (p)
*(p - 2) = '\0';
return p ? demangled : 0;
}
// split_caller_object -> XXX
char *split_caller_object(const char *split_name) { return (char *)split_name; }
// split_caller_method -> YYY
char *split_caller_method(const char *split_name) {
char *p = (char *)split_name;
while (*p++)
;
return (*p == ':') ? p + 1 : 0;
}
bool IsConst_Destructor(const char *demangled) {
char *p = demangled_method(demangled);
if (*p == '\0')
return false;
const char *cl = demangled;
const char *mt = p;
while (*cl && *mt && *cl != ':' && *mt != '\0' && *mt != '(') {
if (*cl != *mt)
return false;
cl++;
mt++;
}
return true;
}
#endif /* end of include guard: __TRACER_UTIL_H__ */