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
40 #include <babeltrace/babeltrace-internal.h>
47 * An address printed in hex is at most 20 bytes (16 for 64-bits +
48 * leading 0x + optional leading '+' if addr is an offset + null
51 #define ADDR_STR_LEN 20
54 int bin_info_init(void)
58 if (elf_version(EV_CURRENT
) == EV_NONE
) {
59 printf_debug("ELF library initialization failed: %s\n",
68 struct bin_info
*bin_info_create(const char *path
, uint64_t low_addr
,
69 uint64_t memsz
, bool is_pic
)
71 struct bin_info
*bin
= NULL
;
77 bin
= g_new0(struct bin_info
, 1);
82 if (opt_debug_info_target_prefix
) {
83 bin
->elf_path
= g_build_path("/", opt_debug_info_target_prefix
,
86 bin
->elf_path
= strdup(path
);
95 bin
->low_addr
= low_addr
;
96 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
101 bin_info_destroy(bin
);
106 void bin_info_destroy(struct bin_info
*bin
)
112 dwarf_end(bin
->dwarf_info
);
115 free(bin
->dwarf_path
);
116 g_free(bin
->build_id
);
117 free(bin
->dbg_link_filename
);
119 elf_end(bin
->elf_file
);
122 close(bin
->dwarf_fd
);
129 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
132 if (!bin
|| !build_id
) {
136 bin
->build_id
= malloc(build_id_len
);
137 if (!bin
->build_id
) {
141 memcpy(bin
->build_id
, build_id
, build_id_len
);
142 bin
->build_id_len
= build_id_len
;
145 * Reset the is_elf_only flag in case it had been set
146 * previously, because we might find separate debug info using
147 * the new build id information.
149 bin
->is_elf_only
= false;
159 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
162 if (!bin
|| !filename
) {
166 bin
->dbg_link_filename
= strdup(filename
);
167 if (!bin
->dbg_link_filename
) {
171 bin
->dbg_link_crc
= crc
;
174 * Reset the is_elf_only flag in case it had been set
175 * previously, because we might find separate debug info using
176 * the new build id information.
178 bin
->is_elf_only
= false;
188 * Tries to read DWARF info from the location given by path, and
189 * attach it to the given bin_info instance if it exists.
191 * @param bin bin_info instance for which to set DWARF info
192 * @param path Presumed location of the DWARF info
193 * @returns 0 on success, negative value on failure
196 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
198 int fd
= -1, ret
= 0;
199 struct bt_dwarf_cu
*cu
= NULL
;
200 Dwarf
*dwarf_info
= NULL
;
206 fd
= open(path
, O_RDONLY
);
212 dwarf_info
= dwarf_begin(fd
, DWARF_C_READ
);
218 * Check if the dwarf info has any CU. If not, the
219 * executable's object file contains no DWARF info.
221 cu
= bt_dwarf_cu_create(dwarf_info
);
226 ret
= bt_dwarf_cu_next(cu
);
232 bin
->dwarf_path
= strdup(path
);
233 if (!bin
->dwarf_path
) {
236 bin
->dwarf_info
= dwarf_info
;
246 dwarf_end(dwarf_info
);
254 * Try to set the dwarf_info for a given bin_info instance via the
257 * @param bin bin_info instance for which to retrieve the
258 * DWARF info via build ID
259 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
262 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
265 char *path
= NULL
, *build_id_file
= NULL
;
266 const char *dbg_dir
= NULL
;
267 size_t build_id_file_len
;
269 if (!bin
|| !bin
->build_id
) {
273 dbg_dir
= opt_debug_info_dir
? : DEFAULT_DEBUG_DIR
;
275 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
276 build_id_file_len
= (2 * bin
->build_id_len
) + 1 +
277 strlen(BUILD_ID_SUFFIX
) + 1;
278 build_id_file
= malloc(build_id_file_len
);
279 if (!build_id_file
) {
283 snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
284 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
285 int path_idx
= 3 + 2 * (i
- 1);
287 snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
289 strcat(build_id_file
, BUILD_ID_SUFFIX
);
291 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
296 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
313 * Tests whether the file located at path exists and has the expected
316 * This predicate is used when looking up separate debug info via the
317 * GNU debuglink method. The expected crc can be found .gnu_debuglink
318 * section in the original ELF file, along with the filename for the
319 * file containing the debug info.
321 * @param path Full path at which to look for the debug file
322 * @param crc Expected checksum for the debug file
323 * @returns 1 if the file exists and has the correct checksum,
327 int is_valid_debug_file(char *path
, uint32_t crc
)
329 int ret
= 0, fd
= -1;
336 fd
= open(path
, O_RDONLY
);
341 ret
= crc32(fd
, &_crc
);
356 * Try to set the dwarf_info for a given bin_info instance via the
359 * @param bin bin_info instance for which to retrieve the
360 * DWARF info via debug link
361 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
364 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
367 const char *dbg_dir
= NULL
;
368 char *dir_name
= NULL
, *bin_dir
= NULL
, *path
= NULL
;
369 size_t max_path_len
= 0;
371 if (!bin
|| !bin
->dbg_link_filename
) {
375 dbg_dir
= opt_debug_info_dir
? : DEFAULT_DEBUG_DIR
;
377 dir_name
= dirname(bin
->elf_path
);
382 /* bin_dir is just dir_name with a trailing slash */
383 bin_dir
= malloc(strlen(dir_name
) + 2);
388 strcpy(bin_dir
, dir_name
);
389 strcat(bin_dir
, "/");
391 max_path_len
= strlen(dbg_dir
) + strlen(bin_dir
) +
392 strlen(DEBUG_SUBDIR
) + strlen(bin
->dbg_link_filename
)
394 path
= malloc(max_path_len
);
399 /* First look in the executable's dir */
400 strcpy(path
, bin_dir
);
401 strcat(path
, bin
->dbg_link_filename
);
403 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
407 /* If not found, look in .debug subdir */
408 strcpy(path
, bin_dir
);
409 strcat(path
, DEBUG_SUBDIR
);
410 strcat(path
, bin
->dbg_link_filename
);
412 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
416 /* Lastly, look under the global debug directory */
417 strcpy(path
, dbg_dir
);
418 strcat(path
, bin_dir
);
419 strcat(path
, bin
->dbg_link_filename
);
421 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
434 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
443 * Initialize the DWARF info for a given executable.
445 * @param bin bin_info instance
446 * @returns 0 on success, negative value on failure
449 int bin_info_set_dwarf_info(struct bin_info
*bin
)
458 /* First try to set the DWARF info from the ELF file */
459 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
465 * If that fails, try to find separate debug info via build ID
468 ret
= bin_info_set_dwarf_info_build_id(bin
);
473 ret
= bin_info_set_dwarf_info_debug_link(bin
);
483 * Initialize the ELF file for a given executable.
485 * @param bin bin_info instance
486 * @returns 0 on success, negative value on error.
489 int bin_info_set_elf_file(struct bin_info
*bin
)
492 Elf
*elf_file
= NULL
;
498 elf_fd
= open(bin
->elf_path
, O_RDONLY
);
501 printf_verbose("Failed to open %s\n", bin
->elf_path
);
505 elf_file
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
507 printf_debug("elf_begin failed: %s\n", elf_errmsg(-1));
511 if (elf_kind(elf_file
) != ELF_K_ELF
) {
512 printf_verbose("Error: %s is not an ELF object\n",
517 bin
->elf_fd
= elf_fd
;
518 bin
->elf_file
= elf_file
;
531 void source_location_destroy(struct source_location
*src_loc
)
537 free(src_loc
->filename
);
542 * Append a string representation of an address offset to an existing
545 * On success, the out parameter `result` will contain the base string
546 * followed by the offset string of the form "+0x1234". On failure,
547 * `result` remains unchanged.
549 * @param base_str The string to which to append an offset string
550 * @param low_addr The lower virtual memory address, the base from
551 * which the offset is computed
552 * @param high_addr The higher virtual memory address
553 * @param result Out parameter, the base string followed by the
555 * @returns 0 on success, -1 on failure
558 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
559 uint64_t high_addr
, char **result
)
563 char *_result
= NULL
;
564 char offset_str
[ADDR_STR_LEN
];
566 if (!base_str
|| !result
) {
570 offset
= high_addr
- low_addr
;
572 _result
= malloc(strlen(base_str
) + ADDR_STR_LEN
);
577 ret
= snprintf(offset_str
, ADDR_STR_LEN
, "+%#0" PRIx64
, offset
);
581 strcpy(_result
, base_str
);
582 strcat(_result
, offset_str
);
593 * Try to find the symbol closest to an address within a given ELF
596 * Only function symbols are taken into account. The symbol's address
597 * must precede `addr`. A symbol with a closer address might exist
598 * after `addr` but is irrelevant because it cannot encompass `addr`.
600 * On success, if found, the out parameters `sym` and `shdr` are
601 * set. On failure or if none are found, they remain unchanged.
603 * @param scn ELF section in which to look for the address
604 * @param addr Virtual memory address for which to find the
605 * nearest function symbol
606 * @param sym Out parameter, the nearest function symbol
607 * @param shdr Out parameter, the section header for scn
608 * @returns 0 on success, -1 on failure
611 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
612 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
616 Elf_Data
*data
= NULL
;
617 GElf_Shdr
*_shdr
= NULL
;
618 GElf_Sym
*nearest_sym
= NULL
;
620 if (!scn
|| !sym
|| !shdr
) {
624 _shdr
= g_new0(GElf_Shdr
, 1);
629 _shdr
= gelf_getshdr(scn
, _shdr
);
634 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
636 * We are only interested in symbol table (symtab)
637 * sections, skip this one.
642 data
= elf_getdata(scn
, NULL
);
647 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
649 for (i
= 0; i
< symbol_count
; ++i
) {
650 GElf_Sym
*cur_sym
= NULL
;
652 cur_sym
= g_new0(GElf_Sym
, 1);
656 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
660 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
661 /* We're only interested in the functions. */
666 if (cur_sym
->st_value
<= addr
&&
668 cur_sym
->st_value
> nearest_sym
->st_value
)) {
670 nearest_sym
= cur_sym
;
693 * Get the name of the function containing a given address within an
694 * executable using ELF symbols.
696 * The function name is in fact the name of the nearest ELF symbol,
697 * followed by the offset in bytes between the address and the symbol
698 * (in hex), separated by a '+' character.
700 * If found, the out parameter `func_name` is set on success. On failure,
701 * it remains unchanged.
703 * @param bin bin_info instance for the executable containing
705 * @param addr Virtual memory address for which to find the
707 * @param func_name Out parameter, the function name
708 * @returns 0 on success, -1 on failure
711 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
715 * TODO (possible optimisation): if an ELF has no symtab
716 * section, it has been stripped. Therefore, it would be wise
717 * to store a flag indicating the stripped status after the
718 * first iteration to prevent subsequent ones.
722 GElf_Sym
*sym
= NULL
;
723 GElf_Shdr
*shdr
= NULL
;
724 char *sym_name
= NULL
;
726 /* Set ELF file if it hasn't been accessed yet. */
727 if (!bin
->elf_file
) {
728 ret
= bin_info_set_elf_file(bin
);
730 /* Failed to set ELF file. */
735 scn
= elf_nextscn(bin
->elf_file
, scn
);
740 while (scn
&& !sym
) {
741 ret
= bin_info_get_nearest_symbol_from_section(
742 scn
, addr
, &sym
, &shdr
);
747 scn
= elf_nextscn(bin
->elf_file
, scn
);
751 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
757 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
775 * Get the name of the function containing a given address within a
776 * given compile unit (CU).
778 * If found, the out parameter `func_name` is set on success. On
779 * failure, it remains unchanged.
781 * @param cu bt_dwarf_cu instance which may contain the address
782 * @param addr Virtual memory address for which to find the
784 * @param func_name Out parameter, the function name
785 * @returns 0 on success, -1 on failure
788 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
793 struct bt_dwarf_die
*die
= NULL
;
795 if (!cu
|| !func_name
) {
799 die
= bt_dwarf_die_create(cu
);
804 while (bt_dwarf_die_next(die
) == 0) {
807 ret
= bt_dwarf_die_get_tag(die
, &tag
);
812 if (tag
== DW_TAG_subprogram
) {
813 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
825 uint64_t low_addr
= 0;
826 char *die_name
= NULL
;
828 ret
= bt_dwarf_die_get_name(die
, &die_name
);
833 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
839 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
847 bt_dwarf_die_destroy(die
);
851 bt_dwarf_die_destroy(die
);
856 * Get the name of the function containing a given address within an
857 * executable using DWARF debug info.
859 * If found, the out parameter `func_name` is set on success. On
860 * failure, it remains unchanged.
862 * @param bin bin_info instance for the executable containing
864 * @param addr Virtual memory address for which to find the
866 * @param func_name Out parameter, the function name
867 * @returns 0 on success, -1 on failure
870 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
874 char *_func_name
= NULL
;
875 struct bt_dwarf_cu
*cu
= NULL
;
877 if (!bin
|| !func_name
) {
881 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
886 while (bt_dwarf_cu_next(cu
) == 0) {
887 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
898 *func_name
= _func_name
;
903 bt_dwarf_cu_destroy(cu
);
907 bt_dwarf_cu_destroy(cu
);
912 int bin_info_lookup_function_name(struct bin_info
*bin
, uint64_t addr
,
916 char *_func_name
= NULL
;
918 if (!bin
|| !func_name
) {
922 /* Set DWARF info if it hasn't been accessed yet. */
923 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
924 ret
= bin_info_set_dwarf_info(bin
);
926 printf_verbose("Failed to set bin dwarf info, falling back to ELF lookup.\n");
927 /* Failed to set DWARF info, fallback to ELF. */
928 bin
->is_elf_only
= true;
932 if (!bin_info_has_address(bin
, addr
)) {
937 * Addresses in ELF and DWARF are relative to base address for
938 * PIC, so make the address argument relative too if needed.
941 addr
-= bin
->low_addr
;
944 if (bin
->is_elf_only
) {
945 ret
= bin_info_lookup_elf_function_name(bin
, addr
, &_func_name
);
946 printf_verbose("Failed to lookup function name (elf), error %i\n", ret
);
948 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
, &_func_name
);
949 printf_verbose("Failed to lookup function name (dwarf), error %i\n", ret
);
952 *func_name
= _func_name
;
960 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
963 char *_bin_loc
= NULL
;
965 if (!bin
|| !bin_loc
) {
970 addr
-= bin
->low_addr
;
971 ret
= asprintf(&_bin_loc
, "+%#0" PRIx64
, addr
);
973 ret
= asprintf(&_bin_loc
, "@%#0" PRIx64
, addr
);
976 if (ret
== -1 || !_bin_loc
) {
988 * Predicate used to determine whether the children of a given DIE
989 * contain a specific address.
991 * More specifically, the parameter `die` is expected to be a
992 * subprogram (function) DIE, and this predicate tells whether any
993 * subroutines are inlined within this function and would contain
996 * On success, the out parameter `contains` is set with the boolean
997 * value indicating whether the DIE's range covers `addr`. On failure,
998 * it remains unchanged.
1000 * Do note that this function advances the position of `die`. If the
1001 * address is found within one of its children, `die` will be pointing
1002 * to that child upon returning from the function, allowing to extract
1003 * the information deemed necessary.
1005 * @param die The parent DIE in whose children the address will be
1007 * @param addr The address for which to look for in the DIEs
1008 * @param contains Out parameter, true if addr is contained,
1010 * @returns Returns 0 on success, -1 on failure
1013 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1016 bool _contains
= false;
1022 ret
= bt_dwarf_die_child(die
);
1030 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1035 if (tag
== DW_TAG_inlined_subroutine
) {
1036 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1045 } while (bt_dwarf_die_next(die
) == 0);
1048 *contains
= _contains
;
1056 * Lookup the source location for a given address within a CU, making
1057 * the assumption that it is contained within an inline routine in a
1060 * @param cu bt_dwarf_cu instance in which to look for the address
1061 * @param addr The address for which to look for
1062 * @param src_loc Out parameter, the source location (filename and
1063 * line number) for the address
1064 * @returns 0 on success, -1 on failure
1067 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1068 struct source_location
**src_loc
)
1072 struct bt_dwarf_die
*die
= NULL
;
1073 struct source_location
*_src_loc
= NULL
;
1075 if (!cu
|| !src_loc
) {
1079 die
= bt_dwarf_die_create(cu
);
1084 while (bt_dwarf_die_next(die
) == 0) {
1087 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1092 if (tag
== DW_TAG_subprogram
) {
1093 bool contains
= false;
1095 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1102 * Try to find an inlined subroutine
1103 * child of this DIE containing addr.
1105 ret
= bin_info_child_die_has_address(die
, addr
,
1118 char *filename
= NULL
;
1121 _src_loc
= g_new0(struct source_location
, 1);
1126 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1130 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1136 _src_loc
->filename
= filename
;
1137 _src_loc
->line_no
= line_no
;
1138 *src_loc
= _src_loc
;
1141 bt_dwarf_die_destroy(die
);
1145 source_location_destroy(_src_loc
);
1146 bt_dwarf_die_destroy(die
);
1151 * Lookup the source location for a given address within a CU,
1152 * assuming that it is contained within an inlined function.
1154 * A source location can be found regardless of inlining status for
1155 * this method, but in the case of an inlined function, the returned
1156 * source location will point not to the callsite but rather to the
1157 * definition site of the inline function.
1159 * @param cu bt_dwarf_cu instance in which to look for the address
1160 * @param addr The address for which to look for
1161 * @param src_loc Out parameter, the source location (filename and
1162 * line number) for the address
1163 * @returns 0 on success, -1 on failure
1166 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1167 struct source_location
**src_loc
)
1169 struct source_location
*_src_loc
= NULL
;
1170 struct bt_dwarf_die
*die
= NULL
;
1171 const char *filename
= NULL
;
1172 Dwarf_Line
*line
= NULL
;
1173 Dwarf_Addr line_addr
;
1176 if (!cu
|| !src_loc
) {
1180 die
= bt_dwarf_die_create(cu
);
1185 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1190 ret
= dwarf_lineaddr(line
, &line_addr
);
1195 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1200 if (addr
== line_addr
) {
1201 _src_loc
= g_new0(struct source_location
, 1);
1206 ret
= dwarf_lineno(line
, &line_no
);
1211 _src_loc
->line_no
= line_no
;
1212 _src_loc
->filename
= strdup(filename
);
1215 bt_dwarf_die_destroy(die
);
1218 *src_loc
= _src_loc
;
1224 source_location_destroy(_src_loc
);
1225 bt_dwarf_die_destroy(die
);
1230 * Get the source location (file name and line number) for a given
1231 * address within a compile unit (CU).
1233 * On success, the out parameter `src_loc` is set if found. On
1234 * failure, it remains unchanged.
1236 * @param cu bt_dwarf_cu instance for the compile unit which
1237 * may contain the address
1238 * @param addr Virtual memory address for which to find the
1240 * @param src_loc Out parameter, the source location
1241 * @returns 0 on success, -1 on failure
1244 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1245 struct source_location
**src_loc
)
1248 struct source_location
*_src_loc
= NULL
;
1250 if (!cu
|| !src_loc
) {
1254 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1263 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1274 *src_loc
= _src_loc
;
1280 source_location_destroy(_src_loc
);
1285 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1286 struct source_location
**src_loc
)
1288 struct bt_dwarf_cu
*cu
= NULL
;
1289 struct source_location
*_src_loc
= NULL
;
1291 if (!bin
|| !src_loc
) {
1295 /* Set DWARF info if it hasn't been accessed yet. */
1296 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1297 if (bin_info_set_dwarf_info(bin
)) {
1298 /* Failed to set DWARF info. */
1299 bin
->is_elf_only
= true;
1303 if (bin
->is_elf_only
) {
1304 /* We cannot lookup source location without DWARF info. */
1308 if (!bin_info_has_address(bin
, addr
)) {
1313 * Addresses in ELF and DWARF are relative to base address for
1314 * PIC, so make the address argument relative too if needed.
1317 addr
-= bin
->low_addr
;
1320 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1325 while (bt_dwarf_cu_next(cu
) == 0) {
1328 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1338 bt_dwarf_cu_destroy(cu
);
1340 *src_loc
= _src_loc
;
1346 source_location_destroy(_src_loc
);
1347 bt_dwarf_cu_destroy(cu
);