Display binary location even if source and symbol lookups fail
[babeltrace.git] / lib / bin-info.c
CommitLineData
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
29#include <fcntl.h>
30#include <math.h>
31#include <libgen.h>
32#include <stdio.h>
33#include <inttypes.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include <dwarf.h>
38#include <glib.h>
39#include <babeltrace/dwarf.h>
d5ddf820 40#include <babeltrace/bin-info.h>
c40a57e5
AB
41#include <babeltrace/crc32.h>
42#include <babeltrace/babeltrace-internal.h>
55cd033d 43#include <babeltrace/utils.h>
545e1e92 44#include <errno.h>
c40a57e5
AB
45
46/*
47 * An address printed in hex is at most 20 bytes (16 for 64-bits +
48 * leading 0x + optional leading '+' if addr is an offset + null
49 * character).
50 */
51#define ADDR_STR_LEN 20
52
53BT_HIDDEN
d5ddf820 54int bin_info_init(void)
c40a57e5
AB
55{
56 int ret = 0;
57
58 if (elf_version(EV_CURRENT) == EV_NONE) {
6764ed54
AB
59 printf_debug("ELF library initialization failed: %s\n",
60 elf_errmsg(-1));
c40a57e5
AB
61 ret = -1;
62 }
63
64 return ret;
65}
66
67BT_HIDDEN
d5ddf820 68struct bin_info *bin_info_create(const char *path, uint64_t low_addr,
9f2b13ca 69 uint64_t memsz, bool is_pic)
c40a57e5 70{
d5ddf820 71 struct bin_info *bin = NULL;
c40a57e5
AB
72
73 if (!path) {
74 goto error;
75 }
76
d5ddf820
AB
77 bin = g_new0(struct bin_info, 1);
78 if (!bin) {
c40a57e5
AB
79 goto error;
80 }
81
5cde0dc1 82 if (opt_debug_info_target_prefix) {
d5ddf820 83 bin->elf_path = g_build_path("/", opt_debug_info_target_prefix,
5cde0dc1
AB
84 path, NULL);
85 } else {
d5ddf820 86 bin->elf_path = strdup(path);
5cde0dc1
AB
87 }
88
d5ddf820 89 if (!bin->elf_path) {
c40a57e5
AB
90 goto error;
91 }
92
d5ddf820
AB
93 bin->is_pic = is_pic;
94 bin->memsz = memsz;
95 bin->low_addr = low_addr;
96 bin->high_addr = bin->low_addr + bin->memsz;
c40a57e5 97
d5ddf820 98 return bin;
c40a57e5
AB
99
100error:
d5ddf820 101 bin_info_destroy(bin);
c40a57e5
AB
102 return NULL;
103}
104
105BT_HIDDEN
d5ddf820 106void bin_info_destroy(struct bin_info *bin)
c40a57e5 107{
d5ddf820 108 if (!bin) {
c40a57e5
AB
109 return;
110 }
111
d5ddf820 112 dwarf_end(bin->dwarf_info);
c40a57e5 113
d5ddf820
AB
114 free(bin->elf_path);
115 free(bin->dwarf_path);
116 free(bin->build_id);
117 free(bin->dbg_link_filename);
c40a57e5 118
d5ddf820 119 elf_end(bin->elf_file);
c40a57e5 120
d5ddf820
AB
121 close(bin->elf_fd);
122 close(bin->dwarf_fd);
c40a57e5 123
d5ddf820 124 g_free(bin);
c40a57e5
AB
125}
126
49824faa 127
c40a57e5 128BT_HIDDEN
d5ddf820 129int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id,
c40a57e5
AB
130 size_t build_id_len)
131{
d5ddf820 132 if (!bin || !build_id) {
c40a57e5
AB
133 goto error;
134 }
135
d5ddf820
AB
136 bin->build_id = malloc(build_id_len);
137 if (!bin->build_id) {
c40a57e5
AB
138 goto error;
139 }
140
d5ddf820
AB
141 memcpy(bin->build_id, build_id, build_id_len);
142 bin->build_id_len = build_id_len;
c40a57e5
AB
143
144 /*
145 * Reset the is_elf_only flag in case it had been set
146 * previously, because we might find separate debug info using
147 * the new build id information.
148 */
d5ddf820 149 bin->is_elf_only = false;
c40a57e5
AB
150
151 return 0;
152
153error:
154
155 return -1;
156}
157
158BT_HIDDEN
d5ddf820 159int bin_info_set_debug_link(struct bin_info *bin, char *filename, uint32_t crc)
c40a57e5 160{
d5ddf820 161 if (!bin || !filename) {
c40a57e5
AB
162 goto error;
163 }
164
d5ddf820
AB
165 bin->dbg_link_filename = strdup(filename);
166 if (!bin->dbg_link_filename) {
c40a57e5
AB
167 goto error;
168 }
169
d5ddf820 170 bin->dbg_link_crc = crc;
c40a57e5
AB
171
172 /*
173 * Reset the is_elf_only flag in case it had been set
174 * previously, because we might find separate debug info using
175 * the new build id information.
176 */
d5ddf820 177 bin->is_elf_only = false;
c40a57e5
AB
178
179 return 0;
180
181error:
182
183 return -1;
184}
185
186/**
187 * Tries to read DWARF info from the location given by path, and
d5ddf820 188 * attach it to the given bin_info instance if it exists.
c40a57e5 189 *
d5ddf820 190 * @param bin bin_info instance for which to set DWARF info
c40a57e5 191 * @param path Presumed location of the DWARF info
545e1e92 192 * @returns 0 on success, negative value on failure
c40a57e5
AB
193 */
194static
d5ddf820 195int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path)
c40a57e5
AB
196{
197 int fd = -1, ret = 0;
198 struct bt_dwarf_cu *cu = NULL;
199 Dwarf *dwarf_info = NULL;
200
d5ddf820 201 if (!bin || !path) {
c40a57e5
AB
202 goto error;
203 }
204
205 fd = open(path, O_RDONLY);
206 if (fd < 0) {
545e1e92 207 fd = -errno;
c40a57e5
AB
208 goto error;
209 }
210
211 dwarf_info = dwarf_begin(fd, DWARF_C_READ);
212 if (!dwarf_info) {
213 goto error;
214 }
215
216 /*
d5ddf820
AB
217 * Check if the dwarf info has any CU. If not, the
218 * executable's object file contains no DWARF info.
c40a57e5
AB
219 */
220 cu = bt_dwarf_cu_create(dwarf_info);
221 if (!cu) {
222 goto error;
223 }
224
225 ret = bt_dwarf_cu_next(cu);
226 if (ret) {
227 goto error;
228 }
229
d5ddf820
AB
230 bin->dwarf_fd = fd;
231 bin->dwarf_path = strdup(path);
232 if (!bin->dwarf_path) {
c40a57e5
AB
233 goto error;
234 }
d5ddf820 235 bin->dwarf_info = dwarf_info;
c40a57e5
AB
236 free(cu);
237
238 return 0;
239
240error:
545e1e92
JG
241 if (fd >= 0) {
242 close(fd);
243 fd = -1;
244 }
c40a57e5
AB
245 dwarf_end(dwarf_info);
246 g_free(dwarf_info);
247 free(cu);
248
545e1e92 249 return fd;
c40a57e5
AB
250}
251
252/**
d5ddf820 253 * Try to set the dwarf_info for a given bin_info instance via the
c40a57e5
AB
254 * build ID method.
255 *
d5ddf820 256 * @param bin bin_info instance for which to retrieve the
c40a57e5
AB
257 * DWARF info via build ID
258 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
259 */
260static
d5ddf820 261int bin_info_set_dwarf_info_build_id(struct bin_info *bin)
c40a57e5 262{
f06ce5e5 263 int i = 0, ret = 0;
c40a57e5
AB
264 char *path = NULL, *build_id_file = NULL;
265 const char *dbg_dir = NULL;
f06ce5e5 266 size_t build_id_file_len;
c40a57e5 267
d5ddf820 268 if (!bin || !bin->build_id) {
c40a57e5
AB
269 goto error;
270 }
271
05984e0c 272 dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR;
c40a57e5 273
b3599eb3 274 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
d5ddf820 275 build_id_file_len = (2 * bin->build_id_len) + 1 +
f06ce5e5 276 strlen(BUILD_ID_SUFFIX) + 1;
c40a57e5
AB
277 build_id_file = malloc(build_id_file_len);
278 if (!build_id_file) {
279 goto error;
280 }
281
d5ddf820
AB
282 snprintf(build_id_file, 4, "%02x/", bin->build_id[0]);
283 for (i = 1; i < bin->build_id_len; ++i) {
c40a57e5
AB
284 int path_idx = 3 + 2 * (i - 1);
285
d5ddf820 286 snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]);
c40a57e5 287 }
f06ce5e5 288 strcat(build_id_file, BUILD_ID_SUFFIX);
c40a57e5 289
f06ce5e5 290 path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL);
c40a57e5
AB
291 if (!path) {
292 goto error;
293 }
294
d5ddf820 295 ret = bin_info_set_dwarf_info_from_path(bin, path);
c40a57e5
AB
296 if (ret) {
297 goto error;
298 }
299
300 goto end;
301
302error:
303 ret = -1;
304end:
305 free(build_id_file);
306 free(path);
307
308 return ret;
309}
310
311/**
312 * Tests whether the file located at path exists and has the expected
313 * checksum.
314 *
315 * This predicate is used when looking up separate debug info via the
316 * GNU debuglink method. The expected crc can be found .gnu_debuglink
317 * section in the original ELF file, along with the filename for the
318 * file containing the debug info.
319 *
320 * @param path Full path at which to look for the debug file
321 * @param crc Expected checksum for the debug file
322 * @returns 1 if the file exists and has the correct checksum,
323 * 0 otherwise
324 */
325static
326int is_valid_debug_file(char *path, uint32_t crc)
327{
328 int ret = 0, fd = -1;
329 uint32_t _crc = 0;
330
331 if (!path) {
332 goto end;
333 }
334
335 fd = open(path, O_RDONLY);
336 if (fd < 0) {
337 goto end;
338 }
339
340 ret = crc32(fd, &_crc);
341 if (ret) {
342 ret = 0;
343 goto end;
344 }
345
346 ret = (crc == _crc);
347
348end:
349 close(fd);
350 return ret;
351}
352
353/**
d5ddf820 354 * Try to set the dwarf_info for a given bin_info instance via the
c40a57e5
AB
355 * build ID method.
356 *
d5ddf820 357 * @param bin bin_info instance for which to retrieve the
c40a57e5
AB
358 * DWARF info via debug link
359 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
360 */
361static
d5ddf820 362int bin_info_set_dwarf_info_debug_link(struct bin_info *bin)
c40a57e5
AB
363{
364 int ret = 0;
365 const char *dbg_dir = NULL;
d5ddf820 366 char *dir_name = NULL, *bin_dir = NULL, *path = NULL;
c40a57e5
AB
367 size_t max_path_len = 0;
368
d5ddf820 369 if (!bin || !bin->dbg_link_filename) {
c40a57e5
AB
370 goto error;
371 }
372
05984e0c 373 dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR;
c40a57e5 374
d5ddf820 375 dir_name = dirname(bin->elf_path);
c40a57e5
AB
376 if (!dir_name) {
377 goto error;
378 }
379
d5ddf820
AB
380 /* bin_dir is just dir_name with a trailing slash */
381 bin_dir = malloc(strlen(dir_name) + 2);
382 if (!bin_dir) {
c40a57e5
AB
383 goto error;
384 }
385
d5ddf820
AB
386 strcpy(bin_dir, dir_name);
387 strcat(bin_dir, "/");
c40a57e5 388
d5ddf820
AB
389 max_path_len = strlen(dbg_dir) + strlen(bin_dir) +
390 strlen(DEBUG_SUBDIR) + strlen(bin->dbg_link_filename)
c40a57e5
AB
391 + 1;
392 path = malloc(max_path_len);
393 if (!path) {
394 goto error;
395 }
396
d5ddf820
AB
397 /* First look in the executable's dir */
398 strcpy(path, bin_dir);
399 strcat(path, bin->dbg_link_filename);
c40a57e5 400
d5ddf820 401 if (is_valid_debug_file(path, bin->dbg_link_crc)) {
c40a57e5
AB
402 goto found;
403 }
404
405 /* If not found, look in .debug subdir */
d5ddf820 406 strcpy(path, bin_dir);
c40a57e5 407 strcat(path, DEBUG_SUBDIR);
d5ddf820 408 strcat(path, bin->dbg_link_filename);
c40a57e5 409
d5ddf820 410 if (is_valid_debug_file(path, bin->dbg_link_crc)) {
c40a57e5
AB
411 goto found;
412 }
413
414 /* Lastly, look under the global debug directory */
415 strcpy(path, dbg_dir);
d5ddf820
AB
416 strcat(path, bin_dir);
417 strcat(path, bin->dbg_link_filename);
c40a57e5 418
d5ddf820 419 if (is_valid_debug_file(path, bin->dbg_link_crc)) {
c40a57e5
AB
420 goto found;
421 }
422
423error:
424 ret = -1;
425end:
426 free(path);
d5ddf820 427 free(bin_dir);
c40a57e5
AB
428
429 return ret;
430
431found:
d5ddf820 432 ret = bin_info_set_dwarf_info_from_path(bin, path);
c40a57e5
AB
433 if (ret) {
434 goto error;
435 }
436
437 goto end;
438}
439
440/**
441 * Initialize the DWARF info for a given executable.
442 *
d5ddf820 443 * @param bin bin_info instance
545e1e92 444 * @returns 0 on success, negative value on failure
c40a57e5
AB
445 */
446static
d5ddf820 447int bin_info_set_dwarf_info(struct bin_info *bin)
c40a57e5
AB
448{
449 int ret = 0;
450
d5ddf820 451 if (!bin) {
545e1e92
JG
452 ret = -1;
453 goto end;
c40a57e5
AB
454 }
455
456 /* First try to set the DWARF info from the ELF file */
d5ddf820 457 ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path);
c40a57e5
AB
458 if (!ret) {
459 goto end;
460 }
461
462 /*
463 * If that fails, try to find separate debug info via build ID
464 * and debug link.
465 */
d5ddf820 466 ret = bin_info_set_dwarf_info_build_id(bin);
c40a57e5
AB
467 if (!ret) {
468 goto end;
469 }
470
d5ddf820 471 ret = bin_info_set_dwarf_info_debug_link(bin);
c40a57e5
AB
472 if (!ret) {
473 goto end;
474 }
475
c40a57e5
AB
476end:
477 return ret;
478}
479
49824faa
AB
480/**
481 * Initialize the ELF file for a given executable.
482 *
d5ddf820 483 * @param bin bin_info instance
545e1e92 484 * @returns 0 on success, negative value on error.
49824faa
AB
485 */
486static
d5ddf820 487int bin_info_set_elf_file(struct bin_info *bin)
49824faa 488{
d460dc7f 489 int elf_fd = -1;
f99dc219 490 Elf *elf_file = NULL;
49824faa 491
d5ddf820 492 if (!bin) {
49824faa
AB
493 goto error;
494 }
495
d5ddf820 496 elf_fd = open(bin->elf_path, O_RDONLY);
49824faa 497 if (elf_fd < 0) {
545e1e92 498 elf_fd = -errno;
d5ddf820 499 printf_verbose("Failed to open %s\n", bin->elf_path);
49824faa
AB
500 goto error;
501 }
502
503 elf_file = elf_begin(elf_fd, ELF_C_READ, NULL);
504 if (!elf_file) {
6764ed54 505 printf_debug("elf_begin failed: %s\n", elf_errmsg(-1));
49824faa
AB
506 goto error;
507 }
508
509 if (elf_kind(elf_file) != ELF_K_ELF) {
6764ed54 510 printf_verbose("Error: %s is not an ELF object\n",
d5ddf820 511 bin->elf_path);
49824faa
AB
512 goto error;
513 }
514
d5ddf820
AB
515 bin->elf_fd = elf_fd;
516 bin->elf_file = elf_file;
49824faa
AB
517 return 0;
518
519error:
d460dc7f
JG
520 if (elf_fd >= 0) {
521 close(elf_fd);
545e1e92 522 elf_fd = -1;
d460dc7f 523 }
49824faa 524 elf_end(elf_file);
545e1e92 525 return elf_fd;
49824faa
AB
526}
527
c40a57e5
AB
528BT_HIDDEN
529void source_location_destroy(struct source_location *src_loc)
530{
531 if (!src_loc) {
532 return;
533 }
534
535 free(src_loc->filename);
536 g_free(src_loc);
537}
d6d3f4e8 538
d2ac1099
AB
539/**
540 * Append a string representation of an address offset to an existing
541 * string.
542 *
543 * On success, the out parameter `result` will contain the base string
544 * followed by the offset string of the form "+0x1234". On failure,
545 * `result` remains unchanged.
546 *
547 * @param base_str The string to which to append an offset string
548 * @param low_addr The lower virtual memory address, the base from
549 * which the offset is computed
550 * @param high_addr The higher virtual memory address
551 * @param result Out parameter, the base string followed by the
552 * offset string
553 * @returns 0 on success, -1 on failure
554 */
555static
d5ddf820 556int bin_info_append_offset_str(const char *base_str, uint64_t low_addr,
d2ac1099
AB
557 uint64_t high_addr, char **result)
558{
559 int ret;
560 uint64_t offset;
561 char *_result = NULL;
562 char offset_str[ADDR_STR_LEN];
563
564 if (!base_str || !result) {
565 goto error;
566 }
567
568 offset = high_addr - low_addr;
569
570 _result = malloc(strlen(base_str) + ADDR_STR_LEN);
571 if (!_result) {
572 goto error;
573 }
574
575 ret = snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, offset);
576 if (ret < 0) {
577 goto error;
578 }
579 strcpy(_result, base_str);
580 strcat(_result, offset_str);
581 *result = _result;
582
583 return 0;
584
585error:
586 free(_result);
587 return -1;
588}
c40a57e5
AB
589
590/**
591 * Try to find the symbol closest to an address within a given ELF
592 * section.
593 *
594 * Only function symbols are taken into account. The symbol's address
595 * must precede `addr`. A symbol with a closer address might exist
596 * after `addr` but is irrelevant because it cannot encompass `addr`.
597 *
598 * On success, if found, the out parameters `sym` and `shdr` are
599 * set. On failure or if none are found, they remain unchanged.
600 *
601 * @param scn ELF section in which to look for the address
602 * @param addr Virtual memory address for which to find the
603 * nearest function symbol
604 * @param sym Out parameter, the nearest function symbol
605 * @param shdr Out parameter, the section header for scn
606 * @returns 0 on success, -1 on failure
607 */
608static
d5ddf820 609int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr,
c40a57e5
AB
610 GElf_Sym **sym, GElf_Shdr **shdr)
611{
612 int i;
613 size_t symbol_count;
614 Elf_Data *data = NULL;
615 GElf_Shdr *_shdr = NULL;
616 GElf_Sym *nearest_sym = NULL;
617
618 if (!scn || !sym || !shdr) {
619 goto error;
620 }
621
622 _shdr = g_new0(GElf_Shdr, 1);
623 if (!_shdr) {
624 goto error;
625 }
626
627 _shdr = gelf_getshdr(scn, _shdr);
628 if (!_shdr) {
629 goto error;
630 }
631
632 if (_shdr->sh_type != SHT_SYMTAB) {
633 /*
634 * We are only interested in symbol table (symtab)
635 * sections, skip this one.
636 */
637 goto end;
638 }
639
640 data = elf_getdata(scn, NULL);
641 if (!data) {
642 goto error;
643 }
644
645 symbol_count = _shdr->sh_size / _shdr->sh_entsize;
646
647 for (i = 0; i < symbol_count; ++i) {
648 GElf_Sym *cur_sym = NULL;
649
650 cur_sym = g_new0(GElf_Sym, 1);
651 if (!cur_sym) {
652 goto error;
653 }
654 cur_sym = gelf_getsym(data, i, cur_sym);
655 if (!cur_sym) {
656 goto error;
657 }
658 if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) {
659 /* We're only interested in the functions. */
660 g_free(cur_sym);
661 continue;
662 }
663
664 if (cur_sym->st_value <= addr &&
665 (!nearest_sym ||
666 cur_sym->st_value > nearest_sym->st_value)) {
667 g_free(nearest_sym);
668 nearest_sym = cur_sym;
669 } else {
670 g_free(cur_sym);
671 }
672 }
673
674end:
675 if (nearest_sym) {
676 *sym = nearest_sym;
677 *shdr = _shdr;
678 } else {
679 g_free(_shdr);
680 }
681
682 return 0;
683
684error:
685 g_free(nearest_sym);
686 g_free(_shdr);
687 return -1;
688}
689
690/**
691 * Get the name of the function containing a given address within an
692 * executable using ELF symbols.
693 *
694 * The function name is in fact the name of the nearest ELF symbol,
695 * followed by the offset in bytes between the address and the symbol
696 * (in hex), separated by a '+' character.
697 *
698 * If found, the out parameter `func_name` is set on success. On failure,
699 * it remains unchanged.
700 *
d5ddf820 701 * @param bin bin_info instance for the executable containing
c40a57e5
AB
702 * the address
703 * @param addr Virtual memory address for which to find the
704 * function name
705 * @param func_name Out parameter, the function name
706 * @returns 0 on success, -1 on failure
707 */
708static
d5ddf820 709int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr,
c40a57e5
AB
710 char **func_name)
711{
712 /*
713 * TODO (possible optimisation): if an ELF has no symtab
714 * section, it has been stripped. Therefore, it would be wise
715 * to store a flag indicating the stripped status after the
716 * first iteration to prevent subsequent ones.
717 */
718 int ret = 0;
719 Elf_Scn *scn = NULL;
720 GElf_Sym *sym = NULL;
721 GElf_Shdr *shdr = NULL;
722 char *sym_name = NULL;
c40a57e5 723
49824faa 724 /* Set ELF file if it hasn't been accessed yet. */
d5ddf820
AB
725 if (!bin->elf_file) {
726 ret = bin_info_set_elf_file(bin);
49824faa
AB
727 if (ret) {
728 /* Failed to set ELF file. */
729 goto error;
730 }
731 }
732
d5ddf820 733 scn = elf_nextscn(bin->elf_file, scn);
c40a57e5
AB
734 if (!scn) {
735 goto error;
736 }
737
738 while (scn && !sym) {
d5ddf820 739 ret = bin_info_get_nearest_symbol_from_section(
c40a57e5
AB
740 scn, addr, &sym, &shdr);
741 if (ret) {
742 goto error;
743 }
744
d5ddf820 745 scn = elf_nextscn(bin->elf_file, scn);
c40a57e5
AB
746 }
747
748 if (sym) {
d5ddf820 749 sym_name = elf_strptr(bin->elf_file, shdr->sh_link,
c40a57e5
AB
750 sym->st_name);
751 if (!sym_name) {
752 goto error;
753 }
754
d5ddf820 755 ret = bin_info_append_offset_str(sym_name, sym->st_value, addr,
d2ac1099
AB
756 func_name);
757 if (ret) {
c40a57e5
AB
758 goto error;
759 }
c40a57e5
AB
760 }
761
762 g_free(shdr);
763 g_free(sym);
764 return 0;
765
766error:
767 g_free(shdr);
768 g_free(sym);
545e1e92 769 return ret;
c40a57e5
AB
770}
771
772/**
773 * Get the name of the function containing a given address within a
774 * given compile unit (CU).
775 *
776 * If found, the out parameter `func_name` is set on success. On
777 * failure, it remains unchanged.
778 *
779 * @param cu bt_dwarf_cu instance which may contain the address
780 * @param addr Virtual memory address for which to find the
781 * function name
782 * @param func_name Out parameter, the function name
783 * @returns 0 on success, -1 on failure
784 */
785static
d5ddf820 786int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr,
c40a57e5
AB
787 char **func_name)
788{
a54aa699
AB
789 int ret = 0;
790 bool found = false;
c40a57e5
AB
791 struct bt_dwarf_die *die = NULL;
792
793 if (!cu || !func_name) {
794 goto error;
795 }
796
797 die = bt_dwarf_die_create(cu);
798 if (!die) {
799 goto error;
800 }
801
802 while (bt_dwarf_die_next(die) == 0) {
803 int tag;
804
805 ret = bt_dwarf_die_get_tag(die, &tag);
806 if (ret) {
807 goto error;
808 }
809
810 if (tag == DW_TAG_subprogram) {
811 ret = bt_dwarf_die_contains_addr(die, addr, &found);
812 if (ret) {
813 goto error;
814 }
815
816 if (found) {
817 break;
818 }
819 }
820 }
821
822 if (found) {
d2ac1099
AB
823 uint64_t low_addr = 0;
824 char *die_name = NULL;
825
7935ee7a
AB
826 ret = bt_dwarf_die_get_name(die, &die_name);
827 if (ret) {
828 goto error;
829 }
830
831 ret = dwarf_lowpc(die->dwarf_die, &low_addr);
c40a57e5
AB
832 if (ret) {
833 goto error;
834 }
835
d5ddf820 836 ret = bin_info_append_offset_str(die_name, low_addr, addr,
d2ac1099
AB
837 func_name);
838 if (ret) {
7935ee7a
AB
839 goto error;
840 }
c40a57e5
AB
841 }
842
843 bt_dwarf_die_destroy(die);
844 return 0;
845
846error:
847 bt_dwarf_die_destroy(die);
848 return -1;
849}
850
851/**
852 * Get the name of the function containing a given address within an
853 * executable using DWARF debug info.
854 *
855 * If found, the out parameter `func_name` is set on success. On
856 * failure, it remains unchanged.
857 *
d5ddf820 858 * @param bin bin_info instance for the executable containing
c40a57e5
AB
859 * the address
860 * @param addr Virtual memory address for which to find the
861 * function name
862 * @param func_name Out parameter, the function name
863 * @returns 0 on success, -1 on failure
864 */
865static
d5ddf820 866int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr,
c40a57e5
AB
867 char **func_name)
868{
869 int ret = 0;
870 char *_func_name = NULL;
871 struct bt_dwarf_cu *cu = NULL;
872
d5ddf820 873 if (!bin || !func_name) {
c40a57e5
AB
874 goto error;
875 }
876
d5ddf820 877 cu = bt_dwarf_cu_create(bin->dwarf_info);
c40a57e5
AB
878 if (!cu) {
879 goto error;
880 }
881
882 while (bt_dwarf_cu_next(cu) == 0) {
d5ddf820 883 ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name);
c40a57e5
AB
884 if (ret) {
885 goto error;
886 }
887
888 if (_func_name) {
889 break;
890 }
891 }
892
893 if (_func_name) {
894 *func_name = _func_name;
93d65223
JG
895 } else {
896 goto error;
c40a57e5
AB
897 }
898
899 bt_dwarf_cu_destroy(cu);
900 return 0;
901
902error:
903 bt_dwarf_cu_destroy(cu);
904 return -1;
905}
906
907BT_HIDDEN
d5ddf820 908int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr,
c40a57e5
AB
909 char **func_name)
910{
911 int ret = 0;
912 char *_func_name = NULL;
913
d5ddf820 914 if (!bin || !func_name) {
c40a57e5
AB
915 goto error;
916 }
917
918 /* Set DWARF info if it hasn't been accessed yet. */
d5ddf820
AB
919 if (!bin->dwarf_info && !bin->is_elf_only) {
920 ret = bin_info_set_dwarf_info(bin);
c40a57e5 921 if (ret) {
545e1e92 922 printf_verbose("Failed to set bin dwarf info, falling back to ELF lookup.\n");
c40a57e5 923 /* Failed to set DWARF info, fallback to ELF. */
d5ddf820 924 bin->is_elf_only = true;
c40a57e5
AB
925 }
926 }
927
d5ddf820 928 if (!bin_info_has_address(bin, addr)) {
c40a57e5
AB
929 goto error;
930 }
931
932 /*
933 * Addresses in ELF and DWARF are relative to base address for
934 * PIC, so make the address argument relative too if needed.
935 */
d5ddf820
AB
936 if (bin->is_pic) {
937 addr -= bin->low_addr;
36ae9941
AB
938 }
939
d5ddf820
AB
940 if (bin->is_elf_only) {
941 ret = bin_info_lookup_elf_function_name(bin, addr, &_func_name);
545e1e92 942 printf_verbose("Failed to lookup function name (elf), error %i\n", ret);
c40a57e5 943 } else {
d5ddf820 944 ret = bin_info_lookup_dwarf_function_name(bin, addr, &_func_name);
545e1e92 945 printf_verbose("Failed to lookup function name (dwarf), error %i\n", ret);
c40a57e5
AB
946 }
947
36ae9941
AB
948 *func_name = _func_name;
949 return 0;
55cd033d 950
36ae9941
AB
951error:
952 return -1;
953}
954
955BT_HIDDEN
d5ddf820 956int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc)
36ae9941
AB
957{
958 int ret = 0;
959 char *_bin_loc = NULL;
960
d5ddf820 961 if (!bin || !bin_loc) {
36ae9941 962 goto error;
c40a57e5
AB
963 }
964
d5ddf820
AB
965 if (bin->is_pic) {
966 addr -= bin->low_addr;
36ae9941
AB
967 ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr);
968 } else {
969 ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr);
970 }
971
972 if (ret == -1 || !_bin_loc) {
973 goto error;
974 }
975
976 *bin_loc = _bin_loc;
c40a57e5
AB
977 return 0;
978
979error:
980 return -1;
981}
982
983/**
984 * Predicate used to determine whether the children of a given DIE
985 * contain a specific address.
986 *
987 * More specifically, the parameter `die` is expected to be a
988 * subprogram (function) DIE, and this predicate tells whether any
989 * subroutines are inlined within this function and would contain
990 * `addr`.
991 *
a54aa699
AB
992 * On success, the out parameter `contains` is set with the boolean
993 * value indicating whether the DIE's range covers `addr`. On failure,
994 * it remains unchanged.
995 *
c40a57e5
AB
996 * Do note that this function advances the position of `die`. If the
997 * address is found within one of its children, `die` will be pointing
998 * to that child upon returning from the function, allowing to extract
999 * the information deemed necessary.
1000 *
a54aa699
AB
1001 * @param die The parent DIE in whose children the address will be
1002 * looked for
1003 * @param addr The address for which to look for in the DIEs
1004 * @param contains Out parameter, true if addr is contained,
1005 * false if not
1006 * @returns Returns 0 on success, -1 on failure
c40a57e5
AB
1007 */
1008static
a54aa699 1009int bin_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr, bool *contains)
c40a57e5 1010{
a54aa699
AB
1011 int ret = 0;
1012 bool _contains = false;
c40a57e5
AB
1013
1014 if (!die) {
1015 goto error;
1016 }
1017
1018 ret = bt_dwarf_die_child(die);
1019 if (ret) {
1020 goto error;
1021 }
1022
1023 do {
1024 int tag;
1025
1026 ret = bt_dwarf_die_get_tag(die, &tag);
1027 if (ret) {
1028 goto error;
1029 }
1030
1031 if (tag == DW_TAG_inlined_subroutine) {
a54aa699 1032 ret = bt_dwarf_die_contains_addr(die, addr, &_contains);
c40a57e5
AB
1033 if (ret) {
1034 goto error;
1035 }
1036
1037 if (contains) {
c40a57e5
AB
1038 goto end;
1039 }
1040 }
1041 } while (bt_dwarf_die_next(die) == 0);
1042
1043end:
a54aa699
AB
1044 *contains = _contains;
1045 return 0;
c40a57e5
AB
1046
1047error:
a54aa699 1048 return -1;
c40a57e5
AB
1049}
1050
1051/**
1052 * Lookup the source location for a given address within a CU, making
1053 * the assumption that it is contained within an inline routine in a
1054 * function.
1055 *
1056 * @param cu bt_dwarf_cu instance in which to look for the address
1057 * @param addr The address for which to look for
1058 * @param src_loc Out parameter, the source location (filename and
1059 * line number) for the address
1060 * @returns 0 on success, -1 on failure
1061 */
1062static
d5ddf820 1063int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr,
c40a57e5
AB
1064 struct source_location **src_loc)
1065{
a54aa699
AB
1066 int ret = 0;
1067 bool found = false;
c40a57e5
AB
1068 struct bt_dwarf_die *die = NULL;
1069 struct source_location *_src_loc = NULL;
1070
1071 if (!cu || !src_loc) {
1072 goto error;
1073 }
1074
1075 die = bt_dwarf_die_create(cu);
1076 if (!die) {
1077 goto error;
1078 }
1079
1080 while (bt_dwarf_die_next(die) == 0) {
1081 int tag;
1082
1083 ret = bt_dwarf_die_get_tag(die, &tag);
1084 if (ret) {
1085 goto error;
1086 }
1087
1088 if (tag == DW_TAG_subprogram) {
a54aa699 1089 bool contains = false;
c40a57e5
AB
1090
1091 ret = bt_dwarf_die_contains_addr(die, addr, &contains);
1092 if (ret) {
1093 goto error;
1094 }
1095
1096 if (contains) {
1097 /*
1098 * Try to find an inlined subroutine
1099 * child of this DIE containing addr.
1100 */
a54aa699
AB
1101 ret = bin_info_child_die_has_address(die, addr,
1102 &found);
1103 if(ret) {
1104 goto error;
1105 }
1106
c40a57e5
AB
1107 goto end;
1108 }
1109 }
1110 }
1111
1112end:
1113 if (found) {
1114 char *filename = NULL;
1115 uint64_t line_no;
1116
1117 _src_loc = g_new0(struct source_location, 1);
1118 if (!_src_loc) {
1119 goto error;
1120 }
1121
1122 ret = bt_dwarf_die_get_call_file(die, &filename);
1123 if (ret) {
1124 goto error;
1125 }
1126 ret = bt_dwarf_die_get_call_line(die, &line_no);
1127 if (ret) {
1128 free(filename);
1129 goto error;
1130 }
1131
1132 _src_loc->filename = filename;
1133 _src_loc->line_no = line_no;
1134 *src_loc = _src_loc;
1135 }
1136
1137 bt_dwarf_die_destroy(die);
1138 return 0;
1139
1140error:
1141 source_location_destroy(_src_loc);
1142 bt_dwarf_die_destroy(die);
1143 return -1;
1144}
1145
1146/**
1147 * Lookup the source location for a given address within a CU,
1148 * assuming that it is contained within an inlined function.
1149 *
1150 * A source location can be found regardless of inlining status for
1151 * this method, but in the case of an inlined function, the returned
1152 * source location will point not to the callsite but rather to the
1153 * definition site of the inline function.
1154 *
1155 * @param cu bt_dwarf_cu instance in which to look for the address
1156 * @param addr The address for which to look for
1157 * @param src_loc Out parameter, the source location (filename and
1158 * line number) for the address
1159 * @returns 0 on success, -1 on failure
1160 */
1161static
d5ddf820 1162int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr,
c40a57e5
AB
1163 struct source_location **src_loc)
1164{
1165 struct source_location *_src_loc = NULL;
1166 struct bt_dwarf_die *die = NULL;
1167 const char *filename = NULL;
1168 Dwarf_Line *line = NULL;
1169 Dwarf_Addr line_addr;
1170 int ret, line_no;
1171
1172 if (!cu || !src_loc) {
1173 goto error;
1174 }
1175
1176 die = bt_dwarf_die_create(cu);
1177 if (!die) {
1178 goto error;
1179 }
1180
1181 line = dwarf_getsrc_die(die->dwarf_die, addr);
1182 if (!line) {
1183 goto error;
1184 }
1185
1186 ret = dwarf_lineaddr(line, &line_addr);
1187 if (ret) {
1188 goto error;
1189 }
1190
1191 filename = dwarf_linesrc(line, NULL, NULL);
1192 if (!filename) {
1193 goto error;
1194 }
1195
1196 if (addr == line_addr) {
1197 _src_loc = g_new0(struct source_location, 1);
1198 if (!_src_loc) {
1199 goto error;
1200 }
1201
1202 ret = dwarf_lineno(line, &line_no);
1203 if (ret) {
1204 goto error;
1205 }
1206
1207 _src_loc->line_no = line_no;
1208 _src_loc->filename = strdup(filename);
1209 }
1210
1211 bt_dwarf_die_destroy(die);
1212
1213 if (_src_loc) {
1214 *src_loc = _src_loc;
1215 }
1216
1217 return 0;
1218
1219error:
1220 source_location_destroy(_src_loc);
1221 bt_dwarf_die_destroy(die);
1222 return -1;
1223}
1224
1225/**
1226 * Get the source location (file name and line number) for a given
1227 * address within a compile unit (CU).
1228 *
1229 * On success, the out parameter `src_loc` is set if found. On
1230 * failure, it remains unchanged.
1231 *
d5ddf820 1232 * @param cu bt_dwarf_cu instance for the compile unit which
c40a57e5
AB
1233 * may contain the address
1234 * @param addr Virtual memory address for which to find the
1235 * source location
1236 * @param src_loc Out parameter, the source location
1237 * @returns 0 on success, -1 on failure
1238 */
1239static
d5ddf820 1240int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr,
c40a57e5
AB
1241 struct source_location **src_loc)
1242{
1243 int ret = 0;
1244 struct source_location *_src_loc = NULL;
1245
1246 if (!cu || !src_loc) {
1247 goto error;
1248 }
1249
d5ddf820 1250 ret = bin_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc);
c40a57e5
AB
1251 if (ret) {
1252 goto error;
1253 }
1254
1255 if (_src_loc) {
1256 goto end;
1257 }
1258
d5ddf820 1259 ret = bin_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc);
c40a57e5
AB
1260 if (ret) {
1261 goto error;
1262 }
1263
1264 if (_src_loc) {
1265 goto end;
1266 }
1267
1268end:
1269 if (_src_loc) {
1270 *src_loc = _src_loc;
1271 }
1272
1273 return 0;
1274
1275error:
1276 source_location_destroy(_src_loc);
1277 return -1;
1278}
1279
1280BT_HIDDEN
d5ddf820 1281int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr,
c40a57e5
AB
1282 struct source_location **src_loc)
1283{
1284 struct bt_dwarf_cu *cu = NULL;
1285 struct source_location *_src_loc = NULL;
1286
d5ddf820 1287 if (!bin || !src_loc) {
c40a57e5
AB
1288 goto error;
1289 }
1290
1291 /* Set DWARF info if it hasn't been accessed yet. */
d5ddf820
AB
1292 if (!bin->dwarf_info && !bin->is_elf_only) {
1293 if (bin_info_set_dwarf_info(bin)) {
c40a57e5 1294 /* Failed to set DWARF info. */
d5ddf820 1295 bin->is_elf_only = true;
c40a57e5
AB
1296 }
1297 }
1298
d5ddf820 1299 if (bin->is_elf_only) {
c40a57e5
AB
1300 /* We cannot lookup source location without DWARF info. */
1301 goto error;
1302 }
1303
d5ddf820 1304 if (!bin_info_has_address(bin, addr)) {
c40a57e5
AB
1305 goto error;
1306 }
1307
1308 /*
1309 * Addresses in ELF and DWARF are relative to base address for
1310 * PIC, so make the address argument relative too if needed.
1311 */
d5ddf820
AB
1312 if (bin->is_pic) {
1313 addr -= bin->low_addr;
c40a57e5
AB
1314 }
1315
d5ddf820 1316 cu = bt_dwarf_cu_create(bin->dwarf_info);
c40a57e5
AB
1317 if (!cu) {
1318 goto error;
1319 }
1320
1321 while (bt_dwarf_cu_next(cu) == 0) {
1322 int ret;
1323
d5ddf820 1324 ret = bin_info_lookup_cu_src_loc(cu, addr, &_src_loc);
c40a57e5
AB
1325 if (ret) {
1326 goto error;
1327 }
1328
1329 if (_src_loc) {
1330 break;
1331 }
1332 }
1333
1334 bt_dwarf_cu_destroy(cu);
1335 if (_src_loc) {
1336 *src_loc = _src_loc;
1337 }
1338
1339 return 0;
1340
1341error:
1342 source_location_destroy(_src_loc);
1343 bt_dwarf_cu_destroy(cu);
1344 return -1;
1345}
This page took 0.078626 seconds and 4 git commands to generate.