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
;
171 elf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, bin
->elf_path
);
173 BT_COMP_LOGI("Failed to open %s", bin
->elf_path
);
176 bin
->elf_handle
= elf_handle
;
178 elf_file
= elf_begin(bt_fd_cache_handle_get_fd(bin
->elf_handle
),
181 BT_COMP_LOGE("elf_begin failed: %s", elf_errmsg(-1));
185 bin
->elf_file
= elf_file
;
187 if (elf_kind(elf_file
) != ELF_K_ELF
) {
188 BT_COMP_LOGE("Error: %s is not an ELF object", bin
->elf_path
);
195 bt_fd_cache_put_handle(bin
->fd_cache
, elf_handle
);
201 * From a note section data struct, check if it is a build id note.
203 * @param note_data Pointer to a note section
205 * @returns 1 on match, 0 if `buf` does not contain a
206 * valid build id note
209 int is_build_id_note_section(Elf_Data
*note_data
)
211 size_t name_offset
, desc_offset
;
212 GElf_Nhdr note_header
;
216 * Discard the return value as it contains the size of the note section
217 * and we don't need it.
219 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
223 * Check the note name length. The name_sz field includes the
224 * terminating null byte.
226 if (note_header
.n_namesz
!= sizeof(BUILD_ID_NOTE_NAME
)) {
230 /* Check the note type. */
231 if (note_header
.n_type
!= NT_GNU_BUILD_ID
) {
235 /* Check the note name. */
236 if (memcmp(note_data
->d_buf
+ name_offset
, BUILD_ID_NOTE_NAME
,
237 note_header
.n_namesz
) != 0) {
248 * From a build id note section data struct, check if the build id it contains
249 * is identical to the build id passed as parameter.
251 * @param note_data Pointer to the file build id note section.
252 * @param build_id Pointer to a build id to compare to.
253 * @param build_id_len length of the build id.
255 * @returns 1 on match, 0 otherwise.
258 int is_build_id_note_section_matching(Elf_Data
*note_data
,
259 uint8_t *build_id
, size_t build_id_len
)
261 size_t name_offset
, desc_offset
;
262 GElf_Nhdr note_header
;
264 if (build_id_len
<= 0) {
269 * Discard the return value as it contains the size of the note section
270 * and we don't need it.
272 (void) gelf_getnote(note_data
, 0, ¬e_header
, &name_offset
,
276 * Compare the binary build id with the supplied build id.
278 if (memcmp(build_id
, note_data
->d_buf
+ desc_offset
,
279 build_id_len
) == 0) {
287 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
288 * id of the ondisk file (bin->elf_file).
290 * @param bin bin_info instance
291 * @param build_id build id to compare ot the on disk file
292 * @param build_id_len length of the build id
294 * @returns 1 on if the build id of stored in `bin` matches
295 * the build id of the ondisk file.
296 * 0 on if they are different or an error occured.
299 int is_build_id_matching(struct bin_info
*bin
)
301 int ret
, is_build_id
, is_matching
= 0;
302 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
303 GElf_Shdr curr_section_hdr
;
305 if (!bin
->build_id
) {
309 /* Set ELF file if it hasn't been accessed yet. */
310 if (!bin
->elf_file
) {
311 ret
= bin_info_set_elf_file(bin
);
313 /* Failed to set ELF file. */
318 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
323 while (next_section
) {
324 Elf_Data
*note_data
= NULL
;
326 curr_section
= next_section
;
327 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
329 if (!gelf_getshdr(curr_section
, &curr_section_hdr
)) {
333 if (curr_section_hdr
.sh_type
!= SHT_NOTE
) {
338 * elf_getdata() translates the data to native byte order.
340 note_data
= elf_getdata(curr_section
, NULL
);
345 /* Check if the note is of the build-id type. */
346 is_build_id
= is_build_id_note_section(note_data
);
352 * Compare the build id of the on-disk file and
353 * the build id recorded in the trace.
355 is_matching
= is_build_id_note_section_matching(
356 note_data
, bin
->build_id
, bin
->build_id_len
);
366 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
369 if (!bin
|| !build_id
) {
373 /* Set the build id. */
374 bin
->build_id
= g_new0(uint8_t, build_id_len
);
375 if (!bin
->build_id
) {
379 memcpy(bin
->build_id
, build_id
, build_id_len
);
380 bin
->build_id_len
= build_id_len
;
383 * Check if the file found on the file system has the same build id
384 * that what was recorded in the trace.
386 bin
->file_build_id_matches
= is_build_id_matching(bin
);
387 if (!bin
->file_build_id_matches
) {
388 BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the "
389 "binary or library found on the file system.");
394 * Reset the is_elf_only flag in case it had been set
395 * previously, because we might find separate debug info using
396 * the new build id information.
398 bin
->is_elf_only
= false;
407 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
410 if (!bin
|| !filename
) {
414 bin
->dbg_link_filename
= g_strdup(filename
);
415 if (!bin
->dbg_link_filename
) {
419 bin
->dbg_link_crc
= crc
;
422 * Reset the is_elf_only flag in case it had been set
423 * previously, because we might find separate debug info using
424 * the new build id information.
426 bin
->is_elf_only
= false;
436 * Tries to read DWARF info from the location given by path, and
437 * attach it to the given bin_info instance if it exists.
439 * @param bin bin_info instance for which to set DWARF info
440 * @param path Presumed location of the DWARF info
441 * @returns 0 on success, negative value on failure
444 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
447 struct bt_fd_cache_handle
*dwarf_handle
= NULL
;
448 struct bt_dwarf_cu
*cu
= NULL
;
449 Dwarf
*dwarf_info
= NULL
;
455 dwarf_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
460 dwarf_info
= dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle
),
467 * Check if the dwarf info has any CU. If not, the
468 * executable's object file contains no DWARF info.
470 cu
= bt_dwarf_cu_create(dwarf_info
);
475 ret
= bt_dwarf_cu_next(cu
);
480 bin
->dwarf_handle
= dwarf_handle
;
481 bin
->dwarf_path
= g_strdup(path
);
482 if (!bin
->dwarf_path
) {
485 bin
->dwarf_info
= dwarf_info
;
492 bt_fd_cache_put_handle(bin
->fd_cache
, dwarf_handle
);
494 dwarf_end(dwarf_info
);
502 * Try to set the dwarf_info for a given bin_info instance via the
505 * @param bin bin_info instance for which to retrieve the
506 * DWARF info via build ID
507 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
510 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
513 char *path
= NULL
, *build_id_prefix_dir
= NULL
, *build_id_file
= NULL
;
514 const char *dbg_dir
= NULL
;
515 size_t build_id_char_len
, build_id_suffix_char_len
, build_id_file_len
;
517 if (!bin
|| !bin
->build_id
) {
521 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
524 * The prefix dir is the first byte of the build id, represented in
525 * lowercase hex as two characters per byte, +1 for '\0'.
527 build_id_prefix_dir
= g_new0(gchar
, BUILD_ID_PREFIX_DIR_LEN
+ 1);
528 if (!build_id_prefix_dir
) {
531 g_snprintf(build_id_prefix_dir
, BUILD_ID_PREFIX_DIR_LEN
+ 1, "%02x", bin
->build_id
[0]);
534 * The build id file is the remaining bytes of the build id,
535 * represented in lowercase hex, as two characters per byte.
537 build_id_char_len
= (2 * (bin
->build_id_len
- 1));
539 /* To which the build id suffix is added, +1 for '\0'. */
540 build_id_suffix_char_len
= strlen(BUILD_ID_SUFFIX
) + 1;
543 * The resulting filename string is the concatenation of the
544 * hex build id and the suffix.
546 build_id_file_len
= build_id_char_len
+ build_id_suffix_char_len
;
547 build_id_file
= g_new0(gchar
, build_id_file_len
);
548 if (!build_id_file
) {
553 * For each byte, starting at offset 1, append two characters
556 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
557 int path_idx
= 2 * (i
- 1);
559 g_snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
561 /* Append the suffix to the generated string, including the '\0'. */
562 g_snprintf(&build_id_file
[build_id_char_len
], build_id_suffix_char_len
,
565 path
= g_build_filename(dbg_dir
, BUILD_ID_SUBDIR
, build_id_prefix_dir
, build_id_file
, NULL
);
570 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
580 g_free(build_id_prefix_dir
);
581 g_free(build_id_file
);
588 * Tests whether the file located at path exists and has the expected
591 * This predicate is used when looking up separate debug info via the
592 * GNU debuglink method. The expected crc can be found .gnu_debuglink
593 * section in the original ELF file, along with the filename for the
594 * file containing the debug info.
596 * @param path Full path at which to look for the debug file
597 * @param crc Expected checksum for the debug file
598 * @returns 1 if the file exists and has the correct checksum,
602 int is_valid_debug_file(struct bin_info
*bin
, char *path
, uint32_t crc
)
605 struct bt_fd_cache_handle
*debug_handle
= NULL
;
612 debug_handle
= bt_fd_cache_get_handle(bin
->fd_cache
, path
);
617 ret
= crc32(bt_fd_cache_handle_get_fd(debug_handle
), &_crc
);
626 bt_fd_cache_put_handle(bin
->fd_cache
, debug_handle
);
631 * Try to set the dwarf_info for a given bin_info instance via the
634 * @param bin bin_info instance for which to retrieve the
635 * DWARF info via debug link
636 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
639 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
642 const gchar
*dbg_dir
= NULL
;
643 gchar
*bin_dir
= NULL
, *path
= NULL
;
645 if (!bin
|| !bin
->dbg_link_filename
) {
649 dbg_dir
= bin
->debug_info_dir
? bin
->debug_info_dir
: DEFAULT_DEBUG_DIR
;
650 bin_dir
= g_path_get_dirname(bin
->elf_path
);
652 /* First look in the executable's dir */
653 path
= g_build_filename(bin_dir
, bin
->dbg_link_filename
, NULL
);
655 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
659 /* If not found, look in .debug subdir */
661 path
= g_build_filename(bin_dir
, DEBUG_SUBDIR
, bin
->dbg_link_filename
, NULL
);
663 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
667 /* Lastly, look under the global debug directory */
670 path
= g_build_filename(dbg_dir
, bin_dir
, bin
->dbg_link_filename
, NULL
);
671 if (is_valid_debug_file(bin
, path
, bin
->dbg_link_crc
)) {
684 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
693 * Initialize the DWARF info for a given executable.
695 * @param bin bin_info instance
696 * @returns 0 on success, negative value on failure
699 int bin_info_set_dwarf_info(struct bin_info
*bin
)
708 /* First try to set the DWARF info from the ELF file */
709 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
715 * If that fails, try to find separate debug info via build ID
718 ret
= bin_info_set_dwarf_info_build_id(bin
);
723 ret
= bin_info_set_dwarf_info_debug_link(bin
);
733 void source_location_destroy(struct source_location
*src_loc
)
739 free(src_loc
->filename
);
744 * Append a string representation of an address offset to an existing
747 * On success, the out parameter `result` will contain the base string
748 * followed by the offset string of the form "+0x1234". On failure,
749 * `result` remains unchanged.
751 * @param base_str The string to which to append an offset string
752 * @param low_addr The lower virtual memory address, the base from
753 * which the offset is computed
754 * @param high_addr The higher virtual memory address
755 * @param result Out parameter, the base string followed by the
757 * @returns 0 on success, -1 on failure
760 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
761 uint64_t high_addr
, char **result
)
764 char *_result
= NULL
;
766 if (!base_str
|| !result
) {
770 offset
= high_addr
- low_addr
;
772 _result
= g_strdup_printf("%s+%#0" PRIx64
, base_str
, offset
);
786 * Try to find the symbol closest to an address within a given ELF
789 * Only function symbols are taken into account. The symbol's address
790 * must precede `addr`. A symbol with a closer address might exist
791 * after `addr` but is irrelevant because it cannot encompass `addr`.
793 * On success, if found, the out parameters `sym` and `shdr` are
794 * set. On failure or if none are found, they remain unchanged.
796 * @param scn ELF section in which to look for the address
797 * @param addr Virtual memory address for which to find the
798 * nearest function symbol
799 * @param sym Out parameter, the nearest function symbol
800 * @param shdr Out parameter, the section header for scn
801 * @returns 0 on success, -1 on failure
804 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
805 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
809 Elf_Data
*data
= NULL
;
810 GElf_Shdr
*_shdr
= NULL
;
811 GElf_Sym
*nearest_sym
= NULL
;
813 if (!scn
|| !sym
|| !shdr
) {
817 _shdr
= g_new0(GElf_Shdr
, 1);
822 _shdr
= gelf_getshdr(scn
, _shdr
);
827 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
829 * We are only interested in symbol table (symtab)
830 * sections, skip this one.
835 data
= elf_getdata(scn
, NULL
);
840 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
842 for (i
= 0; i
< symbol_count
; ++i
) {
843 GElf_Sym
*cur_sym
= NULL
;
845 cur_sym
= g_new0(GElf_Sym
, 1);
849 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
853 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
854 /* We're only interested in the functions. */
859 if (cur_sym
->st_value
<= addr
&&
861 cur_sym
->st_value
> nearest_sym
->st_value
)) {
863 nearest_sym
= cur_sym
;
886 * Get the name of the function containing a given address within an
887 * executable using ELF symbols.
889 * The function name is in fact the name of the nearest ELF symbol,
890 * followed by the offset in bytes between the address and the symbol
891 * (in hex), separated by a '+' character.
893 * If found, the out parameter `func_name` is set on success. On failure,
894 * it remains unchanged.
896 * @param bin bin_info instance for the executable containing
898 * @param addr Virtual memory address for which to find the
900 * @param func_name Out parameter, the function name
901 * @returns 0 on success, -1 on failure
904 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
908 * TODO (possible optimisation): if an ELF has no symtab
909 * section, it has been stripped. Therefore, it would be wise
910 * to store a flag indicating the stripped status after the
911 * first iteration to prevent subsequent ones.
915 GElf_Sym
*sym
= NULL
;
916 GElf_Shdr
*shdr
= NULL
;
917 char *sym_name
= NULL
;
919 /* Set ELF file if it hasn't been accessed yet. */
920 if (!bin
->elf_file
) {
921 ret
= bin_info_set_elf_file(bin
);
923 /* Failed to set ELF file. */
928 scn
= elf_nextscn(bin
->elf_file
, scn
);
933 while (scn
&& !sym
) {
934 ret
= bin_info_get_nearest_symbol_from_section(
935 scn
, addr
, &sym
, &shdr
);
940 scn
= elf_nextscn(bin
->elf_file
, scn
);
944 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
950 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
968 * Get the name of the function containing a given address within a
969 * given compile unit (CU).
971 * If found, the out parameter `func_name` is set on success. On
972 * failure, it remains unchanged.
974 * @param cu bt_dwarf_cu instance which may contain the address
975 * @param addr Virtual memory address for which to find the
977 * @param func_name Out parameter, the function name
978 * @returns 0 on success, -1 on failure
981 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
986 struct bt_dwarf_die
*die
= NULL
;
988 if (!cu
|| !func_name
) {
992 die
= bt_dwarf_die_create(cu
);
997 while (bt_dwarf_die_next(die
) == 0) {
1000 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1005 if (tag
== DW_TAG_subprogram
) {
1006 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1018 uint64_t low_addr
= 0;
1019 char *die_name
= NULL
;
1021 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1026 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1032 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1040 bt_dwarf_die_destroy(die
);
1044 bt_dwarf_die_destroy(die
);
1049 * Get the name of the function containing a given address within an
1050 * executable using DWARF debug info.
1052 * If found, the out parameter `func_name` is set on success. On
1053 * failure, it remains unchanged.
1055 * @param bin bin_info instance for the executable containing
1057 * @param addr Virtual memory address for which to find the
1059 * @param func_name Out parameter, the function name
1060 * @returns 0 on success, -1 on failure
1063 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1067 char *_func_name
= NULL
;
1068 struct bt_dwarf_cu
*cu
= NULL
;
1070 if (!bin
|| !func_name
) {
1074 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1079 while (bt_dwarf_cu_next(cu
) == 0) {
1080 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1091 *func_name
= _func_name
;
1096 bt_dwarf_cu_destroy(cu
);
1100 bt_dwarf_cu_destroy(cu
);
1105 int bin_info_lookup_function_name(struct bin_info
*bin
,
1106 uint64_t addr
, char **func_name
)
1109 char *_func_name
= NULL
;
1111 if (!bin
|| !func_name
) {
1116 * If the bin_info has a build id but it does not match the build id
1117 * that was found on the file system, return an error.
1119 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1123 /* Set DWARF info if it hasn't been accessed yet. */
1124 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1125 ret
= bin_info_set_dwarf_info(bin
);
1127 BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling "
1128 "back to ELF lookup.");
1129 /* Failed to set DWARF info, fallback to ELF. */
1130 bin
->is_elf_only
= true;
1134 if (!bin_info_has_address(bin
, addr
)) {
1139 * Addresses in ELF and DWARF are relative to base address for
1140 * PIC, so make the address argument relative too if needed.
1143 addr
-= bin
->low_addr
;
1146 if (bin
->is_elf_only
) {
1147 ret
= bin_info_lookup_elf_function_name(bin
, addr
,
1150 BT_COMP_LOGI("Failed to lookup function name (ELF): "
1154 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
,
1157 BT_COMP_LOGI("Failed to lookup function name (DWARF): "
1162 *func_name
= _func_name
;
1170 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1172 gchar
*_bin_loc
= NULL
;
1174 if (!bin
|| !bin_loc
) {
1179 * If the bin_info has a build id but it does not match the build id
1180 * that was found on the file system, return an error.
1182 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1187 addr
-= bin
->low_addr
;
1188 _bin_loc
= g_strdup_printf("+%#0" PRIx64
, addr
);
1190 _bin_loc
= g_strdup_printf("@%#0" PRIx64
, addr
);
1197 *bin_loc
= _bin_loc
;
1205 * Predicate used to determine whether the children of a given DIE
1206 * contain a specific address.
1208 * More specifically, the parameter `die` is expected to be a
1209 * subprogram (function) DIE, and this predicate tells whether any
1210 * subroutines are inlined within this function and would contain
1213 * On success, the out parameter `contains` is set with the boolean
1214 * value indicating whether the DIE's range covers `addr`. On failure,
1215 * it remains unchanged.
1217 * Do note that this function advances the position of `die`. If the
1218 * address is found within one of its children, `die` will be pointing
1219 * to that child upon returning from the function, allowing to extract
1220 * the information deemed necessary.
1222 * @param die The parent DIE in whose children the address will be
1224 * @param addr The address for which to look for in the DIEs
1225 * @param contains Out parameter, true if addr is contained,
1227 * @returns Returns 0 on success, -1 on failure
1230 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1233 bool _contains
= false;
1239 ret
= bt_dwarf_die_child(die
);
1245 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1252 * The address is within the range of the current DIE
1257 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1262 if (tag
== DW_TAG_inlined_subroutine
) {
1263 /* Found the tracepoint. */
1267 if (bt_dwarf_die_has_children(die
)) {
1269 * Look for the address in the children DIEs.
1271 ret
= bt_dwarf_die_child(die
);
1277 } while (bt_dwarf_die_next(die
) == 0);
1280 *contains
= _contains
;
1288 * Lookup the source location for a given address within a CU, making
1289 * the assumption that it is contained within an inline routine in a
1292 * @param cu bt_dwarf_cu instance in which to look for the address
1293 * @param addr The address for which to look for
1294 * @param src_loc Out parameter, the source location (filename and
1295 * line number) for the address
1296 * @returns 0 on success, -1 on failure
1299 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1300 struct source_location
**src_loc
)
1304 struct bt_dwarf_die
*die
= NULL
;
1305 struct source_location
*_src_loc
= NULL
;
1307 if (!cu
|| !src_loc
) {
1311 die
= bt_dwarf_die_create(cu
);
1316 while (bt_dwarf_die_next(die
) == 0) {
1319 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1324 if (tag
== DW_TAG_subprogram
) {
1325 bool contains
= false;
1327 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1334 * Try to find an inlined subroutine
1335 * child of this DIE containing addr.
1337 ret
= bin_info_child_die_has_address(die
, addr
,
1350 char *filename
= NULL
;
1353 _src_loc
= g_new0(struct source_location
, 1);
1358 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1362 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1368 _src_loc
->filename
= filename
;
1369 _src_loc
->line_no
= line_no
;
1370 *src_loc
= _src_loc
;
1373 bt_dwarf_die_destroy(die
);
1377 source_location_destroy(_src_loc
);
1378 bt_dwarf_die_destroy(die
);
1383 * Lookup the source location for a given address within a CU,
1384 * assuming that it is contained within an inlined function.
1386 * A source location can be found regardless of inlining status for
1387 * this method, but in the case of an inlined function, the returned
1388 * source location will point not to the callsite but rather to the
1389 * definition site of the inline function.
1391 * @param cu bt_dwarf_cu instance in which to look for the address
1392 * @param addr The address for which to look for
1393 * @param src_loc Out parameter, the source location (filename and
1394 * line number) for the address. Set only if the address
1395 * is found and resolved successfully
1397 * @returns 0 on success, -1 on failure
1400 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1401 struct source_location
**src_loc
)
1403 struct source_location
*_src_loc
= NULL
;
1404 struct bt_dwarf_die
*die
= NULL
;
1405 const char *filename
= NULL
;
1406 Dwarf_Line
*line
= NULL
;
1407 Dwarf_Addr line_addr
;
1408 int ret
= 0, line_no
;
1410 if (!cu
|| !src_loc
) {
1414 die
= bt_dwarf_die_create(cu
);
1419 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1421 /* This is not an error. The caller needs to keep looking. */
1425 ret
= dwarf_lineaddr(line
, &line_addr
);
1430 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1435 if (addr
== line_addr
) {
1436 _src_loc
= g_new0(struct source_location
, 1);
1441 ret
= dwarf_lineno(line
, &line_no
);
1446 _src_loc
->line_no
= line_no
;
1447 _src_loc
->filename
= g_strdup(filename
);
1451 *src_loc
= _src_loc
;
1457 source_location_destroy(_src_loc
);
1460 bt_dwarf_die_destroy(die
);
1465 * Get the source location (file name and line number) for a given
1466 * address within a compile unit (CU).
1468 * On success, the out parameter `src_loc` is set if found. On
1469 * failure, it remains unchanged.
1471 * @param cu bt_dwarf_cu instance for the compile unit which
1472 * may contain the address
1473 * @param addr Virtual memory address for which to find the
1475 * @param src_loc Out parameter, the source location
1476 * @returns 0 on success, -1 on failure
1479 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1480 struct source_location
**src_loc
)
1483 struct source_location
*_src_loc
= NULL
;
1485 if (!cu
|| !src_loc
) {
1489 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1498 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1509 *src_loc
= _src_loc
;
1515 source_location_destroy(_src_loc
);
1520 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1521 struct source_location
**src_loc
)
1523 struct bt_dwarf_cu
*cu
= NULL
;
1524 struct source_location
*_src_loc
= NULL
;
1526 if (!bin
|| !src_loc
) {
1531 * If the bin_info has a build id but it does not match the build id
1532 * that was found on the file system, return an error.
1534 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1538 /* Set DWARF info if it hasn't been accessed yet. */
1539 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1540 if (bin_info_set_dwarf_info(bin
)) {
1541 /* Failed to set DWARF info. */
1542 bin
->is_elf_only
= true;
1546 if (bin
->is_elf_only
) {
1547 /* We cannot lookup source location without DWARF info. */
1551 if (!bin_info_has_address(bin
, addr
)) {
1556 * Addresses in ELF and DWARF are relative to base address for
1557 * PIC, so make the address argument relative too if needed.
1560 addr
-= bin
->low_addr
;
1563 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1568 while (bt_dwarf_cu_next(cu
) == 0) {
1571 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1581 bt_dwarf_cu_destroy(cu
);
1583 *src_loc
= _src_loc
;
1589 source_location_destroy(_src_loc
);
1590 bt_dwarf_cu_destroy(cu
);