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_COMP_LOG_SELF_COMP (bin->self_comp)
30 #define BT_LOG_OUTPUT_LEVEL (bin->log_level)
31 #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/BIN-INFO"
32 #include "logging/comp-logging.h"
34 #include <babeltrace2/logging.h>
48 #include "common/common.h"
56 * An address printed in hex is at most 20 bytes (16 for 64-bits +
57 * leading 0x + optional leading '+' if addr is an offset + null
60 #define ADDR_STR_LEN 20
61 #define BUILD_ID_NOTE_NAME "GNU"
64 int bin_info_init(bt_logging_level log_level
, bt_self_component
*self_comp
)
68 if (elf_version(EV_CURRENT
) == EV_NONE
) {
69 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
70 "ELF library initialization failed: %s.",
79 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
80 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
81 const char *debug_info_dir
, const char *target_prefix
,
82 bt_logging_level log_level
, bt_self_component
*self_comp
)
84 struct bin_info
*bin
= NULL
;
92 bin
= g_new0(struct bin_info
, 1);
97 bin
->log_level
= log_level
;
98 bin
->self_comp
= self_comp
;
100 bin
->elf_path
= g_build_filename(target_prefix
, path
, NULL
);
102 bin
->elf_path
= g_strdup(path
);
105 if (!bin
->elf_path
) {
109 if (debug_info_dir
) {
110 bin
->debug_info_dir
= g_strdup(debug_info_dir
);
111 if (!bin
->debug_info_dir
) {
116 bin
->is_pic
= is_pic
;
118 bin
->low_addr
= low_addr
;
119 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
120 bin
->build_id
= NULL
;
121 bin
->build_id_len
= 0;
122 bin
->file_build_id_matches
= false;
128 bin_info_destroy(bin
);
133 void bin_info_destroy(struct bin_info
*bin
)
139 dwarf_end(bin
->dwarf_info
);
141 g_free(bin
->debug_info_dir
);
142 g_free(bin
->elf_path
);
143 g_free(bin
->dwarf_path
);
144 g_free(bin
->build_id
);
145 g_free(bin
->dbg_link_filename
);
147 elf_end(bin
->elf_file
);
149 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->elf_handle
);
150 bt_fd_cache_put_handle(bin
->fd_cache
, bin
->dwarf_handle
);
156 * Initialize the ELF file for a given executable.
158 * @param bin bin_info instance
159 * @returns 0 on success, negative value on error.
162 int bin_info_set_elf_file(struct bin_info
*bin
)
164 struct bt_fd_cache_handle
*elf_handle
= NULL
;
165 Elf
*elf_file
= NULL
;
170 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
172 BT_COMP_LOGI("Failed to open %s", bin
->elf_path
);
175 bin
->elf_handle
= elf_handle
;
177 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
180 BT_COMP_LOGE("elf_begin failed: %s", elf_errmsg(-1));
184 bin
->elf_file
= elf_file
;
186 if (elf_kind(elf_file
) != ELF_K_ELF
) {
187 BT_COMP_LOGE("Error: %s is not an ELF object", bin
->elf_path
);
196 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
205 * From a note section data struct, check if it is a build id note.
207 * @param note_data Pointer to a note section
209 * @returns 1 on match, 0 if `buf` does not contain a
210 * valid build id note
213 int is_build_id_note_section(Elf_Data
*note_data
)
215 size_t name_offset
, desc_offset
;
216 GElf_Nhdr note_header
;
220 * Discard the return value as it contains the size of the note section
221 * and we don't need it.
223 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
227 * Check the note name length. The name_sz field includes the
228 * terminating null byte.
230 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
234 /* Check the note type. */
235 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
239 /* Check the note name. */
240 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
241 note_header
.n_namesz
) != 0) {
252 * From a build id note section data struct, check if the build id it contains
253 * is identical to the build id passed as parameter.
255 * @param note_data Pointer to the file build id note section.
256 * @param build_id Pointer to a build id to compare to.
257 * @param build_id_len length of the build id.
259 * @returns 1 on match, 0 otherwise.
262 int is_build_id_note_section_matching(Elf_Data
*note_data
,
263 uint8_t *build_id
, size_t build_id_len
)
265 size_t name_offset
, desc_offset
;
266 GElf_Nhdr note_header
;
268 if (build_id_len
<= 0) {
273 * Discard the return value as it contains the size of the note section
274 * and we don't need it.
276 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
280 * Compare the binary build id with the supplied build id.
282 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
283 build_id_len
) == 0) {
291 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
292 * id of the ondisk file (bin->elf_file).
294 * @param bin bin_info instance
295 * @param build_id build id to compare ot the on disk file
296 * @param build_id_len length of the build id
298 * @returns 1 on if the build id of stored in `bin` matches
299 * the build id of the ondisk file.
300 * 0 on if they are different or an error occured.
303 int is_build_id_matching(struct bin_info
*bin
)
305 int ret
, is_build_id
, is_matching
= 0;
306 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
307 GElf_Shdr curr_section_hdr
;
309 if (!bin
->build_id
) {
313 /* Set ELF file if it hasn't been accessed yet. */
314 if (!bin
->elf_file
) {
315 ret
= bin_info_set_elf_file(bin
);
317 /* Failed to set ELF file. */
322 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
327 while (next_section
) {
328 Elf_Data
*note_data
= NULL
;
330 curr_section
= next_section
;
331 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
333 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
337 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
342 * elf_getdata() translates the data to native byte order.
344 note_data
= elf_getdata(curr_section
, NULL
);
349 /* Check if the note is of the build-id type. */
350 is_build_id
= is_build_id_note_section(note_data
);
356 * Compare the build id of the on-disk file and
357 * the build id recorded in the trace.
359 is_matching
= is_build_id_note_section_matching(
360 note_data
, bin
->build_id
, bin
->build_id_len
);
370 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
373 if (!bin
|| !build_id
) {
377 /* Set the build id. */
378 bin
->build_id
= g_new0(uint8_t, build_id_len
);
379 if (!bin
->build_id
) {
383 memcpy(bin
->build_id
, build_id
, build_id_len
);
384 bin
->build_id_len
= build_id_len
;
387 * Check if the file found on the file system has the same build id
388 * that what was recorded in the trace.
390 bin
->file_build_id_matches
= is_build_id_matching(bin
);
391 if (!bin
->file_build_id_matches
) {
392 BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the "
393 "binary or library found on the file system.");
398 * Reset the is_elf_only flag in case it had been set
399 * previously, because we might find separate debug info using
400 * the new build id information.
402 bin
->is_elf_only
= false;
411 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
414 if (!bin
|| !filename
) {
418 bin
->dbg_link_filename
= g_strdup(filename
);
419 if (!bin
->dbg_link_filename
) {
423 bin
->dbg_link_crc
= crc
;
426 * Reset the is_elf_only flag in case it had been set
427 * previously, because we might find separate debug info using
428 * the new build id information.
430 bin
->is_elf_only
= false;
440 * Tries to read DWARF info from the location given by path, and
441 * attach it to the given bin_info instance if it exists.
443 * @param bin bin_info instance for which to set DWARF info
444 * @param path Presumed location of the DWARF info
445 * @returns 0 on success, negative value on failure
448 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
451 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
452 struct bt_dwarf_cu
*cu
= NULL
;
453 Dwarf
*dwarf_info
= NULL
;
459 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
464 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
471 * Check if the dwarf info has any CU. If not, the
472 * executable's object file contains no DWARF info.
474 cu
= bt_dwarf_cu_create(dwarf_info
);
479 ret
= bt_dwarf_cu_next(cu
);
484 bin
->dwarf_handle
= dwarf_handle
;
485 bin
->dwarf_path
= g_strdup(path
);
486 if (!bin
->dwarf_path
) {
489 bin
->dwarf_info
= dwarf_info
;
496 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
498 dwarf_end(dwarf_info
);
506 * Try to set the dwarf_info for a given bin_info instance via the
509 * @param bin bin_info instance for which to retrieve the
510 * DWARF info via build ID
511 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
514 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
517 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
518 const char *dbg_dir
= NULL
;
519 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
521 if (!bin
|| !bin
->build_id
) {
525 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
528 * The prefix dir is the first byte of the build id, represented in
529 * lowercase hex as two characters per byte, +1 for '\0'.
531 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
532 if (!build_id_prefix_dir
) {
535 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
538 * The build id file is the remaining bytes of the build id,
539 * represented in lowercase hex, as two characters per byte.
541 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
543 /* To which the build id suffix is added, +1 for '\0'. */
544 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
547 * The resulting filename string is the concatenation of the
548 * hex build id and the suffix.
550 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
551 build_id_file
= g_new0(gchar
, build_id_file_len
);
552 if (!build_id_file
) {
557 * For each byte, starting at offset 1, append two characters
560 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
561 int path_idx
= 2 * (i
- 1);
563 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
565 /* Append the suffix to the generated string, including the '\0'. */
566 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
569 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
574 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
584 g_free(build_id_prefix_dir
);
585 g_free(build_id_file
);
592 * Tests whether the file located at path exists and has the expected
595 * This predicate is used when looking up separate debug info via the
596 * GNU debuglink method. The expected crc can be found .gnu_debuglink
597 * section in the original ELF file, along with the filename for the
598 * file containing the debug info.
600 * @param path Full path at which to look for the debug file
601 * @param crc Expected checksum for the debug file
602 * @returns 1 if the file exists and has the correct checksum,
606 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
609 struct bt_fd_cache_handle
*debug_handle
= NULL
;
616 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
621 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
630 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
635 * Try to set the dwarf_info for a given bin_info instance via the
638 * @param bin bin_info instance for which to retrieve the
639 * DWARF info via debug link
640 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
643 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
646 const gchar
*dbg_dir
= NULL
;
647 gchar
*bin_dir
= NULL
, *path
= NULL
;
649 if (!bin
|| !bin
->dbg_link_filename
) {
653 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
654 bin_dir
= g_path_get_dirname(bin
->elf_path
);
656 /* First look in the executable's dir */
657 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
659 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
663 /* If not found, look in .debug subdir */
665 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
667 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
671 /* Lastly, look under the global debug directory */
674 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
675 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
688 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
697 * Initialize the DWARF info for a given executable.
699 * @param bin bin_info instance
700 * @returns 0 on success, negative value on failure
703 int bin_info_set_dwarf_info(struct bin_info
*bin
)
712 /* First try to set the DWARF info from the ELF file */
713 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
719 * If that fails, try to find separate debug info via build ID
722 ret
= bin_info_set_dwarf_info_build_id(bin
);
727 ret
= bin_info_set_dwarf_info_debug_link(bin
);
737 void source_location_destroy(struct source_location
*src_loc
)
743 free(src_loc
->filename
);
748 * Append a string representation of an address offset to an existing
751 * On success, the out parameter `result` will contain the base string
752 * followed by the offset string of the form "+0x1234". On failure,
753 * `result` remains unchanged.
755 * @param base_str The string to which to append an offset string
756 * @param low_addr The lower virtual memory address, the base from
757 * which the offset is computed
758 * @param high_addr The higher virtual memory address
759 * @param result Out parameter, the base string followed by the
761 * @returns 0 on success, -1 on failure
764 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
765 uint64_t high_addr
, char **result
)
768 char *_result
= NULL
;
770 if (!base_str
|| !result
) {
774 offset
= high_addr
- low_addr
;
776 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
790 * Try to find the symbol closest to an address within a given ELF
793 * Only function symbols are taken into account. The symbol's address
794 * must precede `addr`. A symbol with a closer address might exist
795 * after `addr` but is irrelevant because it cannot encompass `addr`.
797 * On success, if found, the out parameters `sym` and `shdr` are
798 * set. On failure or if none are found, they remain unchanged.
800 * @param scn ELF section in which to look for the address
801 * @param addr Virtual memory address for which to find the
802 * nearest function symbol
803 * @param sym Out parameter, the nearest function symbol
804 * @param shdr Out parameter, the section header for scn
805 * @returns 0 on success, -1 on failure
808 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
809 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
813 Elf_Data
*data
= NULL
;
814 GElf_Shdr
*_shdr
= NULL
;
815 GElf_Sym
*nearest_sym
= NULL
;
817 if (!scn
|| !sym
|| !shdr
) {
821 _shdr
= g_new0(GElf_Shdr
, 1);
826 _shdr
= gelf_getshdr(scn
, _shdr
);
831 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
833 * We are only interested in symbol table (symtab)
834 * sections, skip this one.
839 data
= elf_getdata(scn
, NULL
);
844 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
846 for (i
= 0; i
< symbol_count
; ++i
) {
847 GElf_Sym
*cur_sym
= NULL
;
849 cur_sym
= g_new0(GElf_Sym
, 1);
853 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
857 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
858 /* We're only interested in the functions. */
863 if (cur_sym
->st_value
<= addr
&&
865 cur_sym
->st_value
> nearest_sym
->st_value
)) {
867 nearest_sym
= cur_sym
;
890 * Get the name of the function containing a given address within an
891 * executable using ELF symbols.
893 * The function name is in fact the name of the nearest ELF symbol,
894 * followed by the offset in bytes between the address and the symbol
895 * (in hex), separated by a '+' character.
897 * If found, the out parameter `func_name` is set on success. On failure,
898 * it remains unchanged.
900 * @param bin bin_info instance for the executable containing
902 * @param addr Virtual memory address for which to find the
904 * @param func_name Out parameter, the function name
905 * @returns 0 on success, -1 on failure
908 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
912 * TODO (possible optimisation): if an ELF has no symtab
913 * section, it has been stripped. Therefore, it would be wise
914 * to store a flag indicating the stripped status after the
915 * first iteration to prevent subsequent ones.
919 GElf_Sym
*sym
= NULL
;
920 GElf_Shdr
*shdr
= NULL
;
921 char *sym_name
= NULL
;
923 /* Set ELF file if it hasn't been accessed yet. */
924 if (!bin
->elf_file
) {
925 ret
= bin_info_set_elf_file(bin
);
927 /* Failed to set ELF file. */
932 scn
= elf_nextscn(bin
->elf_file
, scn
);
937 while (scn
&& !sym
) {
938 ret
= bin_info_get_nearest_symbol_from_section(
939 scn
, addr
, &sym
, &shdr
);
944 scn
= elf_nextscn(bin
->elf_file
, scn
);
948 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
954 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
972 * Get the name of the function containing a given address within a
973 * given compile unit (CU).
975 * If found, the out parameter `func_name` is set on success. On
976 * failure, it remains unchanged.
978 * @param cu bt_dwarf_cu instance which may contain the address
979 * @param addr Virtual memory address for which to find the
981 * @param func_name Out parameter, the function name
982 * @returns 0 on success, -1 on failure
985 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
990 struct bt_dwarf_die
*die
= NULL
;
992 if (!cu
|| !func_name
) {
996 die
= bt_dwarf_die_create(cu
);
1001 while (bt_dwarf_die_next(die
) == 0) {
1004 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1009 if (tag
== DW_TAG_subprogram
) {
1010 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1022 uint64_t low_addr
= 0;
1023 char *die_name
= NULL
;
1025 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1030 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1036 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1044 bt_dwarf_die_destroy(die
);
1048 bt_dwarf_die_destroy(die
);
1053 * Get the name of the function containing a given address within an
1054 * executable using DWARF debug info.
1056 * If found, the out parameter `func_name` is set on success. On
1057 * failure, it remains unchanged.
1059 * @param bin bin_info instance for the executable containing
1061 * @param addr Virtual memory address for which to find the
1063 * @param func_name Out parameter, the function name
1064 * @returns 0 on success, -1 on failure
1067 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1071 char *_func_name
= NULL
;
1072 struct bt_dwarf_cu
*cu
= NULL
;
1074 if (!bin
|| !func_name
) {
1078 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1083 while (bt_dwarf_cu_next(cu
) == 0) {
1084 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1095 *func_name
= _func_name
;
1100 bt_dwarf_cu_destroy(cu
);
1104 bt_dwarf_cu_destroy(cu
);
1109 int bin_info_lookup_function_name(struct bin_info
*bin
,
1110 uint64_t addr
, char **func_name
)
1113 char *_func_name
= NULL
;
1115 if (!bin
|| !func_name
) {
1120 * If the bin_info has a build id but it does not match the build id
1121 * that was found on the file system, return an error.
1123 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1127 /* Set DWARF info if it hasn't been accessed yet. */
1128 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1129 ret
= bin_info_set_dwarf_info(bin
);
1131 BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling "
1132 "back to ELF lookup.");
1133 /* Failed to set DWARF info, fallback to ELF. */
1134 bin
->is_elf_only
= true;
1138 if (!bin_info_has_address(bin
, addr
)) {
1143 * Addresses in ELF and DWARF are relative to base address for
1144 * PIC, so make the address argument relative too if needed.
1147 addr
-= bin
->low_addr
;
1150 if (bin
->is_elf_only
) {
1151 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1154 BT_COMP_LOGI("Failed to lookup function name (ELF): "
1158 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1161 BT_COMP_LOGI("Failed to lookup function name (DWARF): "
1166 *func_name
= _func_name
;
1174 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1176 gchar
*_bin_loc
= NULL
;
1178 if (!bin
|| !bin_loc
) {
1183 * If the bin_info has a build id but it does not match the build id
1184 * that was found on the file system, return an error.
1186 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1191 addr
-= bin
->low_addr
;
1192 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1194 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1201 *bin_loc
= _bin_loc
;
1209 * Predicate used to determine whether the children of a given DIE
1210 * contain a specific address.
1212 * More specifically, the parameter `die` is expected to be a
1213 * subprogram (function) DIE, and this predicate tells whether any
1214 * subroutines are inlined within this function and would contain
1217 * On success, the out parameter `contains` is set with the boolean
1218 * value indicating whether the DIE's range covers `addr`. On failure,
1219 * it remains unchanged.
1221 * Do note that this function advances the position of `die`. If the
1222 * address is found within one of its children, `die` will be pointing
1223 * to that child upon returning from the function, allowing to extract
1224 * the information deemed necessary.
1226 * @param die The parent DIE in whose children the address will be
1228 * @param addr The address for which to look for in the DIEs
1229 * @param contains Out parameter, true if addr is contained,
1231 * @returns Returns 0 on success, -1 on failure
1234 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1237 bool _contains
= false;
1243 ret
= bt_dwarf_die_child(die
);
1249 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1256 * The address is within the range of the current DIE
1261 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1266 if (tag
== DW_TAG_inlined_subroutine
) {
1267 /* Found the tracepoint. */
1271 if (bt_dwarf_die_has_children(die
)) {
1273 * Look for the address in the children DIEs.
1275 ret
= bt_dwarf_die_child(die
);
1281 } while (bt_dwarf_die_next(die
) == 0);
1284 *contains
= _contains
;
1292 * Lookup the source location for a given address within a CU, making
1293 * the assumption that it is contained within an inline routine in a
1296 * @param cu bt_dwarf_cu instance in which to look for the address
1297 * @param addr The address for which to look for
1298 * @param src_loc Out parameter, the source location (filename and
1299 * line number) for the address
1300 * @returns 0 on success, -1 on failure
1303 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1304 struct source_location
**src_loc
)
1308 struct bt_dwarf_die
*die
= NULL
;
1309 struct source_location
*_src_loc
= NULL
;
1311 if (!cu
|| !src_loc
) {
1315 die
= bt_dwarf_die_create(cu
);
1320 while (bt_dwarf_die_next(die
) == 0) {
1323 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1328 if (tag
== DW_TAG_subprogram
) {
1329 bool contains
= false;
1331 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1338 * Try to find an inlined subroutine
1339 * child of this DIE containing addr.
1341 ret
= bin_info_child_die_has_address(die
, addr
,
1354 char *filename
= NULL
;
1357 _src_loc
= g_new0(struct source_location
, 1);
1362 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1366 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1372 _src_loc
->filename
= filename
;
1373 _src_loc
->line_no
= line_no
;
1374 *src_loc
= _src_loc
;
1377 bt_dwarf_die_destroy(die
);
1381 source_location_destroy(_src_loc
);
1382 bt_dwarf_die_destroy(die
);
1387 * Lookup the source location for a given address within a CU,
1388 * assuming that it is contained within an inlined function.
1390 * A source location can be found regardless of inlining status for
1391 * this method, but in the case of an inlined function, the returned
1392 * source location will point not to the callsite but rather to the
1393 * definition site of the inline function.
1395 * @param cu bt_dwarf_cu instance in which to look for the address
1396 * @param addr The address for which to look for
1397 * @param src_loc Out parameter, the source location (filename and
1398 * line number) for the address. Set only if the address
1399 * is found and resolved successfully
1401 * @returns 0 on success, -1 on failure
1404 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1405 struct source_location
**src_loc
)
1407 struct source_location
*_src_loc
= NULL
;
1408 struct bt_dwarf_die
*die
= NULL
;
1409 const char *filename
= NULL
;
1410 Dwarf_Line
*line
= NULL
;
1411 Dwarf_Addr line_addr
;
1412 int ret
= 0, line_no
;
1414 if (!cu
|| !src_loc
) {
1418 die
= bt_dwarf_die_create(cu
);
1423 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1425 /* This is not an error. The caller needs to keep looking. */
1429 ret
= dwarf_lineaddr(line
, &line_addr
);
1434 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1439 if (addr
== line_addr
) {
1440 _src_loc
= g_new0(struct source_location
, 1);
1445 ret
= dwarf_lineno(line
, &line_no
);
1450 _src_loc
->line_no
= line_no
;
1451 _src_loc
->filename
= g_strdup(filename
);
1455 *src_loc
= _src_loc
;
1461 source_location_destroy(_src_loc
);
1464 bt_dwarf_die_destroy(die
);
1469 * Get the source location (file name and line number) for a given
1470 * address within a compile unit (CU).
1472 * On success, the out parameter `src_loc` is set if found. On
1473 * failure, it remains unchanged.
1475 * @param cu bt_dwarf_cu instance for the compile unit which
1476 * may contain the address
1477 * @param addr Virtual memory address for which to find the
1479 * @param src_loc Out parameter, the source location
1480 * @returns 0 on success, -1 on failure
1483 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1484 struct source_location
**src_loc
)
1487 struct source_location
*_src_loc
= NULL
;
1489 if (!cu
|| !src_loc
) {
1493 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1502 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1513 *src_loc
= _src_loc
;
1519 source_location_destroy(_src_loc
);
1524 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1525 struct source_location
**src_loc
)
1527 struct bt_dwarf_cu
*cu
= NULL
;
1528 struct source_location
*_src_loc
= NULL
;
1530 if (!bin
|| !src_loc
) {
1535 * If the bin_info has a build id but it does not match the build id
1536 * that was found on the file system, return an error.
1538 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1542 /* Set DWARF info if it hasn't been accessed yet. */
1543 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1544 if (bin_info_set_dwarf_info(bin
)) {
1545 /* Failed to set DWARF info. */
1546 bin
->is_elf_only
= true;
1550 if (bin
->is_elf_only
) {
1551 /* We cannot lookup source location without DWARF info. */
1555 if (!bin_info_has_address(bin
, addr
)) {
1560 * Addresses in ELF and DWARF are relative to base address for
1561 * PIC, so make the address argument relative too if needed.
1564 addr
-= bin
->low_addr
;
1567 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1572 while (bt_dwarf_cu_next(cu
) == 0) {
1575 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1585 bt_dwarf_cu_destroy(cu
);
1587 *src_loc
= _src_loc
;
1593 source_location_destroy(_src_loc
);
1594 bt_dwarf_cu_destroy(cu
);