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"
49 * An address printed in hex is at most 20 bytes (16 for 64-bits +
50 * leading 0x + optional leading '+' if addr is an offset + null
53 #define ADDR_STR_LEN 20
54 #define BUILD_ID_NOTE_NAME "GNU"
57 int bin_info_init(void)
61 if (elf_version(EV_CURRENT
) == EV_NONE
) {
62 BT_LOGD("ELF library initialization failed: %s.",
71 struct bin_info
*bin_info_create(const char *path
, uint64_t low_addr
,
72 uint64_t memsz
, bool is_pic
, const char *debug_info_dir
,
73 const char *target_prefix
)
75 struct bin_info
*bin
= NULL
;
81 bin
= g_new0(struct bin_info
, 1);
87 bin
->elf_path
= g_build_path("/", target_prefix
,
90 bin
->elf_path
= g_strdup(path
);
98 bin
->debug_info_dir
= strdup(debug_info_dir
);
99 if (!bin
->debug_info_dir
) {
104 bin
->is_pic
= is_pic
;
106 bin
->low_addr
= low_addr
;
107 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
108 bin
->build_id
= NULL
;
109 bin
->build_id_len
= 0;
110 bin
->file_build_id_matches
= false;
115 bin_info_destroy(bin
);
120 void bin_info_destroy(struct bin_info
*bin
)
126 dwarf_end(bin
->dwarf_info
);
128 free(bin
->debug_info_dir
);
130 free(bin
->dwarf_path
);
131 g_free(bin
->build_id
);
132 free(bin
->dbg_link_filename
);
134 elf_end(bin
->elf_file
);
137 close(bin
->dwarf_fd
);
143 * Initialize the ELF file for a given executable.
145 * @param bin bin_info instance
146 * @returns 0 on success, negative value on error.
149 int bin_info_set_elf_file(struct bin_info
*bin
)
152 Elf
*elf_file
= NULL
;
158 elf_fd
= open(bin
->elf_path
, O_RDONLY
);
161 BT_LOGE("Failed to open %s\n", bin
->elf_path
);
165 elf_file
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
167 BT_LOGE("elf_begin failed: %s\n", elf_errmsg(-1));
171 if (elf_kind(elf_file
) != ELF_K_ELF
) {
172 BT_LOGE("Error: %s is not an ELF object\n",
177 bin
->elf_fd
= elf_fd
;
178 bin
->elf_file
= elf_file
;
191 * From a note section data buffer, check if it is a build id note.
193 * @param buf Pointer to a note section
195 * @returns 1 on match, 0 if `buf` does not contain a
196 * valid build id note
199 int is_build_id_note_section(uint8_t *buf
)
202 uint32_t name_sz
, desc_sz
, note_type
;
204 /* The note section header has 3 32bit integer for the following:
205 * - Section name size
209 name_sz
= (uint32_t) *buf
;
210 buf
+= sizeof(name_sz
);
212 buf
+= sizeof(desc_sz
);
214 note_type
= (uint32_t) *buf
;
215 buf
+= sizeof(note_type
);
217 /* Check the note type. */
218 if (note_type
!= NT_GNU_BUILD_ID
) {
222 /* Check the note name. */
223 if (memcmp(buf
, BUILD_ID_NOTE_NAME
, name_sz
) != 0) {
234 * From a build id note section data buffer, check if the build id it contains
235 * is identical to the build id passed as parameter.
237 * @param file_build_id_note Pointer to the file build id note section.
238 * @param build_id Pointer to a build id to compare to.
239 * @param build_id_len length of the build id.
241 * @returns 1 on match, 0 otherwise.
244 int is_build_id_note_section_matching(uint8_t *file_build_id_note
,
245 uint8_t *build_id
, size_t build_id_len
)
247 uint32_t name_sz
, desc_sz
, note_type
;
249 if (build_id_len
<= 0) {
253 /* The note section header has 3 32bit integer for the following:
254 * - Section name size
258 name_sz
= (uint32_t) *file_build_id_note
;
259 file_build_id_note
+= sizeof(name_sz
);
260 file_build_id_note
+= sizeof(desc_sz
);
261 file_build_id_note
+= sizeof(note_type
);
264 * Move the pointer pass the name char array. This corresponds to the
265 * beginning of the description section. The description is the build
266 * id in the case of a build id note.
268 file_build_id_note
+= name_sz
;
271 * Compare the binary build id with the supplied build id.
273 if (memcmp(build_id
, file_build_id_note
, build_id_len
) == 0) {
281 * Checks if the build id stored in `bin` (bin->build_id) is matching the build
282 * id of the ondisk file (bin->elf_file).
284 * @param bin bin_info instance
285 * @param build_id build id to compare ot the on disk file
286 * @param build_id_len length of the build id
288 * @returns 1 on if the build id of stored in `bin` matches
289 * the build id of the ondisk file.
290 * 0 on if they are different or an error occured.
293 int is_build_id_matching(struct bin_info
*bin
)
295 int ret
, is_build_id
, is_matching
= 0;
296 Elf_Scn
*curr_section
= NULL
, *next_section
= NULL
;
297 Elf_Data
*note_data
= NULL
;
298 GElf_Shdr
*curr_section_hdr
= NULL
;
300 if (!bin
->build_id
) {
304 /* Set ELF file if it hasn't been accessed yet. */
305 if (!bin
->elf_file
) {
306 ret
= bin_info_set_elf_file(bin
);
308 /* Failed to set ELF file. */
313 curr_section_hdr
= g_new0(GElf_Shdr
, 1);
314 if (!curr_section_hdr
) {
318 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
323 while (next_section
) {
324 curr_section
= next_section
;
325 next_section
= elf_nextscn(bin
->elf_file
, curr_section
);
327 curr_section_hdr
= gelf_getshdr(curr_section
, curr_section_hdr
);
329 if (!curr_section_hdr
) {
333 if (curr_section_hdr
->sh_type
!= SHT_NOTE
) {
337 note_data
= elf_getdata(curr_section
, NULL
);
342 /* Check if the note is of the build-id type. */
343 is_build_id
= is_build_id_note_section(note_data
->d_buf
);
349 * Compare the build id of the on-disk file and
350 * the build id recorded in the trace.
352 is_matching
= is_build_id_note_section_matching(note_data
->d_buf
,
353 bin
->build_id
, bin
->build_id_len
);
359 g_free(curr_section_hdr
);
364 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
367 if (!bin
|| !build_id
) {
371 /* Set the build id. */
372 bin
->build_id
= g_new0(uint8_t, build_id_len
);
373 if (!bin
->build_id
) {
377 memcpy(bin
->build_id
, build_id
, build_id_len
);
378 bin
->build_id_len
= build_id_len
;
381 * Check if the file found on the file system has the same build id
382 * that what was recorded in the trace.
384 bin
->file_build_id_matches
= is_build_id_matching(bin
);
385 if (!bin
->file_build_id_matches
) {
386 BT_LOGD_STR("Supplied Build ID does not match Build ID of the "
387 "binary or library found on the file system.");
392 * Reset the is_elf_only flag in case it had been set
393 * previously, because we might find separate debug info using
394 * the new build id information.
396 bin
->is_elf_only
= false;
405 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
408 if (!bin
|| !filename
) {
412 bin
->dbg_link_filename
= strdup(filename
);
413 if (!bin
->dbg_link_filename
) {
417 bin
->dbg_link_crc
= crc
;
420 * Reset the is_elf_only flag in case it had been set
421 * previously, because we might find separate debug info using
422 * the new build id information.
424 bin
->is_elf_only
= false;
434 * Tries to read DWARF info from the location given by path, and
435 * attach it to the given bin_info instance if it exists.
437 * @param bin bin_info instance for which to set DWARF info
438 * @param path Presumed location of the DWARF info
439 * @returns 0 on success, negative value on failure
442 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
444 int fd
= -1, ret
= 0;
445 struct bt_dwarf_cu
*cu
= NULL
;
446 Dwarf
*dwarf_info
= NULL
;
452 fd
= open(path
, O_RDONLY
);
458 dwarf_info
= dwarf_begin(fd
, DWARF_C_READ
);
464 * Check if the dwarf info has any CU. If not, the
465 * executable's object file contains no DWARF info.
467 cu
= bt_dwarf_cu_create(dwarf_info
);
472 ret
= bt_dwarf_cu_next(cu
);
478 bin
->dwarf_path
= strdup(path
);
479 if (!bin
->dwarf_path
) {
482 bin
->dwarf_info
= dwarf_info
;
492 dwarf_end(dwarf_info
);
500 * Try to set the dwarf_info for a given bin_info instance via the
503 * @param bin bin_info instance for which to retrieve the
504 * DWARF info via build ID
505 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
508 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
511 char *path
= NULL
, *build_id_file
= NULL
;
512 const char *dbg_dir
= NULL
;
513 size_t build_id_file_len
;
515 if (!bin
|| !bin
->build_id
) {
519 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
521 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
522 build_id_file_len
= (2 * bin
->build_id_len
) + 1 +
523 strlen(BUILD_ID_SUFFIX
) + 1;
524 build_id_file
= malloc(build_id_file_len
);
525 if (!build_id_file
) {
529 snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
530 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
531 int path_idx
= 3 + 2 * (i
- 1);
533 snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
535 strcat(build_id_file
, BUILD_ID_SUFFIX
);
537 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
542 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
559 * Tests whether the file located at path exists and has the expected
562 * This predicate is used when looking up separate debug info via the
563 * GNU debuglink method. The expected crc can be found .gnu_debuglink
564 * section in the original ELF file, along with the filename for the
565 * file containing the debug info.
567 * @param path Full path at which to look for the debug file
568 * @param crc Expected checksum for the debug file
569 * @returns 1 if the file exists and has the correct checksum,
573 int is_valid_debug_file(char *path
, uint32_t crc
)
575 int ret
= 0, fd
= -1;
582 fd
= open(path
, O_RDONLY
);
587 ret
= crc32(fd
, &_crc
);
602 * Try to set the dwarf_info for a given bin_info instance via the
605 * @param bin bin_info instance for which to retrieve the
606 * DWARF info via debug link
607 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
610 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
613 const char *dbg_dir
= NULL
;
614 char *dir_name
= NULL
, *bin_dir
= NULL
, *path
= NULL
;
615 size_t max_path_len
= 0;
617 if (!bin
|| !bin
->dbg_link_filename
) {
621 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
623 dir_name
= g_path_get_dirname(bin
->elf_path
);
628 /* bin_dir is just dir_name with a trailing slash */
629 bin_dir
= g_new0(char, strlen(dir_name
) + 2);
634 strcpy(bin_dir
, dir_name
);
635 strcat(bin_dir
, "/");
637 max_path_len
= strlen(dbg_dir
) + strlen(bin_dir
) +
638 strlen(DEBUG_SUBDIR
) + strlen(bin
->dbg_link_filename
)
640 path
= g_new0(char, max_path_len
);
645 /* First look in the executable's dir */
646 strcpy(path
, bin_dir
);
647 strcat(path
, bin
->dbg_link_filename
);
649 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
653 /* If not found, look in .debug subdir */
654 strcpy(path
, bin_dir
);
655 strcat(path
, DEBUG_SUBDIR
);
656 strcat(path
, bin
->dbg_link_filename
);
658 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
662 /* Lastly, look under the global debug directory */
663 strcpy(path
, dbg_dir
);
664 strcat(path
, bin_dir
);
665 strcat(path
, bin
->dbg_link_filename
);
667 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
681 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
690 * Initialize the DWARF info for a given executable.
692 * @param bin bin_info instance
693 * @returns 0 on success, negative value on failure
696 int bin_info_set_dwarf_info(struct bin_info
*bin
)
705 /* First try to set the DWARF info from the ELF file */
706 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
712 * If that fails, try to find separate debug info via build ID
715 ret
= bin_info_set_dwarf_info_build_id(bin
);
720 ret
= bin_info_set_dwarf_info_debug_link(bin
);
730 void source_location_destroy(struct source_location
*src_loc
)
736 free(src_loc
->filename
);
741 * Append a string representation of an address offset to an existing
744 * On success, the out parameter `result` will contain the base string
745 * followed by the offset string of the form "+0x1234". On failure,
746 * `result` remains unchanged.
748 * @param base_str The string to which to append an offset string
749 * @param low_addr The lower virtual memory address, the base from
750 * which the offset is computed
751 * @param high_addr The higher virtual memory address
752 * @param result Out parameter, the base string followed by the
754 * @returns 0 on success, -1 on failure
757 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
758 uint64_t high_addr
, char **result
)
762 char *_result
= NULL
;
763 char offset_str
[ADDR_STR_LEN
];
765 if (!base_str
|| !result
) {
769 offset
= high_addr
- low_addr
;
771 _result
= malloc(strlen(base_str
) + ADDR_STR_LEN
);
776 ret
= snprintf(offset_str
, ADDR_STR_LEN
, "+%#0" PRIx64
, offset
);
780 strcpy(_result
, base_str
);
781 strcat(_result
, offset_str
);
792 * Try to find the symbol closest to an address within a given ELF
795 * Only function symbols are taken into account. The symbol's address
796 * must precede `addr`. A symbol with a closer address might exist
797 * after `addr` but is irrelevant because it cannot encompass `addr`.
799 * On success, if found, the out parameters `sym` and `shdr` are
800 * set. On failure or if none are found, they remain unchanged.
802 * @param scn ELF section in which to look for the address
803 * @param addr Virtual memory address for which to find the
804 * nearest function symbol
805 * @param sym Out parameter, the nearest function symbol
806 * @param shdr Out parameter, the section header for scn
807 * @returns 0 on success, -1 on failure
810 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
811 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
815 Elf_Data
*data
= NULL
;
816 GElf_Shdr
*_shdr
= NULL
;
817 GElf_Sym
*nearest_sym
= NULL
;
819 if (!scn
|| !sym
|| !shdr
) {
823 _shdr
= g_new0(GElf_Shdr
, 1);
828 _shdr
= gelf_getshdr(scn
, _shdr
);
833 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
835 * We are only interested in symbol table (symtab)
836 * sections, skip this one.
841 data
= elf_getdata(scn
, NULL
);
846 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
848 for (i
= 0; i
< symbol_count
; ++i
) {
849 GElf_Sym
*cur_sym
= NULL
;
851 cur_sym
= g_new0(GElf_Sym
, 1);
855 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
859 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
860 /* We're only interested in the functions. */
865 if (cur_sym
->st_value
<= addr
&&
867 cur_sym
->st_value
> nearest_sym
->st_value
)) {
869 nearest_sym
= cur_sym
;
892 * Get the name of the function containing a given address within an
893 * executable using ELF symbols.
895 * The function name is in fact the name of the nearest ELF symbol,
896 * followed by the offset in bytes between the address and the symbol
897 * (in hex), separated by a '+' character.
899 * If found, the out parameter `func_name` is set on success. On failure,
900 * it remains unchanged.
902 * @param bin bin_info instance for the executable containing
904 * @param addr Virtual memory address for which to find the
906 * @param func_name Out parameter, the function name
907 * @returns 0 on success, -1 on failure
910 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
914 * TODO (possible optimisation): if an ELF has no symtab
915 * section, it has been stripped. Therefore, it would be wise
916 * to store a flag indicating the stripped status after the
917 * first iteration to prevent subsequent ones.
921 GElf_Sym
*sym
= NULL
;
922 GElf_Shdr
*shdr
= NULL
;
923 char *sym_name
= NULL
;
925 /* Set ELF file if it hasn't been accessed yet. */
926 if (!bin
->elf_file
) {
927 ret
= bin_info_set_elf_file(bin
);
929 /* Failed to set ELF file. */
934 scn
= elf_nextscn(bin
->elf_file
, scn
);
939 while (scn
&& !sym
) {
940 ret
= bin_info_get_nearest_symbol_from_section(
941 scn
, addr
, &sym
, &shdr
);
946 scn
= elf_nextscn(bin
->elf_file
, scn
);
950 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
956 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
974 * Get the name of the function containing a given address within a
975 * given compile unit (CU).
977 * If found, the out parameter `func_name` is set on success. On
978 * failure, it remains unchanged.
980 * @param cu bt_dwarf_cu instance which may contain the address
981 * @param addr Virtual memory address for which to find the
983 * @param func_name Out parameter, the function name
984 * @returns 0 on success, -1 on failure
987 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
992 struct bt_dwarf_die
*die
= NULL
;
994 if (!cu
|| !func_name
) {
998 die
= bt_dwarf_die_create(cu
);
1003 while (bt_dwarf_die_next(die
) == 0) {
1006 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1011 if (tag
== DW_TAG_subprogram
) {
1012 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
1024 uint64_t low_addr
= 0;
1025 char *die_name
= NULL
;
1027 ret
= bt_dwarf_die_get_name(die
, &die_name
);
1032 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
1038 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
1046 bt_dwarf_die_destroy(die
);
1050 bt_dwarf_die_destroy(die
);
1055 * Get the name of the function containing a given address within an
1056 * executable using DWARF debug info.
1058 * If found, the out parameter `func_name` is set on success. On
1059 * failure, it remains unchanged.
1061 * @param bin bin_info instance for the executable containing
1063 * @param addr Virtual memory address for which to find the
1065 * @param func_name Out parameter, the function name
1066 * @returns 0 on success, -1 on failure
1069 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
1073 char *_func_name
= NULL
;
1074 struct bt_dwarf_cu
*cu
= NULL
;
1076 if (!bin
|| !func_name
) {
1080 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1085 while (bt_dwarf_cu_next(cu
) == 0) {
1086 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
1097 *func_name
= _func_name
;
1102 bt_dwarf_cu_destroy(cu
);
1106 bt_dwarf_cu_destroy(cu
);
1111 int bin_info_lookup_function_name(struct bin_info
*bin
,
1112 uint64_t addr
, char **func_name
)
1115 char *_func_name
= NULL
;
1117 if (!bin
|| !func_name
) {
1122 * If the bin_info has a build id but it does not match the build id
1123 * that was found on the file system, return an error.
1125 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1129 /* Set DWARF info if it hasn't been accessed yet. */
1130 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1131 ret
= bin_info_set_dwarf_info(bin
);
1133 BT_LOGD_STR("Failed to set bin dwarf info, falling back to ELF lookup.");
1134 /* Failed to set DWARF info, fallback to ELF. */
1135 bin
->is_elf_only
= true;
1139 if (!bin_info_has_address(bin
, addr
)) {
1144 * Addresses in ELF and DWARF are relative to base address for
1145 * PIC, so make the address argument relative too if needed.
1148 addr
-= bin
->low_addr
;
1151 if (bin
->is_elf_only
) {
1152 ret
= bin_info_lookup_elf_function_name(bin
, addr
, &_func_name
);
1153 BT_LOGD("Failed to lookup function name (ELF): ret=%d", ret
);
1155 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
, &_func_name
);
1156 BT_LOGD("Failed to lookup function name (DWARF): ret=%d", ret
);
1159 *func_name
= _func_name
;
1167 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
1170 char *_bin_loc
= NULL
;
1172 if (!bin
|| !bin_loc
) {
1177 * If the bin_info has a build id but it does not match the build id
1178 * that was found on the file system, return an error.
1180 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1185 addr
-= bin
->low_addr
;
1186 ret
= asprintf(&_bin_loc
, "+%#0" PRIx64
, addr
);
1188 ret
= asprintf(&_bin_loc
, "@%#0" PRIx64
, addr
);
1191 if (ret
== -1 || !_bin_loc
) {
1195 *bin_loc
= _bin_loc
;
1203 * Predicate used to determine whether the children of a given DIE
1204 * contain a specific address.
1206 * More specifically, the parameter `die` is expected to be a
1207 * subprogram (function) DIE, and this predicate tells whether any
1208 * subroutines are inlined within this function and would contain
1211 * On success, the out parameter `contains` is set with the boolean
1212 * value indicating whether the DIE's range covers `addr`. On failure,
1213 * it remains unchanged.
1215 * Do note that this function advances the position of `die`. If the
1216 * address is found within one of its children, `die` will be pointing
1217 * to that child upon returning from the function, allowing to extract
1218 * the information deemed necessary.
1220 * @param die The parent DIE in whose children the address will be
1222 * @param addr The address for which to look for in the DIEs
1223 * @param contains Out parameter, true if addr is contained,
1225 * @returns Returns 0 on success, -1 on failure
1228 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1231 bool _contains
= false;
1237 ret
= bt_dwarf_die_child(die
);
1245 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1250 if (tag
== DW_TAG_inlined_subroutine
) {
1251 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1260 } while (bt_dwarf_die_next(die
) == 0);
1263 *contains
= _contains
;
1271 * Lookup the source location for a given address within a CU, making
1272 * the assumption that it is contained within an inline routine in a
1275 * @param cu bt_dwarf_cu instance in which to look for the address
1276 * @param addr The address for which to look for
1277 * @param src_loc Out parameter, the source location (filename and
1278 * line number) for the address
1279 * @returns 0 on success, -1 on failure
1282 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1283 struct source_location
**src_loc
)
1287 struct bt_dwarf_die
*die
= NULL
;
1288 struct source_location
*_src_loc
= NULL
;
1290 if (!cu
|| !src_loc
) {
1294 die
= bt_dwarf_die_create(cu
);
1299 while (bt_dwarf_die_next(die
) == 0) {
1302 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1307 if (tag
== DW_TAG_subprogram
) {
1308 bool contains
= false;
1310 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1317 * Try to find an inlined subroutine
1318 * child of this DIE containing addr.
1320 ret
= bin_info_child_die_has_address(die
, addr
,
1333 char *filename
= NULL
;
1336 _src_loc
= g_new0(struct source_location
, 1);
1341 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1345 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1351 _src_loc
->filename
= filename
;
1352 _src_loc
->line_no
= line_no
;
1353 *src_loc
= _src_loc
;
1356 bt_dwarf_die_destroy(die
);
1360 source_location_destroy(_src_loc
);
1361 bt_dwarf_die_destroy(die
);
1366 * Lookup the source location for a given address within a CU,
1367 * assuming that it is contained within an inlined function.
1369 * A source location can be found regardless of inlining status for
1370 * this method, but in the case of an inlined function, the returned
1371 * source location will point not to the callsite but rather to the
1372 * definition site of the inline function.
1374 * @param cu bt_dwarf_cu instance in which to look for the address
1375 * @param addr The address for which to look for
1376 * @param src_loc Out parameter, the source location (filename and
1377 * line number) for the address
1378 * @returns 0 on success, -1 on failure
1381 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1382 struct source_location
**src_loc
)
1384 struct source_location
*_src_loc
= NULL
;
1385 struct bt_dwarf_die
*die
= NULL
;
1386 const char *filename
= NULL
;
1387 Dwarf_Line
*line
= NULL
;
1388 Dwarf_Addr line_addr
;
1391 if (!cu
|| !src_loc
) {
1395 die
= bt_dwarf_die_create(cu
);
1400 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1405 ret
= dwarf_lineaddr(line
, &line_addr
);
1410 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1415 if (addr
== line_addr
) {
1416 _src_loc
= g_new0(struct source_location
, 1);
1421 ret
= dwarf_lineno(line
, &line_no
);
1426 _src_loc
->line_no
= line_no
;
1427 _src_loc
->filename
= strdup(filename
);
1430 bt_dwarf_die_destroy(die
);
1433 *src_loc
= _src_loc
;
1439 source_location_destroy(_src_loc
);
1440 bt_dwarf_die_destroy(die
);
1445 * Get the source location (file name and line number) for a given
1446 * address within a compile unit (CU).
1448 * On success, the out parameter `src_loc` is set if found. On
1449 * failure, it remains unchanged.
1451 * @param cu bt_dwarf_cu instance for the compile unit which
1452 * may contain the address
1453 * @param addr Virtual memory address for which to find the
1455 * @param src_loc Out parameter, the source location
1456 * @returns 0 on success, -1 on failure
1459 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1460 struct source_location
**src_loc
)
1463 struct source_location
*_src_loc
= NULL
;
1465 if (!cu
|| !src_loc
) {
1469 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1478 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1489 *src_loc
= _src_loc
;
1495 source_location_destroy(_src_loc
);
1500 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1501 struct source_location
**src_loc
)
1503 struct bt_dwarf_cu
*cu
= NULL
;
1504 struct source_location
*_src_loc
= NULL
;
1506 if (!bin
|| !src_loc
) {
1511 * If the bin_info has a build id but it does not match the build id
1512 * that was found on the file system, return an error.
1514 if (bin
->build_id
&& !bin
->file_build_id_matches
) {
1518 /* Set DWARF info if it hasn't been accessed yet. */
1519 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1520 if (bin_info_set_dwarf_info(bin
)) {
1521 /* Failed to set DWARF info. */
1522 bin
->is_elf_only
= true;
1526 if (bin
->is_elf_only
) {
1527 /* We cannot lookup source location without DWARF info. */
1531 if (!bin_info_has_address(bin
, addr
)) {
1536 * Addresses in ELF and DWARF are relative to base address for
1537 * PIC, so make the address argument relative too if needed.
1540 addr
-= bin
->low_addr
;
1543 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1548 while (bt_dwarf_cu_next(cu
) == 0) {
1551 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1561 bt_dwarf_cu_destroy(cu
);
1563 *src_loc
= _src_loc
;
1569 source_location_destroy(_src_loc
);
1570 bt_dwarf_cu_destroy(cu
);