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