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"
44 int bin_info_init(bt_logging_level log_level
, bt_self_component
*self_comp
)
48 if (elf_version(EV_CURRENT
) == EV_NONE
) {
49 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
50 "ELF library initialization failed: %s.",
58 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
59 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
60 const char *debug_info_dir
, const char *target_prefix
,
61 bt_logging_level log_level
, bt_self_component
*self_comp
)
63 struct bin_info
*bin
= NULL
;
71 bin
= g_new0(struct bin_info
, 1);
76 bin
->log_level
= log_level
;
77 bin
->self_comp
= self_comp
;
79 bin
->elf_path
= g_build_filename(target_prefix
, path
, NULL
);
81 bin
->elf_path
= g_strdup(path
);
89 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
90 if (!bin
->debug_info_dir
) {
97 bin
->low_addr
= low_addr
;
98 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
100 bin
->build_id_len
= 0;
101 bin
->file_build_id_matches
= false;
107 bin_info_destroy(bin
);
111 void bin_info_destroy(struct bin_info
*bin
)
117 dwarf_end(bin
->dwarf_info
);
119 g_free(bin
->debug_info_dir
);
120 g_free(bin
->elf_path
);
121 g_free(bin
->dwarf_path
);
122 g_free(bin
->build_id
);
123 g_free(bin
->dbg_link_filename
);
125 elf_end(bin
->elf_file
);
127 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
128 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
134 * Initialize the ELF file for a given executable.
136 * @param bin bin_info instance
137 * @returns 0 on success, negative value on error.
140 int bin_info_set_elf_file(struct bin_info
*bin
)
142 struct bt_fd_cache_handle
*elf_handle
= NULL
;
143 Elf
*elf_file
= NULL
;
148 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
150 BT_COMP_LOGI("Failed to open %s", bin
->elf_path
);
153 bin
->elf_handle
= elf_handle
;
155 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
158 BT_COMP_LOGE_APPEND_CAUSE(bin
->self_comp
,
159 "elf_begin failed: %s", elf_errmsg(-1));
163 bin
->elf_file
= elf_file
;
165 if (elf_kind(elf_file
) != ELF_K_ELF
) {
166 BT_COMP_LOGE_APPEND_CAUSE(bin
->self_comp
,
167 "Error: %s is not an ELF object", bin
->elf_path
);
176 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
185 * From a note section data struct, check if it is a build id note.
187 * @param note_data Pointer to a note section
189 * @returns 1 on match, 0 if `buf` does not contain a
190 * valid build id note
193 int is_build_id_note_section(Elf_Data
*note_data
)
195 size_t name_offset
, desc_offset
;
196 GElf_Nhdr note_header
;
200 * Discard the return value as it contains the size of the note section
201 * and we don't need it.
203 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
207 * Check the note name length. The name_sz field includes the
208 * terminating null byte.
210 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
214 /* Check the note type. */
215 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
219 /* Check the note name. */
220 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
221 note_header
.n_namesz
) != 0) {
232 * From a build id note section data struct, check if the build id it contains
233 * is identical to the build id passed as parameter.
235 * @param note_data Pointer to the file build id note section.
236 * @param build_id Pointer to a build id to compare to.
237 * @param build_id_len length of the build id.
239 * @returns 1 on match, 0 otherwise.
242 int is_build_id_note_section_matching(Elf_Data
*note_data
,
243 uint8_t *build_id
, size_t build_id_len
)
245 size_t name_offset
, desc_offset
;
246 GElf_Nhdr note_header
;
248 if (build_id_len
<= 0) {
253 * Discard the return value as it contains the size of the note section
254 * and we don't need it.
256 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
260 * Compare the binary build id with the supplied build id.
262 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
263 build_id_len
) == 0) {
271 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
272 * id of the ondisk file (bin->elf_file).
274 * @param bin bin_info instance
275 * @param build_id build id to compare ot the on disk file
276 * @param build_id_len length of the build id
278 * @returns 1 on if the build id of stored in `bin` matches
279 * the build id of the ondisk file.
280 * 0 on if they are different or an error occurred.
283 int is_build_id_matching(struct bin_info
*bin
)
285 int ret
, is_build_id
, is_matching
= 0;
286 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
287 GElf_Shdr curr_section_hdr
;
289 if (!bin
->build_id
) {
293 /* Set ELF file if it hasn't been accessed yet. */
294 if (!bin
->elf_file
) {
295 ret
= bin_info_set_elf_file(bin
);
297 /* Failed to set ELF file. */
302 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
307 while (next_section
) {
308 Elf_Data
*note_data
= NULL
;
310 curr_section
= next_section
;
311 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
313 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
317 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
322 * elf_getdata() translates the data to native byte order.
324 note_data
= elf_getdata(curr_section
, NULL
);
329 /* Check if the note is of the build-id type. */
330 is_build_id
= is_build_id_note_section(note_data
);
336 * Compare the build id of the on-disk file and
337 * the build id recorded in the trace.
339 is_matching
= is_build_id_note_section_matching(
340 note_data
, bin
->build_id
, bin
->build_id_len
);
349 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
352 if (!bin
|| !build_id
) {
356 /* Free any previously set build id. */
357 g_free(bin
->build_id
);
359 /* Set the build id. */
360 bin
->build_id
= g_new0(uint8_t, build_id_len
);
361 if (!bin
->build_id
) {
365 memcpy(bin
->build_id
, build_id
, build_id_len
);
366 bin
->build_id_len
= build_id_len
;
369 * Check if the file found on the file system has the same build id
370 * that what was recorded in the trace.
372 bin
->file_build_id_matches
= is_build_id_matching(bin
);
373 if (!bin
->file_build_id_matches
) {
374 BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the "
375 "binary or library found on the file system.");
380 * Reset the is_elf_only flag in case it had been set
381 * previously, because we might find separate debug info using
382 * the new build id information.
384 bin
->is_elf_only
= false;
392 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
395 if (!bin
|| !filename
) {
399 bin
->dbg_link_filename
= g_strdup(filename
);
400 if (!bin
->dbg_link_filename
) {
404 bin
->dbg_link_crc
= crc
;
407 * Reset the is_elf_only flag in case it had been set
408 * previously, because we might find separate debug info using
409 * the new build id information.
411 bin
->is_elf_only
= false;
421 * Tries to read DWARF info from the location given by path, and
422 * attach it to the given bin_info instance if it exists.
424 * @param bin bin_info instance for which to set DWARF info
425 * @param path Presumed location of the DWARF info
426 * @returns 0 on success, negative value on failure
429 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
432 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
433 struct bt_dwarf_cu
*cu
= NULL
;
434 Dwarf
*dwarf_info
= NULL
;
440 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
445 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
452 * Check if the dwarf info has any CU. If not, the
453 * executable's object file contains no DWARF info.
455 cu
= bt_dwarf_cu_create(dwarf_info
);
460 ret
= bt_dwarf_cu_next(cu
);
465 bin
->dwarf_handle
= dwarf_handle
;
466 bin
->dwarf_path
= g_strdup(path
);
467 if (!bin
->dwarf_path
) {
470 bin
->dwarf_info
= dwarf_info
;
477 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
479 dwarf_end(dwarf_info
);
487 * Try to set the dwarf_info for a given bin_info instance via the
490 * @param bin bin_info instance for which to retrieve the
491 * DWARF info via build ID
492 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
495 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
498 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
499 const char *dbg_dir
= NULL
;
500 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
502 if (!bin
|| !bin
->build_id
) {
506 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
509 * The prefix dir is the first byte of the build id, represented in
510 * lowercase hex as two characters per byte, +1 for '\0'.
512 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
513 if (!build_id_prefix_dir
) {
516 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
519 * The build id file is the remaining bytes of the build id,
520 * represented in lowercase hex, as two characters per byte.
522 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
524 /* To which the build id suffix is added, +1 for '\0'. */
525 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
528 * The resulting filename string is the concatenation of the
529 * hex build id and the suffix.
531 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
532 build_id_file
= g_new0(gchar
, build_id_file_len
);
533 if (!build_id_file
) {
538 * For each byte, starting at offset 1, append two characters
541 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
542 int path_idx
= 2 * (i
- 1);
544 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
546 /* Append the suffix to the generated string, including the '\0'. */
547 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
550 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
555 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
565 g_free(build_id_prefix_dir
);
566 g_free(build_id_file
);
573 * Tests whether the file located at path exists and has the expected
576 * This predicate is used when looking up separate debug info via the
577 * GNU debuglink method. The expected crc can be found .gnu_debuglink
578 * section in the original ELF file, along with the filename for the
579 * file containing the debug info.
581 * @param path Full path at which to look for the debug file
582 * @param crc Expected checksum for the debug file
583 * @returns 1 if the file exists and has the correct checksum,
587 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
590 struct bt_fd_cache_handle
*debug_handle
= NULL
;
597 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
602 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
611 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
616 * Try to set the dwarf_info for a given bin_info instance via the
619 * @param bin bin_info instance for which to retrieve the
620 * DWARF info via debug link
621 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
624 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
627 const gchar
*dbg_dir
= NULL
;
628 gchar
*bin_dir
= NULL
, *path
= NULL
;
630 if (!bin
|| !bin
->dbg_link_filename
) {
634 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
635 bin_dir
= g_path_get_dirname(bin
->elf_path
);
637 /* First look in the executable's dir */
638 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
640 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
644 /* If not found, look in .debug subdir */
646 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
648 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
652 /* Lastly, look under the global debug directory */
655 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
656 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
669 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
678 * Initialize the DWARF info for a given executable.
680 * @param bin bin_info instance
681 * @returns 0 on success, negative value on failure
684 int bin_info_set_dwarf_info(struct bin_info
*bin
)
693 /* First try to set the DWARF info from the ELF file */
694 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
700 * If that fails, try to find separate debug info via build ID
703 ret
= bin_info_set_dwarf_info_build_id(bin
);
708 ret
= bin_info_set_dwarf_info_debug_link(bin
);
717 void source_location_destroy(struct source_location
*src_loc
)
723 free(src_loc
->filename
);
728 * Append a string representation of an address offset to an existing
731 * On success, the out parameter `result` will contain the base string
732 * followed by the offset string of the form "+0x1234". On failure,
733 * `result` remains unchanged.
735 * @param base_str The string to which to append an offset string
736 * @param low_addr The lower virtual memory address, the base from
737 * which the offset is computed
738 * @param high_addr The higher virtual memory address
739 * @param result Out parameter, the base string followed by the
741 * @returns 0 on success, -1 on failure
744 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
745 uint64_t high_addr
, char **result
)
748 char *_result
= NULL
;
750 if (!base_str
|| !result
) {
754 offset
= high_addr
- low_addr
;
756 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
770 * Try to find the symbol closest to an address within a given ELF
773 * Only function symbols are taken into account. The symbol's address
774 * must precede `addr`. A symbol with a closer address might exist
775 * after `addr` but is irrelevant because it cannot encompass `addr`.
777 * On success, if found, the out parameters `sym` and `shdr` are
778 * set. On failure or if none are found, they remain unchanged.
780 * @param scn ELF section in which to look for the address
781 * @param addr Virtual memory address for which to find the
782 * nearest function symbol
783 * @param sym Out parameter, the nearest function symbol
784 * @param shdr Out parameter, the section header for scn
785 * @returns 0 on success, -1 on failure
788 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
789 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
793 Elf_Data
*data
= NULL
;
794 GElf_Shdr
*_shdr
= NULL
;
795 GElf_Sym
*nearest_sym
= NULL
;
797 if (!scn
|| !sym
|| !shdr
) {
801 _shdr
= g_new0(GElf_Shdr
, 1);
806 _shdr
= gelf_getshdr(scn
, _shdr
);
811 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
813 * We are only interested in symbol table (symtab)
814 * sections, skip this one.
819 data
= elf_getdata(scn
, NULL
);
824 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
826 for (i
= 0; i
< symbol_count
; ++i
) {
827 GElf_Sym
*cur_sym
= NULL
;
829 cur_sym
= g_new0(GElf_Sym
, 1);
833 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
837 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
838 /* We're only interested in the functions. */
843 if (cur_sym
->st_value
<= addr
&&
845 cur_sym
->st_value
> nearest_sym
->st_value
)) {
847 nearest_sym
= cur_sym
;
870 * Get the name of the function containing a given address within an
871 * executable using ELF symbols.
873 * The function name is in fact the name of the nearest ELF symbol,
874 * followed by the offset in bytes between the address and the symbol
875 * (in hex), separated by a '+' character.
877 * If found, the out parameter `func_name` is set on success. On failure,
878 * it remains unchanged.
880 * @param bin bin_info instance for the executable containing
882 * @param addr Virtual memory address for which to find the
884 * @param func_name Out parameter, the function name
885 * @returns 0 on success, -1 on failure
888 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
892 * TODO (possible optimisation): if an ELF has no symtab
893 * section, it has been stripped. Therefore, it would be wise
894 * to store a flag indicating the stripped status after the
895 * first iteration to prevent subsequent ones.
899 GElf_Sym
*sym
= NULL
;
900 GElf_Shdr
*shdr
= NULL
;
901 char *sym_name
= NULL
;
903 /* Set ELF file if it hasn't been accessed yet. */
904 if (!bin
->elf_file
) {
905 ret
= bin_info_set_elf_file(bin
);
907 /* Failed to set ELF file. */
912 scn
= elf_nextscn(bin
->elf_file
, scn
);
917 while (scn
&& !sym
) {
918 ret
= bin_info_get_nearest_symbol_from_section(
919 scn
, addr
, &sym
, &shdr
);
924 scn
= elf_nextscn(bin
->elf_file
, scn
);
928 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
934 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
952 * Get the name of the function containing a given address within a
953 * given compile unit (CU).
955 * If found, the out parameter `func_name` is set on success. On
956 * failure, it remains unchanged.
958 * @param cu bt_dwarf_cu instance which may contain the address
959 * @param addr Virtual memory address for which to find the
961 * @param func_name Out parameter, the function name
962 * @returns 0 on success, -1 on failure
965 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
970 struct bt_dwarf_die
*die
= NULL
;
972 if (!cu
|| !func_name
) {
976 die
= bt_dwarf_die_create(cu
);
981 while (bt_dwarf_die_next(die
) == 0) {
984 ret
= bt_dwarf_die_get_tag(die
, &tag
);
989 if (tag
== DW_TAG_subprogram
) {
990 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1002 uint64_t low_addr
= 0;
1003 char *die_name
= NULL
;
1005 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1010 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1016 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1024 bt_dwarf_die_destroy(die
);
1028 bt_dwarf_die_destroy(die
);
1033 * Get the name of the function containing a given address within an
1034 * executable using DWARF debug info.
1036 * If found, the out parameter `func_name` is set on success. On
1037 * failure, it remains unchanged.
1039 * @param bin bin_info instance for the executable containing
1041 * @param addr Virtual memory address for which to find the
1043 * @param func_name Out parameter, the function name
1044 * @returns 0 on success, -1 on failure
1047 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1051 char *_func_name
= NULL
;
1052 struct bt_dwarf_cu
*cu
= NULL
;
1054 if (!bin
|| !func_name
) {
1058 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1063 while (bt_dwarf_cu_next(cu
) == 0) {
1064 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1075 *func_name
= _func_name
;
1080 bt_dwarf_cu_destroy(cu
);
1084 bt_dwarf_cu_destroy(cu
);
1088 int bin_info_lookup_function_name(struct bin_info
*bin
,
1089 uint64_t addr
, char **func_name
)
1092 char *_func_name
= NULL
;
1094 if (!bin
|| !func_name
) {
1099 * If the bin_info has a build id but it does not match the build id
1100 * that was found on the file system, return an error.
1102 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1106 /* Set DWARF info if it hasn't been accessed yet. */
1107 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1108 ret
= bin_info_set_dwarf_info(bin
);
1110 BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling "
1111 "back to ELF lookup.");
1112 /* Failed to set DWARF info, fallback to ELF. */
1113 bin
->is_elf_only
= true;
1117 if (!bin_info_has_address(bin
, addr
)) {
1122 * Addresses in ELF and DWARF are relative to base address for
1123 * PIC, so make the address argument relative too if needed.
1126 addr
-= bin
->low_addr
;
1129 if (bin
->is_elf_only
) {
1130 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1133 BT_COMP_LOGI("Failed to lookup function name (ELF): "
1137 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1140 BT_COMP_LOGI("Failed to lookup function name (DWARF): "
1145 *func_name
= _func_name
;
1152 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1154 gchar
*_bin_loc
= NULL
;
1156 if (!bin
|| !bin_loc
) {
1161 * If the bin_info has a build id but it does not match the build id
1162 * that was found on the file system, return an error.
1164 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1169 addr
-= bin
->low_addr
;
1170 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1172 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1179 *bin_loc
= _bin_loc
;
1187 * Predicate used to determine whether the children of a given DIE
1188 * contain a specific address.
1190 * More specifically, the parameter `die` is expected to be a
1191 * subprogram (function) DIE, and this predicate tells whether any
1192 * subroutines are inlined within this function and would contain
1195 * On success, the out parameter `contains` is set with the boolean
1196 * value indicating whether the DIE's range covers `addr`. On failure,
1197 * it remains unchanged.
1199 * Do note that this function advances the position of `die`. If the
1200 * address is found within one of its children, `die` will be pointing
1201 * to that child upon returning from the function, allowing to extract
1202 * the information deemed necessary.
1204 * @param die The parent DIE in whose children the address will be
1206 * @param addr The address for which to look for in the DIEs
1207 * @param contains Out parameter, true if addr is contained,
1209 * @returns Returns 0 on success, -1 on failure
1212 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1215 bool _contains
= false;
1221 ret
= bt_dwarf_die_child(die
);
1227 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1234 * The address is within the range of the current DIE
1239 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1244 if (tag
== DW_TAG_inlined_subroutine
) {
1245 /* Found the tracepoint. */
1249 if (bt_dwarf_die_has_children(die
)) {
1251 * Look for the address in the children DIEs.
1253 ret
= bt_dwarf_die_child(die
);
1259 } while (bt_dwarf_die_next(die
) == 0);
1262 *contains
= _contains
;
1270 * Lookup the source location for a given address within a CU, making
1271 * the assumption that it is contained within an inline routine in a
1274 * @param cu bt_dwarf_cu instance in which to look for the address
1275 * @param addr The address for which to look for
1276 * @param src_loc Out parameter, the source location (filename and
1277 * line number) for the address
1278 * @returns 0 on success, -1 on failure
1281 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1282 struct source_location
**src_loc
)
1286 struct bt_dwarf_die
*die
= NULL
;
1287 struct source_location
*_src_loc
= NULL
;
1289 if (!cu
|| !src_loc
) {
1293 die
= bt_dwarf_die_create(cu
);
1298 while (bt_dwarf_die_next(die
) == 0) {
1301 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1306 if (tag
== DW_TAG_subprogram
) {
1307 bool contains
= false;
1309 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1316 * Try to find an inlined subroutine
1317 * child of this DIE containing addr.
1319 ret
= bin_info_child_die_has_address(die
, addr
,
1332 char *filename
= NULL
;
1335 _src_loc
= g_new0(struct source_location
, 1);
1340 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1344 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1350 _src_loc
->filename
= filename
;
1351 _src_loc
->line_no
= line_no
;
1352 *src_loc
= _src_loc
;
1355 bt_dwarf_die_destroy(die
);
1359 source_location_destroy(_src_loc
);
1360 bt_dwarf_die_destroy(die
);
1365 * Lookup the source location for a given address within a CU,
1366 * assuming that it is contained within an inlined function.
1368 * A source location can be found regardless of inlining status for
1369 * this method, but in the case of an inlined function, the returned
1370 * source location will point not to the callsite but rather to the
1371 * definition site of the inline function.
1373 * @param cu bt_dwarf_cu instance in which to look for the address
1374 * @param addr The address for which to look for
1375 * @param src_loc Out parameter, the source location (filename and
1376 * line number) for the address. Set only if the address
1377 * is found and resolved successfully
1379 * @returns 0 on success, -1 on failure
1382 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1383 struct source_location
**src_loc
)
1385 struct source_location
*_src_loc
= NULL
;
1386 struct bt_dwarf_die
*die
= NULL
;
1387 const char *filename
= NULL
;
1388 Dwarf_Line
*line
= NULL
;
1389 Dwarf_Addr line_addr
;
1390 int ret
= 0, line_no
;
1392 if (!cu
|| !src_loc
) {
1396 die
= bt_dwarf_die_create(cu
);
1401 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1403 /* This is not an error. The caller needs to keep looking. */
1407 ret
= dwarf_lineaddr(line
, &line_addr
);
1412 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1417 if (addr
== line_addr
) {
1418 _src_loc
= g_new0(struct source_location
, 1);
1423 ret
= dwarf_lineno(line
, &line_no
);
1428 _src_loc
->line_no
= line_no
;
1429 _src_loc
->filename
= g_strdup(filename
);
1433 *src_loc
= _src_loc
;
1439 source_location_destroy(_src_loc
);
1442 bt_dwarf_die_destroy(die
);
1447 * Get the source location (file name and line number) for a given
1448 * address within a compile unit (CU).
1450 * On success, the out parameter `src_loc` is set if found. On
1451 * failure, it remains unchanged.
1453 * @param cu bt_dwarf_cu instance for the compile unit which
1454 * may contain the address
1455 * @param addr Virtual memory address for which to find the
1457 * @param src_loc Out parameter, the source location
1458 * @returns 0 on success, -1 on failure
1461 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1462 struct source_location
**src_loc
)
1465 struct source_location
*_src_loc
= NULL
;
1467 if (!cu
|| !src_loc
) {
1471 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1480 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1491 *src_loc
= _src_loc
;
1497 source_location_destroy(_src_loc
);
1501 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1502 struct source_location
**src_loc
)
1504 struct bt_dwarf_cu
*cu
= NULL
;
1505 struct source_location
*_src_loc
= NULL
;
1507 if (!bin
|| !src_loc
) {
1512 * If the bin_info has a build id but it does not match the build id
1513 * that was found on the file system, return an error.
1515 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1519 /* Set DWARF info if it hasn't been accessed yet. */
1520 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1521 if (bin_info_set_dwarf_info(bin
)) {
1522 /* Failed to set DWARF info. */
1523 bin
->is_elf_only
= true;
1527 if (bin
->is_elf_only
) {
1528 /* We cannot lookup source location without DWARF info. */
1532 if (!bin_info_has_address(bin
, addr
)) {
1537 * Addresses in ELF and DWARF are relative to base address for
1538 * PIC, so make the address argument relative too if needed.
1541 addr
-= bin
->low_addr
;
1544 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1549 while (bt_dwarf_cu_next(cu
) == 0) {
1552 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1562 bt_dwarf_cu_destroy(cu
);
1564 *src_loc
= _src_loc
;
1570 source_location_destroy(_src_loc
);
1571 bt_dwarf_cu_destroy(cu
);