2 * SPDX-License-Identifier: MIT
4 * Babeltrace - Executable and Shared Object Debug Info Reader
6 * Copyright 2015 Antoine Busque <abusque@efficios.com>
9 #define BT_COMP_LOG_SELF_COMP (bin->self_comp)
10 #define BT_LOG_OUTPUT_LEVEL (bin->log_level)
11 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/BIN-INFO"
12 #include "logging/comp-logging.h"
14 #include <babeltrace2/logging.h>
29 #include "common/common.h"
37 * An address printed in hex is at most 20 bytes (16 for 64-bits +
38 * leading 0x + optional leading '+' if addr is an offset + null
41 #define ADDR_STR_LEN 20
42 #define BUILD_ID_NOTE_NAME "GNU"
45 int bin_info_init(bt_logging_level log_level
, bt_self_component
*self_comp
)
49 if (elf_version(EV_CURRENT
) == EV_NONE
) {
50 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
51 "ELF library initialization failed: %s.",
60 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
61 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
62 const char *debug_info_dir
, const char *target_prefix
,
63 bt_logging_level log_level
, bt_self_component
*self_comp
)
65 struct bin_info
*bin
= NULL
;
73 bin
= g_new0(struct bin_info
, 1);
78 bin
->log_level
= log_level
;
79 bin
->self_comp
= self_comp
;
81 bin
->elf_path
= g_build_filename(target_prefix
, path
, NULL
);
83 bin
->elf_path
= g_strdup(path
);
91 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
92 if (!bin
->debug_info_dir
) {
99 bin
->low_addr
= low_addr
;
100 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
101 bin
->build_id
= NULL
;
102 bin
->build_id_len
= 0;
103 bin
->file_build_id_matches
= false;
109 bin_info_destroy(bin
);
114 void bin_info_destroy(struct bin_info
*bin
)
120 dwarf_end(bin
->dwarf_info
);
122 g_free(bin
->debug_info_dir
);
123 g_free(bin
->elf_path
);
124 g_free(bin
->dwarf_path
);
125 g_free(bin
->build_id
);
126 g_free(bin
->dbg_link_filename
);
128 elf_end(bin
->elf_file
);
130 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
131 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
137 * Initialize the ELF file for a given executable.
139 * @param bin bin_info instance
140 * @returns 0 on success, negative value on error.
143 int bin_info_set_elf_file(struct bin_info
*bin
)
145 struct bt_fd_cache_handle
*elf_handle
= NULL
;
146 Elf
*elf_file
= NULL
;
151 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
153 BT_COMP_LOGI("Failed to open %s", bin
->elf_path
);
156 bin
->elf_handle
= elf_handle
;
158 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
161 BT_COMP_LOGE_APPEND_CAUSE(bin
->self_comp
,
162 "elf_begin failed: %s", elf_errmsg(-1));
166 bin
->elf_file
= elf_file
;
168 if (elf_kind(elf_file
) != ELF_K_ELF
) {
169 BT_COMP_LOGE_APPEND_CAUSE(bin
->self_comp
,
170 "Error: %s is not an ELF object", bin
->elf_path
);
179 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
188 * From a note section data struct, check if it is a build id note.
190 * @param note_data Pointer to a note section
192 * @returns 1 on match, 0 if `buf` does not contain a
193 * valid build id note
196 int is_build_id_note_section(Elf_Data
*note_data
)
198 size_t name_offset
, desc_offset
;
199 GElf_Nhdr note_header
;
203 * Discard the return value as it contains the size of the note section
204 * and we don't need it.
206 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
210 * Check the note name length. The name_sz field includes the
211 * terminating null byte.
213 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
217 /* Check the note type. */
218 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
222 /* Check the note name. */
223 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
224 note_header
.n_namesz
) != 0) {
235 * From a build id note section data struct, check if the build id it contains
236 * is identical to the build id passed as parameter.
238 * @param note_data Pointer to the file build id note section.
239 * @param build_id Pointer to a build id to compare to.
240 * @param build_id_len length of the build id.
242 * @returns 1 on match, 0 otherwise.
245 int is_build_id_note_section_matching(Elf_Data
*note_data
,
246 uint8_t *build_id
, size_t build_id_len
)
248 size_t name_offset
, desc_offset
;
249 GElf_Nhdr note_header
;
251 if (build_id_len
<= 0) {
256 * Discard the return value as it contains the size of the note section
257 * and we don't need it.
259 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
263 * Compare the binary build id with the supplied build id.
265 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
266 build_id_len
) == 0) {
274 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
275 * id of the ondisk file (bin->elf_file).
277 * @param bin bin_info instance
278 * @param build_id build id to compare ot the on disk file
279 * @param build_id_len length of the build id
281 * @returns 1 on if the build id of stored in `bin` matches
282 * the build id of the ondisk file.
283 * 0 on if they are different or an error occurred.
286 int is_build_id_matching(struct bin_info
*bin
)
288 int ret
, is_build_id
, is_matching
= 0;
289 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
290 GElf_Shdr curr_section_hdr
;
292 if (!bin
->build_id
) {
296 /* Set ELF file if it hasn't been accessed yet. */
297 if (!bin
->elf_file
) {
298 ret
= bin_info_set_elf_file(bin
);
300 /* Failed to set ELF file. */
305 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
310 while (next_section
) {
311 Elf_Data
*note_data
= NULL
;
313 curr_section
= next_section
;
314 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
316 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
320 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
325 * elf_getdata() translates the data to native byte order.
327 note_data
= elf_getdata(curr_section
, NULL
);
332 /* Check if the note is of the build-id type. */
333 is_build_id
= is_build_id_note_section(note_data
);
339 * Compare the build id of the on-disk file and
340 * the build id recorded in the trace.
342 is_matching
= is_build_id_note_section_matching(
343 note_data
, bin
->build_id
, bin
->build_id_len
);
353 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
356 if (!bin
|| !build_id
) {
360 /* Free any previously set build id. */
361 g_free(bin
->build_id
);
363 /* Set the build id. */
364 bin
->build_id
= g_new0(uint8_t, build_id_len
);
365 if (!bin
->build_id
) {
369 memcpy(bin
->build_id
, build_id
, build_id_len
);
370 bin
->build_id_len
= build_id_len
;
373 * Check if the file found on the file system has the same build id
374 * that what was recorded in the trace.
376 bin
->file_build_id_matches
= is_build_id_matching(bin
);
377 if (!bin
->file_build_id_matches
) {
378 BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the "
379 "binary or library found on the file system.");
384 * Reset the is_elf_only flag in case it had been set
385 * previously, because we might find separate debug info using
386 * the new build id information.
388 bin
->is_elf_only
= false;
397 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
400 if (!bin
|| !filename
) {
404 bin
->dbg_link_filename
= g_strdup(filename
);
405 if (!bin
->dbg_link_filename
) {
409 bin
->dbg_link_crc
= crc
;
412 * Reset the is_elf_only flag in case it had been set
413 * previously, because we might find separate debug info using
414 * the new build id information.
416 bin
->is_elf_only
= false;
426 * Tries to read DWARF info from the location given by path, and
427 * attach it to the given bin_info instance if it exists.
429 * @param bin bin_info instance for which to set DWARF info
430 * @param path Presumed location of the DWARF info
431 * @returns 0 on success, negative value on failure
434 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
437 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
438 struct bt_dwarf_cu
*cu
= NULL
;
439 Dwarf
*dwarf_info
= NULL
;
445 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
450 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
457 * Check if the dwarf info has any CU. If not, the
458 * executable's object file contains no DWARF info.
460 cu
= bt_dwarf_cu_create(dwarf_info
);
465 ret
= bt_dwarf_cu_next(cu
);
470 bin
->dwarf_handle
= dwarf_handle
;
471 bin
->dwarf_path
= g_strdup(path
);
472 if (!bin
->dwarf_path
) {
475 bin
->dwarf_info
= dwarf_info
;
482 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
484 dwarf_end(dwarf_info
);
492 * Try to set the dwarf_info for a given bin_info instance via the
495 * @param bin bin_info instance for which to retrieve the
496 * DWARF info via build ID
497 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
500 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
503 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
504 const char *dbg_dir
= NULL
;
505 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
507 if (!bin
|| !bin
->build_id
) {
511 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
514 * The prefix dir is the first byte of the build id, represented in
515 * lowercase hex as two characters per byte, +1 for '\0'.
517 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
518 if (!build_id_prefix_dir
) {
521 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
524 * The build id file is the remaining bytes of the build id,
525 * represented in lowercase hex, as two characters per byte.
527 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
529 /* To which the build id suffix is added, +1 for '\0'. */
530 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
533 * The resulting filename string is the concatenation of the
534 * hex build id and the suffix.
536 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
537 build_id_file
= g_new0(gchar
, build_id_file_len
);
538 if (!build_id_file
) {
543 * For each byte, starting at offset 1, append two characters
546 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
547 int path_idx
= 2 * (i
- 1);
549 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
551 /* Append the suffix to the generated string, including the '\0'. */
552 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
555 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
560 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
570 g_free(build_id_prefix_dir
);
571 g_free(build_id_file
);
578 * Tests whether the file located at path exists and has the expected
581 * This predicate is used when looking up separate debug info via the
582 * GNU debuglink method. The expected crc can be found .gnu_debuglink
583 * section in the original ELF file, along with the filename for the
584 * file containing the debug info.
586 * @param path Full path at which to look for the debug file
587 * @param crc Expected checksum for the debug file
588 * @returns 1 if the file exists and has the correct checksum,
592 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
595 struct bt_fd_cache_handle
*debug_handle
= NULL
;
602 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
607 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
616 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
621 * Try to set the dwarf_info for a given bin_info instance via the
624 * @param bin bin_info instance for which to retrieve the
625 * DWARF info via debug link
626 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
629 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
632 const gchar
*dbg_dir
= NULL
;
633 gchar
*bin_dir
= NULL
, *path
= NULL
;
635 if (!bin
|| !bin
->dbg_link_filename
) {
639 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
640 bin_dir
= g_path_get_dirname(bin
->elf_path
);
642 /* First look in the executable's dir */
643 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
645 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
649 /* If not found, look in .debug subdir */
651 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
653 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
657 /* Lastly, look under the global debug directory */
660 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
661 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
674 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
683 * Initialize the DWARF info for a given executable.
685 * @param bin bin_info instance
686 * @returns 0 on success, negative value on failure
689 int bin_info_set_dwarf_info(struct bin_info
*bin
)
698 /* First try to set the DWARF info from the ELF file */
699 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
705 * If that fails, try to find separate debug info via build ID
708 ret
= bin_info_set_dwarf_info_build_id(bin
);
713 ret
= bin_info_set_dwarf_info_debug_link(bin
);
723 void source_location_destroy(struct source_location
*src_loc
)
729 free(src_loc
->filename
);
734 * Append a string representation of an address offset to an existing
737 * On success, the out parameter `result` will contain the base string
738 * followed by the offset string of the form "+0x1234". On failure,
739 * `result` remains unchanged.
741 * @param base_str The string to which to append an offset string
742 * @param low_addr The lower virtual memory address, the base from
743 * which the offset is computed
744 * @param high_addr The higher virtual memory address
745 * @param result Out parameter, the base string followed by the
747 * @returns 0 on success, -1 on failure
750 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
751 uint64_t high_addr
, char **result
)
754 char *_result
= NULL
;
756 if (!base_str
|| !result
) {
760 offset
= high_addr
- low_addr
;
762 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
776 * Try to find the symbol closest to an address within a given ELF
779 * Only function symbols are taken into account. The symbol's address
780 * must precede `addr`. A symbol with a closer address might exist
781 * after `addr` but is irrelevant because it cannot encompass `addr`.
783 * On success, if found, the out parameters `sym` and `shdr` are
784 * set. On failure or if none are found, they remain unchanged.
786 * @param scn ELF section in which to look for the address
787 * @param addr Virtual memory address for which to find the
788 * nearest function symbol
789 * @param sym Out parameter, the nearest function symbol
790 * @param shdr Out parameter, the section header for scn
791 * @returns 0 on success, -1 on failure
794 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
795 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
799 Elf_Data
*data
= NULL
;
800 GElf_Shdr
*_shdr
= NULL
;
801 GElf_Sym
*nearest_sym
= NULL
;
803 if (!scn
|| !sym
|| !shdr
) {
807 _shdr
= g_new0(GElf_Shdr
, 1);
812 _shdr
= gelf_getshdr(scn
, _shdr
);
817 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
819 * We are only interested in symbol table (symtab)
820 * sections, skip this one.
825 data
= elf_getdata(scn
, NULL
);
830 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
832 for (i
= 0; i
< symbol_count
; ++i
) {
833 GElf_Sym
*cur_sym
= NULL
;
835 cur_sym
= g_new0(GElf_Sym
, 1);
839 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
843 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
844 /* We're only interested in the functions. */
849 if (cur_sym
->st_value
<= addr
&&
851 cur_sym
->st_value
> nearest_sym
->st_value
)) {
853 nearest_sym
= cur_sym
;
876 * Get the name of the function containing a given address within an
877 * executable using ELF symbols.
879 * The function name is in fact the name of the nearest ELF symbol,
880 * followed by the offset in bytes between the address and the symbol
881 * (in hex), separated by a '+' character.
883 * If found, the out parameter `func_name` is set on success. On failure,
884 * it remains unchanged.
886 * @param bin bin_info instance for the executable containing
888 * @param addr Virtual memory address for which to find the
890 * @param func_name Out parameter, the function name
891 * @returns 0 on success, -1 on failure
894 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
898 * TODO (possible optimisation): if an ELF has no symtab
899 * section, it has been stripped. Therefore, it would be wise
900 * to store a flag indicating the stripped status after the
901 * first iteration to prevent subsequent ones.
905 GElf_Sym
*sym
= NULL
;
906 GElf_Shdr
*shdr
= NULL
;
907 char *sym_name
= NULL
;
909 /* Set ELF file if it hasn't been accessed yet. */
910 if (!bin
->elf_file
) {
911 ret
= bin_info_set_elf_file(bin
);
913 /* Failed to set ELF file. */
918 scn
= elf_nextscn(bin
->elf_file
, scn
);
923 while (scn
&& !sym
) {
924 ret
= bin_info_get_nearest_symbol_from_section(
925 scn
, addr
, &sym
, &shdr
);
930 scn
= elf_nextscn(bin
->elf_file
, scn
);
934 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
940 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
958 * Get the name of the function containing a given address within a
959 * given compile unit (CU).
961 * If found, the out parameter `func_name` is set on success. On
962 * failure, it remains unchanged.
964 * @param cu bt_dwarf_cu instance which may contain the address
965 * @param addr Virtual memory address for which to find the
967 * @param func_name Out parameter, the function name
968 * @returns 0 on success, -1 on failure
971 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
976 struct bt_dwarf_die
*die
= NULL
;
978 if (!cu
|| !func_name
) {
982 die
= bt_dwarf_die_create(cu
);
987 while (bt_dwarf_die_next(die
) == 0) {
990 ret
= bt_dwarf_die_get_tag(die
, &tag
);
995 if (tag
== DW_TAG_subprogram
) {
996 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1008 uint64_t low_addr
= 0;
1009 char *die_name
= NULL
;
1011 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1016 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1022 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1030 bt_dwarf_die_destroy(die
);
1034 bt_dwarf_die_destroy(die
);
1039 * Get the name of the function containing a given address within an
1040 * executable using DWARF debug info.
1042 * If found, the out parameter `func_name` is set on success. On
1043 * failure, it remains unchanged.
1045 * @param bin bin_info instance for the executable containing
1047 * @param addr Virtual memory address for which to find the
1049 * @param func_name Out parameter, the function name
1050 * @returns 0 on success, -1 on failure
1053 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1057 char *_func_name
= NULL
;
1058 struct bt_dwarf_cu
*cu
= NULL
;
1060 if (!bin
|| !func_name
) {
1064 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1069 while (bt_dwarf_cu_next(cu
) == 0) {
1070 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1081 *func_name
= _func_name
;
1086 bt_dwarf_cu_destroy(cu
);
1090 bt_dwarf_cu_destroy(cu
);
1095 int bin_info_lookup_function_name(struct bin_info
*bin
,
1096 uint64_t addr
, char **func_name
)
1099 char *_func_name
= NULL
;
1101 if (!bin
|| !func_name
) {
1106 * If the bin_info has a build id but it does not match the build id
1107 * that was found on the file system, return an error.
1109 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1113 /* Set DWARF info if it hasn't been accessed yet. */
1114 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1115 ret
= bin_info_set_dwarf_info(bin
);
1117 BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling "
1118 "back to ELF lookup.");
1119 /* Failed to set DWARF info, fallback to ELF. */
1120 bin
->is_elf_only
= true;
1124 if (!bin_info_has_address(bin
, addr
)) {
1129 * Addresses in ELF and DWARF are relative to base address for
1130 * PIC, so make the address argument relative too if needed.
1133 addr
-= bin
->low_addr
;
1136 if (bin
->is_elf_only
) {
1137 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1140 BT_COMP_LOGI("Failed to lookup function name (ELF): "
1144 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1147 BT_COMP_LOGI("Failed to lookup function name (DWARF): "
1152 *func_name
= _func_name
;
1160 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1162 gchar
*_bin_loc
= NULL
;
1164 if (!bin
|| !bin_loc
) {
1169 * If the bin_info has a build id but it does not match the build id
1170 * that was found on the file system, return an error.
1172 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1177 addr
-= bin
->low_addr
;
1178 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1180 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1187 *bin_loc
= _bin_loc
;
1195 * Predicate used to determine whether the children of a given DIE
1196 * contain a specific address.
1198 * More specifically, the parameter `die` is expected to be a
1199 * subprogram (function) DIE, and this predicate tells whether any
1200 * subroutines are inlined within this function and would contain
1203 * On success, the out parameter `contains` is set with the boolean
1204 * value indicating whether the DIE's range covers `addr`. On failure,
1205 * it remains unchanged.
1207 * Do note that this function advances the position of `die`. If the
1208 * address is found within one of its children, `die` will be pointing
1209 * to that child upon returning from the function, allowing to extract
1210 * the information deemed necessary.
1212 * @param die The parent DIE in whose children the address will be
1214 * @param addr The address for which to look for in the DIEs
1215 * @param contains Out parameter, true if addr is contained,
1217 * @returns Returns 0 on success, -1 on failure
1220 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1223 bool _contains
= false;
1229 ret
= bt_dwarf_die_child(die
);
1235 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1242 * The address is within the range of the current DIE
1247 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1252 if (tag
== DW_TAG_inlined_subroutine
) {
1253 /* Found the tracepoint. */
1257 if (bt_dwarf_die_has_children(die
)) {
1259 * Look for the address in the children DIEs.
1261 ret
= bt_dwarf_die_child(die
);
1267 } while (bt_dwarf_die_next(die
) == 0);
1270 *contains
= _contains
;
1278 * Lookup the source location for a given address within a CU, making
1279 * the assumption that it is contained within an inline routine in a
1282 * @param cu bt_dwarf_cu instance in which to look for the address
1283 * @param addr The address for which to look for
1284 * @param src_loc Out parameter, the source location (filename and
1285 * line number) for the address
1286 * @returns 0 on success, -1 on failure
1289 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1290 struct source_location
**src_loc
)
1294 struct bt_dwarf_die
*die
= NULL
;
1295 struct source_location
*_src_loc
= NULL
;
1297 if (!cu
|| !src_loc
) {
1301 die
= bt_dwarf_die_create(cu
);
1306 while (bt_dwarf_die_next(die
) == 0) {
1309 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1314 if (tag
== DW_TAG_subprogram
) {
1315 bool contains
= false;
1317 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1324 * Try to find an inlined subroutine
1325 * child of this DIE containing addr.
1327 ret
= bin_info_child_die_has_address(die
, addr
,
1340 char *filename
= NULL
;
1343 _src_loc
= g_new0(struct source_location
, 1);
1348 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1352 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1358 _src_loc
->filename
= filename
;
1359 _src_loc
->line_no
= line_no
;
1360 *src_loc
= _src_loc
;
1363 bt_dwarf_die_destroy(die
);
1367 source_location_destroy(_src_loc
);
1368 bt_dwarf_die_destroy(die
);
1373 * Lookup the source location for a given address within a CU,
1374 * assuming that it is contained within an inlined function.
1376 * A source location can be found regardless of inlining status for
1377 * this method, but in the case of an inlined function, the returned
1378 * source location will point not to the callsite but rather to the
1379 * definition site of the inline function.
1381 * @param cu bt_dwarf_cu instance in which to look for the address
1382 * @param addr The address for which to look for
1383 * @param src_loc Out parameter, the source location (filename and
1384 * line number) for the address. Set only if the address
1385 * is found and resolved successfully
1387 * @returns 0 on success, -1 on failure
1390 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1391 struct source_location
**src_loc
)
1393 struct source_location
*_src_loc
= NULL
;
1394 struct bt_dwarf_die
*die
= NULL
;
1395 const char *filename
= NULL
;
1396 Dwarf_Line
*line
= NULL
;
1397 Dwarf_Addr line_addr
;
1398 int ret
= 0, line_no
;
1400 if (!cu
|| !src_loc
) {
1404 die
= bt_dwarf_die_create(cu
);
1409 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1411 /* This is not an error. The caller needs to keep looking. */
1415 ret
= dwarf_lineaddr(line
, &line_addr
);
1420 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1425 if (addr
== line_addr
) {
1426 _src_loc
= g_new0(struct source_location
, 1);
1431 ret
= dwarf_lineno(line
, &line_no
);
1436 _src_loc
->line_no
= line_no
;
1437 _src_loc
->filename
= g_strdup(filename
);
1441 *src_loc
= _src_loc
;
1447 source_location_destroy(_src_loc
);
1450 bt_dwarf_die_destroy(die
);
1455 * Get the source location (file name and line number) for a given
1456 * address within a compile unit (CU).
1458 * On success, the out parameter `src_loc` is set if found. On
1459 * failure, it remains unchanged.
1461 * @param cu bt_dwarf_cu instance for the compile unit which
1462 * may contain the address
1463 * @param addr Virtual memory address for which to find the
1465 * @param src_loc Out parameter, the source location
1466 * @returns 0 on success, -1 on failure
1469 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1470 struct source_location
**src_loc
)
1473 struct source_location
*_src_loc
= NULL
;
1475 if (!cu
|| !src_loc
) {
1479 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1488 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1499 *src_loc
= _src_loc
;
1505 source_location_destroy(_src_loc
);
1510 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1511 struct source_location
**src_loc
)
1513 struct bt_dwarf_cu
*cu
= NULL
;
1514 struct source_location
*_src_loc
= NULL
;
1516 if (!bin
|| !src_loc
) {
1521 * If the bin_info has a build id but it does not match the build id
1522 * that was found on the file system, return an error.
1524 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1528 /* Set DWARF info if it hasn't been accessed yet. */
1529 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1530 if (bin_info_set_dwarf_info(bin
)) {
1531 /* Failed to set DWARF info. */
1532 bin
->is_elf_only
= true;
1536 if (bin
->is_elf_only
) {
1537 /* We cannot lookup source location without DWARF info. */
1541 if (!bin_info_has_address(bin
, addr
)) {
1546 * Addresses in ELF and DWARF are relative to base address for
1547 * PIC, so make the address argument relative too if needed.
1550 addr
-= bin
->low_addr
;
1553 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1558 while (bt_dwarf_cu_next(cu
) == 0) {
1561 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1571 bt_dwarf_cu_destroy(cu
);
1573 *src_loc
= _src_loc
;
1579 source_location_destroy(_src_loc
);
1580 bt_dwarf_cu_destroy(cu
);