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_TAG "PLUGIN-CTF-LTTNG-UTILS-DEBUG-INFO-FLT-BIN-INFO"
51 * An address printed in hex is at most 20 bytes (16 for 64-bits +
52 * leading 0x + optional leading '+' if addr is an offset + null
55 #define ADDR_STR_LEN 20
56 #define BUILD_ID_NOTE_NAME "GNU"
59 int bin_info_init(void)
63 if (elf_version(EV_CURRENT
) == EV_NONE
) {
64 BT_LOGD("ELF library initialization failed: %s.",
73 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
74 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
75 const char *debug_info_dir
, const char *target_prefix
)
77 struct bin_info
*bin
= NULL
;
85 bin
= g_new0(struct bin_info
, 1);
91 bin
->elf_path
= g_build_path("/", target_prefix
,
94 bin
->elf_path
= g_strdup(path
);
101 if (debug_info_dir
) {
102 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
103 if (!bin
->debug_info_dir
) {
108 bin
->is_pic
= is_pic
;
110 bin
->low_addr
= low_addr
;
111 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
112 bin
->build_id
= NULL
;
113 bin
->build_id_len
= 0;
114 bin
->file_build_id_matches
= false;
120 bin_info_destroy(bin
);
125 void bin_info_destroy(struct bin_info
*bin
)
131 dwarf_end(bin
->dwarf_info
);
133 g_free(bin
->debug_info_dir
);
134 g_free(bin
->elf_path
);
135 g_free(bin
->dwarf_path
);
136 g_free(bin
->build_id
);
137 g_free(bin
->dbg_link_filename
);
139 elf_end(bin
->elf_file
);
141 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
142 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
148 * Initialize the ELF file for a given executable.
150 * @param bin bin_info instance
151 * @returns 0 on success, negative value on error.
154 int bin_info_set_elf_file(struct bin_info
*bin
)
156 struct bt_fd_cache_handle
*elf_handle
= NULL
;
157 Elf
*elf_file
= NULL
;
163 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
165 BT_LOGD("Failed to open %s", bin
->elf_path
);
169 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(elf_handle
),
172 BT_LOGE("elf_begin failed: %s", elf_errmsg(-1));
176 if (elf_kind(elf_file
) != ELF_K_ELF
) {
177 BT_LOGE("Error: %s is not an ELF object", bin
->elf_path
);
181 bin
->elf_handle
= elf_handle
;
182 bin
->elf_file
= elf_file
;
186 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
192 * From a note section data buffer, check if it is a build id note.
194 * @param buf Pointer to a note section
196 * @returns 1 on match, 0 if `buf` does not contain a
197 * valid build id note
200 int is_build_id_note_section(uint8_t *buf
)
203 uint32_t name_sz
, desc_sz
, note_type
;
205 /* The note section header has 3 32bit integer for the following:
206 * - Section name size
210 name_sz
= (uint32_t) *buf
;
211 buf
+= sizeof(name_sz
);
213 buf
+= sizeof(desc_sz
);
215 note_type
= (uint32_t) *buf
;
216 buf
+= sizeof(note_type
);
218 /* Check the note type. */
219 if (note_type
!= NT_GNU_BUILD_ID
) {
223 /* Check the note name. */
224 if (memcmp(buf
, BUILD_ID_NOTE_NAME
, name_sz
) != 0) {
235 * From a build id note section data buffer, check if the build id it contains
236 * is identical to the build id passed as parameter.
238 * @param file_build_id_note 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(uint8_t *file_build_id_note
,
246 uint8_t *build_id
, size_t build_id_len
)
248 uint32_t name_sz
, desc_sz
, note_type
;
250 if (build_id_len
<= 0) {
254 /* The note section header has 3 32bit integer for the following:
255 * - Section name size
259 name_sz
= (uint32_t) *file_build_id_note
;
260 file_build_id_note
+= sizeof(name_sz
);
261 file_build_id_note
+= sizeof(desc_sz
);
262 file_build_id_note
+= sizeof(note_type
);
265 * Move the pointer pass the name char array. This corresponds to the
266 * beginning of the description section. The description is the build
267 * id in the case of a build id note.
269 file_build_id_note
+= name_sz
;
272 * Compare the binary build id with the supplied build id.
274 if (memcmp(build_id
, file_build_id_note
, build_id_len
) == 0) {
282 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
283 * id of the ondisk file (bin->elf_file).
285 * @param bin bin_info instance
286 * @param build_id build id to compare ot the on disk file
287 * @param build_id_len length of the build id
289 * @returns 1 on if the build id of stored in `bin` matches
290 * the build id of the ondisk file.
291 * 0 on if they are different or an error occured.
294 int is_build_id_matching(struct bin_info
*bin
)
296 int ret
, is_build_id
, is_matching
= 0;
297 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
298 Elf_Data
*note_data
= NULL
;
299 GElf_Shdr
*curr_section_hdr
= NULL
;
301 if (!bin
->build_id
) {
305 /* Set ELF file if it hasn't been accessed yet. */
306 if (!bin
->elf_file
) {
307 ret
= bin_info_set_elf_file(bin
);
309 /* Failed to set ELF file. */
314 curr_section_hdr
= g_new0(GElf_Shdr
, 1);
315 if (!curr_section_hdr
) {
319 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
324 while (next_section
) {
325 curr_section
= next_section
;
326 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
328 curr_section_hdr
= gelf_getshdr(curr_section
, curr_section_hdr
);
330 if (!curr_section_hdr
) {
334 if (curr_section_hdr
->sh_type
!= SHT_NOTE
) {
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
->d_buf
);
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(note_data
->d_buf
,
354 bin
->build_id
, bin
->build_id_len
);
360 g_free(curr_section_hdr
);
365 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
368 if (!bin
|| !build_id
) {
372 /* Set the build id. */
373 bin
->build_id
= g_new0(uint8_t, build_id_len
);
374 if (!bin
->build_id
) {
378 memcpy(bin
->build_id
, build_id
, build_id_len
);
379 bin
->build_id_len
= build_id_len
;
382 * Check if the file found on the file system has the same build id
383 * that what was recorded in the trace.
385 bin
->file_build_id_matches
= is_build_id_matching(bin
);
386 if (!bin
->file_build_id_matches
) {
387 BT_LOGD_STR("Supplied Build ID does not match Build ID of the "
388 "binary or library found on the file system.");
393 * Reset the is_elf_only flag in case it had been set
394 * previously, because we might find separate debug info using
395 * the new build id information.
397 bin
->is_elf_only
= false;
406 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
409 if (!bin
|| !filename
) {
413 bin
->dbg_link_filename
= g_strdup(filename
);
414 if (!bin
->dbg_link_filename
) {
418 bin
->dbg_link_crc
= crc
;
421 * Reset the is_elf_only flag in case it had been set
422 * previously, because we might find separate debug info using
423 * the new build id information.
425 bin
->is_elf_only
= false;
435 * Tries to read DWARF info from the location given by path, and
436 * attach it to the given bin_info instance if it exists.
438 * @param bin bin_info instance for which to set DWARF info
439 * @param path Presumed location of the DWARF info
440 * @returns 0 on success, negative value on failure
443 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
446 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
447 struct bt_dwarf_cu
*cu
= NULL
;
448 Dwarf
*dwarf_info
= NULL
;
454 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
459 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
466 * Check if the dwarf info has any CU. If not, the
467 * executable's object file contains no DWARF info.
469 cu
= bt_dwarf_cu_create(dwarf_info
);
474 ret
= bt_dwarf_cu_next(cu
);
479 bin
->dwarf_handle
= dwarf_handle
;
480 bin
->dwarf_path
= g_strdup(path
);
481 if (!bin
->dwarf_path
) {
484 bin
->dwarf_info
= dwarf_info
;
490 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
491 dwarf_end(dwarf_info
);
499 * Try to set the dwarf_info for a given bin_info instance via the
502 * @param bin bin_info instance for which to retrieve the
503 * DWARF info via build ID
504 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
507 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
510 char *path
= NULL
, *build_id_file
= NULL
;
511 const char *dbg_dir
= NULL
;
512 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
514 if (!bin
|| !bin
->build_id
) {
518 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
520 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
521 build_id_char_len
= (2 * bin
->build_id_len
) + 1;
522 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
523 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
524 build_id_file
= g_new0(gchar
, build_id_file_len
);
525 if (!build_id_file
) {
529 g_snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
530 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
531 int path_idx
= 3 + 2 * (i
- 1);
533 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
535 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
538 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
543 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
560 * Tests whether the file located at path exists and has the expected
563 * This predicate is used when looking up separate debug info via the
564 * GNU debuglink method. The expected crc can be found .gnu_debuglink
565 * section in the original ELF file, along with the filename for the
566 * file containing the debug info.
568 * @param path Full path at which to look for the debug file
569 * @param crc Expected checksum for the debug file
570 * @returns 1 if the file exists and has the correct checksum,
574 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
577 struct bt_fd_cache_handle
*debug_handle
= NULL
;
584 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
589 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
598 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
603 * Try to set the dwarf_info for a given bin_info instance via the
606 * @param bin bin_info instance for which to retrieve the
607 * DWARF info via debug link
608 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
611 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
614 const gchar
*dbg_dir
= NULL
;
615 gchar
*bin_dir
= NULL
, *dir_name
= NULL
, *path
= NULL
;
617 if (!bin
|| !bin
->dbg_link_filename
) {
621 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
622 dir_name
= g_path_get_dirname(bin
->elf_path
);
627 bin_dir
= g_strconcat(dir_name
, "/", NULL
);
629 /* First look in the executable's dir */
630 path
= g_strconcat(bin_dir
, bin
->dbg_link_filename
, NULL
);
632 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
636 /* If not found, look in .debug subdir */
638 path
= g_strconcat(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
640 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
644 /* Lastly, look under the global debug directory */
647 path
= g_strconcat(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
648 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
662 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
671 * Initialize the DWARF info for a given executable.
673 * @param bin bin_info instance
674 * @returns 0 on success, negative value on failure
677 int bin_info_set_dwarf_info(struct bin_info
*bin
)
686 /* First try to set the DWARF info from the ELF file */
687 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
693 * If that fails, try to find separate debug info via build ID
696 ret
= bin_info_set_dwarf_info_build_id(bin
);
701 ret
= bin_info_set_dwarf_info_debug_link(bin
);
711 void source_location_destroy(struct source_location
*src_loc
)
717 free(src_loc
->filename
);
722 * Append a string representation of an address offset to an existing
725 * On success, the out parameter `result` will contain the base string
726 * followed by the offset string of the form "+0x1234". On failure,
727 * `result` remains unchanged.
729 * @param base_str The string to which to append an offset string
730 * @param low_addr The lower virtual memory address, the base from
731 * which the offset is computed
732 * @param high_addr The higher virtual memory address
733 * @param result Out parameter, the base string followed by the
735 * @returns 0 on success, -1 on failure
738 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
739 uint64_t high_addr
, char **result
)
742 char *_result
= NULL
;
745 if (!base_str
|| !result
) {
749 offset
= high_addr
- low_addr
;
751 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
765 * Try to find the symbol closest to an address within a given ELF
768 * Only function symbols are taken into account. The symbol's address
769 * must precede `addr`. A symbol with a closer address might exist
770 * after `addr` but is irrelevant because it cannot encompass `addr`.
772 * On success, if found, the out parameters `sym` and `shdr` are
773 * set. On failure or if none are found, they remain unchanged.
775 * @param scn ELF section in which to look for the address
776 * @param addr Virtual memory address for which to find the
777 * nearest function symbol
778 * @param sym Out parameter, the nearest function symbol
779 * @param shdr Out parameter, the section header for scn
780 * @returns 0 on success, -1 on failure
783 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
784 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
788 Elf_Data
*data
= NULL
;
789 GElf_Shdr
*_shdr
= NULL
;
790 GElf_Sym
*nearest_sym
= NULL
;
792 if (!scn
|| !sym
|| !shdr
) {
796 _shdr
= g_new0(GElf_Shdr
, 1);
801 _shdr
= gelf_getshdr(scn
, _shdr
);
806 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
808 * We are only interested in symbol table (symtab)
809 * sections, skip this one.
814 data
= elf_getdata(scn
, NULL
);
819 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
821 for (i
= 0; i
< symbol_count
; ++i
) {
822 GElf_Sym
*cur_sym
= NULL
;
824 cur_sym
= g_new0(GElf_Sym
, 1);
828 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
832 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
833 /* We're only interested in the functions. */
838 if (cur_sym
->st_value
<= addr
&&
840 cur_sym
->st_value
> nearest_sym
->st_value
)) {
842 nearest_sym
= cur_sym
;
865 * Get the name of the function containing a given address within an
866 * executable using ELF symbols.
868 * The function name is in fact the name of the nearest ELF symbol,
869 * followed by the offset in bytes between the address and the symbol
870 * (in hex), separated by a '+' character.
872 * If found, the out parameter `func_name` is set on success. On failure,
873 * it remains unchanged.
875 * @param bin bin_info instance for the executable containing
877 * @param addr Virtual memory address for which to find the
879 * @param func_name Out parameter, the function name
880 * @returns 0 on success, -1 on failure
883 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
887 * TODO (possible optimisation): if an ELF has no symtab
888 * section, it has been stripped. Therefore, it would be wise
889 * to store a flag indicating the stripped status after the
890 * first iteration to prevent subsequent ones.
894 GElf_Sym
*sym
= NULL
;
895 GElf_Shdr
*shdr
= NULL
;
896 char *sym_name
= NULL
;
898 /* Set ELF file if it hasn't been accessed yet. */
899 if (!bin
->elf_file
) {
900 ret
= bin_info_set_elf_file(bin
);
902 /* Failed to set ELF file. */
907 scn
= elf_nextscn(bin
->elf_file
, scn
);
912 while (scn
&& !sym
) {
913 ret
= bin_info_get_nearest_symbol_from_section(
914 scn
, addr
, &sym
, &shdr
);
919 scn
= elf_nextscn(bin
->elf_file
, scn
);
923 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
929 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
947 * Get the name of the function containing a given address within a
948 * given compile unit (CU).
950 * If found, the out parameter `func_name` is set on success. On
951 * failure, it remains unchanged.
953 * @param cu bt_dwarf_cu instance which may contain the address
954 * @param addr Virtual memory address for which to find the
956 * @param func_name Out parameter, the function name
957 * @returns 0 on success, -1 on failure
960 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
965 struct bt_dwarf_die
*die
= NULL
;
967 if (!cu
|| !func_name
) {
971 die
= bt_dwarf_die_create(cu
);
976 while (bt_dwarf_die_next(die
) == 0) {
979 ret
= bt_dwarf_die_get_tag(die
, &tag
);
984 if (tag
== DW_TAG_subprogram
) {
985 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
997 uint64_t low_addr
= 0;
998 char *die_name
= NULL
;
1000 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1005 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1011 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1019 bt_dwarf_die_destroy(die
);
1023 bt_dwarf_die_destroy(die
);
1028 * Get the name of the function containing a given address within an
1029 * executable using DWARF debug info.
1031 * If found, the out parameter `func_name` is set on success. On
1032 * failure, it remains unchanged.
1034 * @param bin bin_info instance for the executable containing
1036 * @param addr Virtual memory address for which to find the
1038 * @param func_name Out parameter, the function name
1039 * @returns 0 on success, -1 on failure
1042 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1046 char *_func_name
= NULL
;
1047 struct bt_dwarf_cu
*cu
= NULL
;
1049 if (!bin
|| !func_name
) {
1053 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1058 while (bt_dwarf_cu_next(cu
) == 0) {
1059 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1070 *func_name
= _func_name
;
1075 bt_dwarf_cu_destroy(cu
);
1079 bt_dwarf_cu_destroy(cu
);
1084 int bin_info_lookup_function_name(struct bin_info
*bin
,
1085 uint64_t addr
, char **func_name
)
1088 char *_func_name
= NULL
;
1090 if (!bin
|| !func_name
) {
1095 * If the bin_info has a build id but it does not match the build id
1096 * that was found on the file system, return an error.
1098 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1102 /* Set DWARF info if it hasn't been accessed yet. */
1103 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1104 ret
= bin_info_set_dwarf_info(bin
);
1106 BT_LOGD_STR("Failed to set bin dwarf info, falling "
1107 "back to ELF lookup.");
1108 /* Failed to set DWARF info, fallback to ELF. */
1109 bin
->is_elf_only
= true;
1113 if (!bin_info_has_address(bin
, addr
)) {
1118 * Addresses in ELF and DWARF are relative to base address for
1119 * PIC, so make the address argument relative too if needed.
1122 addr
-= bin
->low_addr
;
1125 if (bin
->is_elf_only
) {
1126 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1129 BT_LOGD("Failed to lookup function name (ELF): "
1133 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1136 BT_LOGD("Failed to lookup function name (DWARF): "
1141 *func_name
= _func_name
;
1149 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1151 gchar
*_bin_loc
= NULL
;
1153 if (!bin
|| !bin_loc
) {
1158 * If the bin_info has a build id but it does not match the build id
1159 * that was found on the file system, return an error.
1161 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1166 addr
-= bin
->low_addr
;
1167 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1169 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1176 *bin_loc
= _bin_loc
;
1184 * Predicate used to determine whether the children of a given DIE
1185 * contain a specific address.
1187 * More specifically, the parameter `die` is expected to be a
1188 * subprogram (function) DIE, and this predicate tells whether any
1189 * subroutines are inlined within this function and would contain
1192 * On success, the out parameter `contains` is set with the boolean
1193 * value indicating whether the DIE's range covers `addr`. On failure,
1194 * it remains unchanged.
1196 * Do note that this function advances the position of `die`. If the
1197 * address is found within one of its children, `die` will be pointing
1198 * to that child upon returning from the function, allowing to extract
1199 * the information deemed necessary.
1201 * @param die The parent DIE in whose children the address will be
1203 * @param addr The address for which to look for in the DIEs
1204 * @param contains Out parameter, true if addr is contained,
1206 * @returns Returns 0 on success, -1 on failure
1209 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1212 bool _contains
= false;
1218 ret
= bt_dwarf_die_child(die
);
1224 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1231 * The address is within the range of the current DIE
1236 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1241 if (tag
== DW_TAG_inlined_subroutine
) {
1242 /* Found the tracepoint. */
1246 if (bt_dwarf_die_has_children(die
)) {
1248 * Look for the address in the children DIEs.
1250 ret
= bt_dwarf_die_child(die
);
1256 } while (bt_dwarf_die_next(die
) == 0);
1259 *contains
= _contains
;
1267 * Lookup the source location for a given address within a CU, making
1268 * the assumption that it is contained within an inline routine in a
1271 * @param cu bt_dwarf_cu instance in which to look for the address
1272 * @param addr The address for which to look for
1273 * @param src_loc Out parameter, the source location (filename and
1274 * line number) for the address
1275 * @returns 0 on success, -1 on failure
1278 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1279 struct source_location
**src_loc
)
1283 struct bt_dwarf_die
*die
= NULL
;
1284 struct source_location
*_src_loc
= NULL
;
1286 if (!cu
|| !src_loc
) {
1290 die
= bt_dwarf_die_create(cu
);
1295 while (bt_dwarf_die_next(die
) == 0) {
1298 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1303 if (tag
== DW_TAG_subprogram
) {
1304 bool contains
= false;
1306 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1313 * Try to find an inlined subroutine
1314 * child of this DIE containing addr.
1316 ret
= bin_info_child_die_has_address(die
, addr
,
1329 char *filename
= NULL
;
1332 _src_loc
= g_new0(struct source_location
, 1);
1337 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1341 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1347 _src_loc
->filename
= filename
;
1348 _src_loc
->line_no
= line_no
;
1349 *src_loc
= _src_loc
;
1352 bt_dwarf_die_destroy(die
);
1356 source_location_destroy(_src_loc
);
1357 bt_dwarf_die_destroy(die
);
1362 * Lookup the source location for a given address within a CU,
1363 * assuming that it is contained within an inlined function.
1365 * A source location can be found regardless of inlining status for
1366 * this method, but in the case of an inlined function, the returned
1367 * source location will point not to the callsite but rather to the
1368 * definition site of the inline function.
1370 * @param cu bt_dwarf_cu instance in which to look for the address
1371 * @param addr The address for which to look for
1372 * @param src_loc Out parameter, the source location (filename and
1373 * line number) for the address
1374 * @returns 0 on success, -1 on failure
1377 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1378 struct source_location
**src_loc
)
1380 struct source_location
*_src_loc
= NULL
;
1381 struct bt_dwarf_die
*die
= NULL
;
1382 const char *filename
= NULL
;
1383 Dwarf_Line
*line
= NULL
;
1384 Dwarf_Addr line_addr
;
1387 if (!cu
|| !src_loc
) {
1391 die
= bt_dwarf_die_create(cu
);
1396 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1401 ret
= dwarf_lineaddr(line
, &line_addr
);
1406 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1411 if (addr
== line_addr
) {
1412 _src_loc
= g_new0(struct source_location
, 1);
1417 ret
= dwarf_lineno(line
, &line_no
);
1422 _src_loc
->line_no
= line_no
;
1423 _src_loc
->filename
= g_strdup(filename
);
1426 bt_dwarf_die_destroy(die
);
1429 *src_loc
= _src_loc
;
1435 source_location_destroy(_src_loc
);
1436 bt_dwarf_die_destroy(die
);
1441 * Get the source location (file name and line number) for a given
1442 * address within a compile unit (CU).
1444 * On success, the out parameter `src_loc` is set if found. On
1445 * failure, it remains unchanged.
1447 * @param cu bt_dwarf_cu instance for the compile unit which
1448 * may contain the address
1449 * @param addr Virtual memory address for which to find the
1451 * @param src_loc Out parameter, the source location
1452 * @returns 0 on success, -1 on failure
1455 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1456 struct source_location
**src_loc
)
1459 struct source_location
*_src_loc
= NULL
;
1461 if (!cu
|| !src_loc
) {
1465 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1474 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1485 *src_loc
= _src_loc
;
1491 source_location_destroy(_src_loc
);
1496 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1497 struct source_location
**src_loc
)
1499 struct bt_dwarf_cu
*cu
= NULL
;
1500 struct source_location
*_src_loc
= NULL
;
1502 if (!bin
|| !src_loc
) {
1507 * If the bin_info has a build id but it does not match the build id
1508 * that was found on the file system, return an error.
1510 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1514 /* Set DWARF info if it hasn't been accessed yet. */
1515 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1516 if (bin_info_set_dwarf_info(bin
)) {
1517 /* Failed to set DWARF info. */
1518 bin
->is_elf_only
= true;
1522 if (bin
->is_elf_only
) {
1523 /* We cannot lookup source location without DWARF info. */
1527 if (!bin_info_has_address(bin
, addr
)) {
1532 * Addresses in ELF and DWARF are relative to base address for
1533 * PIC, so make the address argument relative too if needed.
1536 addr
-= bin
->low_addr
;
1539 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1544 while (bt_dwarf_cu_next(cu
) == 0) {
1547 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1557 bt_dwarf_cu_destroy(cu
);
1559 *src_loc
= _src_loc
;
1565 source_location_destroy(_src_loc
);
1566 bt_dwarf_cu_destroy(cu
);