-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbacktrace.c
163 lines (117 loc) · 3.73 KB
/
backtrace.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
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
#include "all.h"
static library_info_t runtime_libs[100];
static int runtime_lib_count;
static library_info_t *_get_runtime_lib(const struct dl_phdr_info *info)
{
const char *path = info->dlpi_name;
if (path == NULL || path[0] == '\0') {
//pr_warn("%s: path null/empty, giving up\n", __func__);
return NULL;
}
//pr_debug("%s: '%s'\n", __func__, path);
for (int i = 0; i < runtime_lib_count; ++i) {
if (strcmp(path, runtime_libs[i].path) == 0) {
//pr_debug(" already exists\n");
return runtime_libs + i;
}
}
//pr_debug(" getting new one\n");
library_info_t *lib = runtime_libs + runtime_lib_count;
char *path_dir = strdup(path);
char *path_base = strdup(path);
path_dir = dirname(path_dir);
path_base = basename(path_base);
lib->path = path;
lib->name = path_base;
if ((lib->fd = open(lib->path, O_RDONLY)) == -1) {
//warn("open('%s') failed", lib->name);
return NULL;
}
if (fstat(lib->fd, &lib->stat) != 0) {
//warn("fstat('%s') failed", lib->name);
return NULL;
}
lib->size = lib->stat.st_size;
if ((lib->map = mmap(NULL, lib->size, PROT_READ, MAP_PRIVATE, lib->fd, 0))
== MAP_FAILED) {
//warn("mmap('%s') failed", lib->name);
return NULL;
}
lib->baseaddr = info->dlpi_addr;
symtab_init(lib);
++runtime_lib_count;
assert(runtime_lib_count < (sizeof(runtime_libs) / sizeof(*runtime_libs)));
return lib;
}
static uintptr_t find_addr;
static struct dl_phdr_info find_match;
static int _dl_iterate_callback(struct dl_phdr_info *info, size_t size,
void *data)
{
/* find the library that's most likely to own this address */
if (info->dlpi_addr <= find_addr &&
info->dlpi_addr > find_match.dlpi_addr) {
find_match = *info;
}
return 0;
}
static bool _get_runtime_sym(symbol_t *sym, uintptr_t addr)
{
find_addr = addr;
memset(&find_match, 0, sizeof(find_match));
dl_iterate_phdr(_dl_iterate_callback, NULL);
library_info_t *lib = _get_runtime_lib(&find_match);
if (lib != NULL) {
return symtab_lookup_addr_range(lib, sym, STT_FUNC,
addr - lib->baseaddr);
}
return false;
}
void print_backtrace(const char *from)
{
uintptr_t entries[100];
memset(entries, 0, sizeof(entries));
int num_entries = backtrace((void **)entries,
sizeof(entries) / sizeof(*entries));
pr_warn("BACKTRACE in %s:\n", from);
/* start at 1 so we skip this function's frame */
for (int i = 1; i < num_entries; ++i) {
pr_info(" #%-2d ", i);
pr_debug("%08x ", entries[i]);
symbol_t sym;
if (symtab_func_addr_range_abs(&sym, entries[i]) ||
_get_runtime_sym(&sym, entries[i])) {
uintptr_t func_base = sym.lib->baseaddr + sym.addr;
pr_debug("%-20s %s + 0x%x\n",
sym.lib->name,
try_demangle(sym.name),
entries[i] - func_base);
} else {
pr_debug("???\n");
}
}
}
void print_backtrace_cl_con(const char *from, const char *prefix, int skip)
{
uintptr_t entries[100];
memset(entries, 0, sizeof(entries));
int num_entries = backtrace((void **)entries,
sizeof(entries) / sizeof(*entries));
cl_con_printf("%sBACKTRACE in %s:\n", prefix, from);
/* start at 1 so we skip this function's frame */
for (int i = 1 + skip; i < num_entries; ++i) {
char buf[128];
snprintf(buf, sizeof(buf), " #%-2d %08x ", i, entries[i]);
symbol_t sym;
if (symtab_func_addr_range_abs(&sym, entries[i]) ||
_get_runtime_sym(&sym, entries[i])) {
uintptr_t func_base = sym.lib->baseaddr + sym.addr;
cl_con_printf("%s%s%-20s %s + 0x%x\n", prefix, buf,
sym.lib->name,
try_demangle(sym.name),
entries[i] - func_base);
} else {
cl_con_printf("%s%s???\n", prefix, buf);
}
}
}