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