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
56 int bin_info_init(void)
60 if (elf_version(EV_CURRENT
) == EV_NONE
) {
61 BT_LOGD("ELF library initialization failed: %s.",
70 struct bin_info
*bin_info_create(const char *path
, uint64_t low_addr
,
71 uint64_t memsz
, bool is_pic
, const char *debug_info_dir
,
72 const char *target_prefix
)
74 struct bin_info
*bin
= NULL
;
80 bin
= g_new0(struct bin_info
, 1);
86 bin
->elf_path
= g_build_path("/", target_prefix
,
89 bin
->elf_path
= strdup(path
);
97 bin
->debug_info_dir
= strdup(debug_info_dir
);
98 if (!bin
->debug_info_dir
) {
103 bin
->is_pic
= is_pic
;
105 bin
->low_addr
= low_addr
;
106 bin
->high_addr
= bin
->low_addr
+ bin
->memsz
;
111 bin_info_destroy(bin
);
116 void bin_info_destroy(struct bin_info
*bin
)
122 dwarf_end(bin
->dwarf_info
);
124 free(bin
->debug_info_dir
);
126 free(bin
->dwarf_path
);
127 g_free(bin
->build_id
);
128 free(bin
->dbg_link_filename
);
130 elf_end(bin
->elf_file
);
133 close(bin
->dwarf_fd
);
140 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
143 if (!bin
|| !build_id
) {
147 bin
->build_id
= malloc(build_id_len
);
148 if (!bin
->build_id
) {
152 memcpy(bin
->build_id
, build_id
, build_id_len
);
153 bin
->build_id_len
= build_id_len
;
156 * Reset the is_elf_only flag in case it had been set
157 * previously, because we might find separate debug info using
158 * the new build id information.
160 bin
->is_elf_only
= false;
170 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
173 if (!bin
|| !filename
) {
177 bin
->dbg_link_filename
= strdup(filename
);
178 if (!bin
->dbg_link_filename
) {
182 bin
->dbg_link_crc
= crc
;
185 * Reset the is_elf_only flag in case it had been set
186 * previously, because we might find separate debug info using
187 * the new build id information.
189 bin
->is_elf_only
= false;
199 * Tries to read DWARF info from the location given by path, and
200 * attach it to the given bin_info instance if it exists.
202 * @param bin bin_info instance for which to set DWARF info
203 * @param path Presumed location of the DWARF info
204 * @returns 0 on success, negative value on failure
207 int bin_info_set_dwarf_info_from_path(struct bin_info
*bin
, char *path
)
209 int fd
= -1, ret
= 0;
210 struct bt_dwarf_cu
*cu
= NULL
;
211 Dwarf
*dwarf_info
= NULL
;
217 fd
= open(path
, O_RDONLY
);
223 dwarf_info
= dwarf_begin(fd
, DWARF_C_READ
);
229 * Check if the dwarf info has any CU. If not, the
230 * executable's object file contains no DWARF info.
232 cu
= bt_dwarf_cu_create(dwarf_info
);
237 ret
= bt_dwarf_cu_next(cu
);
243 bin
->dwarf_path
= strdup(path
);
244 if (!bin
->dwarf_path
) {
247 bin
->dwarf_info
= dwarf_info
;
257 dwarf_end(dwarf_info
);
265 * Try to set the dwarf_info for a given bin_info instance via the
268 * @param bin bin_info instance for which to retrieve the
269 * DWARF info via build ID
270 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
273 int bin_info_set_dwarf_info_build_id(struct bin_info
*bin
)
276 char *path
= NULL
, *build_id_file
= NULL
;
277 const char *dbg_dir
= NULL
;
278 size_t build_id_file_len
;
280 if (!bin
|| !bin
->build_id
) {
284 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
286 /* 2 characters per byte printed in hex, +1 for '/' and +1 for '\0' */
287 build_id_file_len
= (2 * bin
->build_id_len
) + 1 +
288 strlen(BUILD_ID_SUFFIX
) + 1;
289 build_id_file
= malloc(build_id_file_len
);
290 if (!build_id_file
) {
294 snprintf(build_id_file
, 4, "%02x/", bin
->build_id
[0]);
295 for (i
= 1; i
< bin
->build_id_len
; ++i
) {
296 int path_idx
= 3 + 2 * (i
- 1);
298 snprintf(&build_id_file
[path_idx
], 3, "%02x", bin
->build_id
[i
]);
300 strcat(build_id_file
, BUILD_ID_SUFFIX
);
302 path
= g_build_path("/", dbg_dir
, BUILD_ID_SUBDIR
, build_id_file
, NULL
);
307 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
324 * Tests whether the file located at path exists and has the expected
327 * This predicate is used when looking up separate debug info via the
328 * GNU debuglink method. The expected crc can be found .gnu_debuglink
329 * section in the original ELF file, along with the filename for the
330 * file containing the debug info.
332 * @param path Full path at which to look for the debug file
333 * @param crc Expected checksum for the debug file
334 * @returns 1 if the file exists and has the correct checksum,
338 int is_valid_debug_file(char *path
, uint32_t crc
)
340 int ret
= 0, fd
= -1;
347 fd
= open(path
, O_RDONLY
);
352 ret
= crc32(fd
, &_crc
);
367 * Try to set the dwarf_info for a given bin_info instance via the
370 * @param bin bin_info instance for which to retrieve the
371 * DWARF info via debug link
372 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
375 int bin_info_set_dwarf_info_debug_link(struct bin_info
*bin
)
378 const char *dbg_dir
= NULL
;
379 char *dir_name
= NULL
, *bin_dir
= NULL
, *path
= NULL
;
380 size_t max_path_len
= 0;
382 if (!bin
|| !bin
->dbg_link_filename
) {
386 dbg_dir
= bin
->debug_info_dir
? : DEFAULT_DEBUG_DIR
;
388 dir_name
= dirname(bin
->elf_path
);
393 /* bin_dir is just dir_name with a trailing slash */
394 bin_dir
= malloc(strlen(dir_name
) + 2);
399 strcpy(bin_dir
, dir_name
);
400 strcat(bin_dir
, "/");
402 max_path_len
= strlen(dbg_dir
) + strlen(bin_dir
) +
403 strlen(DEBUG_SUBDIR
) + strlen(bin
->dbg_link_filename
)
405 path
= malloc(max_path_len
);
410 /* First look in the executable's dir */
411 strcpy(path
, bin_dir
);
412 strcat(path
, bin
->dbg_link_filename
);
414 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
418 /* If not found, look in .debug subdir */
419 strcpy(path
, bin_dir
);
420 strcat(path
, DEBUG_SUBDIR
);
421 strcat(path
, bin
->dbg_link_filename
);
423 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
427 /* Lastly, look under the global debug directory */
428 strcpy(path
, dbg_dir
);
429 strcat(path
, bin_dir
);
430 strcat(path
, bin
->dbg_link_filename
);
432 if (is_valid_debug_file(path
, bin
->dbg_link_crc
)) {
445 ret
= bin_info_set_dwarf_info_from_path(bin
, path
);
454 * Initialize the DWARF info for a given executable.
456 * @param bin bin_info instance
457 * @returns 0 on success, negative value on failure
460 int bin_info_set_dwarf_info(struct bin_info
*bin
)
469 /* First try to set the DWARF info from the ELF file */
470 ret
= bin_info_set_dwarf_info_from_path(bin
, bin
->elf_path
);
476 * If that fails, try to find separate debug info via build ID
479 ret
= bin_info_set_dwarf_info_build_id(bin
);
484 ret
= bin_info_set_dwarf_info_debug_link(bin
);
494 * Initialize the ELF file for a given executable.
496 * @param bin bin_info instance
497 * @returns 0 on success, negative value on error.
500 int bin_info_set_elf_file(struct bin_info
*bin
)
503 Elf
*elf_file
= NULL
;
509 elf_fd
= open(bin
->elf_path
, O_RDONLY
);
512 BT_LOGD("Failed to open ELF file: path=\"%s\"", bin
->elf_path
);
516 elf_file
= elf_begin(elf_fd
, ELF_C_READ
, NULL
);
518 BT_LOGD("elf_begin() failed: %s.", elf_errmsg(-1));
522 if (elf_kind(elf_file
) != ELF_K_ELF
) {
523 BT_LOGD("ELF file is not an ELF object: path=\"%s\"",
528 bin
->elf_fd
= elf_fd
;
529 bin
->elf_file
= elf_file
;
542 void source_location_destroy(struct source_location
*src_loc
)
548 free(src_loc
->filename
);
553 * Append a string representation of an address offset to an existing
556 * On success, the out parameter `result` will contain the base string
557 * followed by the offset string of the form "+0x1234". On failure,
558 * `result` remains unchanged.
560 * @param base_str The string to which to append an offset string
561 * @param low_addr The lower virtual memory address, the base from
562 * which the offset is computed
563 * @param high_addr The higher virtual memory address
564 * @param result Out parameter, the base string followed by the
566 * @returns 0 on success, -1 on failure
569 int bin_info_append_offset_str(const char *base_str
, uint64_t low_addr
,
570 uint64_t high_addr
, char **result
)
574 char *_result
= NULL
;
575 char offset_str
[ADDR_STR_LEN
];
577 if (!base_str
|| !result
) {
581 offset
= high_addr
- low_addr
;
583 _result
= malloc(strlen(base_str
) + ADDR_STR_LEN
);
588 ret
= snprintf(offset_str
, ADDR_STR_LEN
, "+%#0" PRIx64
, offset
);
592 strcpy(_result
, base_str
);
593 strcat(_result
, offset_str
);
604 * Try to find the symbol closest to an address within a given ELF
607 * Only function symbols are taken into account. The symbol's address
608 * must precede `addr`. A symbol with a closer address might exist
609 * after `addr` but is irrelevant because it cannot encompass `addr`.
611 * On success, if found, the out parameters `sym` and `shdr` are
612 * set. On failure or if none are found, they remain unchanged.
614 * @param scn ELF section in which to look for the address
615 * @param addr Virtual memory address for which to find the
616 * nearest function symbol
617 * @param sym Out parameter, the nearest function symbol
618 * @param shdr Out parameter, the section header for scn
619 * @returns 0 on success, -1 on failure
622 int bin_info_get_nearest_symbol_from_section(Elf_Scn
*scn
, uint64_t addr
,
623 GElf_Sym
**sym
, GElf_Shdr
**shdr
)
627 Elf_Data
*data
= NULL
;
628 GElf_Shdr
*_shdr
= NULL
;
629 GElf_Sym
*nearest_sym
= NULL
;
631 if (!scn
|| !sym
|| !shdr
) {
635 _shdr
= g_new0(GElf_Shdr
, 1);
640 _shdr
= gelf_getshdr(scn
, _shdr
);
645 if (_shdr
->sh_type
!= SHT_SYMTAB
) {
647 * We are only interested in symbol table (symtab)
648 * sections, skip this one.
653 data
= elf_getdata(scn
, NULL
);
658 symbol_count
= _shdr
->sh_size
/ _shdr
->sh_entsize
;
660 for (i
= 0; i
< symbol_count
; ++i
) {
661 GElf_Sym
*cur_sym
= NULL
;
663 cur_sym
= g_new0(GElf_Sym
, 1);
667 cur_sym
= gelf_getsym(data
, i
, cur_sym
);
671 if (GELF_ST_TYPE(cur_sym
->st_info
) != STT_FUNC
) {
672 /* We're only interested in the functions. */
677 if (cur_sym
->st_value
<= addr
&&
679 cur_sym
->st_value
> nearest_sym
->st_value
)) {
681 nearest_sym
= cur_sym
;
704 * Get the name of the function containing a given address within an
705 * executable using ELF symbols.
707 * The function name is in fact the name of the nearest ELF symbol,
708 * followed by the offset in bytes between the address and the symbol
709 * (in hex), separated by a '+' character.
711 * If found, the out parameter `func_name` is set on success. On failure,
712 * it remains unchanged.
714 * @param bin bin_info instance for the executable containing
716 * @param addr Virtual memory address for which to find the
718 * @param func_name Out parameter, the function name
719 * @returns 0 on success, -1 on failure
722 int bin_info_lookup_elf_function_name(struct bin_info
*bin
, uint64_t addr
,
726 * TODO (possible optimisation): if an ELF has no symtab
727 * section, it has been stripped. Therefore, it would be wise
728 * to store a flag indicating the stripped status after the
729 * first iteration to prevent subsequent ones.
733 GElf_Sym
*sym
= NULL
;
734 GElf_Shdr
*shdr
= NULL
;
735 char *sym_name
= NULL
;
737 /* Set ELF file if it hasn't been accessed yet. */
738 if (!bin
->elf_file
) {
739 ret
= bin_info_set_elf_file(bin
);
741 /* Failed to set ELF file. */
746 scn
= elf_nextscn(bin
->elf_file
, scn
);
751 while (scn
&& !sym
) {
752 ret
= bin_info_get_nearest_symbol_from_section(
753 scn
, addr
, &sym
, &shdr
);
758 scn
= elf_nextscn(bin
->elf_file
, scn
);
762 sym_name
= elf_strptr(bin
->elf_file
, shdr
->sh_link
,
768 ret
= bin_info_append_offset_str(sym_name
, sym
->st_value
, addr
,
786 * Get the name of the function containing a given address within a
787 * given compile unit (CU).
789 * If found, the out parameter `func_name` is set on success. On
790 * failure, it remains unchanged.
792 * @param cu bt_dwarf_cu instance which may contain the address
793 * @param addr Virtual memory address for which to find the
795 * @param func_name Out parameter, the function name
796 * @returns 0 on success, -1 on failure
799 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu
*cu
, uint64_t addr
,
804 struct bt_dwarf_die
*die
= NULL
;
806 if (!cu
|| !func_name
) {
810 die
= bt_dwarf_die_create(cu
);
815 while (bt_dwarf_die_next(die
) == 0) {
818 ret
= bt_dwarf_die_get_tag(die
, &tag
);
823 if (tag
== DW_TAG_subprogram
) {
824 ret
= bt_dwarf_die_contains_addr(die
, addr
, &found
);
836 uint64_t low_addr
= 0;
837 char *die_name
= NULL
;
839 ret
= bt_dwarf_die_get_name(die
, &die_name
);
844 ret
= dwarf_lowpc(die
->dwarf_die
, &low_addr
);
850 ret
= bin_info_append_offset_str(die_name
, low_addr
, addr
,
858 bt_dwarf_die_destroy(die
);
862 bt_dwarf_die_destroy(die
);
867 * Get the name of the function containing a given address within an
868 * executable using DWARF debug info.
870 * If found, the out parameter `func_name` is set on success. On
871 * failure, it remains unchanged.
873 * @param bin bin_info instance for the executable containing
875 * @param addr Virtual memory address for which to find the
877 * @param func_name Out parameter, the function name
878 * @returns 0 on success, -1 on failure
881 int bin_info_lookup_dwarf_function_name(struct bin_info
*bin
, uint64_t addr
,
885 char *_func_name
= NULL
;
886 struct bt_dwarf_cu
*cu
= NULL
;
888 if (!bin
|| !func_name
) {
892 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
897 while (bt_dwarf_cu_next(cu
) == 0) {
898 ret
= bin_info_lookup_cu_function_name(cu
, addr
, &_func_name
);
909 *func_name
= _func_name
;
914 bt_dwarf_cu_destroy(cu
);
918 bt_dwarf_cu_destroy(cu
);
923 int bin_info_lookup_function_name(struct bin_info
*bin
,
924 uint64_t addr
, char **func_name
)
927 char *_func_name
= NULL
;
929 if (!bin
|| !func_name
) {
933 /* Set DWARF info if it hasn't been accessed yet. */
934 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
935 ret
= bin_info_set_dwarf_info(bin
);
937 BT_LOGD_STR("Failed to set bin dwarf info, falling back to ELF lookup.");
938 /* Failed to set DWARF info, fallback to ELF. */
939 bin
->is_elf_only
= true;
943 if (!bin_info_has_address(bin
, addr
)) {
948 * Addresses in ELF and DWARF are relative to base address for
949 * PIC, so make the address argument relative too if needed.
952 addr
-= bin
->low_addr
;
955 if (bin
->is_elf_only
) {
956 ret
= bin_info_lookup_elf_function_name(bin
, addr
, &_func_name
);
957 BT_LOGD("Failed to lookup function name (ELF): ret=%d", ret
);
959 ret
= bin_info_lookup_dwarf_function_name(bin
, addr
, &_func_name
);
960 BT_LOGD("Failed to lookup function name (DWARF): ret=%d", ret
);
963 *func_name
= _func_name
;
971 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
)
974 char *_bin_loc
= NULL
;
976 if (!bin
|| !bin_loc
) {
981 addr
-= bin
->low_addr
;
982 ret
= asprintf(&_bin_loc
, "+%#0" PRIx64
, addr
);
984 ret
= asprintf(&_bin_loc
, "@%#0" PRIx64
, addr
);
987 if (ret
== -1 || !_bin_loc
) {
999 * Predicate used to determine whether the children of a given DIE
1000 * contain a specific address.
1002 * More specifically, the parameter `die` is expected to be a
1003 * subprogram (function) DIE, and this predicate tells whether any
1004 * subroutines are inlined within this function and would contain
1007 * On success, the out parameter `contains` is set with the boolean
1008 * value indicating whether the DIE's range covers `addr`. On failure,
1009 * it remains unchanged.
1011 * Do note that this function advances the position of `die`. If the
1012 * address is found within one of its children, `die` will be pointing
1013 * to that child upon returning from the function, allowing to extract
1014 * the information deemed necessary.
1016 * @param die The parent DIE in whose children the address will be
1018 * @param addr The address for which to look for in the DIEs
1019 * @param contains Out parameter, true if addr is contained,
1021 * @returns Returns 0 on success, -1 on failure
1024 int bin_info_child_die_has_address(struct bt_dwarf_die
*die
, uint64_t addr
, bool *contains
)
1027 bool _contains
= false;
1033 ret
= bt_dwarf_die_child(die
);
1041 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1046 if (tag
== DW_TAG_inlined_subroutine
) {
1047 ret
= bt_dwarf_die_contains_addr(die
, addr
, &_contains
);
1056 } while (bt_dwarf_die_next(die
) == 0);
1059 *contains
= _contains
;
1067 * Lookup the source location for a given address within a CU, making
1068 * the assumption that it is contained within an inline routine in a
1071 * @param cu bt_dwarf_cu instance in which to look for the address
1072 * @param addr The address for which to look for
1073 * @param src_loc Out parameter, the source location (filename and
1074 * line number) for the address
1075 * @returns 0 on success, -1 on failure
1078 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1079 struct source_location
**src_loc
)
1083 struct bt_dwarf_die
*die
= NULL
;
1084 struct source_location
*_src_loc
= NULL
;
1086 if (!cu
|| !src_loc
) {
1090 die
= bt_dwarf_die_create(cu
);
1095 while (bt_dwarf_die_next(die
) == 0) {
1098 ret
= bt_dwarf_die_get_tag(die
, &tag
);
1103 if (tag
== DW_TAG_subprogram
) {
1104 bool contains
= false;
1106 ret
= bt_dwarf_die_contains_addr(die
, addr
, &contains
);
1113 * Try to find an inlined subroutine
1114 * child of this DIE containing addr.
1116 ret
= bin_info_child_die_has_address(die
, addr
,
1129 char *filename
= NULL
;
1132 _src_loc
= g_new0(struct source_location
, 1);
1137 ret
= bt_dwarf_die_get_call_file(die
, &filename
);
1141 ret
= bt_dwarf_die_get_call_line(die
, &line_no
);
1147 _src_loc
->filename
= filename
;
1148 _src_loc
->line_no
= line_no
;
1149 *src_loc
= _src_loc
;
1152 bt_dwarf_die_destroy(die
);
1156 source_location_destroy(_src_loc
);
1157 bt_dwarf_die_destroy(die
);
1162 * Lookup the source location for a given address within a CU,
1163 * assuming that it is contained within an inlined function.
1165 * A source location can be found regardless of inlining status for
1166 * this method, but in the case of an inlined function, the returned
1167 * source location will point not to the callsite but rather to the
1168 * definition site of the inline function.
1170 * @param cu bt_dwarf_cu instance in which to look for the address
1171 * @param addr The address for which to look for
1172 * @param src_loc Out parameter, the source location (filename and
1173 * line number) for the address
1174 * @returns 0 on success, -1 on failure
1177 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1178 struct source_location
**src_loc
)
1180 struct source_location
*_src_loc
= NULL
;
1181 struct bt_dwarf_die
*die
= NULL
;
1182 const char *filename
= NULL
;
1183 Dwarf_Line
*line
= NULL
;
1184 Dwarf_Addr line_addr
;
1187 if (!cu
|| !src_loc
) {
1191 die
= bt_dwarf_die_create(cu
);
1196 line
= dwarf_getsrc_die(die
->dwarf_die
, addr
);
1201 ret
= dwarf_lineaddr(line
, &line_addr
);
1206 filename
= dwarf_linesrc(line
, NULL
, NULL
);
1211 if (addr
== line_addr
) {
1212 _src_loc
= g_new0(struct source_location
, 1);
1217 ret
= dwarf_lineno(line
, &line_no
);
1222 _src_loc
->line_no
= line_no
;
1223 _src_loc
->filename
= strdup(filename
);
1226 bt_dwarf_die_destroy(die
);
1229 *src_loc
= _src_loc
;
1235 source_location_destroy(_src_loc
);
1236 bt_dwarf_die_destroy(die
);
1241 * Get the source location (file name and line number) for a given
1242 * address within a compile unit (CU).
1244 * On success, the out parameter `src_loc` is set if found. On
1245 * failure, it remains unchanged.
1247 * @param cu bt_dwarf_cu instance for the compile unit which
1248 * may contain the address
1249 * @param addr Virtual memory address for which to find the
1251 * @param src_loc Out parameter, the source location
1252 * @returns 0 on success, -1 on failure
1255 int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu
*cu
, uint64_t addr
,
1256 struct source_location
**src_loc
)
1259 struct source_location
*_src_loc
= NULL
;
1261 if (!cu
|| !src_loc
) {
1265 ret
= bin_info_lookup_cu_src_loc_inl(cu
, addr
, &_src_loc
);
1274 ret
= bin_info_lookup_cu_src_loc_no_inl(cu
, addr
, &_src_loc
);
1285 *src_loc
= _src_loc
;
1291 source_location_destroy(_src_loc
);
1296 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
1297 struct source_location
**src_loc
)
1299 struct bt_dwarf_cu
*cu
= NULL
;
1300 struct source_location
*_src_loc
= NULL
;
1302 if (!bin
|| !src_loc
) {
1306 /* Set DWARF info if it hasn't been accessed yet. */
1307 if (!bin
->dwarf_info
&& !bin
->is_elf_only
) {
1308 if (bin_info_set_dwarf_info(bin
)) {
1309 /* Failed to set DWARF info. */
1310 bin
->is_elf_only
= true;
1314 if (bin
->is_elf_only
) {
1315 /* We cannot lookup source location without DWARF info. */
1319 if (!bin_info_has_address(bin
, addr
)) {
1324 * Addresses in ELF and DWARF are relative to base address for
1325 * PIC, so make the address argument relative too if needed.
1328 addr
-= bin
->low_addr
;
1331 cu
= bt_dwarf_cu_create(bin
->dwarf_info
);
1336 while (bt_dwarf_cu_next(cu
) == 0) {
1339 ret
= bin_info_lookup_cu_src_loc(cu
, addr
, &_src_loc
);
1349 bt_dwarf_cu_destroy(cu
);
1351 *src_loc
= _src_loc
;
1357 source_location_destroy(_src_loc
);
1358 bt_dwarf_cu_destroy(cu
);