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
, const char *debug_info_dir
,
70 const char *target_prefix
)
72 struct bin_info
*bin
= NULL
;
78 bin
= g_new0(struct bin_info
, 1);
84 bin
->elf_path
= g_build_path("/", target_prefix
,
87 bin
->elf_path
= strdup(path
);
95 bin
->debug_info_dir
= strdup(debug_info_dir
);
96 if (!bin
->debug_info_dir
) {
101 bin
->is_pic
= is_pic
;
103 bin
->low_addr
= low_addr
;
104 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
109 bin_info_destroy(bin
);
114 void bin_info_destroy(struct bin_info
*bin
)
120 dwarf_end(bin
->dwarf_info
);
122 free(bin
->debug_info_dir
);
124 free(bin
->dwarf_path
);
125 g_free(bin
->build_id
);
126 free(bin
->dbg_link_filename
);
128 elf_end(bin
->elf_file
);
131 close(bin
->dwarf_fd
);
138 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
141 if (!bin
|| !build_id
) {
145 bin
->build_id
= malloc(build_id_len
);
146 if (!bin
->build_id
) {
150 memcpy(bin
->build_id
, build_id
, build_id_len
);
151 bin
->build_id_len
= build_id_len
;
154 * Reset the is_elf_only flag in case it had been set
155 * previously, because we might find separate debug info using
156 * the new build id information.
158 bin
->is_elf_only
= false;
168 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
171 if (!bin
|| !filename
) {
175 bin
->dbg_link_filename
= strdup(filename
);
176 if (!bin
->dbg_link_filename
) {
180 bin
->dbg_link_crc
= crc
;
183 * Reset the is_elf_only flag in case it had been set
184 * previously, because we might find separate debug info using
185 * the new build id information.
187 bin
->is_elf_only
= false;
197 * Tries to read DWARF info from the location given by path, and
198 * attach it to the given bin_info instance if it exists.
200 * @param bin bin_info instance for which to set DWARF info
201 * @param path Presumed location of the DWARF info
202 * @returns 0 on success, negative value on failure
205 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
207 int fd
= -1, ret
= 0;
208 struct bt_dwarf_cu
*cu
= NULL
;
209 Dwarf
*dwarf_info
= NULL
;
215 fd
= open(path
, O_RDONLY
);
221 dwarf_info
= dwarf_begin(fd
, DWARF_C_READ
);
227 * Check if the dwarf info has any CU. If not, the
228 * executable's object file contains no DWARF info.
230 cu
= bt_dwarf_cu_create(dwarf_info
);
235 ret
= bt_dwarf_cu_next(cu
);
241 bin
->dwarf_path
= strdup(path
);
242 if (!bin
->dwarf_path
) {
245 bin
->dwarf_info
= dwarf_info
;
255 dwarf_end(dwarf_info
);
263 * Try to set the dwarf_info for a given bin_info instance via the
266 * @param bin bin_info instance for which to retrieve the
267 * DWARF info via build ID
268 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
271 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
274 char *path
= NULL
, *build_id_file
= NULL
;
275 const char *dbg_dir
= NULL
;
276 size_t build_id_file_len
;
278 if (!bin
|| !bin
->build_id
) {
282 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
284 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
285 build_id_file_len
= (2 * bin
->build_id_len
) + 1 +
286 strlen(BUILD_ID_SUFFIX
) + 1;
287 build_id_file
= malloc(build_id_file_len
);
288 if (!build_id_file
) {
292 snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
293 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
294 int path_idx
= 3 + 2 * (i
- 1);
296 snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
298 strcat(build_id_file
, BUILD_ID_SUFFIX
);
300 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
305 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
322 * Tests whether the file located at path exists and has the expected
325 * This predicate is used when looking up separate debug info via the
326 * GNU debuglink method. The expected crc can be found .gnu_debuglink
327 * section in the original ELF file, along with the filename for the
328 * file containing the debug info.
330 * @param path Full path at which to look for the debug file
331 * @param crc Expected checksum for the debug file
332 * @returns 1 if the file exists and has the correct checksum,
336 int is_valid_debug_file(char *path
, uint32_t crc
)
338 int ret
= 0, fd
= -1;
345 fd
= open(path
, O_RDONLY
);
350 ret
= crc32(fd
, &_crc
);
365 * Try to set the dwarf_info for a given bin_info instance via the
368 * @param bin bin_info instance for which to retrieve the
369 * DWARF info via debug link
370 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
373 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
376 const char *dbg_dir
= NULL
;
377 char *dir_name
= NULL
, *bin_dir
= NULL
, *path
= NULL
;
378 size_t max_path_len
= 0;
380 if (!bin
|| !bin
->dbg_link_filename
) {
384 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
386 dir_name
= dirname(bin
->elf_path
);
391 /* bin_dir is just dir_name with a trailing slash */
392 bin_dir
= malloc(strlen(dir_name
) + 2);
397 strcpy(bin_dir
, dir_name
);
398 strcat(bin_dir
, "/");
400 max_path_len
= strlen(dbg_dir
) + strlen(bin_dir
) +
401 strlen(DEBUG_SUBDIR
) + strlen(bin
->dbg_link_filename
)
403 path
= malloc(max_path_len
);
408 /* First look in the executable's dir */
409 strcpy(path
, bin_dir
);
410 strcat(path
, bin
->dbg_link_filename
);
412 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
416 /* If not found, look in .debug subdir */
417 strcpy(path
, bin_dir
);
418 strcat(path
, DEBUG_SUBDIR
);
419 strcat(path
, bin
->dbg_link_filename
);
421 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
425 /* Lastly, look under the global debug directory */
426 strcpy(path
, dbg_dir
);
427 strcat(path
, bin_dir
);
428 strcat(path
, bin
->dbg_link_filename
);
430 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
443 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
452 * Initialize the DWARF info for a given executable.
454 * @param bin bin_info instance
455 * @returns 0 on success, negative value on failure
458 int bin_info_set_dwarf_info(struct bin_info
*bin
)
467 /* First try to set the DWARF info from the ELF file */
468 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
474 * If that fails, try to find separate debug info via build ID
477 ret
= bin_info_set_dwarf_info_build_id(bin
);
482 ret
= bin_info_set_dwarf_info_debug_link(bin
);
492 * Initialize the ELF file for a given executable.
494 * @param bin bin_info instance
495 * @returns 0 on success, negative value on error.
498 int bin_info_set_elf_file(struct bin_info
*bin
)
501 Elf
*elf_file
= NULL
;
507 elf_fd
= open(bin
->elf_path
, O_RDONLY
);
510 printf_verbose("Failed to open %s\n", bin
->elf_path
);
514 elf_file
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
516 printf_debug("elf_begin failed: %s\n", elf_errmsg(-1));
520 if (elf_kind(elf_file
) != ELF_K_ELF
) {
521 printf_verbose("Error: %s is not an ELF object\n",
526 bin
->elf_fd
= elf_fd
;
527 bin
->elf_file
= elf_file
;
540 void source_location_destroy(struct source_location
*src_loc
)
546 free(src_loc
->filename
);
551 * Append a string representation of an address offset to an existing
554 * On success, the out parameter `result` will contain the base string
555 * followed by the offset string of the form "+0x1234". On failure,
556 * `result` remains unchanged.
558 * @param base_str The string to which to append an offset string
559 * @param low_addr The lower virtual memory address, the base from
560 * which the offset is computed
561 * @param high_addr The higher virtual memory address
562 * @param result Out parameter, the base string followed by the
564 * @returns 0 on success, -1 on failure
567 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
568 uint64_t high_addr
, char **result
)
572 char *_result
= NULL
;
573 char offset_str
[ADDR_STR_LEN
];
575 if (!base_str
|| !result
) {
579 offset
= high_addr
- low_addr
;
581 _result
= malloc(strlen(base_str
) + ADDR_STR_LEN
);
586 ret
= snprintf(offset_str
, ADDR_STR_LEN
, "+%#0" PRIx64
, offset
);
590 strcpy(_result
, base_str
);
591 strcat(_result
, offset_str
);
602 * Try to find the symbol closest to an address within a given ELF
605 * Only function symbols are taken into account. The symbol's address
606 * must precede `addr`. A symbol with a closer address might exist
607 * after `addr` but is irrelevant because it cannot encompass `addr`.
609 * On success, if found, the out parameters `sym` and `shdr` are
610 * set. On failure or if none are found, they remain unchanged.
612 * @param scn ELF section in which to look for the address
613 * @param addr Virtual memory address for which to find the
614 * nearest function symbol
615 * @param sym Out parameter, the nearest function symbol
616 * @param shdr Out parameter, the section header for scn
617 * @returns 0 on success, -1 on failure
620 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
621 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
625 Elf_Data
*data
= NULL
;
626 GElf_Shdr
*_shdr
= NULL
;
627 GElf_Sym
*nearest_sym
= NULL
;
629 if (!scn
|| !sym
|| !shdr
) {
633 _shdr
= g_new0(GElf_Shdr
, 1);
638 _shdr
= gelf_getshdr(scn
, _shdr
);
643 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
645 * We are only interested in symbol table (symtab)
646 * sections, skip this one.
651 data
= elf_getdata(scn
, NULL
);
656 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
658 for (i
= 0; i
< symbol_count
; ++i
) {
659 GElf_Sym
*cur_sym
= NULL
;
661 cur_sym
= g_new0(GElf_Sym
, 1);
665 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
669 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
670 /* We're only interested in the functions. */
675 if (cur_sym
->st_value
<= addr
&&
677 cur_sym
->st_value
> nearest_sym
->st_value
)) {
679 nearest_sym
= cur_sym
;
702 * Get the name of the function containing a given address within an
703 * executable using ELF symbols.
705 * The function name is in fact the name of the nearest ELF symbol,
706 * followed by the offset in bytes between the address and the symbol
707 * (in hex), separated by a '+' character.
709 * If found, the out parameter `func_name` is set on success. On failure,
710 * it remains unchanged.
712 * @param bin bin_info instance for the executable containing
714 * @param addr Virtual memory address for which to find the
716 * @param func_name Out parameter, the function name
717 * @returns 0 on success, -1 on failure
720 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
724 * TODO (possible optimisation): if an ELF has no symtab
725 * section, it has been stripped. Therefore, it would be wise
726 * to store a flag indicating the stripped status after the
727 * first iteration to prevent subsequent ones.
731 GElf_Sym
*sym
= NULL
;
732 GElf_Shdr
*shdr
= NULL
;
733 char *sym_name
= NULL
;
735 /* Set ELF file if it hasn't been accessed yet. */
736 if (!bin
->elf_file
) {
737 ret
= bin_info_set_elf_file(bin
);
739 /* Failed to set ELF file. */
744 scn
= elf_nextscn(bin
->elf_file
, scn
);
749 while (scn
&& !sym
) {
750 ret
= bin_info_get_nearest_symbol_from_section(
751 scn
, addr
, &sym
, &shdr
);
756 scn
= elf_nextscn(bin
->elf_file
, scn
);
760 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
766 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
784 * Get the name of the function containing a given address within a
785 * given compile unit (CU).
787 * If found, the out parameter `func_name` is set on success. On
788 * failure, it remains unchanged.
790 * @param cu bt_dwarf_cu instance which may contain the address
791 * @param addr Virtual memory address for which to find the
793 * @param func_name Out parameter, the function name
794 * @returns 0 on success, -1 on failure
797 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
802 struct bt_dwarf_die
*die
= NULL
;
804 if (!cu
|| !func_name
) {
808 die
= bt_dwarf_die_create(cu
);
813 while (bt_dwarf_die_next(die
) == 0) {
816 ret
= bt_dwarf_die_get_tag(die
, &tag
);
821 if (tag
== DW_TAG_subprogram
) {
822 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
834 uint64_t low_addr
= 0;
835 char *die_name
= NULL
;
837 ret
= bt_dwarf_die_get_name(die
, &die_name
);
842 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
848 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
856 bt_dwarf_die_destroy(die
);
860 bt_dwarf_die_destroy(die
);
865 * Get the name of the function containing a given address within an
866 * executable using DWARF debug info.
868 * If found, the out parameter `func_name` is set on success. On
869 * failure, it remains unchanged.
871 * @param bin bin_info instance for the executable containing
873 * @param addr Virtual memory address for which to find the
875 * @param func_name Out parameter, the function name
876 * @returns 0 on success, -1 on failure
879 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
883 char *_func_name
= NULL
;
884 struct bt_dwarf_cu
*cu
= NULL
;
886 if (!bin
|| !func_name
) {
890 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
895 while (bt_dwarf_cu_next(cu
) == 0) {
896 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
907 *func_name
= _func_name
;
912 bt_dwarf_cu_destroy(cu
);
916 bt_dwarf_cu_destroy(cu
);
921 int bin_info_lookup_function_name(struct bin_info
*bin
,
922 uint64_t addr
, char **func_name
)
925 char *_func_name
= NULL
;
927 if (!bin
|| !func_name
) {
931 /* Set DWARF info if it hasn't been accessed yet. */
932 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
933 ret
= bin_info_set_dwarf_info(bin
);
935 printf_verbose("Failed to set bin dwarf info, falling back to ELF lookup.\n");
936 /* Failed to set DWARF info, fallback to ELF. */
937 bin
->is_elf_only
= true;
941 if (!bin_info_has_address(bin
, addr
)) {
946 * Addresses in ELF and DWARF are relative to base address for
947 * PIC, so make the address argument relative too if needed.
950 addr
-= bin
->low_addr
;
953 if (bin
->is_elf_only
) {
954 ret
= bin_info_lookup_elf_function_name(bin
, addr
, &_func_name
);
955 printf_verbose("Failed to lookup function name (elf), error %i\n", ret
);
957 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
, &_func_name
);
958 printf_verbose("Failed to lookup function name (dwarf), error %i\n", ret
);
961 *func_name
= _func_name
;
969 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
972 char *_bin_loc
= NULL
;
974 if (!bin
|| !bin_loc
) {
979 addr
-= bin
->low_addr
;
980 ret
= asprintf(&_bin_loc
, "+%#0" PRIx64
, addr
);
982 ret
= asprintf(&_bin_loc
, "@%#0" PRIx64
, addr
);
985 if (ret
== -1 || !_bin_loc
) {
997 * Predicate used to determine whether the children of a given DIE
998 * contain a specific address.
1000 * More specifically, the parameter `die` is expected to be a
1001 * subprogram (function) DIE, and this predicate tells whether any
1002 * subroutines are inlined within this function and would contain
1005 * On success, the out parameter `contains` is set with the boolean
1006 * value indicating whether the DIE's range covers `addr`. On failure,
1007 * it remains unchanged.
1009 * Do note that this function advances the position of `die`. If the
1010 * address is found within one of its children, `die` will be pointing
1011 * to that child upon returning from the function, allowing to extract
1012 * the information deemed necessary.
1014 * @param die The parent DIE in whose children the address will be
1016 * @param addr The address for which to look for in the DIEs
1017 * @param contains Out parameter, true if addr is contained,
1019 * @returns Returns 0 on success, -1 on failure
1022 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1025 bool _contains
= false;
1031 ret
= bt_dwarf_die_child(die
);
1039 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1044 if (tag
== DW_TAG_inlined_subroutine
) {
1045 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1054 } while (bt_dwarf_die_next(die
) == 0);
1057 *contains
= _contains
;
1065 * Lookup the source location for a given address within a CU, making
1066 * the assumption that it is contained within an inline routine in a
1069 * @param cu bt_dwarf_cu instance in which to look for the address
1070 * @param addr The address for which to look for
1071 * @param src_loc Out parameter, the source location (filename and
1072 * line number) for the address
1073 * @returns 0 on success, -1 on failure
1076 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1077 struct source_location
**src_loc
)
1081 struct bt_dwarf_die
*die
= NULL
;
1082 struct source_location
*_src_loc
= NULL
;
1084 if (!cu
|| !src_loc
) {
1088 die
= bt_dwarf_die_create(cu
);
1093 while (bt_dwarf_die_next(die
) == 0) {
1096 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1101 if (tag
== DW_TAG_subprogram
) {
1102 bool contains
= false;
1104 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1111 * Try to find an inlined subroutine
1112 * child of this DIE containing addr.
1114 ret
= bin_info_child_die_has_address(die
, addr
,
1127 char *filename
= NULL
;
1130 _src_loc
= g_new0(struct source_location
, 1);
1135 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1139 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1145 _src_loc
->filename
= filename
;
1146 _src_loc
->line_no
= line_no
;
1147 *src_loc
= _src_loc
;
1150 bt_dwarf_die_destroy(die
);
1154 source_location_destroy(_src_loc
);
1155 bt_dwarf_die_destroy(die
);
1160 * Lookup the source location for a given address within a CU,
1161 * assuming that it is contained within an inlined function.
1163 * A source location can be found regardless of inlining status for
1164 * this method, but in the case of an inlined function, the returned
1165 * source location will point not to the callsite but rather to the
1166 * definition site of the inline function.
1168 * @param cu bt_dwarf_cu instance in which to look for the address
1169 * @param addr The address for which to look for
1170 * @param src_loc Out parameter, the source location (filename and
1171 * line number) for the address
1172 * @returns 0 on success, -1 on failure
1175 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1176 struct source_location
**src_loc
)
1178 struct source_location
*_src_loc
= NULL
;
1179 struct bt_dwarf_die
*die
= NULL
;
1180 const char *filename
= NULL
;
1181 Dwarf_Line
*line
= NULL
;
1182 Dwarf_Addr line_addr
;
1185 if (!cu
|| !src_loc
) {
1189 die
= bt_dwarf_die_create(cu
);
1194 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1199 ret
= dwarf_lineaddr(line
, &line_addr
);
1204 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1209 if (addr
== line_addr
) {
1210 _src_loc
= g_new0(struct source_location
, 1);
1215 ret
= dwarf_lineno(line
, &line_no
);
1220 _src_loc
->line_no
= line_no
;
1221 _src_loc
->filename
= strdup(filename
);
1224 bt_dwarf_die_destroy(die
);
1227 *src_loc
= _src_loc
;
1233 source_location_destroy(_src_loc
);
1234 bt_dwarf_die_destroy(die
);
1239 * Get the source location (file name and line number) for a given
1240 * address within a compile unit (CU).
1242 * On success, the out parameter `src_loc` is set if found. On
1243 * failure, it remains unchanged.
1245 * @param cu bt_dwarf_cu instance for the compile unit which
1246 * may contain the address
1247 * @param addr Virtual memory address for which to find the
1249 * @param src_loc Out parameter, the source location
1250 * @returns 0 on success, -1 on failure
1253 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1254 struct source_location
**src_loc
)
1257 struct source_location
*_src_loc
= NULL
;
1259 if (!cu
|| !src_loc
) {
1263 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1272 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1283 *src_loc
= _src_loc
;
1289 source_location_destroy(_src_loc
);
1294 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1295 struct source_location
**src_loc
)
1297 struct bt_dwarf_cu
*cu
= NULL
;
1298 struct source_location
*_src_loc
= NULL
;
1300 if (!bin
|| !src_loc
) {
1304 /* Set DWARF info if it hasn't been accessed yet. */
1305 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1306 if (bin_info_set_dwarf_info(bin
)) {
1307 /* Failed to set DWARF info. */
1308 bin
->is_elf_only
= true;
1312 if (bin
->is_elf_only
) {
1313 /* We cannot lookup source location without DWARF info. */
1317 if (!bin_info_has_address(bin
, addr
)) {
1322 * Addresses in ELF and DWARF are relative to base address for
1323 * PIC, so make the address argument relative too if needed.
1326 addr
-= bin
->low_addr
;
1329 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1334 while (bt_dwarf_cu_next(cu
) == 0) {
1337 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1347 bt_dwarf_cu_destroy(cu
);
1349 *src_loc
= _src_loc
;
1355 source_location_destroy(_src_loc
);
1356 bt_dwarf_cu_destroy(cu
);