2 * SPDX-License-Identifier: MIT
4 * Copyright 2015 Antoine Busque <abusque@efficios.com>
6 * Babeltrace - Executable and Shared Object Debug Info Reader
9 #ifndef _BABELTRACE_BIN_INFO_H
10 #define _BABELTRACE_BIN_INFO_H
12 #include <babeltrace2/babeltrace.h>
16 #include <elfutils/libdw.h>
17 #include "common/macros.h"
18 #include "fd-cache/fd-cache.h"
20 #define DEFAULT_DEBUG_DIR "/usr/lib/debug"
21 #define DEBUG_SUBDIR ".debug"
22 #define BUILD_ID_SUBDIR ".build-id"
23 #define BUILD_ID_SUFFIX ".debug"
24 #define BUILD_ID_PREFIX_DIR_LEN 2
27 bt_logging_level log_level
;
29 /* Used for logging; can be `NULL` */
30 bt_self_component
*self_comp
;
32 /* Base virtual memory address. */
34 /* Upper bound of exec address space. */
36 /* Size of exec address space. */
38 /* Paths to ELF and DWARF files. */
41 /* libelf and libdw objects representing the files. */
44 /* Optional build ID info. */
48 /* Optional debug link info. */
49 gchar
*dbg_link_filename
;
50 uint32_t dbg_link_crc
;
51 /* fd cache handles to ELF and DWARF files. */
52 struct bt_fd_cache_handle
*elf_handle
;
53 struct bt_fd_cache_handle
*dwarf_handle
;
55 gchar
*debug_info_dir
;
56 /* Denotes whether the executable is position independent code. */
58 /* Denotes whether the build id in the trace matches to one on disk. */
59 bool file_build_id_matches
:1;
61 * Denotes whether the executable only has ELF symbols and no
65 /* Weak ref. Owned by the iterator. */
66 struct bt_fd_cache
*fd_cache
;
69 struct source_location
{
75 * Initializes the bin_info framework. Call this before calling
78 * @returns 0 on success, -1 on failure
81 int bin_info_init(bt_logging_level log_level
,
82 bt_self_component
*self_comp
);
85 * Instantiate a structure representing an ELF executable, possibly
86 * with DWARF info, located at the given path.
88 * @param path Path to the ELF file
89 * @param low_addr Base address of the executable
90 * @param memsz In-memory size of the executable
91 * @param is_pic Whether the executable is position independent
93 * @param debug_info_dir Directory containing debug info or NULL.
94 * @param target_prefix Path to the root file system of the target
96 * @returns Pointer to the new bin_info on success,
100 struct bin_info
*bin_info_create(struct bt_fd_cache
*fdc
, const char *path
,
101 uint64_t low_addr
, uint64_t memsz
, bool is_pic
,
102 const char *debug_info_dir
, const char *target_prefix
,
103 bt_logging_level log_level
, bt_self_component
*self_comp
);
106 * Destroy the given bin_info instance
108 * @param bin bin_info instance to destroy
111 void bin_info_destroy(struct bin_info
*bin
);
114 * Sets the build ID information for a given bin_info instance.
116 * @param bin The bin_info instance for which to set
118 * @param build_id Array of bytes containing the actual ID
119 * @param build_id_len Length in bytes of the build_id
120 * @returns 0 on success, -1 on failure
123 int bin_info_set_build_id(struct bin_info
*bin
, uint8_t *build_id
,
124 size_t build_id_len
);
127 * Sets the debug link information for a given bin_info instance.
129 * @param bin The bin_info instance for which to set
131 * @param filename Name of the separate debug info file
132 * @param crc Checksum for the debug info file
133 * @returns 0 on success, -1 on failure
136 int bin_info_set_debug_link(struct bin_info
*bin
, const char *filename
,
140 * Returns whether or not the given bin info \p bin contains the
143 * @param bin bin_info instance
144 * @param addr Address to lookup
145 * @returns 1 if \p bin contains \p addr, 0 if it does not,
149 int bin_info_has_address(struct bin_info
*bin
, uint64_t addr
)
155 return addr
>= bin
->low_addr
&& addr
< bin
->high_addr
;
159 * Get the name of the function containing a given address within an
162 * If no DWARF info is available, the function falls back to ELF
163 * symbols and the "function name" is in fact the name of the closest
164 * symbol, followed by the offset between the symbol and the address.
166 * On success, if found, the out parameter `func_name` is set. The ownership
167 * of `func_name` is passed to the caller. On failure, `func_name` remains
170 * @param bin bin_info instance for the executable containing
172 * @param addr Virtual memory address for which to find the
174 * @param func_name Out parameter, the function name.
175 * @returns 0 on success, -1 on failure
178 int bin_info_lookup_function_name(struct bin_info
*bin
, uint64_t addr
,
182 * Get the source location (file name and line number) for a given
183 * address within an executable.
185 * If no DWARF info is available, the source location cannot be found
186 * and the function will return unsuccessfully.
188 * On success, if found, the out parameter `src_loc` is set. The ownership
189 * of `src_loc` is passed to the caller. On failure, `src_loc` remains
192 * @param bin bin_info instance for the executable containing
194 * @param addr Virtual memory address for which to find the
196 * @param src_loc Out parameter, the source location
197 * @returns 0 on success, -1 on failure
200 int bin_info_lookup_source_location(struct bin_info
*bin
, uint64_t addr
,
201 struct source_location
**src_loc
);
203 * Get a string representing the location within the binary of a given
206 * In the case of a PIC binary, the location is relative (+0x1234).
207 * For a non-PIC binary, the location is absolute (@0x1234)
209 * On success, the out parameter `bin_loc` is set. The ownership is
210 * passed to the caller. On failure, `bin_loc` remains unchanged.
212 * @param bin bin_info instance for the executable containing
214 * @param addr Virtual memory address for which to find the
216 * @param bin_loc Out parameter, the binary location
217 * @returns 0 on success, -1 on failure
220 int bin_info_get_bin_loc(struct bin_info
*bin
, uint64_t addr
, char **bin_loc
);
223 * Destroy the given source_location instance
225 * @param src_loc source_location instance to destroy
228 void source_location_destroy(struct source_location
*src_loc
);
230 #endif /* _BABELTRACE_BIN_INFO_H */