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
39 #include <babeltrace/dwarf.h>
40 #include <babeltrace/bin-info.h>
41 #include <babeltrace/crc32.h>
42 #include <babeltrace/babeltrace-internal.h>
43 #include <babeltrace/utils.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
);
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
, char *filename
, uint32_t crc
)
161 if (!bin
|| !filename
) {
165 bin
->dbg_link_filename
= strdup(filename
);
166 if (!bin
->dbg_link_filename
) {
170 bin
->dbg_link_crc
= crc
;
173 * Reset the is_elf_only flag in case it had been set
174 * previously, because we might find separate debug info using
175 * the new build id information.
177 bin
->is_elf_only
= false;
187 * Tries to read DWARF info from the location given by path, and
188 * attach it to the given bin_info instance if it exists.
190 * @param bin bin_info instance for which to set DWARF info
191 * @param path Presumed location of the DWARF info
192 * @returns 0 on success, negative value on failure
195 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
197 int fd
= -1, ret
= 0;
198 struct bt_dwarf_cu
*cu
= NULL
;
199 Dwarf
*dwarf_info
= NULL
;
205 fd
= open(path
, O_RDONLY
);
211 dwarf_info
= dwarf_begin(fd
, DWARF_C_READ
);
217 * Check if the dwarf info has any CU. If not, the
218 * executable's object file contains no DWARF info.
220 cu
= bt_dwarf_cu_create(dwarf_info
);
225 ret
= bt_dwarf_cu_next(cu
);
231 bin
->dwarf_path
= strdup(path
);
232 if (!bin
->dwarf_path
) {
235 bin
->dwarf_info
= dwarf_info
;
245 dwarf_end(dwarf_info
);
253 * Try to set the dwarf_info for a given bin_info instance via the
256 * @param bin bin_info instance for which to retrieve the
257 * DWARF info via build ID
258 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
261 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
264 char *path
= NULL
, *build_id_file
= NULL
;
265 const char *dbg_dir
= NULL
;
266 size_t build_id_file_len
;
268 if (!bin
|| !bin
->build_id
) {
272 dbg_dir
= opt_debug_info_dir
? : DEFAULT_DEBUG_DIR
;
274 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
275 build_id_file_len
= (2 * bin
->build_id_len
) + 1 +
276 strlen(BUILD_ID_SUFFIX
) + 1;
277 build_id_file
= malloc(build_id_file_len
);
278 if (!build_id_file
) {
282 snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
283 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
284 int path_idx
= 3 + 2 * (i
- 1);
286 snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
288 strcat(build_id_file
, BUILD_ID_SUFFIX
);
290 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
295 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
312 * Tests whether the file located at path exists and has the expected
315 * This predicate is used when looking up separate debug info via the
316 * GNU debuglink method. The expected crc can be found .gnu_debuglink
317 * section in the original ELF file, along with the filename for the
318 * file containing the debug info.
320 * @param path Full path at which to look for the debug file
321 * @param crc Expected checksum for the debug file
322 * @returns 1 if the file exists and has the correct checksum,
326 int is_valid_debug_file(char *path
, uint32_t crc
)
328 int ret
= 0, fd
= -1;
335 fd
= open(path
, O_RDONLY
);
340 ret
= crc32(fd
, &_crc
);
354 * Try to set the dwarf_info for a given bin_info instance via the
357 * @param bin bin_info instance for which to retrieve the
358 * DWARF info via debug link
359 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
362 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
365 const char *dbg_dir
= NULL
;
366 char *dir_name
= NULL
, *bin_dir
= NULL
, *path
= NULL
;
367 size_t max_path_len
= 0;
369 if (!bin
|| !bin
->dbg_link_filename
) {
373 dbg_dir
= opt_debug_info_dir
? : DEFAULT_DEBUG_DIR
;
375 dir_name
= dirname(bin
->elf_path
);
380 /* bin_dir is just dir_name with a trailing slash */
381 bin_dir
= malloc(strlen(dir_name
) + 2);
386 strcpy(bin_dir
, dir_name
);
387 strcat(bin_dir
, "/");
389 max_path_len
= strlen(dbg_dir
) + strlen(bin_dir
) +
390 strlen(DEBUG_SUBDIR
) + strlen(bin
->dbg_link_filename
)
392 path
= malloc(max_path_len
);
397 /* First look in the executable's dir */
398 strcpy(path
, bin_dir
);
399 strcat(path
, bin
->dbg_link_filename
);
401 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
405 /* If not found, look in .debug subdir */
406 strcpy(path
, bin_dir
);
407 strcat(path
, DEBUG_SUBDIR
);
408 strcat(path
, bin
->dbg_link_filename
);
410 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
414 /* Lastly, look under the global debug directory */
415 strcpy(path
, dbg_dir
);
416 strcat(path
, bin_dir
);
417 strcat(path
, bin
->dbg_link_filename
);
419 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
432 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
441 * Initialize the DWARF info for a given executable.
443 * @param bin bin_info instance
444 * @returns 0 on success, negative value on failure
447 int bin_info_set_dwarf_info(struct bin_info
*bin
)
456 /* First try to set the DWARF info from the ELF file */
457 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
463 * If that fails, try to find separate debug info via build ID
466 ret
= bin_info_set_dwarf_info_build_id(bin
);
471 ret
= bin_info_set_dwarf_info_debug_link(bin
);
481 * Initialize the ELF file for a given executable.
483 * @param bin bin_info instance
484 * @returns 0 on success, negative value on error.
487 int bin_info_set_elf_file(struct bin_info
*bin
)
490 Elf
*elf_file
= NULL
;
496 elf_fd
= open(bin
->elf_path
, O_RDONLY
);
499 printf_verbose("Failed to open %s\n", bin
->elf_path
);
503 elf_file
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
505 printf_debug("elf_begin failed: %s\n", elf_errmsg(-1));
509 if (elf_kind(elf_file
) != ELF_K_ELF
) {
510 printf_verbose("Error: %s is not an ELF object\n",
515 bin
->elf_fd
= elf_fd
;
516 bin
->elf_file
= elf_file
;
529 void source_location_destroy(struct source_location
*src_loc
)
535 free(src_loc
->filename
);
540 * Append a string representation of an address offset to an existing
543 * On success, the out parameter `result` will contain the base string
544 * followed by the offset string of the form "+0x1234". On failure,
545 * `result` remains unchanged.
547 * @param base_str The string to which to append an offset string
548 * @param low_addr The lower virtual memory address, the base from
549 * which the offset is computed
550 * @param high_addr The higher virtual memory address
551 * @param result Out parameter, the base string followed by the
553 * @returns 0 on success, -1 on failure
556 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
557 uint64_t high_addr
, char **result
)
561 char *_result
= NULL
;
562 char offset_str
[ADDR_STR_LEN
];
564 if (!base_str
|| !result
) {
568 offset
= high_addr
- low_addr
;
570 _result
= malloc(strlen(base_str
) + ADDR_STR_LEN
);
575 ret
= snprintf(offset_str
, ADDR_STR_LEN
, "+%#0" PRIx64
, offset
);
579 strcpy(_result
, base_str
);
580 strcat(_result
, offset_str
);
591 * Try to find the symbol closest to an address within a given ELF
594 * Only function symbols are taken into account. The symbol's address
595 * must precede `addr`. A symbol with a closer address might exist
596 * after `addr` but is irrelevant because it cannot encompass `addr`.
598 * On success, if found, the out parameters `sym` and `shdr` are
599 * set. On failure or if none are found, they remain unchanged.
601 * @param scn ELF section in which to look for the address
602 * @param addr Virtual memory address for which to find the
603 * nearest function symbol
604 * @param sym Out parameter, the nearest function symbol
605 * @param shdr Out parameter, the section header for scn
606 * @returns 0 on success, -1 on failure
609 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
610 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
614 Elf_Data
*data
= NULL
;
615 GElf_Shdr
*_shdr
= NULL
;
616 GElf_Sym
*nearest_sym
= NULL
;
618 if (!scn
|| !sym
|| !shdr
) {
622 _shdr
= g_new0(GElf_Shdr
, 1);
627 _shdr
= gelf_getshdr(scn
, _shdr
);
632 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
634 * We are only interested in symbol table (symtab)
635 * sections, skip this one.
640 data
= elf_getdata(scn
, NULL
);
645 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
647 for (i
= 0; i
< symbol_count
; ++i
) {
648 GElf_Sym
*cur_sym
= NULL
;
650 cur_sym
= g_new0(GElf_Sym
, 1);
654 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
658 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
659 /* We're only interested in the functions. */
664 if (cur_sym
->st_value
<= addr
&&
666 cur_sym
->st_value
> nearest_sym
->st_value
)) {
668 nearest_sym
= cur_sym
;
691 * Get the name of the function containing a given address within an
692 * executable using ELF symbols.
694 * The function name is in fact the name of the nearest ELF symbol,
695 * followed by the offset in bytes between the address and the symbol
696 * (in hex), separated by a '+' character.
698 * If found, the out parameter `func_name` is set on success. On failure,
699 * it remains unchanged.
701 * @param bin bin_info instance for the executable containing
703 * @param addr Virtual memory address for which to find the
705 * @param func_name Out parameter, the function name
706 * @returns 0 on success, -1 on failure
709 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
713 * TODO (possible optimisation): if an ELF has no symtab
714 * section, it has been stripped. Therefore, it would be wise
715 * to store a flag indicating the stripped status after the
716 * first iteration to prevent subsequent ones.
720 GElf_Sym
*sym
= NULL
;
721 GElf_Shdr
*shdr
= NULL
;
722 char *sym_name
= NULL
;
724 /* Set ELF file if it hasn't been accessed yet. */
725 if (!bin
->elf_file
) {
726 ret
= bin_info_set_elf_file(bin
);
728 /* Failed to set ELF file. */
733 scn
= elf_nextscn(bin
->elf_file
, scn
);
738 while (scn
&& !sym
) {
739 ret
= bin_info_get_nearest_symbol_from_section(
740 scn
, addr
, &sym
, &shdr
);
745 scn
= elf_nextscn(bin
->elf_file
, scn
);
749 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
755 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
773 * Get the name of the function containing a given address within a
774 * given compile unit (CU).
776 * If found, the out parameter `func_name` is set on success. On
777 * failure, it remains unchanged.
779 * @param cu bt_dwarf_cu instance which may contain the address
780 * @param addr Virtual memory address for which to find the
782 * @param func_name Out parameter, the function name
783 * @returns 0 on success, -1 on failure
786 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
791 struct bt_dwarf_die
*die
= NULL
;
793 if (!cu
|| !func_name
) {
797 die
= bt_dwarf_die_create(cu
);
802 while (bt_dwarf_die_next(die
) == 0) {
805 ret
= bt_dwarf_die_get_tag(die
, &tag
);
810 if (tag
== DW_TAG_subprogram
) {
811 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
823 uint64_t low_addr
= 0;
824 char *die_name
= NULL
;
826 ret
= bt_dwarf_die_get_name(die
, &die_name
);
831 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
836 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
843 bt_dwarf_die_destroy(die
);
847 bt_dwarf_die_destroy(die
);
852 * Get the name of the function containing a given address within an
853 * executable using DWARF debug info.
855 * If found, the out parameter `func_name` is set on success. On
856 * failure, it remains unchanged.
858 * @param bin bin_info instance for the executable containing
860 * @param addr Virtual memory address for which to find the
862 * @param func_name Out parameter, the function name
863 * @returns 0 on success, -1 on failure
866 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
870 char *_func_name
= NULL
;
871 struct bt_dwarf_cu
*cu
= NULL
;
873 if (!bin
|| !func_name
) {
877 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
882 while (bt_dwarf_cu_next(cu
) == 0) {
883 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
894 *func_name
= _func_name
;
899 bt_dwarf_cu_destroy(cu
);
903 bt_dwarf_cu_destroy(cu
);
908 int bin_info_lookup_function_name(struct bin_info
*bin
, uint64_t addr
,
912 char *_func_name
= NULL
;
914 if (!bin
|| !func_name
) {
918 /* Set DWARF info if it hasn't been accessed yet. */
919 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
920 ret
= bin_info_set_dwarf_info(bin
);
922 printf_verbose("Failed to set bin dwarf info, falling back to ELF lookup.\n");
923 /* Failed to set DWARF info, fallback to ELF. */
924 bin
->is_elf_only
= true;
928 if (!bin_info_has_address(bin
, addr
)) {
933 * Addresses in ELF and DWARF are relative to base address for
934 * PIC, so make the address argument relative too if needed.
937 addr
-= bin
->low_addr
;
940 if (bin
->is_elf_only
) {
941 ret
= bin_info_lookup_elf_function_name(bin
, addr
, &_func_name
);
942 printf_verbose("Failed to lookup function name (elf), error %i\n", ret
);
944 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
, &_func_name
);
945 printf_verbose("Failed to lookup function name (dwarf), error %i\n", ret
);
948 *func_name
= _func_name
;
956 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
959 char *_bin_loc
= NULL
;
961 if (!bin
|| !bin_loc
) {
966 addr
-= bin
->low_addr
;
967 ret
= asprintf(&_bin_loc
, "+%#0" PRIx64
, addr
);
969 ret
= asprintf(&_bin_loc
, "@%#0" PRIx64
, addr
);
972 if (ret
== -1 || !_bin_loc
) {
984 * Predicate used to determine whether the children of a given DIE
985 * contain a specific address.
987 * More specifically, the parameter `die` is expected to be a
988 * subprogram (function) DIE, and this predicate tells whether any
989 * subroutines are inlined within this function and would contain
992 * On success, the out parameter `contains` is set with the boolean
993 * value indicating whether the DIE's range covers `addr`. On failure,
994 * it remains unchanged.
996 * Do note that this function advances the position of `die`. If the
997 * address is found within one of its children, `die` will be pointing
998 * to that child upon returning from the function, allowing to extract
999 * the information deemed necessary.
1001 * @param die The parent DIE in whose children the address will be
1003 * @param addr The address for which to look for in the DIEs
1004 * @param contains Out parameter, true if addr is contained,
1006 * @returns Returns 0 on success, -1 on failure
1009 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1012 bool _contains
= false;
1018 ret
= bt_dwarf_die_child(die
);
1026 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1031 if (tag
== DW_TAG_inlined_subroutine
) {
1032 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1041 } while (bt_dwarf_die_next(die
) == 0);
1044 *contains
= _contains
;
1052 * Lookup the source location for a given address within a CU, making
1053 * the assumption that it is contained within an inline routine in a
1056 * @param cu bt_dwarf_cu instance in which to look for the address
1057 * @param addr The address for which to look for
1058 * @param src_loc Out parameter, the source location (filename and
1059 * line number) for the address
1060 * @returns 0 on success, -1 on failure
1063 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1064 struct source_location
**src_loc
)
1068 struct bt_dwarf_die
*die
= NULL
;
1069 struct source_location
*_src_loc
= NULL
;
1071 if (!cu
|| !src_loc
) {
1075 die
= bt_dwarf_die_create(cu
);
1080 while (bt_dwarf_die_next(die
) == 0) {
1083 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1088 if (tag
== DW_TAG_subprogram
) {
1089 bool contains
= false;
1091 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1098 * Try to find an inlined subroutine
1099 * child of this DIE containing addr.
1101 ret
= bin_info_child_die_has_address(die
, addr
,
1114 char *filename
= NULL
;
1117 _src_loc
= g_new0(struct source_location
, 1);
1122 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1126 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1132 _src_loc
->filename
= filename
;
1133 _src_loc
->line_no
= line_no
;
1134 *src_loc
= _src_loc
;
1137 bt_dwarf_die_destroy(die
);
1141 source_location_destroy(_src_loc
);
1142 bt_dwarf_die_destroy(die
);
1147 * Lookup the source location for a given address within a CU,
1148 * assuming that it is contained within an inlined function.
1150 * A source location can be found regardless of inlining status for
1151 * this method, but in the case of an inlined function, the returned
1152 * source location will point not to the callsite but rather to the
1153 * definition site of the inline function.
1155 * @param cu bt_dwarf_cu instance in which to look for the address
1156 * @param addr The address for which to look for
1157 * @param src_loc Out parameter, the source location (filename and
1158 * line number) for the address
1159 * @returns 0 on success, -1 on failure
1162 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1163 struct source_location
**src_loc
)
1165 struct source_location
*_src_loc
= NULL
;
1166 struct bt_dwarf_die
*die
= NULL
;
1167 const char *filename
= NULL
;
1168 Dwarf_Line
*line
= NULL
;
1169 Dwarf_Addr line_addr
;
1172 if (!cu
|| !src_loc
) {
1176 die
= bt_dwarf_die_create(cu
);
1181 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1186 ret
= dwarf_lineaddr(line
, &line_addr
);
1191 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1196 if (addr
== line_addr
) {
1197 _src_loc
= g_new0(struct source_location
, 1);
1202 ret
= dwarf_lineno(line
, &line_no
);
1207 _src_loc
->line_no
= line_no
;
1208 _src_loc
->filename
= strdup(filename
);
1211 bt_dwarf_die_destroy(die
);
1214 *src_loc
= _src_loc
;
1220 source_location_destroy(_src_loc
);
1221 bt_dwarf_die_destroy(die
);
1226 * Get the source location (file name and line number) for a given
1227 * address within a compile unit (CU).
1229 * On success, the out parameter `src_loc` is set if found. On
1230 * failure, it remains unchanged.
1232 * @param cu bt_dwarf_cu instance for the compile unit which
1233 * may contain the address
1234 * @param addr Virtual memory address for which to find the
1236 * @param src_loc Out parameter, the source location
1237 * @returns 0 on success, -1 on failure
1240 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1241 struct source_location
**src_loc
)
1244 struct source_location
*_src_loc
= NULL
;
1246 if (!cu
|| !src_loc
) {
1250 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1259 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1270 *src_loc
= _src_loc
;
1276 source_location_destroy(_src_loc
);
1281 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1282 struct source_location
**src_loc
)
1284 struct bt_dwarf_cu
*cu
= NULL
;
1285 struct source_location
*_src_loc
= NULL
;
1287 if (!bin
|| !src_loc
) {
1291 /* Set DWARF info if it hasn't been accessed yet. */
1292 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1293 if (bin_info_set_dwarf_info(bin
)) {
1294 /* Failed to set DWARF info. */
1295 bin
->is_elf_only
= true;
1299 if (bin
->is_elf_only
) {
1300 /* We cannot lookup source location without DWARF info. */
1304 if (!bin_info_has_address(bin
, addr
)) {
1309 * Addresses in ELF and DWARF are relative to base address for
1310 * PIC, so make the address argument relative too if needed.
1313 addr
-= bin
->low_addr
;
1316 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1321 while (bt_dwarf_cu_next(cu
) == 0) {
1324 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1334 bt_dwarf_cu_destroy(cu
);
1336 *src_loc
= _src_loc
;
1342 source_location_destroy(_src_loc
);
1343 bt_dwarf_cu_destroy(cu
);