4 * Babeltrace - Executable and Shared Object Debug Info Reader
6 * Copyright 2015 Antoine Busque <abusque@efficios.com>
8 * Author: Antoine Busque <abusque@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_OUTPUT_LEVEL (bin->log_level)
30 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/BIN-INFO"
31 #include "logging/log.h"
33 #include <babeltrace2/logging.h>
47 #include "common/common.h"
55 * An address printed in hex is at most 20 bytes (16 for 64-bits +
56 * leading 0x + optional leading '+' if addr is an offset + null
59 #define ADDR_STR_LEN 20
60 #define BUILD_ID_NOTE_NAME "GNU"
63 int bin_info_init(bt_logging_level log_level
)
67 if (elf_version(EV_CURRENT
) == EV_NONE
) {
68 BT_LOG_WRITE_CUR_LVL(BT_LOG_INFO
, log_level
, BT_LOG_TAG
,
69 "ELF library initialization failed: %s.",
78 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
79 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
80 const char *debug_info_dir
, const char *target_prefix
,
81 bt_logging_level log_level
)
83 struct bin_info
*bin
= NULL
;
91 bin
= g_new0(struct bin_info
, 1);
96 bin
->log_level
= log_level
;
98 bin
->elf_path
= g_build_filename(target_prefix
, path
, NULL
);
100 bin
->elf_path
= g_strdup(path
);
103 if (!bin
->elf_path
) {
107 if (debug_info_dir
) {
108 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
109 if (!bin
->debug_info_dir
) {
114 bin
->is_pic
= is_pic
;
116 bin
->low_addr
= low_addr
;
117 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
118 bin
->build_id
= NULL
;
119 bin
->build_id_len
= 0;
120 bin
->file_build_id_matches
= false;
126 bin_info_destroy(bin
);
131 void bin_info_destroy(struct bin_info
*bin
)
137 dwarf_end(bin
->dwarf_info
);
139 g_free(bin
->debug_info_dir
);
140 g_free(bin
->elf_path
);
141 g_free(bin
->dwarf_path
);
142 g_free(bin
->build_id
);
143 g_free(bin
->dbg_link_filename
);
145 elf_end(bin
->elf_file
);
147 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
148 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
154 * Initialize the ELF file for a given executable.
156 * @param bin bin_info instance
157 * @returns 0 on success, negative value on error.
160 int bin_info_set_elf_file(struct bin_info
*bin
)
162 struct bt_fd_cache_handle
*elf_handle
= NULL
;
163 Elf
*elf_file
= NULL
;
169 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
171 BT_LOGI("Failed to open %s", bin
->elf_path
);
174 bin
->elf_handle
= elf_handle
;
176 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
179 BT_LOGE("elf_begin failed: %s", elf_errmsg(-1));
183 bin
->elf_file
= elf_file
;
185 if (elf_kind(elf_file
) != ELF_K_ELF
) {
186 BT_LOGE("Error: %s is not an ELF object", bin
->elf_path
);
193 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
199 * From a note section data struct, check if it is a build id note.
201 * @param note_data Pointer to a note section
203 * @returns 1 on match, 0 if `buf` does not contain a
204 * valid build id note
207 int is_build_id_note_section(Elf_Data
*note_data
)
209 size_t name_offset
, desc_offset
;
210 GElf_Nhdr note_header
;
214 * Discard the return value as it contains the size of the note section
215 * and we don't need it.
217 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
221 * Check the note name length. The name_sz field includes the
222 * terminating null byte.
224 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
228 /* Check the note type. */
229 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
233 /* Check the note name. */
234 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
235 note_header
.n_namesz
) != 0) {
246 * From a build id note section data struct, check if the build id it contains
247 * is identical to the build id passed as parameter.
249 * @param note_data Pointer to the file build id note section.
250 * @param build_id Pointer to a build id to compare to.
251 * @param build_id_len length of the build id.
253 * @returns 1 on match, 0 otherwise.
256 int is_build_id_note_section_matching(Elf_Data
*note_data
,
257 uint8_t *build_id
, size_t build_id_len
)
259 size_t name_offset
, desc_offset
;
260 GElf_Nhdr note_header
;
262 if (build_id_len
<= 0) {
267 * Discard the return value as it contains the size of the note section
268 * and we don't need it.
270 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
274 * Compare the binary build id with the supplied build id.
276 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
277 build_id_len
) == 0) {
285 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
286 * id of the ondisk file (bin->elf_file).
288 * @param bin bin_info instance
289 * @param build_id build id to compare ot the on disk file
290 * @param build_id_len length of the build id
292 * @returns 1 on if the build id of stored in `bin` matches
293 * the build id of the ondisk file.
294 * 0 on if they are different or an error occured.
297 int is_build_id_matching(struct bin_info
*bin
)
299 int ret
, is_build_id
, is_matching
= 0;
300 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
301 GElf_Shdr curr_section_hdr
;
303 if (!bin
->build_id
) {
307 /* Set ELF file if it hasn't been accessed yet. */
308 if (!bin
->elf_file
) {
309 ret
= bin_info_set_elf_file(bin
);
311 /* Failed to set ELF file. */
316 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
321 while (next_section
) {
322 Elf_Data
*note_data
= NULL
;
324 curr_section
= next_section
;
325 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
327 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
331 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
336 * elf_getdata() translates the data to native byte order.
338 note_data
= elf_getdata(curr_section
, NULL
);
343 /* Check if the note is of the build-id type. */
344 is_build_id
= is_build_id_note_section(note_data
);
350 * Compare the build id of the on-disk file and
351 * the build id recorded in the trace.
353 is_matching
= is_build_id_note_section_matching(
354 note_data
, bin
->build_id
, bin
->build_id_len
);
364 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
367 if (!bin
|| !build_id
) {
371 /* Set the build id. */
372 bin
->build_id
= g_new0(uint8_t, build_id_len
);
373 if (!bin
->build_id
) {
377 memcpy(bin
->build_id
, build_id
, build_id_len
);
378 bin
->build_id_len
= build_id_len
;
381 * Check if the file found on the file system has the same build id
382 * that what was recorded in the trace.
384 bin
->file_build_id_matches
= is_build_id_matching(bin
);
385 if (!bin
->file_build_id_matches
) {
386 BT_LOGI_STR("Supplied Build ID does not match Build ID of the "
387 "binary or library found on the file system.");
392 * Reset the is_elf_only flag in case it had been set
393 * previously, because we might find separate debug info using
394 * the new build id information.
396 bin
->is_elf_only
= false;
405 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
408 if (!bin
|| !filename
) {
412 bin
->dbg_link_filename
= g_strdup(filename
);
413 if (!bin
->dbg_link_filename
) {
417 bin
->dbg_link_crc
= crc
;
420 * Reset the is_elf_only flag in case it had been set
421 * previously, because we might find separate debug info using
422 * the new build id information.
424 bin
->is_elf_only
= false;
434 * Tries to read DWARF info from the location given by path, and
435 * attach it to the given bin_info instance if it exists.
437 * @param bin bin_info instance for which to set DWARF info
438 * @param path Presumed location of the DWARF info
439 * @returns 0 on success, negative value on failure
442 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
445 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
446 struct bt_dwarf_cu
*cu
= NULL
;
447 Dwarf
*dwarf_info
= NULL
;
453 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
458 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
465 * Check if the dwarf info has any CU. If not, the
466 * executable's object file contains no DWARF info.
468 cu
= bt_dwarf_cu_create(dwarf_info
);
473 ret
= bt_dwarf_cu_next(cu
);
478 bin
->dwarf_handle
= dwarf_handle
;
479 bin
->dwarf_path
= g_strdup(path
);
480 if (!bin
->dwarf_path
) {
483 bin
->dwarf_info
= dwarf_info
;
489 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
490 dwarf_end(dwarf_info
);
498 * Try to set the dwarf_info for a given bin_info instance via the
501 * @param bin bin_info instance for which to retrieve the
502 * DWARF info via build ID
503 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
506 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
509 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
510 const char *dbg_dir
= NULL
;
511 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
513 if (!bin
|| !bin
->build_id
) {
517 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
520 * The prefix dir is the first byte of the build id, represented in
521 * lowercase hex as two characters per byte, +1 for '\0'.
523 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
524 if (!build_id_prefix_dir
) {
527 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
530 * The build id file is the remaining bytes of the build id,
531 * represented in lowercase hex, as two characters per byte.
533 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
535 /* To which the build id suffix is added, +1 for '\0'. */
536 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
539 * The resulting filename string is the concatenation of the
540 * hex build id and the suffix.
542 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
543 build_id_file
= g_new0(gchar
, build_id_file_len
);
544 if (!build_id_file
) {
549 * For each byte, starting at offset 1, append two characters
552 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
553 int path_idx
= 2 * (i
- 1);
555 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
557 /* Append the suffix to the generated string, including the '\0'. */
558 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
561 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
566 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
576 g_free(build_id_prefix_dir
);
577 g_free(build_id_file
);
584 * Tests whether the file located at path exists and has the expected
587 * This predicate is used when looking up separate debug info via the
588 * GNU debuglink method. The expected crc can be found .gnu_debuglink
589 * section in the original ELF file, along with the filename for the
590 * file containing the debug info.
592 * @param path Full path at which to look for the debug file
593 * @param crc Expected checksum for the debug file
594 * @returns 1 if the file exists and has the correct checksum,
598 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
601 struct bt_fd_cache_handle
*debug_handle
= NULL
;
608 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
613 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
622 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
627 * Try to set the dwarf_info for a given bin_info instance via the
630 * @param bin bin_info instance for which to retrieve the
631 * DWARF info via debug link
632 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
635 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
638 const gchar
*dbg_dir
= NULL
;
639 gchar
*bin_dir
= NULL
, *path
= NULL
;
641 if (!bin
|| !bin
->dbg_link_filename
) {
645 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
646 bin_dir
= g_path_get_dirname(bin
->elf_path
);
648 /* First look in the executable's dir */
649 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
651 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
655 /* If not found, look in .debug subdir */
657 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
659 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
663 /* Lastly, look under the global debug directory */
666 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
667 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
680 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
689 * Initialize the DWARF info for a given executable.
691 * @param bin bin_info instance
692 * @returns 0 on success, negative value on failure
695 int bin_info_set_dwarf_info(struct bin_info
*bin
)
704 /* First try to set the DWARF info from the ELF file */
705 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
711 * If that fails, try to find separate debug info via build ID
714 ret
= bin_info_set_dwarf_info_build_id(bin
);
719 ret
= bin_info_set_dwarf_info_debug_link(bin
);
729 void source_location_destroy(struct source_location
*src_loc
)
735 free(src_loc
->filename
);
740 * Append a string representation of an address offset to an existing
743 * On success, the out parameter `result` will contain the base string
744 * followed by the offset string of the form "+0x1234". On failure,
745 * `result` remains unchanged.
747 * @param base_str The string to which to append an offset string
748 * @param low_addr The lower virtual memory address, the base from
749 * which the offset is computed
750 * @param high_addr The higher virtual memory address
751 * @param result Out parameter, the base string followed by the
753 * @returns 0 on success, -1 on failure
756 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
757 uint64_t high_addr
, char **result
)
760 char *_result
= NULL
;
762 if (!base_str
|| !result
) {
766 offset
= high_addr
- low_addr
;
768 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
782 * Try to find the symbol closest to an address within a given ELF
785 * Only function symbols are taken into account. The symbol's address
786 * must precede `addr`. A symbol with a closer address might exist
787 * after `addr` but is irrelevant because it cannot encompass `addr`.
789 * On success, if found, the out parameters `sym` and `shdr` are
790 * set. On failure or if none are found, they remain unchanged.
792 * @param scn ELF section in which to look for the address
793 * @param addr Virtual memory address for which to find the
794 * nearest function symbol
795 * @param sym Out parameter, the nearest function symbol
796 * @param shdr Out parameter, the section header for scn
797 * @returns 0 on success, -1 on failure
800 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
801 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
805 Elf_Data
*data
= NULL
;
806 GElf_Shdr
*_shdr
= NULL
;
807 GElf_Sym
*nearest_sym
= NULL
;
809 if (!scn
|| !sym
|| !shdr
) {
813 _shdr
= g_new0(GElf_Shdr
, 1);
818 _shdr
= gelf_getshdr(scn
, _shdr
);
823 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
825 * We are only interested in symbol table (symtab)
826 * sections, skip this one.
831 data
= elf_getdata(scn
, NULL
);
836 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
838 for (i
= 0; i
< symbol_count
; ++i
) {
839 GElf_Sym
*cur_sym
= NULL
;
841 cur_sym
= g_new0(GElf_Sym
, 1);
845 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
849 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
850 /* We're only interested in the functions. */
855 if (cur_sym
->st_value
<= addr
&&
857 cur_sym
->st_value
> nearest_sym
->st_value
)) {
859 nearest_sym
= cur_sym
;
882 * Get the name of the function containing a given address within an
883 * executable using ELF symbols.
885 * The function name is in fact the name of the nearest ELF symbol,
886 * followed by the offset in bytes between the address and the symbol
887 * (in hex), separated by a '+' character.
889 * If found, the out parameter `func_name` is set on success. On failure,
890 * it remains unchanged.
892 * @param bin bin_info instance for the executable containing
894 * @param addr Virtual memory address for which to find the
896 * @param func_name Out parameter, the function name
897 * @returns 0 on success, -1 on failure
900 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
904 * TODO (possible optimisation): if an ELF has no symtab
905 * section, it has been stripped. Therefore, it would be wise
906 * to store a flag indicating the stripped status after the
907 * first iteration to prevent subsequent ones.
911 GElf_Sym
*sym
= NULL
;
912 GElf_Shdr
*shdr
= NULL
;
913 char *sym_name
= NULL
;
915 /* Set ELF file if it hasn't been accessed yet. */
916 if (!bin
->elf_file
) {
917 ret
= bin_info_set_elf_file(bin
);
919 /* Failed to set ELF file. */
924 scn
= elf_nextscn(bin
->elf_file
, scn
);
929 while (scn
&& !sym
) {
930 ret
= bin_info_get_nearest_symbol_from_section(
931 scn
, addr
, &sym
, &shdr
);
936 scn
= elf_nextscn(bin
->elf_file
, scn
);
940 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
946 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
964 * Get the name of the function containing a given address within a
965 * given compile unit (CU).
967 * If found, the out parameter `func_name` is set on success. On
968 * failure, it remains unchanged.
970 * @param cu bt_dwarf_cu instance which may contain the address
971 * @param addr Virtual memory address for which to find the
973 * @param func_name Out parameter, the function name
974 * @returns 0 on success, -1 on failure
977 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
982 struct bt_dwarf_die
*die
= NULL
;
984 if (!cu
|| !func_name
) {
988 die
= bt_dwarf_die_create(cu
);
993 while (bt_dwarf_die_next(die
) == 0) {
996 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1001 if (tag
== DW_TAG_subprogram
) {
1002 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1014 uint64_t low_addr
= 0;
1015 char *die_name
= NULL
;
1017 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1022 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1028 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1036 bt_dwarf_die_destroy(die
);
1040 bt_dwarf_die_destroy(die
);
1045 * Get the name of the function containing a given address within an
1046 * executable using DWARF debug info.
1048 * If found, the out parameter `func_name` is set on success. On
1049 * failure, it remains unchanged.
1051 * @param bin bin_info instance for the executable containing
1053 * @param addr Virtual memory address for which to find the
1055 * @param func_name Out parameter, the function name
1056 * @returns 0 on success, -1 on failure
1059 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1063 char *_func_name
= NULL
;
1064 struct bt_dwarf_cu
*cu
= NULL
;
1066 if (!bin
|| !func_name
) {
1070 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1075 while (bt_dwarf_cu_next(cu
) == 0) {
1076 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1087 *func_name
= _func_name
;
1092 bt_dwarf_cu_destroy(cu
);
1096 bt_dwarf_cu_destroy(cu
);
1101 int bin_info_lookup_function_name(struct bin_info
*bin
,
1102 uint64_t addr
, char **func_name
)
1105 char *_func_name
= NULL
;
1107 if (!bin
|| !func_name
) {
1112 * If the bin_info has a build id but it does not match the build id
1113 * that was found on the file system, return an error.
1115 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1119 /* Set DWARF info if it hasn't been accessed yet. */
1120 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1121 ret
= bin_info_set_dwarf_info(bin
);
1123 BT_LOGI_STR("Failed to set bin dwarf info, falling "
1124 "back to ELF lookup.");
1125 /* Failed to set DWARF info, fallback to ELF. */
1126 bin
->is_elf_only
= true;
1130 if (!bin_info_has_address(bin
, addr
)) {
1135 * Addresses in ELF and DWARF are relative to base address for
1136 * PIC, so make the address argument relative too if needed.
1139 addr
-= bin
->low_addr
;
1142 if (bin
->is_elf_only
) {
1143 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1146 BT_LOGI("Failed to lookup function name (ELF): "
1150 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1153 BT_LOGI("Failed to lookup function name (DWARF): "
1158 *func_name
= _func_name
;
1166 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1168 gchar
*_bin_loc
= NULL
;
1170 if (!bin
|| !bin_loc
) {
1175 * If the bin_info has a build id but it does not match the build id
1176 * that was found on the file system, return an error.
1178 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1183 addr
-= bin
->low_addr
;
1184 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1186 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1193 *bin_loc
= _bin_loc
;
1201 * Predicate used to determine whether the children of a given DIE
1202 * contain a specific address.
1204 * More specifically, the parameter `die` is expected to be a
1205 * subprogram (function) DIE, and this predicate tells whether any
1206 * subroutines are inlined within this function and would contain
1209 * On success, the out parameter `contains` is set with the boolean
1210 * value indicating whether the DIE's range covers `addr`. On failure,
1211 * it remains unchanged.
1213 * Do note that this function advances the position of `die`. If the
1214 * address is found within one of its children, `die` will be pointing
1215 * to that child upon returning from the function, allowing to extract
1216 * the information deemed necessary.
1218 * @param die The parent DIE in whose children the address will be
1220 * @param addr The address for which to look for in the DIEs
1221 * @param contains Out parameter, true if addr is contained,
1223 * @returns Returns 0 on success, -1 on failure
1226 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1229 bool _contains
= false;
1235 ret
= bt_dwarf_die_child(die
);
1241 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1248 * The address is within the range of the current DIE
1253 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1258 if (tag
== DW_TAG_inlined_subroutine
) {
1259 /* Found the tracepoint. */
1263 if (bt_dwarf_die_has_children(die
)) {
1265 * Look for the address in the children DIEs.
1267 ret
= bt_dwarf_die_child(die
);
1273 } while (bt_dwarf_die_next(die
) == 0);
1276 *contains
= _contains
;
1284 * Lookup the source location for a given address within a CU, making
1285 * the assumption that it is contained within an inline routine in a
1288 * @param cu bt_dwarf_cu instance in which to look for the address
1289 * @param addr The address for which to look for
1290 * @param src_loc Out parameter, the source location (filename and
1291 * line number) for the address
1292 * @returns 0 on success, -1 on failure
1295 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1296 struct source_location
**src_loc
)
1300 struct bt_dwarf_die
*die
= NULL
;
1301 struct source_location
*_src_loc
= NULL
;
1303 if (!cu
|| !src_loc
) {
1307 die
= bt_dwarf_die_create(cu
);
1312 while (bt_dwarf_die_next(die
) == 0) {
1315 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1320 if (tag
== DW_TAG_subprogram
) {
1321 bool contains
= false;
1323 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1330 * Try to find an inlined subroutine
1331 * child of this DIE containing addr.
1333 ret
= bin_info_child_die_has_address(die
, addr
,
1346 char *filename
= NULL
;
1349 _src_loc
= g_new0(struct source_location
, 1);
1354 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1358 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1364 _src_loc
->filename
= filename
;
1365 _src_loc
->line_no
= line_no
;
1366 *src_loc
= _src_loc
;
1369 bt_dwarf_die_destroy(die
);
1373 source_location_destroy(_src_loc
);
1374 bt_dwarf_die_destroy(die
);
1379 * Lookup the source location for a given address within a CU,
1380 * assuming that it is contained within an inlined function.
1382 * A source location can be found regardless of inlining status for
1383 * this method, but in the case of an inlined function, the returned
1384 * source location will point not to the callsite but rather to the
1385 * definition site of the inline function.
1387 * @param cu bt_dwarf_cu instance in which to look for the address
1388 * @param addr The address for which to look for
1389 * @param src_loc Out parameter, the source location (filename and
1390 * line number) for the address. Set only if the address
1391 * is found and resolved successfully
1393 * @returns 0 on success, -1 on failure
1396 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1397 struct source_location
**src_loc
)
1399 struct source_location
*_src_loc
= NULL
;
1400 struct bt_dwarf_die
*die
= NULL
;
1401 const char *filename
= NULL
;
1402 Dwarf_Line
*line
= NULL
;
1403 Dwarf_Addr line_addr
;
1404 int ret
= 0, line_no
;
1406 if (!cu
|| !src_loc
) {
1410 die
= bt_dwarf_die_create(cu
);
1415 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1417 /* This is not an error. The caller needs to keep looking. */
1421 ret
= dwarf_lineaddr(line
, &line_addr
);
1426 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1431 if (addr
== line_addr
) {
1432 _src_loc
= g_new0(struct source_location
, 1);
1437 ret
= dwarf_lineno(line
, &line_no
);
1442 _src_loc
->line_no
= line_no
;
1443 _src_loc
->filename
= g_strdup(filename
);
1447 *src_loc
= _src_loc
;
1453 source_location_destroy(_src_loc
);
1456 bt_dwarf_die_destroy(die
);
1461 * Get the source location (file name and line number) for a given
1462 * address within a compile unit (CU).
1464 * On success, the out parameter `src_loc` is set if found. On
1465 * failure, it remains unchanged.
1467 * @param cu bt_dwarf_cu instance for the compile unit which
1468 * may contain the address
1469 * @param addr Virtual memory address for which to find the
1471 * @param src_loc Out parameter, the source location
1472 * @returns 0 on success, -1 on failure
1475 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1476 struct source_location
**src_loc
)
1479 struct source_location
*_src_loc
= NULL
;
1481 if (!cu
|| !src_loc
) {
1485 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1494 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1505 *src_loc
= _src_loc
;
1511 source_location_destroy(_src_loc
);
1516 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1517 struct source_location
**src_loc
)
1519 struct bt_dwarf_cu
*cu
= NULL
;
1520 struct source_location
*_src_loc
= NULL
;
1522 if (!bin
|| !src_loc
) {
1527 * If the bin_info has a build id but it does not match the build id
1528 * that was found on the file system, return an error.
1530 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1534 /* Set DWARF info if it hasn't been accessed yet. */
1535 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1536 if (bin_info_set_dwarf_info(bin
)) {
1537 /* Failed to set DWARF info. */
1538 bin
->is_elf_only
= true;
1542 if (bin
->is_elf_only
) {
1543 /* We cannot lookup source location without DWARF info. */
1547 if (!bin_info_has_address(bin
, addr
)) {
1552 * Addresses in ELF and DWARF are relative to base address for
1553 * PIC, so make the address argument relative too if needed.
1556 addr
-= bin
->low_addr
;
1559 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1564 while (bt_dwarf_cu_next(cu
) == 0) {
1567 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1577 bt_dwarf_cu_destroy(cu
);
1579 *src_loc
= _src_loc
;
1585 source_location_destroy(_src_loc
);
1586 bt_dwarf_cu_destroy(cu
);