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"
45 #include "common/common.h"
53 * An address printed in hex is at most 20 bytes (16 for 64-bits +
54 * leading 0x + optional leading '+' if addr is an offset + null
57 #define ADDR_STR_LEN 20
58 #define BUILD_ID_NOTE_NAME "GNU"
61 int bin_info_init(void)
65 if (elf_version(EV_CURRENT
) == EV_NONE
) {
66 BT_LOGI("ELF library initialization failed: %s.",
75 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
76 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
77 const char *debug_info_dir
, const char *target_prefix
)
79 struct bin_info
*bin
= NULL
;
87 bin
= g_new0(struct bin_info
, 1);
93 bin
->elf_path
= g_build_filename(target_prefix
, path
, NULL
);
95 bin
->elf_path
= g_strdup(path
);
102 if (debug_info_dir
) {
103 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
104 if (!bin
->debug_info_dir
) {
109 bin
->is_pic
= is_pic
;
111 bin
->low_addr
= low_addr
;
112 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
113 bin
->build_id
= NULL
;
114 bin
->build_id_len
= 0;
115 bin
->file_build_id_matches
= false;
121 bin_info_destroy(bin
);
126 void bin_info_destroy(struct bin_info
*bin
)
132 dwarf_end(bin
->dwarf_info
);
134 g_free(bin
->debug_info_dir
);
135 g_free(bin
->elf_path
);
136 g_free(bin
->dwarf_path
);
137 g_free(bin
->build_id
);
138 g_free(bin
->dbg_link_filename
);
140 elf_end(bin
->elf_file
);
142 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
143 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
149 * Initialize the ELF file for a given executable.
151 * @param bin bin_info instance
152 * @returns 0 on success, negative value on error.
155 int bin_info_set_elf_file(struct bin_info
*bin
)
157 struct bt_fd_cache_handle
*elf_handle
= NULL
;
158 Elf
*elf_file
= NULL
;
164 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
166 BT_LOGI("Failed to open %s", bin
->elf_path
);
169 bin
->elf_handle
= elf_handle
;
171 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
174 BT_LOGE("elf_begin failed: %s", elf_errmsg(-1));
178 bin
->elf_file
= elf_file
;
180 if (elf_kind(elf_file
) != ELF_K_ELF
) {
181 BT_LOGE("Error: %s is not an ELF object", bin
->elf_path
);
188 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
194 * From a note section data struct, check if it is a build id note.
196 * @param note_data Pointer to a note section
198 * @returns 1 on match, 0 if `buf` does not contain a
199 * valid build id note
202 int is_build_id_note_section(Elf_Data
*note_data
)
204 size_t name_offset
, desc_offset
;
205 GElf_Nhdr note_header
;
209 * Discard the return value as it contains the size of the note section
210 * and we don't need it.
212 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
216 * Check the note name length. The name_sz field includes the
217 * terminating null byte.
219 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
223 /* Check the note type. */
224 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
228 /* Check the note name. */
229 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
230 note_header
.n_namesz
) != 0) {
241 * From a build id note section data struct, check if the build id it contains
242 * is identical to the build id passed as parameter.
244 * @param note_data Pointer to the file build id note section.
245 * @param build_id Pointer to a build id to compare to.
246 * @param build_id_len length of the build id.
248 * @returns 1 on match, 0 otherwise.
251 int is_build_id_note_section_matching(Elf_Data
*note_data
,
252 uint8_t *build_id
, size_t build_id_len
)
254 size_t name_offset
, desc_offset
;
255 GElf_Nhdr note_header
;
257 if (build_id_len
<= 0) {
262 * Discard the return value as it contains the size of the note section
263 * and we don't need it.
265 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
269 * Compare the binary build id with the supplied build id.
271 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
272 build_id_len
) == 0) {
280 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
281 * id of the ondisk file (bin->elf_file).
283 * @param bin bin_info instance
284 * @param build_id build id to compare ot the on disk file
285 * @param build_id_len length of the build id
287 * @returns 1 on if the build id of stored in `bin` matches
288 * the build id of the ondisk file.
289 * 0 on if they are different or an error occured.
292 int is_build_id_matching(struct bin_info
*bin
)
294 int ret
, is_build_id
, is_matching
= 0;
295 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
296 GElf_Shdr curr_section_hdr
;
298 if (!bin
->build_id
) {
302 /* Set ELF file if it hasn't been accessed yet. */
303 if (!bin
->elf_file
) {
304 ret
= bin_info_set_elf_file(bin
);
306 /* Failed to set ELF file. */
311 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
316 while (next_section
) {
317 Elf_Data
*note_data
= NULL
;
319 curr_section
= next_section
;
320 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
322 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
326 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
331 * elf_getdata() translates the data to native byte order.
333 note_data
= elf_getdata(curr_section
, NULL
);
338 /* Check if the note is of the build-id type. */
339 is_build_id
= is_build_id_note_section(note_data
);
345 * Compare the build id of the on-disk file and
346 * the build id recorded in the trace.
348 is_matching
= is_build_id_note_section_matching(
349 note_data
, bin
->build_id
, bin
->build_id_len
);
359 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
362 if (!bin
|| !build_id
) {
366 /* Set the build id. */
367 bin
->build_id
= g_new0(uint8_t, build_id_len
);
368 if (!bin
->build_id
) {
372 memcpy(bin
->build_id
, build_id
, build_id_len
);
373 bin
->build_id_len
= build_id_len
;
376 * Check if the file found on the file system has the same build id
377 * that what was recorded in the trace.
379 bin
->file_build_id_matches
= is_build_id_matching(bin
);
380 if (!bin
->file_build_id_matches
) {
381 BT_LOGI_STR("Supplied Build ID does not match Build ID of the "
382 "binary or library found on the file system.");
387 * Reset the is_elf_only flag in case it had been set
388 * previously, because we might find separate debug info using
389 * the new build id information.
391 bin
->is_elf_only
= false;
400 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
403 if (!bin
|| !filename
) {
407 bin
->dbg_link_filename
= g_strdup(filename
);
408 if (!bin
->dbg_link_filename
) {
412 bin
->dbg_link_crc
= crc
;
415 * Reset the is_elf_only flag in case it had been set
416 * previously, because we might find separate debug info using
417 * the new build id information.
419 bin
->is_elf_only
= false;
429 * Tries to read DWARF info from the location given by path, and
430 * attach it to the given bin_info instance if it exists.
432 * @param bin bin_info instance for which to set DWARF info
433 * @param path Presumed location of the DWARF info
434 * @returns 0 on success, negative value on failure
437 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
440 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
441 struct bt_dwarf_cu
*cu
= NULL
;
442 Dwarf
*dwarf_info
= NULL
;
448 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
453 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
460 * Check if the dwarf info has any CU. If not, the
461 * executable's object file contains no DWARF info.
463 cu
= bt_dwarf_cu_create(dwarf_info
);
468 ret
= bt_dwarf_cu_next(cu
);
473 bin
->dwarf_handle
= dwarf_handle
;
474 bin
->dwarf_path
= g_strdup(path
);
475 if (!bin
->dwarf_path
) {
478 bin
->dwarf_info
= dwarf_info
;
484 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
485 dwarf_end(dwarf_info
);
493 * Try to set the dwarf_info for a given bin_info instance via the
496 * @param bin bin_info instance for which to retrieve the
497 * DWARF info via build ID
498 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
501 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
504 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
505 const char *dbg_dir
= NULL
;
506 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
508 if (!bin
|| !bin
->build_id
) {
512 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
515 * The prefix dir is the first byte of the build id, represented in
516 * lowercase hex as two characters per byte, +1 for '\0'.
518 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
519 if (!build_id_prefix_dir
) {
522 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
525 * The build id file is the remaining bytes of the build id,
526 * represented in lowercase hex, as two characters per byte.
528 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
530 /* To which the build id suffix is added, +1 for '\0'. */
531 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
534 * The resulting filename string is the concatenation of the
535 * hex build id and the suffix.
537 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
538 build_id_file
= g_new0(gchar
, build_id_file_len
);
539 if (!build_id_file
) {
544 * For each byte, starting at offset 1, append two characters
547 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
548 int path_idx
= 2 * (i
- 1);
550 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
552 /* Append the suffix to the generated string, including the '\0'. */
553 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
556 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
561 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
571 g_free(build_id_prefix_dir
);
572 g_free(build_id_file
);
579 * Tests whether the file located at path exists and has the expected
582 * This predicate is used when looking up separate debug info via the
583 * GNU debuglink method. The expected crc can be found .gnu_debuglink
584 * section in the original ELF file, along with the filename for the
585 * file containing the debug info.
587 * @param path Full path at which to look for the debug file
588 * @param crc Expected checksum for the debug file
589 * @returns 1 if the file exists and has the correct checksum,
593 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
596 struct bt_fd_cache_handle
*debug_handle
= NULL
;
603 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
608 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
617 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
622 * Try to set the dwarf_info for a given bin_info instance via the
625 * @param bin bin_info instance for which to retrieve the
626 * DWARF info via debug link
627 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
630 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
633 const gchar
*dbg_dir
= NULL
;
634 gchar
*bin_dir
= NULL
, *path
= NULL
;
636 if (!bin
|| !bin
->dbg_link_filename
) {
640 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
641 bin_dir
= g_path_get_dirname(bin
->elf_path
);
643 /* First look in the executable's dir */
644 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
646 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
650 /* If not found, look in .debug subdir */
652 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
654 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
658 /* Lastly, look under the global debug directory */
661 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
662 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
675 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
684 * Initialize the DWARF info for a given executable.
686 * @param bin bin_info instance
687 * @returns 0 on success, negative value on failure
690 int bin_info_set_dwarf_info(struct bin_info
*bin
)
699 /* First try to set the DWARF info from the ELF file */
700 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
706 * If that fails, try to find separate debug info via build ID
709 ret
= bin_info_set_dwarf_info_build_id(bin
);
714 ret
= bin_info_set_dwarf_info_debug_link(bin
);
724 void source_location_destroy(struct source_location
*src_loc
)
730 free(src_loc
->filename
);
735 * Append a string representation of an address offset to an existing
738 * On success, the out parameter `result` will contain the base string
739 * followed by the offset string of the form "+0x1234". On failure,
740 * `result` remains unchanged.
742 * @param base_str The string to which to append an offset string
743 * @param low_addr The lower virtual memory address, the base from
744 * which the offset is computed
745 * @param high_addr The higher virtual memory address
746 * @param result Out parameter, the base string followed by the
748 * @returns 0 on success, -1 on failure
751 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
752 uint64_t high_addr
, char **result
)
755 char *_result
= NULL
;
757 if (!base_str
|| !result
) {
761 offset
= high_addr
- low_addr
;
763 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
777 * Try to find the symbol closest to an address within a given ELF
780 * Only function symbols are taken into account. The symbol's address
781 * must precede `addr`. A symbol with a closer address might exist
782 * after `addr` but is irrelevant because it cannot encompass `addr`.
784 * On success, if found, the out parameters `sym` and `shdr` are
785 * set. On failure or if none are found, they remain unchanged.
787 * @param scn ELF section in which to look for the address
788 * @param addr Virtual memory address for which to find the
789 * nearest function symbol
790 * @param sym Out parameter, the nearest function symbol
791 * @param shdr Out parameter, the section header for scn
792 * @returns 0 on success, -1 on failure
795 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
796 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
800 Elf_Data
*data
= NULL
;
801 GElf_Shdr
*_shdr
= NULL
;
802 GElf_Sym
*nearest_sym
= NULL
;
804 if (!scn
|| !sym
|| !shdr
) {
808 _shdr
= g_new0(GElf_Shdr
, 1);
813 _shdr
= gelf_getshdr(scn
, _shdr
);
818 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
820 * We are only interested in symbol table (symtab)
821 * sections, skip this one.
826 data
= elf_getdata(scn
, NULL
);
831 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
833 for (i
= 0; i
< symbol_count
; ++i
) {
834 GElf_Sym
*cur_sym
= NULL
;
836 cur_sym
= g_new0(GElf_Sym
, 1);
840 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
844 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
845 /* We're only interested in the functions. */
850 if (cur_sym
->st_value
<= addr
&&
852 cur_sym
->st_value
> nearest_sym
->st_value
)) {
854 nearest_sym
= cur_sym
;
877 * Get the name of the function containing a given address within an
878 * executable using ELF symbols.
880 * The function name is in fact the name of the nearest ELF symbol,
881 * followed by the offset in bytes between the address and the symbol
882 * (in hex), separated by a '+' character.
884 * If found, the out parameter `func_name` is set on success. On failure,
885 * it remains unchanged.
887 * @param bin bin_info instance for the executable containing
889 * @param addr Virtual memory address for which to find the
891 * @param func_name Out parameter, the function name
892 * @returns 0 on success, -1 on failure
895 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
899 * TODO (possible optimisation): if an ELF has no symtab
900 * section, it has been stripped. Therefore, it would be wise
901 * to store a flag indicating the stripped status after the
902 * first iteration to prevent subsequent ones.
906 GElf_Sym
*sym
= NULL
;
907 GElf_Shdr
*shdr
= NULL
;
908 char *sym_name
= NULL
;
910 /* Set ELF file if it hasn't been accessed yet. */
911 if (!bin
->elf_file
) {
912 ret
= bin_info_set_elf_file(bin
);
914 /* Failed to set ELF file. */
919 scn
= elf_nextscn(bin
->elf_file
, scn
);
924 while (scn
&& !sym
) {
925 ret
= bin_info_get_nearest_symbol_from_section(
926 scn
, addr
, &sym
, &shdr
);
931 scn
= elf_nextscn(bin
->elf_file
, scn
);
935 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
941 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
959 * Get the name of the function containing a given address within a
960 * given compile unit (CU).
962 * If found, the out parameter `func_name` is set on success. On
963 * failure, it remains unchanged.
965 * @param cu bt_dwarf_cu instance which may contain the address
966 * @param addr Virtual memory address for which to find the
968 * @param func_name Out parameter, the function name
969 * @returns 0 on success, -1 on failure
972 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
977 struct bt_dwarf_die
*die
= NULL
;
979 if (!cu
|| !func_name
) {
983 die
= bt_dwarf_die_create(cu
);
988 while (bt_dwarf_die_next(die
) == 0) {
991 ret
= bt_dwarf_die_get_tag(die
, &tag
);
996 if (tag
== DW_TAG_subprogram
) {
997 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1009 uint64_t low_addr
= 0;
1010 char *die_name
= NULL
;
1012 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1017 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1023 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1031 bt_dwarf_die_destroy(die
);
1035 bt_dwarf_die_destroy(die
);
1040 * Get the name of the function containing a given address within an
1041 * executable using DWARF debug info.
1043 * If found, the out parameter `func_name` is set on success. On
1044 * failure, it remains unchanged.
1046 * @param bin bin_info instance for the executable containing
1048 * @param addr Virtual memory address for which to find the
1050 * @param func_name Out parameter, the function name
1051 * @returns 0 on success, -1 on failure
1054 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1058 char *_func_name
= NULL
;
1059 struct bt_dwarf_cu
*cu
= NULL
;
1061 if (!bin
|| !func_name
) {
1065 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1070 while (bt_dwarf_cu_next(cu
) == 0) {
1071 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1082 *func_name
= _func_name
;
1087 bt_dwarf_cu_destroy(cu
);
1091 bt_dwarf_cu_destroy(cu
);
1096 int bin_info_lookup_function_name(struct bin_info
*bin
,
1097 uint64_t addr
, char **func_name
)
1100 char *_func_name
= NULL
;
1102 if (!bin
|| !func_name
) {
1107 * If the bin_info has a build id but it does not match the build id
1108 * that was found on the file system, return an error.
1110 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1114 /* Set DWARF info if it hasn't been accessed yet. */
1115 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1116 ret
= bin_info_set_dwarf_info(bin
);
1118 BT_LOGI_STR("Failed to set bin dwarf info, falling "
1119 "back to ELF lookup.");
1120 /* Failed to set DWARF info, fallback to ELF. */
1121 bin
->is_elf_only
= true;
1125 if (!bin_info_has_address(bin
, addr
)) {
1130 * Addresses in ELF and DWARF are relative to base address for
1131 * PIC, so make the address argument relative too if needed.
1134 addr
-= bin
->low_addr
;
1137 if (bin
->is_elf_only
) {
1138 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1141 BT_LOGI("Failed to lookup function name (ELF): "
1145 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1148 BT_LOGI("Failed to lookup function name (DWARF): "
1153 *func_name
= _func_name
;
1161 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1163 gchar
*_bin_loc
= NULL
;
1165 if (!bin
|| !bin_loc
) {
1170 * If the bin_info has a build id but it does not match the build id
1171 * that was found on the file system, return an error.
1173 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1178 addr
-= bin
->low_addr
;
1179 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1181 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1188 *bin_loc
= _bin_loc
;
1196 * Predicate used to determine whether the children of a given DIE
1197 * contain a specific address.
1199 * More specifically, the parameter `die` is expected to be a
1200 * subprogram (function) DIE, and this predicate tells whether any
1201 * subroutines are inlined within this function and would contain
1204 * On success, the out parameter `contains` is set with the boolean
1205 * value indicating whether the DIE's range covers `addr`. On failure,
1206 * it remains unchanged.
1208 * Do note that this function advances the position of `die`. If the
1209 * address is found within one of its children, `die` will be pointing
1210 * to that child upon returning from the function, allowing to extract
1211 * the information deemed necessary.
1213 * @param die The parent DIE in whose children the address will be
1215 * @param addr The address for which to look for in the DIEs
1216 * @param contains Out parameter, true if addr is contained,
1218 * @returns Returns 0 on success, -1 on failure
1221 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1224 bool _contains
= false;
1230 ret
= bt_dwarf_die_child(die
);
1236 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1243 * The address is within the range of the current DIE
1248 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1253 if (tag
== DW_TAG_inlined_subroutine
) {
1254 /* Found the tracepoint. */
1258 if (bt_dwarf_die_has_children(die
)) {
1260 * Look for the address in the children DIEs.
1262 ret
= bt_dwarf_die_child(die
);
1268 } while (bt_dwarf_die_next(die
) == 0);
1271 *contains
= _contains
;
1279 * Lookup the source location for a given address within a CU, making
1280 * the assumption that it is contained within an inline routine in a
1283 * @param cu bt_dwarf_cu instance in which to look for the address
1284 * @param addr The address for which to look for
1285 * @param src_loc Out parameter, the source location (filename and
1286 * line number) for the address
1287 * @returns 0 on success, -1 on failure
1290 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1291 struct source_location
**src_loc
)
1295 struct bt_dwarf_die
*die
= NULL
;
1296 struct source_location
*_src_loc
= NULL
;
1298 if (!cu
|| !src_loc
) {
1302 die
= bt_dwarf_die_create(cu
);
1307 while (bt_dwarf_die_next(die
) == 0) {
1310 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1315 if (tag
== DW_TAG_subprogram
) {
1316 bool contains
= false;
1318 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1325 * Try to find an inlined subroutine
1326 * child of this DIE containing addr.
1328 ret
= bin_info_child_die_has_address(die
, addr
,
1341 char *filename
= NULL
;
1344 _src_loc
= g_new0(struct source_location
, 1);
1349 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1353 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1359 _src_loc
->filename
= filename
;
1360 _src_loc
->line_no
= line_no
;
1361 *src_loc
= _src_loc
;
1364 bt_dwarf_die_destroy(die
);
1368 source_location_destroy(_src_loc
);
1369 bt_dwarf_die_destroy(die
);
1374 * Lookup the source location for a given address within a CU,
1375 * assuming that it is contained within an inlined function.
1377 * A source location can be found regardless of inlining status for
1378 * this method, but in the case of an inlined function, the returned
1379 * source location will point not to the callsite but rather to the
1380 * definition site of the inline function.
1382 * @param cu bt_dwarf_cu instance in which to look for the address
1383 * @param addr The address for which to look for
1384 * @param src_loc Out parameter, the source location (filename and
1385 * line number) for the address. Set only if the address
1386 * is found and resolved successfully
1388 * @returns 0 on success, -1 on failure
1391 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1392 struct source_location
**src_loc
)
1394 struct source_location
*_src_loc
= NULL
;
1395 struct bt_dwarf_die
*die
= NULL
;
1396 const char *filename
= NULL
;
1397 Dwarf_Line
*line
= NULL
;
1398 Dwarf_Addr line_addr
;
1399 int ret
= 0, line_no
;
1401 if (!cu
|| !src_loc
) {
1405 die
= bt_dwarf_die_create(cu
);
1410 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1412 /* This is not an error. The caller needs to keep looking. */
1416 ret
= dwarf_lineaddr(line
, &line_addr
);
1421 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1426 if (addr
== line_addr
) {
1427 _src_loc
= g_new0(struct source_location
, 1);
1432 ret
= dwarf_lineno(line
, &line_no
);
1437 _src_loc
->line_no
= line_no
;
1438 _src_loc
->filename
= g_strdup(filename
);
1442 *src_loc
= _src_loc
;
1448 source_location_destroy(_src_loc
);
1451 bt_dwarf_die_destroy(die
);
1456 * Get the source location (file name and line number) for a given
1457 * address within a compile unit (CU).
1459 * On success, the out parameter `src_loc` is set if found. On
1460 * failure, it remains unchanged.
1462 * @param cu bt_dwarf_cu instance for the compile unit which
1463 * may contain the address
1464 * @param addr Virtual memory address for which to find the
1466 * @param src_loc Out parameter, the source location
1467 * @returns 0 on success, -1 on failure
1470 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1471 struct source_location
**src_loc
)
1474 struct source_location
*_src_loc
= NULL
;
1476 if (!cu
|| !src_loc
) {
1480 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1489 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1500 *src_loc
= _src_loc
;
1506 source_location_destroy(_src_loc
);
1511 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1512 struct source_location
**src_loc
)
1514 struct bt_dwarf_cu
*cu
= NULL
;
1515 struct source_location
*_src_loc
= NULL
;
1517 if (!bin
|| !src_loc
) {
1522 * If the bin_info has a build id but it does not match the build id
1523 * that was found on the file system, return an error.
1525 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1529 /* Set DWARF info if it hasn't been accessed yet. */
1530 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1531 if (bin_info_set_dwarf_info(bin
)) {
1532 /* Failed to set DWARF info. */
1533 bin
->is_elf_only
= true;
1537 if (bin
->is_elf_only
) {
1538 /* We cannot lookup source location without DWARF info. */
1542 if (!bin_info_has_address(bin
, addr
)) {
1547 * Addresses in ELF and DWARF are relative to base address for
1548 * PIC, so make the address argument relative too if needed.
1551 addr
-= bin
->low_addr
;
1554 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1559 while (bt_dwarf_cu_next(cu
) == 0) {
1562 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1572 bt_dwarf_cu_destroy(cu
);
1574 *src_loc
= _src_loc
;
1580 source_location_destroy(_src_loc
);
1581 bt_dwarf_cu_destroy(cu
);