-
Notifications
You must be signed in to change notification settings - Fork 199
/
debug_map.c
309 lines (249 loc) · 7.84 KB
/
debug_map.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2020 Intel Corporation
* Paweł Marczewski <pawel@invisiblethingslab.com>
*/
#include <asm/errno.h>
#include <asm/mman.h>
#include "api.h"
#include "debug_map.h"
#include "linux_utils.h"
#include "spinlock.h"
struct debug_map* _Atomic g_debug_map = NULL;
/* Lock for modifying g_debug_map on our end. Even though the list can be read by GDB at any time,
* we need to prevent concurrent modification. */
static spinlock_t g_debug_map_lock = INIT_SPINLOCK_UNLOCKED;
static struct debug_map* debug_map_new(const char* name, void* addr) {
struct debug_map* map;
if (!(map = malloc(sizeof(*map))))
return NULL;
if (!(map->name = strdup(name))) {
free(map);
return NULL;
}
map->addr = addr;
map->next = NULL;
return map;
}
/* This function is hooked by our gdb integration script and should be left as is. */
__attribute__((__noinline__)) void debug_map_update_debugger(void) {
__asm__ volatile(""); // Required in addition to __noinline__ to prevent deleting this function.
// See GCC docs.
}
int debug_map_add(const char* name, void* addr) {
spinlock_lock(&g_debug_map_lock);
struct debug_map* map = g_debug_map;
while (map) {
if (map->addr == addr) {
bool name_matches = !strcmp(name, map->name);
spinlock_unlock(&g_debug_map_lock);
/* If the exact same map is already there, skip adding it and report success: this can
* happen when we encounter two executable ranges for the same file. */
return name_matches ? 0 : -EEXIST;
}
map = map->next;
}
map = debug_map_new(name, addr);
if (!map) {
spinlock_unlock(&g_debug_map_lock);
return -ENOMEM;
}
map->next = g_debug_map;
g_debug_map = map;
spinlock_unlock(&g_debug_map_lock);
debug_map_update_debugger();
return 0;
}
int debug_map_remove(void* addr) {
spinlock_lock(&g_debug_map_lock);
struct debug_map* prev = NULL;
struct debug_map* map = g_debug_map;
while (map) {
if (map->addr == addr)
break;
prev = map;
map = map->next;
}
if (!map) {
spinlock_unlock(&g_debug_map_lock);
return -EINVAL;
}
if (prev) {
prev->next = map->next;
} else {
g_debug_map = map->next;
}
spinlock_unlock(&g_debug_map_lock);
debug_map_update_debugger();
free(map->name);
free(map);
return 0;
}
/* Find a range that (likely) corresponds to a mapped executable file, and add it to debug maps. */
static int debug_map_init_callback(struct proc_maps_range* r, void* arg) {
__UNUSED(arg);
/* not executable */
if (!(r->prot & PROT_EXEC))
return 0;
/* no name */
if (!r->name)
return 0;
/* [vvar] etc. */
if (r->name[0] != '/' && strcmp(r->name, "[vdso]"))
return 0;
/* /dev/sgx etc. */
if (strstartswith(r->name, "/dev/"))
return 0;
void* addr = (void*)(r->start - r->offset);
return debug_map_add(r->name, addr);
}
int debug_map_init_from_proc_maps(void) {
return parse_proc_maps("/proc/self/maps", debug_map_init_callback, /*arg=*/NULL);
}
/* Search for a debug map the address belongs to. We don't store map sizes, so this searches for the
* closest one. */
static int debug_map_find(uintptr_t addr, char** out_name, uintptr_t* out_offset) {
int ret;
spinlock_lock(&g_debug_map_lock);
const char* best_name = NULL;
uintptr_t best_addr = 0;
struct debug_map* map = g_debug_map;
while (map) {
if ((uintptr_t)map->addr <= addr && (uintptr_t)map->addr > best_addr) {
best_name = map->name;
best_addr = (uintptr_t)map->addr;
}
map = map->next;
}
if (!best_name) {
ret = -ENOENT;
goto out;
}
char* name = strdup(best_name);
if (!name) {
ret = -ENOMEM;
goto out;
}
*out_name = name;
*out_offset = addr - best_addr;
ret = 0;
out:
spinlock_unlock(&g_debug_map_lock);
return ret;
}
struct symbol_map_data {
uintptr_t offset;
char* buf;
size_t buf_size;
bool found;
};
/*
* Parse a single line of the symbol map. The symbol map is generated using the following command:
*
* nm --numeric-sort --defined-only --print-size --line-numbers <file>
*
* We're interested in lines with the following format (and we skip other lines):
*
* <start> <size> [tT] <symbol_name>\t<source_file>:<line_number>
*
* where the source information (`\t` and everything afterwards) is optional. Note that we don't
* actually use the line number: it describes only where the function starts, so it would be too
* confusing.
*/
static int symbol_map_callback(const char* line, void* arg, bool* out_stop) {
struct symbol_map_data* data = arg;
unsigned long val;
const char* next = line;
/* Start address */
if (str_to_ulong(next, 16, &val, &next) < 0)
return 0;
uintptr_t start = val;
if (*next != ' ')
return 0;
next++;
/* Size */
if (str_to_ulong(next, 16, &val, &next) < 0)
return 0;
size_t size = val;
if (*next != ' ')
return 0;
next++;
/* Skip if we're too early; stop iteration if we're too late */
if (start + size <= data->offset)
return 0;
if (data->offset < start) {
*out_stop = true;
return 0;
}
/* `t` or `T` (symbol in a text section) */
if (*next != 't' && *next != 'T')
return 0;
next++;
if (*next != ' ')
return 0;
next++;
/* Symbol name */
const char* symbol_name = next;
next = strchr(next, '\t');
if (next) {
size_t symbol_name_len = next - symbol_name;
next++;
/* File name */
const char* file_name = next;
while (*next != ':' && *next != '\0') {
/* Begin `file_name` after the last '/' encountered */
if (*next == '/')
file_name = next + 1;
next++;
}
size_t file_name_len = next - file_name;
snprintf(data->buf, data->buf_size, "%.*s at %.*s", (int)symbol_name_len, symbol_name,
(int)file_name_len, file_name);
} else {
/* There's no file name, the symbol name ends with null terminator */
snprintf(data->buf, data->buf_size, "%s", symbol_name);
}
data->found = true;
*out_stop = true;
return 0;
}
/* Example output: "func_name at source_file.c" */
static int find_in_symbol_map(const char* name, uintptr_t offset, char* buf, size_t buf_size) {
char* symbol_map_name = alloc_concat(name, /*a_len=*/-1, ".map", /*b_len=*/-1);
if (!symbol_map_name)
return -ENOMEM;
struct symbol_map_data data = {
.offset = offset,
.buf = buf,
.buf_size = buf_size,
.found = false,
};
int ret = read_text_file_iter_lines(symbol_map_name, &symbol_map_callback, &data);
free(symbol_map_name);
if (ret < 0)
return ret;
return data.found ? 0 : -ENOENT;
}
/* Example output: "func_name at source_file.c, libpal.so+0x456" */
int debug_describe_location(uintptr_t addr, char* buf, size_t buf_size) {
int ret;
char* name;
uintptr_t offset;
ret = debug_map_find(addr, &name, &offset);
if (ret < 0)
return ret;
const char* basename = name;
for (const char* s = name; *s != '\0'; s++) {
if (*s == '/')
basename = s + 1;
}
ret = find_in_symbol_map(name, offset, buf, buf_size);
if (ret < 0) {
/* parsing symbol map failed, display just name and offset */
snprintf(buf, buf_size, "%s+0x%lx", basename, offset);
} else {
size_t len = strlen(buf);
snprintf(&buf[len], buf_size - len, ", %s+0x%lx", basename, offset);
}
free(name);
return 0;
}