Commit | Line | Data |
---|---|---|
c40a57e5 | 1 | /* |
d5ddf820 | 2 | * bin-info.c |
c40a57e5 AB |
3 | * |
4 | * Babeltrace - Executable and Shared Object Debug Info Reader | |
5 | * | |
6 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
7 | * | |
8 | * Author: Antoine Busque <abusque@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
b4565e8b PP |
29 | #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT-BIN-INFO" |
30 | #include "logging.h" | |
31 | ||
1e638f98 FD |
32 | #include <dwarf.h> |
33 | #include <errno.h> | |
c40a57e5 | 34 | #include <fcntl.h> |
1e638f98 | 35 | #include <inttypes.h> |
c40a57e5 | 36 | #include <libgen.h> |
1e638f98 | 37 | #include <math.h> |
c40a57e5 | 38 | #include <stdio.h> |
c40a57e5 AB |
39 | #include <stdlib.h> |
40 | #include <string.h> | |
41 | #include <unistd.h> | |
1e638f98 | 42 | |
c40a57e5 | 43 | #include <glib.h> |
1e638f98 | 44 | |
af54bd18 FD |
45 | #include <babeltrace/common-internal.h> |
46 | ||
4f45f9bb JD |
47 | #include "bin-info.h" |
48 | #include "crc32.h" | |
1e638f98 | 49 | #include "dwarf.h" |
4f45f9bb | 50 | #include "utils.h" |
c40a57e5 AB |
51 | |
52 | /* | |
53 | * An address printed in hex is at most 20 bytes (16 for 64-bits + | |
54 | * leading 0x + optional leading '+' if addr is an offset + null | |
55 | * character). | |
56 | */ | |
57 | #define ADDR_STR_LEN 20 | |
ca9f27f3 | 58 | #define BUILD_ID_NOTE_NAME "GNU" |
c40a57e5 AB |
59 | |
60 | BT_HIDDEN | |
d5ddf820 | 61 | int bin_info_init(void) |
c40a57e5 AB |
62 | { |
63 | int ret = 0; | |
64 | ||
65 | if (elf_version(EV_CURRENT) == EV_NONE) { | |
b4565e8b PP |
66 | BT_LOGD("ELF library initialization failed: %s.", |
67 | elf_errmsg(-1)); | |
c40a57e5 AB |
68 | ret = -1; |
69 | } | |
70 | ||
71 | return ret; | |
72 | } | |
73 | ||
74 | BT_HIDDEN | |
1e638f98 FD |
75 | struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path, |
76 | uint64_t low_addr, uint64_t memsz, bool is_pic, | |
77 | const char *debug_info_dir, const char *target_prefix) | |
c40a57e5 | 78 | { |
d5ddf820 | 79 | struct bin_info *bin = NULL; |
c40a57e5 | 80 | |
1e638f98 FD |
81 | BT_ASSERT(fdc); |
82 | ||
c40a57e5 AB |
83 | if (!path) { |
84 | goto error; | |
85 | } | |
86 | ||
d5ddf820 AB |
87 | bin = g_new0(struct bin_info, 1); |
88 | if (!bin) { | |
c40a57e5 AB |
89 | goto error; |
90 | } | |
91 | ||
9d325e17 | 92 | if (target_prefix) { |
0cf9d412 | 93 | bin->elf_path = g_build_filename(target_prefix, path, NULL); |
5cde0dc1 | 94 | } else { |
ca9f27f3 | 95 | bin->elf_path = g_strdup(path); |
5cde0dc1 AB |
96 | } |
97 | ||
d5ddf820 | 98 | if (!bin->elf_path) { |
c40a57e5 AB |
99 | goto error; |
100 | } | |
101 | ||
9d325e17 | 102 | if (debug_info_dir) { |
06d1cf5d | 103 | bin->debug_info_dir = g_strdup(debug_info_dir); |
9d325e17 PP |
104 | if (!bin->debug_info_dir) { |
105 | goto error; | |
106 | } | |
107 | } | |
108 | ||
d5ddf820 AB |
109 | bin->is_pic = is_pic; |
110 | bin->memsz = memsz; | |
111 | bin->low_addr = low_addr; | |
112 | bin->high_addr = bin->low_addr + bin->memsz; | |
ca9f27f3 FD |
113 | bin->build_id = NULL; |
114 | bin->build_id_len = 0; | |
115 | bin->file_build_id_matches = false; | |
1e638f98 | 116 | bin->fd_cache = fdc; |
c40a57e5 | 117 | |
d5ddf820 | 118 | return bin; |
c40a57e5 AB |
119 | |
120 | error: | |
d5ddf820 | 121 | bin_info_destroy(bin); |
c40a57e5 AB |
122 | return NULL; |
123 | } | |
124 | ||
125 | BT_HIDDEN | |
d5ddf820 | 126 | void bin_info_destroy(struct bin_info *bin) |
c40a57e5 | 127 | { |
d5ddf820 | 128 | if (!bin) { |
c40a57e5 AB |
129 | return; |
130 | } | |
131 | ||
d5ddf820 | 132 | dwarf_end(bin->dwarf_info); |
c40a57e5 | 133 | |
06d1cf5d FD |
134 | g_free(bin->debug_info_dir); |
135 | g_free(bin->elf_path); | |
136 | g_free(bin->dwarf_path); | |
4f45f9bb | 137 | g_free(bin->build_id); |
06d1cf5d | 138 | g_free(bin->dbg_link_filename); |
c40a57e5 | 139 | |
d5ddf820 | 140 | elf_end(bin->elf_file); |
c40a57e5 | 141 | |
1e638f98 FD |
142 | bt_fd_cache_put_handle(bin->fd_cache, bin->elf_handle); |
143 | bt_fd_cache_put_handle(bin->fd_cache, bin->dwarf_handle); | |
c40a57e5 | 144 | |
d5ddf820 | 145 | g_free(bin); |
c40a57e5 AB |
146 | } |
147 | ||
af54bd18 FD |
148 | static |
149 | int bin_info_set_endianness(struct bin_info *bin) | |
150 | { | |
151 | int ret, fd; | |
152 | uint8_t e_ident[EI_NIDENT]; | |
153 | ||
154 | fd = bt_fd_cache_handle_get_fd(bin->elf_handle); | |
155 | ||
156 | /* | |
157 | * Read the identification fields of the elf file. | |
158 | */ | |
159 | if (lseek(fd, 0, SEEK_SET) < 0) { | |
160 | BT_LOGE("Error seeking the beginning of ELF file: %s", | |
161 | strerror(errno)); | |
162 | ret = -1; | |
163 | goto error; | |
164 | } | |
165 | ||
166 | ret = bt_common_read(fd, e_ident, EI_NIDENT); | |
167 | if (ret < EI_NIDENT) { | |
168 | BT_LOGE_STR("Error reading the ELF identification fields"); | |
169 | ret = -1; | |
170 | goto error; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Set the endianness. | |
175 | */ | |
176 | bin->endianness = e_ident[EI_DATA]; | |
177 | ret = 0; | |
178 | ||
179 | error: | |
180 | return ret; | |
181 | } | |
182 | ||
ca9f27f3 FD |
183 | /** |
184 | * Initialize the ELF file for a given executable. | |
185 | * | |
186 | * @param bin bin_info instance | |
187 | * @returns 0 on success, negative value on error. | |
188 | */ | |
189 | static | |
190 | int bin_info_set_elf_file(struct bin_info *bin) | |
191 | { | |
1e638f98 | 192 | struct bt_fd_cache_handle *elf_handle = NULL; |
ca9f27f3 | 193 | Elf *elf_file = NULL; |
af54bd18 | 194 | int ret; |
ca9f27f3 FD |
195 | |
196 | if (!bin) { | |
197 | goto error; | |
198 | } | |
199 | ||
1e638f98 FD |
200 | elf_handle = bt_fd_cache_get_handle(bin->fd_cache, bin->elf_path); |
201 | if (!elf_handle) { | |
202 | BT_LOGD("Failed to open %s", bin->elf_path); | |
ca9f27f3 FD |
203 | goto error; |
204 | } | |
af54bd18 FD |
205 | bin->elf_handle = elf_handle; |
206 | ||
207 | ret = bin_info_set_endianness(bin); | |
208 | if (ret) { | |
209 | goto error; | |
210 | } | |
ca9f27f3 | 211 | |
af54bd18 | 212 | elf_file = elf_begin(bt_fd_cache_handle_get_fd(bin->elf_handle), |
1e638f98 | 213 | ELF_C_READ, NULL); |
ca9f27f3 | 214 | if (!elf_file) { |
1e638f98 | 215 | BT_LOGE("elf_begin failed: %s", elf_errmsg(-1)); |
ca9f27f3 FD |
216 | goto error; |
217 | } | |
218 | ||
af54bd18 FD |
219 | bin->elf_file = elf_file; |
220 | ||
ca9f27f3 | 221 | if (elf_kind(elf_file) != ELF_K_ELF) { |
1e638f98 | 222 | BT_LOGE("Error: %s is not an ELF object", bin->elf_path); |
ca9f27f3 FD |
223 | goto error; |
224 | } | |
225 | ||
ca9f27f3 FD |
226 | return 0; |
227 | ||
228 | error: | |
1e638f98 | 229 | bt_fd_cache_put_handle(bin->fd_cache, elf_handle); |
ca9f27f3 | 230 | elf_end(elf_file); |
1e638f98 | 231 | return -1; |
ca9f27f3 FD |
232 | } |
233 | ||
234 | /** | |
235 | * From a note section data buffer, check if it is a build id note. | |
236 | * | |
237 | * @param buf Pointer to a note section | |
238 | * | |
239 | * @returns 1 on match, 0 if `buf` does not contain a | |
240 | * valid build id note | |
241 | */ | |
242 | static | |
243 | int is_build_id_note_section(uint8_t *buf) | |
244 | { | |
245 | int ret = 0; | |
246 | uint32_t name_sz, desc_sz, note_type; | |
247 | ||
248 | /* The note section header has 3 32bit integer for the following: | |
249 | * - Section name size | |
250 | * - Description size | |
251 | * - Note type | |
252 | */ | |
253 | name_sz = (uint32_t) *buf; | |
233b228c FD |
254 | |
255 | /* | |
256 | * Check the note name length. The name_sz field includes the | |
257 | * terminating null byte. | |
258 | */ | |
259 | if (name_sz != sizeof(BUILD_ID_NOTE_NAME)) { | |
260 | goto invalid; | |
261 | } | |
262 | ||
ca9f27f3 FD |
263 | buf += sizeof(name_sz); |
264 | ||
233b228c | 265 | /* Ignore the note description size. */ |
ca9f27f3 FD |
266 | buf += sizeof(desc_sz); |
267 | ||
268 | note_type = (uint32_t) *buf; | |
269 | buf += sizeof(note_type); | |
270 | ||
271 | /* Check the note type. */ | |
272 | if (note_type != NT_GNU_BUILD_ID) { | |
273 | goto invalid; | |
274 | } | |
275 | ||
276 | /* Check the note name. */ | |
277 | if (memcmp(buf, BUILD_ID_NOTE_NAME, name_sz) != 0) { | |
278 | goto invalid; | |
279 | } | |
280 | ||
281 | ret = 1; | |
282 | ||
283 | invalid: | |
284 | return ret; | |
285 | } | |
286 | ||
287 | /** | |
288 | * From a build id note section data buffer, check if the build id it contains | |
289 | * is identical to the build id passed as parameter. | |
290 | * | |
291 | * @param file_build_id_note Pointer to the file build id note section. | |
292 | * @param build_id Pointer to a build id to compare to. | |
293 | * @param build_id_len length of the build id. | |
294 | * | |
295 | * @returns 1 on match, 0 otherwise. | |
296 | */ | |
297 | static | |
298 | int is_build_id_note_section_matching(uint8_t *file_build_id_note, | |
299 | uint8_t *build_id, size_t build_id_len) | |
300 | { | |
301 | uint32_t name_sz, desc_sz, note_type; | |
302 | ||
303 | if (build_id_len <= 0) { | |
304 | goto end; | |
305 | } | |
306 | ||
307 | /* The note section header has 3 32bit integer for the following: | |
308 | * - Section name size | |
309 | * - Description size | |
310 | * - Note type | |
311 | */ | |
312 | name_sz = (uint32_t) *file_build_id_note; | |
313 | file_build_id_note += sizeof(name_sz); | |
314 | file_build_id_note += sizeof(desc_sz); | |
315 | file_build_id_note += sizeof(note_type); | |
316 | ||
317 | /* | |
318 | * Move the pointer pass the name char array. This corresponds to the | |
319 | * beginning of the description section. The description is the build | |
320 | * id in the case of a build id note. | |
321 | */ | |
322 | file_build_id_note += name_sz; | |
323 | ||
324 | /* | |
325 | * Compare the binary build id with the supplied build id. | |
326 | */ | |
327 | if (memcmp(build_id, file_build_id_note, build_id_len) == 0) { | |
328 | return 1; | |
329 | } | |
330 | end: | |
331 | return 0; | |
332 | } | |
333 | ||
334 | /** | |
335 | * Checks if the build id stored in `bin` (bin->build_id) is matching the build | |
336 | * id of the ondisk file (bin->elf_file). | |
337 | * | |
338 | * @param bin bin_info instance | |
339 | * @param build_id build id to compare ot the on disk file | |
340 | * @param build_id_len length of the build id | |
341 | * | |
342 | * @returns 1 on if the build id of stored in `bin` matches | |
343 | * the build id of the ondisk file. | |
344 | * 0 on if they are different or an error occured. | |
345 | */ | |
346 | static | |
347 | int is_build_id_matching(struct bin_info *bin) | |
348 | { | |
349 | int ret, is_build_id, is_matching = 0; | |
350 | Elf_Scn *curr_section = NULL, *next_section = NULL; | |
bfe42dac | 351 | GElf_Shdr curr_section_hdr; |
ca9f27f3 FD |
352 | |
353 | if (!bin->build_id) { | |
354 | goto error; | |
355 | } | |
356 | ||
357 | /* Set ELF file if it hasn't been accessed yet. */ | |
358 | if (!bin->elf_file) { | |
359 | ret = bin_info_set_elf_file(bin); | |
360 | if (ret) { | |
361 | /* Failed to set ELF file. */ | |
362 | goto error; | |
363 | } | |
364 | } | |
365 | ||
ca9f27f3 FD |
366 | next_section = elf_nextscn(bin->elf_file, curr_section); |
367 | if (!next_section) { | |
368 | goto error; | |
369 | } | |
370 | ||
371 | while (next_section) { | |
af54bd18 FD |
372 | Elf_Data *file_note_data = NULL; |
373 | Elf_Data native_note_data; | |
374 | ||
ca9f27f3 FD |
375 | curr_section = next_section; |
376 | next_section = elf_nextscn(bin->elf_file, curr_section); | |
377 | ||
bfe42dac | 378 | if (!gelf_getshdr(curr_section, &curr_section_hdr)) { |
ca9f27f3 FD |
379 | goto error; |
380 | } | |
381 | ||
bfe42dac | 382 | if (curr_section_hdr.sh_type != SHT_NOTE) { |
ca9f27f3 FD |
383 | continue; |
384 | } | |
385 | ||
af54bd18 FD |
386 | file_note_data = elf_getdata(curr_section, NULL); |
387 | if (!file_note_data) { | |
ca9f27f3 FD |
388 | goto error; |
389 | } | |
390 | ||
af54bd18 FD |
391 | /* |
392 | * Prepare the destination buffer to receive the natively | |
393 | * ordered note. The `d_buf`, `d_size`, and `d_version` fields | |
394 | * of the destination structure must be set before invoking the | |
395 | * `gelf_xlatetom()` function. | |
396 | */ | |
397 | native_note_data.d_buf = g_new0(uint8_t, file_note_data->d_size); | |
398 | BT_ASSERT(native_note_data.d_buf); | |
399 | ||
400 | native_note_data.d_size = file_note_data->d_size; | |
401 | native_note_data.d_version = file_note_data->d_version; | |
402 | ||
403 | /* Translate the note data buffer to the host endianness. */ | |
404 | gelf_xlatetom(bin->elf_file, &native_note_data, file_note_data, | |
405 | bin->endianness); | |
406 | ||
ca9f27f3 | 407 | /* Check if the note is of the build-id type. */ |
af54bd18 | 408 | is_build_id = is_build_id_note_section(native_note_data.d_buf); |
ca9f27f3 | 409 | if (!is_build_id) { |
af54bd18 | 410 | g_free(native_note_data.d_buf); |
ca9f27f3 FD |
411 | continue; |
412 | } | |
413 | ||
414 | /* | |
415 | * Compare the build id of the on-disk file and | |
416 | * the build id recorded in the trace. | |
417 | */ | |
af54bd18 FD |
418 | is_matching = is_build_id_note_section_matching( |
419 | native_note_data.d_buf, bin->build_id, | |
420 | bin->build_id_len); | |
421 | g_free(native_note_data.d_buf); | |
ca9f27f3 FD |
422 | if (!is_matching) { |
423 | break; | |
424 | } | |
425 | } | |
426 | error: | |
ca9f27f3 FD |
427 | return is_matching; |
428 | } | |
429 | ||
c40a57e5 | 430 | BT_HIDDEN |
d5ddf820 | 431 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
c40a57e5 AB |
432 | size_t build_id_len) |
433 | { | |
d5ddf820 | 434 | if (!bin || !build_id) { |
c40a57e5 AB |
435 | goto error; |
436 | } | |
437 | ||
ca9f27f3 FD |
438 | /* Set the build id. */ |
439 | bin->build_id = g_new0(uint8_t, build_id_len); | |
d5ddf820 | 440 | if (!bin->build_id) { |
c40a57e5 AB |
441 | goto error; |
442 | } | |
443 | ||
d5ddf820 AB |
444 | memcpy(bin->build_id, build_id, build_id_len); |
445 | bin->build_id_len = build_id_len; | |
c40a57e5 | 446 | |
ca9f27f3 FD |
447 | /* |
448 | * Check if the file found on the file system has the same build id | |
449 | * that what was recorded in the trace. | |
450 | */ | |
451 | bin->file_build_id_matches = is_build_id_matching(bin); | |
452 | if (!bin->file_build_id_matches) { | |
453 | BT_LOGD_STR("Supplied Build ID does not match Build ID of the " | |
454 | "binary or library found on the file system."); | |
455 | goto error; | |
456 | } | |
457 | ||
c40a57e5 AB |
458 | /* |
459 | * Reset the is_elf_only flag in case it had been set | |
460 | * previously, because we might find separate debug info using | |
461 | * the new build id information. | |
462 | */ | |
d5ddf820 | 463 | bin->is_elf_only = false; |
c40a57e5 AB |
464 | |
465 | return 0; | |
466 | ||
467 | error: | |
c40a57e5 AB |
468 | return -1; |
469 | } | |
470 | ||
471 | BT_HIDDEN | |
4f45f9bb JD |
472 | int bin_info_set_debug_link(struct bin_info *bin, const char *filename, |
473 | uint32_t crc) | |
c40a57e5 | 474 | { |
d5ddf820 | 475 | if (!bin || !filename) { |
c40a57e5 AB |
476 | goto error; |
477 | } | |
478 | ||
06d1cf5d | 479 | bin->dbg_link_filename = g_strdup(filename); |
d5ddf820 | 480 | if (!bin->dbg_link_filename) { |
c40a57e5 AB |
481 | goto error; |
482 | } | |
483 | ||
d5ddf820 | 484 | bin->dbg_link_crc = crc; |
c40a57e5 AB |
485 | |
486 | /* | |
487 | * Reset the is_elf_only flag in case it had been set | |
488 | * previously, because we might find separate debug info using | |
489 | * the new build id information. | |
490 | */ | |
d5ddf820 | 491 | bin->is_elf_only = false; |
c40a57e5 AB |
492 | |
493 | return 0; | |
494 | ||
495 | error: | |
496 | ||
497 | return -1; | |
498 | } | |
499 | ||
500 | /** | |
501 | * Tries to read DWARF info from the location given by path, and | |
d5ddf820 | 502 | * attach it to the given bin_info instance if it exists. |
c40a57e5 | 503 | * |
d5ddf820 | 504 | * @param bin bin_info instance for which to set DWARF info |
c40a57e5 | 505 | * @param path Presumed location of the DWARF info |
545e1e92 | 506 | * @returns 0 on success, negative value on failure |
c40a57e5 AB |
507 | */ |
508 | static | |
d5ddf820 | 509 | int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path) |
c40a57e5 | 510 | { |
1e638f98 FD |
511 | int ret = 0; |
512 | struct bt_fd_cache_handle *dwarf_handle = NULL; | |
c40a57e5 AB |
513 | struct bt_dwarf_cu *cu = NULL; |
514 | Dwarf *dwarf_info = NULL; | |
515 | ||
d5ddf820 | 516 | if (!bin || !path) { |
c40a57e5 AB |
517 | goto error; |
518 | } | |
519 | ||
1e638f98 FD |
520 | dwarf_handle = bt_fd_cache_get_handle(bin->fd_cache, path); |
521 | if (!dwarf_handle) { | |
c40a57e5 AB |
522 | goto error; |
523 | } | |
524 | ||
1e638f98 FD |
525 | dwarf_info = dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle), |
526 | DWARF_C_READ); | |
c40a57e5 AB |
527 | if (!dwarf_info) { |
528 | goto error; | |
529 | } | |
530 | ||
531 | /* | |
d5ddf820 AB |
532 | * Check if the dwarf info has any CU. If not, the |
533 | * executable's object file contains no DWARF info. | |
c40a57e5 AB |
534 | */ |
535 | cu = bt_dwarf_cu_create(dwarf_info); | |
536 | if (!cu) { | |
537 | goto error; | |
538 | } | |
539 | ||
540 | ret = bt_dwarf_cu_next(cu); | |
541 | if (ret) { | |
542 | goto error; | |
543 | } | |
544 | ||
1e638f98 | 545 | bin->dwarf_handle = dwarf_handle; |
06d1cf5d | 546 | bin->dwarf_path = g_strdup(path); |
d5ddf820 | 547 | if (!bin->dwarf_path) { |
c40a57e5 AB |
548 | goto error; |
549 | } | |
d5ddf820 | 550 | bin->dwarf_info = dwarf_info; |
c40a57e5 AB |
551 | free(cu); |
552 | ||
553 | return 0; | |
554 | ||
555 | error: | |
1e638f98 | 556 | bt_fd_cache_put_handle(bin->fd_cache, dwarf_handle); |
c40a57e5 AB |
557 | dwarf_end(dwarf_info); |
558 | g_free(dwarf_info); | |
559 | free(cu); | |
560 | ||
1e638f98 | 561 | return -1; |
c40a57e5 AB |
562 | } |
563 | ||
564 | /** | |
d5ddf820 | 565 | * Try to set the dwarf_info for a given bin_info instance via the |
c40a57e5 AB |
566 | * build ID method. |
567 | * | |
d5ddf820 | 568 | * @param bin bin_info instance for which to retrieve the |
c40a57e5 AB |
569 | * DWARF info via build ID |
570 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
571 | */ | |
572 | static | |
d5ddf820 | 573 | int bin_info_set_dwarf_info_build_id(struct bin_info *bin) |
c40a57e5 | 574 | { |
f06ce5e5 | 575 | int i = 0, ret = 0; |
c40a57e5 AB |
576 | char *path = NULL, *build_id_file = NULL; |
577 | const char *dbg_dir = NULL; | |
8486e87a | 578 | size_t build_id_char_len, build_id_suffix_char_len, build_id_file_len; |
c40a57e5 | 579 | |
d5ddf820 | 580 | if (!bin || !bin->build_id) { |
c40a57e5 AB |
581 | goto error; |
582 | } | |
583 | ||
06d1cf5d | 584 | dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR; |
c40a57e5 | 585 | |
b3599eb3 | 586 | /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */ |
8486e87a FD |
587 | build_id_char_len = (2 * bin->build_id_len) + 1; |
588 | build_id_suffix_char_len = strlen(BUILD_ID_SUFFIX) + 1; | |
589 | build_id_file_len = build_id_char_len + build_id_suffix_char_len; | |
06d1cf5d | 590 | build_id_file = g_new0(gchar, build_id_file_len); |
c40a57e5 AB |
591 | if (!build_id_file) { |
592 | goto error; | |
593 | } | |
594 | ||
06d1cf5d | 595 | g_snprintf(build_id_file, 4, "%02x/", bin->build_id[0]); |
d5ddf820 | 596 | for (i = 1; i < bin->build_id_len; ++i) { |
c40a57e5 AB |
597 | int path_idx = 3 + 2 * (i - 1); |
598 | ||
06d1cf5d | 599 | g_snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]); |
c40a57e5 | 600 | } |
8486e87a FD |
601 | g_snprintf(&build_id_file[build_id_char_len], build_id_suffix_char_len, |
602 | BUILD_ID_SUFFIX); | |
c40a57e5 | 603 | |
0cf9d412 | 604 | path = g_build_filename(dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL); |
c40a57e5 AB |
605 | if (!path) { |
606 | goto error; | |
607 | } | |
608 | ||
d5ddf820 | 609 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
c40a57e5 AB |
610 | if (ret) { |
611 | goto error; | |
612 | } | |
613 | ||
614 | goto end; | |
615 | ||
616 | error: | |
617 | ret = -1; | |
618 | end: | |
619 | free(build_id_file); | |
620 | free(path); | |
621 | ||
622 | return ret; | |
623 | } | |
624 | ||
625 | /** | |
626 | * Tests whether the file located at path exists and has the expected | |
627 | * checksum. | |
628 | * | |
629 | * This predicate is used when looking up separate debug info via the | |
630 | * GNU debuglink method. The expected crc can be found .gnu_debuglink | |
631 | * section in the original ELF file, along with the filename for the | |
632 | * file containing the debug info. | |
633 | * | |
634 | * @param path Full path at which to look for the debug file | |
635 | * @param crc Expected checksum for the debug file | |
636 | * @returns 1 if the file exists and has the correct checksum, | |
637 | * 0 otherwise | |
638 | */ | |
639 | static | |
1e638f98 | 640 | int is_valid_debug_file(struct bin_info *bin, char *path, uint32_t crc) |
c40a57e5 | 641 | { |
1e638f98 FD |
642 | int ret = 0; |
643 | struct bt_fd_cache_handle *debug_handle = NULL; | |
c40a57e5 AB |
644 | uint32_t _crc = 0; |
645 | ||
646 | if (!path) { | |
1e638f98 | 647 | goto end; |
c40a57e5 AB |
648 | } |
649 | ||
1e638f98 FD |
650 | debug_handle = bt_fd_cache_get_handle(bin->fd_cache, path); |
651 | if (!debug_handle) { | |
652 | goto end; | |
c40a57e5 AB |
653 | } |
654 | ||
1e638f98 | 655 | ret = crc32(bt_fd_cache_handle_get_fd(debug_handle), &_crc); |
c40a57e5 AB |
656 | if (ret) { |
657 | ret = 0; | |
658 | goto end; | |
659 | } | |
660 | ||
661 | ret = (crc == _crc); | |
662 | ||
663 | end: | |
1e638f98 | 664 | bt_fd_cache_put_handle(bin->fd_cache, debug_handle); |
c40a57e5 AB |
665 | return ret; |
666 | } | |
667 | ||
668 | /** | |
d5ddf820 | 669 | * Try to set the dwarf_info for a given bin_info instance via the |
ca9f27f3 | 670 | * debug-link method. |
c40a57e5 | 671 | * |
d5ddf820 | 672 | * @param bin bin_info instance for which to retrieve the |
c40a57e5 AB |
673 | * DWARF info via debug link |
674 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
675 | */ | |
676 | static | |
d5ddf820 | 677 | int bin_info_set_dwarf_info_debug_link(struct bin_info *bin) |
c40a57e5 AB |
678 | { |
679 | int ret = 0; | |
06d1cf5d FD |
680 | const gchar *dbg_dir = NULL; |
681 | gchar *bin_dir = NULL, *dir_name = NULL, *path = NULL; | |
c40a57e5 | 682 | |
d5ddf820 | 683 | if (!bin || !bin->dbg_link_filename) { |
c40a57e5 AB |
684 | goto error; |
685 | } | |
686 | ||
06d1cf5d | 687 | dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR; |
85cf9985 | 688 | dir_name = g_path_get_dirname(bin->elf_path); |
c40a57e5 AB |
689 | if (!dir_name) { |
690 | goto error; | |
691 | } | |
692 | ||
06d1cf5d | 693 | bin_dir = g_strconcat(dir_name, "/", NULL); |
c40a57e5 | 694 | |
d5ddf820 | 695 | /* First look in the executable's dir */ |
06d1cf5d | 696 | path = g_strconcat(bin_dir, bin->dbg_link_filename, NULL); |
c40a57e5 | 697 | |
1e638f98 | 698 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
699 | goto found; |
700 | } | |
701 | ||
702 | /* If not found, look in .debug subdir */ | |
06d1cf5d FD |
703 | g_free(path); |
704 | path = g_strconcat(bin_dir, DEBUG_SUBDIR, bin->dbg_link_filename, NULL); | |
c40a57e5 | 705 | |
1e638f98 | 706 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
707 | goto found; |
708 | } | |
709 | ||
710 | /* Lastly, look under the global debug directory */ | |
06d1cf5d | 711 | g_free(path); |
c40a57e5 | 712 | |
06d1cf5d | 713 | path = g_strconcat(dbg_dir, bin_dir, bin->dbg_link_filename, NULL); |
1e638f98 | 714 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
715 | goto found; |
716 | } | |
717 | ||
718 | error: | |
719 | ret = -1; | |
720 | end: | |
39588a20 | 721 | g_free(bin_dir); |
85cf9985 FD |
722 | g_free(dir_name); |
723 | g_free(path); | |
c40a57e5 AB |
724 | |
725 | return ret; | |
726 | ||
727 | found: | |
d5ddf820 | 728 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
c40a57e5 AB |
729 | if (ret) { |
730 | goto error; | |
731 | } | |
732 | ||
733 | goto end; | |
734 | } | |
735 | ||
736 | /** | |
737 | * Initialize the DWARF info for a given executable. | |
738 | * | |
d5ddf820 | 739 | * @param bin bin_info instance |
545e1e92 | 740 | * @returns 0 on success, negative value on failure |
c40a57e5 AB |
741 | */ |
742 | static | |
d5ddf820 | 743 | int bin_info_set_dwarf_info(struct bin_info *bin) |
c40a57e5 AB |
744 | { |
745 | int ret = 0; | |
746 | ||
d5ddf820 | 747 | if (!bin) { |
545e1e92 JG |
748 | ret = -1; |
749 | goto end; | |
c40a57e5 AB |
750 | } |
751 | ||
752 | /* First try to set the DWARF info from the ELF file */ | |
d5ddf820 | 753 | ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path); |
c40a57e5 AB |
754 | if (!ret) { |
755 | goto end; | |
756 | } | |
757 | ||
758 | /* | |
759 | * If that fails, try to find separate debug info via build ID | |
760 | * and debug link. | |
761 | */ | |
d5ddf820 | 762 | ret = bin_info_set_dwarf_info_build_id(bin); |
c40a57e5 AB |
763 | if (!ret) { |
764 | goto end; | |
765 | } | |
766 | ||
d5ddf820 | 767 | ret = bin_info_set_dwarf_info_debug_link(bin); |
c40a57e5 AB |
768 | if (!ret) { |
769 | goto end; | |
770 | } | |
771 | ||
c40a57e5 AB |
772 | end: |
773 | return ret; | |
774 | } | |
775 | ||
776 | BT_HIDDEN | |
777 | void source_location_destroy(struct source_location *src_loc) | |
778 | { | |
779 | if (!src_loc) { | |
780 | return; | |
781 | } | |
782 | ||
783 | free(src_loc->filename); | |
784 | g_free(src_loc); | |
785 | } | |
d6d3f4e8 | 786 | |
d2ac1099 AB |
787 | /** |
788 | * Append a string representation of an address offset to an existing | |
789 | * string. | |
790 | * | |
791 | * On success, the out parameter `result` will contain the base string | |
792 | * followed by the offset string of the form "+0x1234". On failure, | |
793 | * `result` remains unchanged. | |
794 | * | |
795 | * @param base_str The string to which to append an offset string | |
796 | * @param low_addr The lower virtual memory address, the base from | |
797 | * which the offset is computed | |
798 | * @param high_addr The higher virtual memory address | |
799 | * @param result Out parameter, the base string followed by the | |
800 | * offset string | |
801 | * @returns 0 on success, -1 on failure | |
802 | */ | |
803 | static | |
d5ddf820 | 804 | int bin_info_append_offset_str(const char *base_str, uint64_t low_addr, |
d2ac1099 AB |
805 | uint64_t high_addr, char **result) |
806 | { | |
d2ac1099 AB |
807 | uint64_t offset; |
808 | char *_result = NULL; | |
06d1cf5d | 809 | |
d2ac1099 AB |
810 | if (!base_str || !result) { |
811 | goto error; | |
812 | } | |
813 | ||
814 | offset = high_addr - low_addr; | |
815 | ||
06d1cf5d | 816 | _result = g_strdup_printf("%s+%#0" PRIx64, base_str, offset); |
d2ac1099 AB |
817 | if (!_result) { |
818 | goto error; | |
819 | } | |
d2ac1099 AB |
820 | *result = _result; |
821 | ||
822 | return 0; | |
823 | ||
824 | error: | |
825 | free(_result); | |
826 | return -1; | |
827 | } | |
c40a57e5 AB |
828 | |
829 | /** | |
830 | * Try to find the symbol closest to an address within a given ELF | |
831 | * section. | |
832 | * | |
833 | * Only function symbols are taken into account. The symbol's address | |
834 | * must precede `addr`. A symbol with a closer address might exist | |
835 | * after `addr` but is irrelevant because it cannot encompass `addr`. | |
836 | * | |
837 | * On success, if found, the out parameters `sym` and `shdr` are | |
838 | * set. On failure or if none are found, they remain unchanged. | |
839 | * | |
840 | * @param scn ELF section in which to look for the address | |
841 | * @param addr Virtual memory address for which to find the | |
842 | * nearest function symbol | |
843 | * @param sym Out parameter, the nearest function symbol | |
844 | * @param shdr Out parameter, the section header for scn | |
845 | * @returns 0 on success, -1 on failure | |
846 | */ | |
847 | static | |
d5ddf820 | 848 | int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, |
c40a57e5 AB |
849 | GElf_Sym **sym, GElf_Shdr **shdr) |
850 | { | |
851 | int i; | |
852 | size_t symbol_count; | |
853 | Elf_Data *data = NULL; | |
854 | GElf_Shdr *_shdr = NULL; | |
855 | GElf_Sym *nearest_sym = NULL; | |
856 | ||
857 | if (!scn || !sym || !shdr) { | |
858 | goto error; | |
859 | } | |
860 | ||
861 | _shdr = g_new0(GElf_Shdr, 1); | |
862 | if (!_shdr) { | |
863 | goto error; | |
864 | } | |
865 | ||
866 | _shdr = gelf_getshdr(scn, _shdr); | |
867 | if (!_shdr) { | |
868 | goto error; | |
869 | } | |
870 | ||
871 | if (_shdr->sh_type != SHT_SYMTAB) { | |
872 | /* | |
873 | * We are only interested in symbol table (symtab) | |
874 | * sections, skip this one. | |
875 | */ | |
876 | goto end; | |
877 | } | |
878 | ||
879 | data = elf_getdata(scn, NULL); | |
880 | if (!data) { | |
881 | goto error; | |
882 | } | |
883 | ||
884 | symbol_count = _shdr->sh_size / _shdr->sh_entsize; | |
885 | ||
886 | for (i = 0; i < symbol_count; ++i) { | |
887 | GElf_Sym *cur_sym = NULL; | |
888 | ||
889 | cur_sym = g_new0(GElf_Sym, 1); | |
890 | if (!cur_sym) { | |
891 | goto error; | |
892 | } | |
893 | cur_sym = gelf_getsym(data, i, cur_sym); | |
894 | if (!cur_sym) { | |
895 | goto error; | |
896 | } | |
897 | if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { | |
898 | /* We're only interested in the functions. */ | |
899 | g_free(cur_sym); | |
900 | continue; | |
901 | } | |
902 | ||
903 | if (cur_sym->st_value <= addr && | |
904 | (!nearest_sym || | |
905 | cur_sym->st_value > nearest_sym->st_value)) { | |
906 | g_free(nearest_sym); | |
907 | nearest_sym = cur_sym; | |
908 | } else { | |
909 | g_free(cur_sym); | |
910 | } | |
911 | } | |
912 | ||
913 | end: | |
914 | if (nearest_sym) { | |
915 | *sym = nearest_sym; | |
916 | *shdr = _shdr; | |
917 | } else { | |
918 | g_free(_shdr); | |
919 | } | |
920 | ||
921 | return 0; | |
922 | ||
923 | error: | |
924 | g_free(nearest_sym); | |
925 | g_free(_shdr); | |
926 | return -1; | |
927 | } | |
928 | ||
929 | /** | |
930 | * Get the name of the function containing a given address within an | |
931 | * executable using ELF symbols. | |
932 | * | |
933 | * The function name is in fact the name of the nearest ELF symbol, | |
934 | * followed by the offset in bytes between the address and the symbol | |
935 | * (in hex), separated by a '+' character. | |
936 | * | |
937 | * If found, the out parameter `func_name` is set on success. On failure, | |
938 | * it remains unchanged. | |
939 | * | |
d5ddf820 | 940 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
941 | * the address |
942 | * @param addr Virtual memory address for which to find the | |
943 | * function name | |
944 | * @param func_name Out parameter, the function name | |
945 | * @returns 0 on success, -1 on failure | |
946 | */ | |
947 | static | |
d5ddf820 | 948 | int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
949 | char **func_name) |
950 | { | |
951 | /* | |
952 | * TODO (possible optimisation): if an ELF has no symtab | |
953 | * section, it has been stripped. Therefore, it would be wise | |
954 | * to store a flag indicating the stripped status after the | |
955 | * first iteration to prevent subsequent ones. | |
956 | */ | |
957 | int ret = 0; | |
958 | Elf_Scn *scn = NULL; | |
959 | GElf_Sym *sym = NULL; | |
960 | GElf_Shdr *shdr = NULL; | |
961 | char *sym_name = NULL; | |
c40a57e5 | 962 | |
49824faa | 963 | /* Set ELF file if it hasn't been accessed yet. */ |
d5ddf820 AB |
964 | if (!bin->elf_file) { |
965 | ret = bin_info_set_elf_file(bin); | |
49824faa AB |
966 | if (ret) { |
967 | /* Failed to set ELF file. */ | |
968 | goto error; | |
969 | } | |
970 | } | |
971 | ||
d5ddf820 | 972 | scn = elf_nextscn(bin->elf_file, scn); |
c40a57e5 AB |
973 | if (!scn) { |
974 | goto error; | |
975 | } | |
976 | ||
977 | while (scn && !sym) { | |
d5ddf820 | 978 | ret = bin_info_get_nearest_symbol_from_section( |
c40a57e5 AB |
979 | scn, addr, &sym, &shdr); |
980 | if (ret) { | |
981 | goto error; | |
982 | } | |
983 | ||
d5ddf820 | 984 | scn = elf_nextscn(bin->elf_file, scn); |
c40a57e5 AB |
985 | } |
986 | ||
987 | if (sym) { | |
d5ddf820 | 988 | sym_name = elf_strptr(bin->elf_file, shdr->sh_link, |
c40a57e5 AB |
989 | sym->st_name); |
990 | if (!sym_name) { | |
991 | goto error; | |
992 | } | |
993 | ||
d5ddf820 | 994 | ret = bin_info_append_offset_str(sym_name, sym->st_value, addr, |
d2ac1099 AB |
995 | func_name); |
996 | if (ret) { | |
c40a57e5 AB |
997 | goto error; |
998 | } | |
c40a57e5 AB |
999 | } |
1000 | ||
1001 | g_free(shdr); | |
1002 | g_free(sym); | |
1003 | return 0; | |
1004 | ||
1005 | error: | |
1006 | g_free(shdr); | |
1007 | g_free(sym); | |
545e1e92 | 1008 | return ret; |
c40a57e5 AB |
1009 | } |
1010 | ||
1011 | /** | |
1012 | * Get the name of the function containing a given address within a | |
1013 | * given compile unit (CU). | |
1014 | * | |
1015 | * If found, the out parameter `func_name` is set on success. On | |
1016 | * failure, it remains unchanged. | |
1017 | * | |
1018 | * @param cu bt_dwarf_cu instance which may contain the address | |
1019 | * @param addr Virtual memory address for which to find the | |
1020 | * function name | |
1021 | * @param func_name Out parameter, the function name | |
1022 | * @returns 0 on success, -1 on failure | |
1023 | */ | |
1024 | static | |
d5ddf820 | 1025 | int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1026 | char **func_name) |
1027 | { | |
a54aa699 AB |
1028 | int ret = 0; |
1029 | bool found = false; | |
c40a57e5 AB |
1030 | struct bt_dwarf_die *die = NULL; |
1031 | ||
1032 | if (!cu || !func_name) { | |
1033 | goto error; | |
1034 | } | |
1035 | ||
1036 | die = bt_dwarf_die_create(cu); | |
1037 | if (!die) { | |
1038 | goto error; | |
1039 | } | |
1040 | ||
1041 | while (bt_dwarf_die_next(die) == 0) { | |
1042 | int tag; | |
1043 | ||
1044 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1045 | if (ret) { | |
1046 | goto error; | |
1047 | } | |
1048 | ||
1049 | if (tag == DW_TAG_subprogram) { | |
1050 | ret = bt_dwarf_die_contains_addr(die, addr, &found); | |
1051 | if (ret) { | |
1052 | goto error; | |
1053 | } | |
1054 | ||
1055 | if (found) { | |
1056 | break; | |
1057 | } | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | if (found) { | |
d2ac1099 AB |
1062 | uint64_t low_addr = 0; |
1063 | char *die_name = NULL; | |
1064 | ||
7935ee7a AB |
1065 | ret = bt_dwarf_die_get_name(die, &die_name); |
1066 | if (ret) { | |
1067 | goto error; | |
1068 | } | |
1069 | ||
1070 | ret = dwarf_lowpc(die->dwarf_die, &low_addr); | |
c40a57e5 | 1071 | if (ret) { |
7d41a84b | 1072 | free(die_name); |
c40a57e5 AB |
1073 | goto error; |
1074 | } | |
1075 | ||
d5ddf820 | 1076 | ret = bin_info_append_offset_str(die_name, low_addr, addr, |
d2ac1099 | 1077 | func_name); |
7d41a84b | 1078 | free(die_name); |
d2ac1099 | 1079 | if (ret) { |
7935ee7a AB |
1080 | goto error; |
1081 | } | |
c40a57e5 AB |
1082 | } |
1083 | ||
1084 | bt_dwarf_die_destroy(die); | |
1085 | return 0; | |
1086 | ||
1087 | error: | |
1088 | bt_dwarf_die_destroy(die); | |
1089 | return -1; | |
1090 | } | |
1091 | ||
1092 | /** | |
1093 | * Get the name of the function containing a given address within an | |
1094 | * executable using DWARF debug info. | |
1095 | * | |
1096 | * If found, the out parameter `func_name` is set on success. On | |
1097 | * failure, it remains unchanged. | |
1098 | * | |
d5ddf820 | 1099 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
1100 | * the address |
1101 | * @param addr Virtual memory address for which to find the | |
1102 | * function name | |
1103 | * @param func_name Out parameter, the function name | |
1104 | * @returns 0 on success, -1 on failure | |
1105 | */ | |
1106 | static | |
d5ddf820 | 1107 | int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
1108 | char **func_name) |
1109 | { | |
1110 | int ret = 0; | |
1111 | char *_func_name = NULL; | |
1112 | struct bt_dwarf_cu *cu = NULL; | |
1113 | ||
d5ddf820 | 1114 | if (!bin || !func_name) { |
c40a57e5 AB |
1115 | goto error; |
1116 | } | |
1117 | ||
d5ddf820 | 1118 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
c40a57e5 AB |
1119 | if (!cu) { |
1120 | goto error; | |
1121 | } | |
1122 | ||
1123 | while (bt_dwarf_cu_next(cu) == 0) { | |
d5ddf820 | 1124 | ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name); |
c40a57e5 AB |
1125 | if (ret) { |
1126 | goto error; | |
1127 | } | |
1128 | ||
1129 | if (_func_name) { | |
1130 | break; | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | if (_func_name) { | |
1135 | *func_name = _func_name; | |
93d65223 JG |
1136 | } else { |
1137 | goto error; | |
c40a57e5 AB |
1138 | } |
1139 | ||
1140 | bt_dwarf_cu_destroy(cu); | |
1141 | return 0; | |
1142 | ||
1143 | error: | |
1144 | bt_dwarf_cu_destroy(cu); | |
1145 | return -1; | |
1146 | } | |
1147 | ||
1148 | BT_HIDDEN | |
9d325e17 PP |
1149 | int bin_info_lookup_function_name(struct bin_info *bin, |
1150 | uint64_t addr, char **func_name) | |
c40a57e5 AB |
1151 | { |
1152 | int ret = 0; | |
1153 | char *_func_name = NULL; | |
1154 | ||
d5ddf820 | 1155 | if (!bin || !func_name) { |
c40a57e5 AB |
1156 | goto error; |
1157 | } | |
1158 | ||
ca9f27f3 FD |
1159 | /* |
1160 | * If the bin_info has a build id but it does not match the build id | |
1161 | * that was found on the file system, return an error. | |
1162 | */ | |
1163 | if (bin->build_id && !bin->file_build_id_matches) { | |
1164 | goto error; | |
1165 | } | |
1166 | ||
c40a57e5 | 1167 | /* Set DWARF info if it hasn't been accessed yet. */ |
d5ddf820 AB |
1168 | if (!bin->dwarf_info && !bin->is_elf_only) { |
1169 | ret = bin_info_set_dwarf_info(bin); | |
c40a57e5 | 1170 | if (ret) { |
1e638f98 FD |
1171 | BT_LOGD_STR("Failed to set bin dwarf info, falling " |
1172 | "back to ELF lookup."); | |
c40a57e5 | 1173 | /* Failed to set DWARF info, fallback to ELF. */ |
d5ddf820 | 1174 | bin->is_elf_only = true; |
c40a57e5 AB |
1175 | } |
1176 | } | |
1177 | ||
d5ddf820 | 1178 | if (!bin_info_has_address(bin, addr)) { |
c40a57e5 AB |
1179 | goto error; |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * Addresses in ELF and DWARF are relative to base address for | |
1184 | * PIC, so make the address argument relative too if needed. | |
1185 | */ | |
d5ddf820 AB |
1186 | if (bin->is_pic) { |
1187 | addr -= bin->low_addr; | |
36ae9941 AB |
1188 | } |
1189 | ||
d5ddf820 | 1190 | if (bin->is_elf_only) { |
b223415b FD |
1191 | ret = bin_info_lookup_elf_function_name(bin, addr, |
1192 | &_func_name); | |
1193 | if (ret) { | |
1194 | BT_LOGD("Failed to lookup function name (ELF): " | |
1195 | "ret=%d", ret); | |
1196 | } | |
c40a57e5 | 1197 | } else { |
b223415b FD |
1198 | ret = bin_info_lookup_dwarf_function_name(bin, addr, |
1199 | &_func_name); | |
1200 | if (ret) { | |
1201 | BT_LOGD("Failed to lookup function name (DWARF): " | |
1202 | "ret=%d", ret); | |
1203 | } | |
c40a57e5 AB |
1204 | } |
1205 | ||
36ae9941 AB |
1206 | *func_name = _func_name; |
1207 | return 0; | |
55cd033d | 1208 | |
36ae9941 AB |
1209 | error: |
1210 | return -1; | |
1211 | } | |
1212 | ||
1213 | BT_HIDDEN | |
d5ddf820 | 1214 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc) |
36ae9941 | 1215 | { |
06d1cf5d | 1216 | gchar *_bin_loc = NULL; |
36ae9941 | 1217 | |
d5ddf820 | 1218 | if (!bin || !bin_loc) { |
36ae9941 | 1219 | goto error; |
c40a57e5 AB |
1220 | } |
1221 | ||
ca9f27f3 FD |
1222 | /* |
1223 | * If the bin_info has a build id but it does not match the build id | |
1224 | * that was found on the file system, return an error. | |
1225 | */ | |
1226 | if (bin->build_id && !bin->file_build_id_matches) { | |
1227 | goto error; | |
1228 | } | |
1229 | ||
d5ddf820 AB |
1230 | if (bin->is_pic) { |
1231 | addr -= bin->low_addr; | |
06d1cf5d | 1232 | _bin_loc = g_strdup_printf("+%#0" PRIx64, addr); |
36ae9941 | 1233 | } else { |
06d1cf5d | 1234 | _bin_loc = g_strdup_printf("@%#0" PRIx64, addr); |
36ae9941 AB |
1235 | } |
1236 | ||
06d1cf5d | 1237 | if (!_bin_loc) { |
36ae9941 AB |
1238 | goto error; |
1239 | } | |
1240 | ||
1241 | *bin_loc = _bin_loc; | |
c40a57e5 AB |
1242 | return 0; |
1243 | ||
1244 | error: | |
1245 | return -1; | |
1246 | } | |
1247 | ||
1248 | /** | |
1249 | * Predicate used to determine whether the children of a given DIE | |
1250 | * contain a specific address. | |
1251 | * | |
1252 | * More specifically, the parameter `die` is expected to be a | |
1253 | * subprogram (function) DIE, and this predicate tells whether any | |
1254 | * subroutines are inlined within this function and would contain | |
1255 | * `addr`. | |
1256 | * | |
a54aa699 AB |
1257 | * On success, the out parameter `contains` is set with the boolean |
1258 | * value indicating whether the DIE's range covers `addr`. On failure, | |
1259 | * it remains unchanged. | |
1260 | * | |
c40a57e5 AB |
1261 | * Do note that this function advances the position of `die`. If the |
1262 | * address is found within one of its children, `die` will be pointing | |
1263 | * to that child upon returning from the function, allowing to extract | |
1264 | * the information deemed necessary. | |
1265 | * | |
a54aa699 AB |
1266 | * @param die The parent DIE in whose children the address will be |
1267 | * looked for | |
1268 | * @param addr The address for which to look for in the DIEs | |
1269 | * @param contains Out parameter, true if addr is contained, | |
1270 | * false if not | |
1271 | * @returns Returns 0 on success, -1 on failure | |
c40a57e5 AB |
1272 | */ |
1273 | static | |
a54aa699 | 1274 | int bin_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr, bool *contains) |
c40a57e5 | 1275 | { |
a54aa699 AB |
1276 | int ret = 0; |
1277 | bool _contains = false; | |
c40a57e5 AB |
1278 | |
1279 | if (!die) { | |
1280 | goto error; | |
1281 | } | |
1282 | ||
1283 | ret = bt_dwarf_die_child(die); | |
1284 | if (ret) { | |
1285 | goto error; | |
1286 | } | |
1287 | ||
1288 | do { | |
e6365242 | 1289 | ret = bt_dwarf_die_contains_addr(die, addr, &_contains); |
c40a57e5 AB |
1290 | if (ret) { |
1291 | goto error; | |
1292 | } | |
1293 | ||
e6365242 FD |
1294 | if (_contains) { |
1295 | /* | |
1296 | * The address is within the range of the current DIE | |
1297 | * or its children. | |
1298 | */ | |
1299 | int tag; | |
1300 | ||
1301 | ret = bt_dwarf_die_get_tag(die, &tag); | |
c40a57e5 AB |
1302 | if (ret) { |
1303 | goto error; | |
1304 | } | |
1305 | ||
e6365242 FD |
1306 | if (tag == DW_TAG_inlined_subroutine) { |
1307 | /* Found the tracepoint. */ | |
c40a57e5 AB |
1308 | goto end; |
1309 | } | |
e6365242 FD |
1310 | |
1311 | if (bt_dwarf_die_has_children(die)) { | |
1312 | /* | |
1313 | * Look for the address in the children DIEs. | |
1314 | */ | |
1315 | ret = bt_dwarf_die_child(die); | |
1316 | if (ret) { | |
1317 | goto error; | |
1318 | } | |
1319 | } | |
c40a57e5 AB |
1320 | } |
1321 | } while (bt_dwarf_die_next(die) == 0); | |
1322 | ||
1323 | end: | |
a54aa699 AB |
1324 | *contains = _contains; |
1325 | return 0; | |
c40a57e5 AB |
1326 | |
1327 | error: | |
a54aa699 | 1328 | return -1; |
c40a57e5 AB |
1329 | } |
1330 | ||
1331 | /** | |
1332 | * Lookup the source location for a given address within a CU, making | |
1333 | * the assumption that it is contained within an inline routine in a | |
1334 | * function. | |
1335 | * | |
1336 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1337 | * @param addr The address for which to look for | |
1338 | * @param src_loc Out parameter, the source location (filename and | |
1339 | * line number) for the address | |
1340 | * @returns 0 on success, -1 on failure | |
1341 | */ | |
1342 | static | |
d5ddf820 | 1343 | int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1344 | struct source_location **src_loc) |
1345 | { | |
a54aa699 AB |
1346 | int ret = 0; |
1347 | bool found = false; | |
c40a57e5 AB |
1348 | struct bt_dwarf_die *die = NULL; |
1349 | struct source_location *_src_loc = NULL; | |
1350 | ||
1351 | if (!cu || !src_loc) { | |
1352 | goto error; | |
1353 | } | |
1354 | ||
1355 | die = bt_dwarf_die_create(cu); | |
1356 | if (!die) { | |
1357 | goto error; | |
1358 | } | |
1359 | ||
1360 | while (bt_dwarf_die_next(die) == 0) { | |
1361 | int tag; | |
1362 | ||
1363 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1364 | if (ret) { | |
1365 | goto error; | |
1366 | } | |
1367 | ||
1368 | if (tag == DW_TAG_subprogram) { | |
a54aa699 | 1369 | bool contains = false; |
c40a57e5 AB |
1370 | |
1371 | ret = bt_dwarf_die_contains_addr(die, addr, &contains); | |
1372 | if (ret) { | |
1373 | goto error; | |
1374 | } | |
1375 | ||
1376 | if (contains) { | |
1377 | /* | |
1378 | * Try to find an inlined subroutine | |
1379 | * child of this DIE containing addr. | |
1380 | */ | |
a54aa699 AB |
1381 | ret = bin_info_child_die_has_address(die, addr, |
1382 | &found); | |
1383 | if(ret) { | |
1384 | goto error; | |
1385 | } | |
1386 | ||
c40a57e5 AB |
1387 | goto end; |
1388 | } | |
1389 | } | |
1390 | } | |
1391 | ||
1392 | end: | |
1393 | if (found) { | |
1394 | char *filename = NULL; | |
1395 | uint64_t line_no; | |
1396 | ||
1397 | _src_loc = g_new0(struct source_location, 1); | |
1398 | if (!_src_loc) { | |
1399 | goto error; | |
1400 | } | |
1401 | ||
1402 | ret = bt_dwarf_die_get_call_file(die, &filename); | |
1403 | if (ret) { | |
1404 | goto error; | |
1405 | } | |
1406 | ret = bt_dwarf_die_get_call_line(die, &line_no); | |
1407 | if (ret) { | |
1408 | free(filename); | |
1409 | goto error; | |
1410 | } | |
1411 | ||
1412 | _src_loc->filename = filename; | |
1413 | _src_loc->line_no = line_no; | |
1414 | *src_loc = _src_loc; | |
1415 | } | |
1416 | ||
1417 | bt_dwarf_die_destroy(die); | |
1418 | return 0; | |
1419 | ||
1420 | error: | |
1421 | source_location_destroy(_src_loc); | |
1422 | bt_dwarf_die_destroy(die); | |
1423 | return -1; | |
1424 | } | |
1425 | ||
1426 | /** | |
1427 | * Lookup the source location for a given address within a CU, | |
1428 | * assuming that it is contained within an inlined function. | |
1429 | * | |
1430 | * A source location can be found regardless of inlining status for | |
1431 | * this method, but in the case of an inlined function, the returned | |
1432 | * source location will point not to the callsite but rather to the | |
1433 | * definition site of the inline function. | |
1434 | * | |
1435 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1436 | * @param addr The address for which to look for | |
1437 | * @param src_loc Out parameter, the source location (filename and | |
1ed6d0cc FD |
1438 | * line number) for the address. Set only if the address |
1439 | * is found and resolved successfully | |
1440 | * | |
c40a57e5 AB |
1441 | * @returns 0 on success, -1 on failure |
1442 | */ | |
1443 | static | |
d5ddf820 | 1444 | int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1445 | struct source_location **src_loc) |
1446 | { | |
1447 | struct source_location *_src_loc = NULL; | |
1448 | struct bt_dwarf_die *die = NULL; | |
1449 | const char *filename = NULL; | |
1450 | Dwarf_Line *line = NULL; | |
1451 | Dwarf_Addr line_addr; | |
1452 | int ret, line_no; | |
1453 | ||
1454 | if (!cu || !src_loc) { | |
1455 | goto error; | |
1456 | } | |
1457 | ||
1458 | die = bt_dwarf_die_create(cu); | |
1459 | if (!die) { | |
1460 | goto error; | |
1461 | } | |
1462 | ||
1463 | line = dwarf_getsrc_die(die->dwarf_die, addr); | |
1464 | if (!line) { | |
1ed6d0cc FD |
1465 | /* This is not an error. The caller needs to keep looking. */ |
1466 | goto end; | |
c40a57e5 AB |
1467 | } |
1468 | ||
1469 | ret = dwarf_lineaddr(line, &line_addr); | |
1470 | if (ret) { | |
1471 | goto error; | |
1472 | } | |
1473 | ||
1474 | filename = dwarf_linesrc(line, NULL, NULL); | |
1475 | if (!filename) { | |
1476 | goto error; | |
1477 | } | |
1478 | ||
1479 | if (addr == line_addr) { | |
1480 | _src_loc = g_new0(struct source_location, 1); | |
1481 | if (!_src_loc) { | |
1482 | goto error; | |
1483 | } | |
1484 | ||
1485 | ret = dwarf_lineno(line, &line_no); | |
1486 | if (ret) { | |
1487 | goto error; | |
1488 | } | |
1489 | ||
1490 | _src_loc->line_no = line_no; | |
06d1cf5d | 1491 | _src_loc->filename = g_strdup(filename); |
c40a57e5 AB |
1492 | } |
1493 | ||
1494 | bt_dwarf_die_destroy(die); | |
1495 | ||
1496 | if (_src_loc) { | |
1497 | *src_loc = _src_loc; | |
1498 | } | |
1499 | ||
1ed6d0cc | 1500 | end: |
c40a57e5 AB |
1501 | return 0; |
1502 | ||
1503 | error: | |
1504 | source_location_destroy(_src_loc); | |
1505 | bt_dwarf_die_destroy(die); | |
1506 | return -1; | |
1507 | } | |
1508 | ||
1509 | /** | |
1510 | * Get the source location (file name and line number) for a given | |
1511 | * address within a compile unit (CU). | |
1512 | * | |
1513 | * On success, the out parameter `src_loc` is set if found. On | |
1514 | * failure, it remains unchanged. | |
1515 | * | |
d5ddf820 | 1516 | * @param cu bt_dwarf_cu instance for the compile unit which |
c40a57e5 AB |
1517 | * may contain the address |
1518 | * @param addr Virtual memory address for which to find the | |
1519 | * source location | |
1520 | * @param src_loc Out parameter, the source location | |
1521 | * @returns 0 on success, -1 on failure | |
1522 | */ | |
1523 | static | |
d5ddf820 | 1524 | int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1525 | struct source_location **src_loc) |
1526 | { | |
1527 | int ret = 0; | |
1528 | struct source_location *_src_loc = NULL; | |
1529 | ||
1530 | if (!cu || !src_loc) { | |
1531 | goto error; | |
1532 | } | |
1533 | ||
d5ddf820 | 1534 | ret = bin_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); |
c40a57e5 AB |
1535 | if (ret) { |
1536 | goto error; | |
1537 | } | |
1538 | ||
1539 | if (_src_loc) { | |
1540 | goto end; | |
1541 | } | |
1542 | ||
d5ddf820 | 1543 | ret = bin_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); |
c40a57e5 AB |
1544 | if (ret) { |
1545 | goto error; | |
1546 | } | |
1547 | ||
1548 | if (_src_loc) { | |
1549 | goto end; | |
1550 | } | |
1551 | ||
1552 | end: | |
1553 | if (_src_loc) { | |
1554 | *src_loc = _src_loc; | |
1555 | } | |
1556 | ||
1557 | return 0; | |
1558 | ||
1559 | error: | |
1560 | source_location_destroy(_src_loc); | |
1561 | return -1; | |
1562 | } | |
1563 | ||
1564 | BT_HIDDEN | |
d5ddf820 | 1565 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
1566 | struct source_location **src_loc) |
1567 | { | |
1568 | struct bt_dwarf_cu *cu = NULL; | |
1569 | struct source_location *_src_loc = NULL; | |
1570 | ||
d5ddf820 | 1571 | if (!bin || !src_loc) { |
c40a57e5 AB |
1572 | goto error; |
1573 | } | |
1574 | ||
ca9f27f3 FD |
1575 | /* |
1576 | * If the bin_info has a build id but it does not match the build id | |
1577 | * that was found on the file system, return an error. | |
1578 | */ | |
1579 | if (bin->build_id && !bin->file_build_id_matches) { | |
1580 | goto error; | |
1581 | } | |
1582 | ||
c40a57e5 | 1583 | /* Set DWARF info if it hasn't been accessed yet. */ |
d5ddf820 AB |
1584 | if (!bin->dwarf_info && !bin->is_elf_only) { |
1585 | if (bin_info_set_dwarf_info(bin)) { | |
c40a57e5 | 1586 | /* Failed to set DWARF info. */ |
d5ddf820 | 1587 | bin->is_elf_only = true; |
c40a57e5 AB |
1588 | } |
1589 | } | |
1590 | ||
d5ddf820 | 1591 | if (bin->is_elf_only) { |
c40a57e5 AB |
1592 | /* We cannot lookup source location without DWARF info. */ |
1593 | goto error; | |
1594 | } | |
1595 | ||
d5ddf820 | 1596 | if (!bin_info_has_address(bin, addr)) { |
c40a57e5 AB |
1597 | goto error; |
1598 | } | |
1599 | ||
1600 | /* | |
1601 | * Addresses in ELF and DWARF are relative to base address for | |
1602 | * PIC, so make the address argument relative too if needed. | |
1603 | */ | |
d5ddf820 AB |
1604 | if (bin->is_pic) { |
1605 | addr -= bin->low_addr; | |
c40a57e5 AB |
1606 | } |
1607 | ||
d5ddf820 | 1608 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
c40a57e5 AB |
1609 | if (!cu) { |
1610 | goto error; | |
1611 | } | |
1612 | ||
1613 | while (bt_dwarf_cu_next(cu) == 0) { | |
1614 | int ret; | |
1615 | ||
d5ddf820 | 1616 | ret = bin_info_lookup_cu_src_loc(cu, addr, &_src_loc); |
c40a57e5 AB |
1617 | if (ret) { |
1618 | goto error; | |
1619 | } | |
1620 | ||
1621 | if (_src_loc) { | |
1622 | break; | |
1623 | } | |
1624 | } | |
1625 | ||
1626 | bt_dwarf_cu_destroy(cu); | |
1627 | if (_src_loc) { | |
1628 | *src_loc = _src_loc; | |
1629 | } | |
1630 | ||
1631 | return 0; | |
1632 | ||
1633 | error: | |
1634 | source_location_destroy(_src_loc); | |
1635 | bt_dwarf_cu_destroy(cu); | |
1636 | return -1; | |
1637 | } |