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_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_file_len
= (2 * bin
->build_id_len
) + 1 +
522 strlen(BUILD_ID_SUFFIX
) + 1;
523 build_id_file
= g_new0(gchar
, build_id_file_len
);
524 if (!build_id_file
) {
528 g_snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
529 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
530 int path_idx
= 3 + 2 * (i
- 1);
532 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
534 g_strconcat(build_id_file
, BUILD_ID_SUFFIX
, NULL
);
536 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
541 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
558 * Tests whether the file located at path exists and has the expected
561 * This predicate is used when looking up separate debug info via the
562 * GNU debuglink method. The expected crc can be found .gnu_debuglink
563 * section in the original ELF file, along with the filename for the
564 * file containing the debug info.
566 * @param path Full path at which to look for the debug file
567 * @param crc Expected checksum for the debug file
568 * @returns 1 if the file exists and has the correct checksum,
572 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
575 struct bt_fd_cache_handle
*debug_handle
= NULL
;
582 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
587 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
596 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
601 * Try to set the dwarf_info for a given bin_info instance via the
604 * @param bin bin_info instance for which to retrieve the
605 * DWARF info via debug link
606 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
609 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
612 const gchar
*dbg_dir
= NULL
;
613 gchar
*bin_dir
= NULL
, *dir_name
= NULL
, *path
= NULL
;
615 if (!bin
|| !bin
->dbg_link_filename
) {
619 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
620 dir_name
= g_path_get_dirname(bin
->elf_path
);
625 bin_dir
= g_strconcat(dir_name
, "/", NULL
);
627 /* First look in the executable's dir */
628 path
= g_strconcat(bin_dir
, bin
->dbg_link_filename
, NULL
);
630 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
634 /* If not found, look in .debug subdir */
636 path
= g_strconcat(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
638 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
642 /* Lastly, look under the global debug directory */
645 path
= g_strconcat(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
646 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
659 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
668 * Initialize the DWARF info for a given executable.
670 * @param bin bin_info instance
671 * @returns 0 on success, negative value on failure
674 int bin_info_set_dwarf_info(struct bin_info
*bin
)
683 /* First try to set the DWARF info from the ELF file */
684 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
690 * If that fails, try to find separate debug info via build ID
693 ret
= bin_info_set_dwarf_info_build_id(bin
);
698 ret
= bin_info_set_dwarf_info_debug_link(bin
);
708 void source_location_destroy(struct source_location
*src_loc
)
714 free(src_loc
->filename
);
719 * Append a string representation of an address offset to an existing
722 * On success, the out parameter `result` will contain the base string
723 * followed by the offset string of the form "+0x1234". On failure,
724 * `result` remains unchanged.
726 * @param base_str The string to which to append an offset string
727 * @param low_addr The lower virtual memory address, the base from
728 * which the offset is computed
729 * @param high_addr The higher virtual memory address
730 * @param result Out parameter, the base string followed by the
732 * @returns 0 on success, -1 on failure
735 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
736 uint64_t high_addr
, char **result
)
739 char *_result
= NULL
;
742 if (!base_str
|| !result
) {
746 offset
= high_addr
- low_addr
;
748 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
762 * Try to find the symbol closest to an address within a given ELF
765 * Only function symbols are taken into account. The symbol's address
766 * must precede `addr`. A symbol with a closer address might exist
767 * after `addr` but is irrelevant because it cannot encompass `addr`.
769 * On success, if found, the out parameters `sym` and `shdr` are
770 * set. On failure or if none are found, they remain unchanged.
772 * @param scn ELF section in which to look for the address
773 * @param addr Virtual memory address for which to find the
774 * nearest function symbol
775 * @param sym Out parameter, the nearest function symbol
776 * @param shdr Out parameter, the section header for scn
777 * @returns 0 on success, -1 on failure
780 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
781 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
785 Elf_Data
*data
= NULL
;
786 GElf_Shdr
*_shdr
= NULL
;
787 GElf_Sym
*nearest_sym
= NULL
;
789 if (!scn
|| !sym
|| !shdr
) {
793 _shdr
= g_new0(GElf_Shdr
, 1);
798 _shdr
= gelf_getshdr(scn
, _shdr
);
803 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
805 * We are only interested in symbol table (symtab)
806 * sections, skip this one.
811 data
= elf_getdata(scn
, NULL
);
816 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
818 for (i
= 0; i
< symbol_count
; ++i
) {
819 GElf_Sym
*cur_sym
= NULL
;
821 cur_sym
= g_new0(GElf_Sym
, 1);
825 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
829 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
830 /* We're only interested in the functions. */
835 if (cur_sym
->st_value
<= addr
&&
837 cur_sym
->st_value
> nearest_sym
->st_value
)) {
839 nearest_sym
= cur_sym
;
862 * Get the name of the function containing a given address within an
863 * executable using ELF symbols.
865 * The function name is in fact the name of the nearest ELF symbol,
866 * followed by the offset in bytes between the address and the symbol
867 * (in hex), separated by a '+' character.
869 * If found, the out parameter `func_name` is set on success. On failure,
870 * it remains unchanged.
872 * @param bin bin_info instance for the executable containing
874 * @param addr Virtual memory address for which to find the
876 * @param func_name Out parameter, the function name
877 * @returns 0 on success, -1 on failure
880 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
884 * TODO (possible optimisation): if an ELF has no symtab
885 * section, it has been stripped. Therefore, it would be wise
886 * to store a flag indicating the stripped status after the
887 * first iteration to prevent subsequent ones.
891 GElf_Sym
*sym
= NULL
;
892 GElf_Shdr
*shdr
= NULL
;
893 char *sym_name
= NULL
;
895 /* Set ELF file if it hasn't been accessed yet. */
896 if (!bin
->elf_file
) {
897 ret
= bin_info_set_elf_file(bin
);
899 /* Failed to set ELF file. */
904 scn
= elf_nextscn(bin
->elf_file
, scn
);
909 while (scn
&& !sym
) {
910 ret
= bin_info_get_nearest_symbol_from_section(
911 scn
, addr
, &sym
, &shdr
);
916 scn
= elf_nextscn(bin
->elf_file
, scn
);
920 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
926 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
944 * Get the name of the function containing a given address within a
945 * given compile unit (CU).
947 * If found, the out parameter `func_name` is set on success. On
948 * failure, it remains unchanged.
950 * @param cu bt_dwarf_cu instance which may contain the address
951 * @param addr Virtual memory address for which to find the
953 * @param func_name Out parameter, the function name
954 * @returns 0 on success, -1 on failure
957 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
962 struct bt_dwarf_die
*die
= NULL
;
964 if (!cu
|| !func_name
) {
968 die
= bt_dwarf_die_create(cu
);
973 while (bt_dwarf_die_next(die
) == 0) {
976 ret
= bt_dwarf_die_get_tag(die
, &tag
);
981 if (tag
== DW_TAG_subprogram
) {
982 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
994 uint64_t low_addr
= 0;
995 char *die_name
= NULL
;
997 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1002 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1008 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1016 bt_dwarf_die_destroy(die
);
1020 bt_dwarf_die_destroy(die
);
1025 * Get the name of the function containing a given address within an
1026 * executable using DWARF debug info.
1028 * If found, the out parameter `func_name` is set on success. On
1029 * failure, it remains unchanged.
1031 * @param bin bin_info instance for the executable containing
1033 * @param addr Virtual memory address for which to find the
1035 * @param func_name Out parameter, the function name
1036 * @returns 0 on success, -1 on failure
1039 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1043 char *_func_name
= NULL
;
1044 struct bt_dwarf_cu
*cu
= NULL
;
1046 if (!bin
|| !func_name
) {
1050 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1055 while (bt_dwarf_cu_next(cu
) == 0) {
1056 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1067 *func_name
= _func_name
;
1072 bt_dwarf_cu_destroy(cu
);
1076 bt_dwarf_cu_destroy(cu
);
1081 int bin_info_lookup_function_name(struct bin_info
*bin
,
1082 uint64_t addr
, char **func_name
)
1085 char *_func_name
= NULL
;
1087 if (!bin
|| !func_name
) {
1092 * If the bin_info has a build id but it does not match the build id
1093 * that was found on the file system, return an error.
1095 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1099 /* Set DWARF info if it hasn't been accessed yet. */
1100 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1101 ret
= bin_info_set_dwarf_info(bin
);
1103 BT_LOGD_STR("Failed to set bin dwarf info, falling "
1104 "back to ELF lookup.");
1105 /* Failed to set DWARF info, fallback to ELF. */
1106 bin
->is_elf_only
= true;
1110 if (!bin_info_has_address(bin
, addr
)) {
1115 * Addresses in ELF and DWARF are relative to base address for
1116 * PIC, so make the address argument relative too if needed.
1119 addr
-= bin
->low_addr
;
1122 if (bin
->is_elf_only
) {
1123 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1126 BT_LOGD("Failed to lookup function name (ELF): "
1130 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1133 BT_LOGD("Failed to lookup function name (DWARF): "
1138 *func_name
= _func_name
;
1146 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1148 gchar
*_bin_loc
= NULL
;
1150 if (!bin
|| !bin_loc
) {
1155 * If the bin_info has a build id but it does not match the build id
1156 * that was found on the file system, return an error.
1158 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1163 addr
-= bin
->low_addr
;
1164 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1166 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1173 *bin_loc
= _bin_loc
;
1181 * Predicate used to determine whether the children of a given DIE
1182 * contain a specific address.
1184 * More specifically, the parameter `die` is expected to be a
1185 * subprogram (function) DIE, and this predicate tells whether any
1186 * subroutines are inlined within this function and would contain
1189 * On success, the out parameter `contains` is set with the boolean
1190 * value indicating whether the DIE's range covers `addr`. On failure,
1191 * it remains unchanged.
1193 * Do note that this function advances the position of `die`. If the
1194 * address is found within one of its children, `die` will be pointing
1195 * to that child upon returning from the function, allowing to extract
1196 * the information deemed necessary.
1198 * @param die The parent DIE in whose children the address will be
1200 * @param addr The address for which to look for in the DIEs
1201 * @param contains Out parameter, true if addr is contained,
1203 * @returns Returns 0 on success, -1 on failure
1206 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1209 bool _contains
= false;
1215 ret
= bt_dwarf_die_child(die
);
1221 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1228 * The address is within the range of the current DIE
1233 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1238 if (tag
== DW_TAG_inlined_subroutine
) {
1239 /* Found the tracepoint. */
1243 if (bt_dwarf_die_has_children(die
)) {
1245 * Look for the address in the children DIEs.
1247 ret
= bt_dwarf_die_child(die
);
1253 } while (bt_dwarf_die_next(die
) == 0);
1256 *contains
= _contains
;
1264 * Lookup the source location for a given address within a CU, making
1265 * the assumption that it is contained within an inline routine in a
1268 * @param cu bt_dwarf_cu instance in which to look for the address
1269 * @param addr The address for which to look for
1270 * @param src_loc Out parameter, the source location (filename and
1271 * line number) for the address
1272 * @returns 0 on success, -1 on failure
1275 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1276 struct source_location
**src_loc
)
1280 struct bt_dwarf_die
*die
= NULL
;
1281 struct source_location
*_src_loc
= NULL
;
1283 if (!cu
|| !src_loc
) {
1287 die
= bt_dwarf_die_create(cu
);
1292 while (bt_dwarf_die_next(die
) == 0) {
1295 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1300 if (tag
== DW_TAG_subprogram
) {
1301 bool contains
= false;
1303 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1310 * Try to find an inlined subroutine
1311 * child of this DIE containing addr.
1313 ret
= bin_info_child_die_has_address(die
, addr
,
1326 char *filename
= NULL
;
1329 _src_loc
= g_new0(struct source_location
, 1);
1334 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1338 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1344 _src_loc
->filename
= filename
;
1345 _src_loc
->line_no
= line_no
;
1346 *src_loc
= _src_loc
;
1349 bt_dwarf_die_destroy(die
);
1353 source_location_destroy(_src_loc
);
1354 bt_dwarf_die_destroy(die
);
1359 * Lookup the source location for a given address within a CU,
1360 * assuming that it is contained within an inlined function.
1362 * A source location can be found regardless of inlining status for
1363 * this method, but in the case of an inlined function, the returned
1364 * source location will point not to the callsite but rather to the
1365 * definition site of the inline function.
1367 * @param cu bt_dwarf_cu instance in which to look for the address
1368 * @param addr The address for which to look for
1369 * @param src_loc Out parameter, the source location (filename and
1370 * line number) for the address
1371 * @returns 0 on success, -1 on failure
1374 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1375 struct source_location
**src_loc
)
1377 struct source_location
*_src_loc
= NULL
;
1378 struct bt_dwarf_die
*die
= NULL
;
1379 const char *filename
= NULL
;
1380 Dwarf_Line
*line
= NULL
;
1381 Dwarf_Addr line_addr
;
1384 if (!cu
|| !src_loc
) {
1388 die
= bt_dwarf_die_create(cu
);
1393 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1398 ret
= dwarf_lineaddr(line
, &line_addr
);
1403 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1408 if (addr
== line_addr
) {
1409 _src_loc
= g_new0(struct source_location
, 1);
1414 ret
= dwarf_lineno(line
, &line_no
);
1419 _src_loc
->line_no
= line_no
;
1420 _src_loc
->filename
= g_strdup(filename
);
1423 bt_dwarf_die_destroy(die
);
1426 *src_loc
= _src_loc
;
1432 source_location_destroy(_src_loc
);
1433 bt_dwarf_die_destroy(die
);
1438 * Get the source location (file name and line number) for a given
1439 * address within a compile unit (CU).
1441 * On success, the out parameter `src_loc` is set if found. On
1442 * failure, it remains unchanged.
1444 * @param cu bt_dwarf_cu instance for the compile unit which
1445 * may contain the address
1446 * @param addr Virtual memory address for which to find the
1448 * @param src_loc Out parameter, the source location
1449 * @returns 0 on success, -1 on failure
1452 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1453 struct source_location
**src_loc
)
1456 struct source_location
*_src_loc
= NULL
;
1458 if (!cu
|| !src_loc
) {
1462 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1471 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1482 *src_loc
= _src_loc
;
1488 source_location_destroy(_src_loc
);
1493 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1494 struct source_location
**src_loc
)
1496 struct bt_dwarf_cu
*cu
= NULL
;
1497 struct source_location
*_src_loc
= NULL
;
1499 if (!bin
|| !src_loc
) {
1504 * If the bin_info has a build id but it does not match the build id
1505 * that was found on the file system, return an error.
1507 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1511 /* Set DWARF info if it hasn't been accessed yet. */
1512 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1513 if (bin_info_set_dwarf_info(bin
)) {
1514 /* Failed to set DWARF info. */
1515 bin
->is_elf_only
= true;
1519 if (bin
->is_elf_only
) {
1520 /* We cannot lookup source location without DWARF info. */
1524 if (!bin_info_has_address(bin
, addr
)) {
1529 * Addresses in ELF and DWARF are relative to base address for
1530 * PIC, so make the address argument relative too if needed.
1533 addr
-= bin
->low_addr
;
1536 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1541 while (bt_dwarf_cu_next(cu
) == 0) {
1544 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1554 bt_dwarf_cu_destroy(cu
);
1556 *src_loc
= _src_loc
;
1562 source_location_destroy(_src_loc
);
1563 bt_dwarf_cu_destroy(cu
);