Commit | Line | Data |
---|---|---|
c40a57e5 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
c40a57e5 AB |
3 | * |
4 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
5 | * | |
0235b0db | 6 | * Babeltrace - Executable and Shared Object Debug Info Reader |
c40a57e5 AB |
7 | */ |
8 | ||
0235b0db MJ |
9 | #ifndef _BABELTRACE_BIN_INFO_H |
10 | #define _BABELTRACE_BIN_INFO_H | |
11 | ||
91bc8451 | 12 | #include <babeltrace2/babeltrace.h> |
c40a57e5 AB |
13 | #include <stdint.h> |
14 | #include <stdbool.h> | |
15 | #include <gelf.h> | |
16 | #include <elfutils/libdw.h> | |
91d81473 | 17 | #include "common/macros.h" |
578e048b | 18 | #include "fd-cache/fd-cache.h" |
c40a57e5 AB |
19 | |
20 | #define DEFAULT_DEBUG_DIR "/usr/lib/debug" | |
73485cf9 MJ |
21 | #define DEBUG_SUBDIR ".debug" |
22 | #define BUILD_ID_SUBDIR ".build-id" | |
c40a57e5 | 23 | #define BUILD_ID_SUFFIX ".debug" |
73485cf9 | 24 | #define BUILD_ID_PREFIX_DIR_LEN 2 |
c40a57e5 | 25 | |
d5ddf820 | 26 | struct bin_info { |
3a3d15f3 PP |
27 | bt_logging_level log_level; |
28 | ||
91bc8451 PP |
29 | /* Used for logging; can be `NULL` */ |
30 | bt_self_component *self_comp; | |
31 | ||
c40a57e5 AB |
32 | /* Base virtual memory address. */ |
33 | uint64_t low_addr; | |
34 | /* Upper bound of exec address space. */ | |
35 | uint64_t high_addr; | |
36 | /* Size of exec address space. */ | |
37 | uint64_t memsz; | |
38 | /* Paths to ELF and DWARF files. */ | |
06d1cf5d FD |
39 | gchar *elf_path; |
40 | gchar *dwarf_path; | |
c40a57e5 AB |
41 | /* libelf and libdw objects representing the files. */ |
42 | Elf *elf_file; | |
43 | Dwarf *dwarf_info; | |
44 | /* Optional build ID info. */ | |
45 | uint8_t *build_id; | |
46 | size_t build_id_len; | |
ca9f27f3 | 47 | |
c40a57e5 | 48 | /* Optional debug link info. */ |
06d1cf5d | 49 | gchar *dbg_link_filename; |
c40a57e5 | 50 | uint32_t dbg_link_crc; |
1e638f98 FD |
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; | |
9d325e17 | 54 | /* Configuration. */ |
06d1cf5d | 55 | gchar *debug_info_dir; |
c40a57e5 AB |
56 | /* Denotes whether the executable is position independent code. */ |
57 | bool is_pic:1; | |
af54bd18 | 58 | /* Denotes whether the build id in the trace matches to one on disk. */ |
ca9f27f3 | 59 | bool file_build_id_matches:1; |
d5ddf820 AB |
60 | /* |
61 | * Denotes whether the executable only has ELF symbols and no | |
62 | * DWARF info. | |
63 | */ | |
c40a57e5 | 64 | bool is_elf_only:1; |
1e638f98 FD |
65 | /* Weak ref. Owned by the iterator. */ |
66 | struct bt_fd_cache *fd_cache; | |
c40a57e5 AB |
67 | }; |
68 | ||
69 | struct source_location { | |
70 | uint64_t line_no; | |
06d1cf5d | 71 | gchar *filename; |
c40a57e5 AB |
72 | }; |
73 | ||
74 | /** | |
d5ddf820 | 75 | * Initializes the bin_info framework. Call this before calling |
c40a57e5 AB |
76 | * anything else. |
77 | * | |
78 | * @returns 0 on success, -1 on failure | |
79 | */ | |
91bc8451 PP |
80 | int bin_info_init(bt_logging_level log_level, |
81 | bt_self_component *self_comp); | |
c40a57e5 AB |
82 | |
83 | /** | |
84 | * Instantiate a structure representing an ELF executable, possibly | |
85 | * with DWARF info, located at the given path. | |
86 | * | |
87 | * @param path Path to the ELF file | |
88 | * @param low_addr Base address of the executable | |
89 | * @param memsz In-memory size of the executable | |
9f2b13ca AB |
90 | * @param is_pic Whether the executable is position independent |
91 | * code (PIC) | |
9d325e17 PP |
92 | * @param debug_info_dir Directory containing debug info or NULL. |
93 | * @param target_prefix Path to the root file system of the target | |
94 | * or NULL. | |
d5ddf820 | 95 | * @returns Pointer to the new bin_info on success, |
c40a57e5 AB |
96 | * NULL on failure. |
97 | */ | |
1e638f98 FD |
98 | struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path, |
99 | uint64_t low_addr, uint64_t memsz, bool is_pic, | |
3a3d15f3 | 100 | const char *debug_info_dir, const char *target_prefix, |
91bc8451 | 101 | bt_logging_level log_level, bt_self_component *self_comp); |
c40a57e5 AB |
102 | |
103 | /** | |
d5ddf820 | 104 | * Destroy the given bin_info instance |
c40a57e5 | 105 | * |
d5ddf820 | 106 | * @param bin bin_info instance to destroy |
c40a57e5 | 107 | */ |
d5ddf820 | 108 | void bin_info_destroy(struct bin_info *bin); |
c40a57e5 AB |
109 | |
110 | /** | |
d5ddf820 | 111 | * Sets the build ID information for a given bin_info instance. |
c40a57e5 | 112 | * |
d5ddf820 | 113 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
114 | * the build ID |
115 | * @param build_id Array of bytes containing the actual ID | |
116 | * @param build_id_len Length in bytes of the build_id | |
117 | * @returns 0 on success, -1 on failure | |
118 | */ | |
d5ddf820 | 119 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
c40a57e5 AB |
120 | size_t build_id_len); |
121 | ||
122 | /** | |
d5ddf820 | 123 | * Sets the debug link information for a given bin_info instance. |
c40a57e5 | 124 | * |
d5ddf820 | 125 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
126 | * the debug link |
127 | * @param filename Name of the separate debug info file | |
128 | * @param crc Checksum for the debug info file | |
129 | * @returns 0 on success, -1 on failure | |
130 | */ | |
4f45f9bb JD |
131 | int bin_info_set_debug_link(struct bin_info *bin, const char *filename, |
132 | uint32_t crc); | |
c40a57e5 AB |
133 | |
134 | /** | |
d5ddf820 AB |
135 | * Returns whether or not the given bin info \p bin contains the |
136 | * address \p addr. | |
c40a57e5 | 137 | * |
d5ddf820 | 138 | * @param bin bin_info instance |
c40a57e5 | 139 | * @param addr Address to lookup |
d5ddf820 | 140 | * @returns 1 if \p bin contains \p addr, 0 if it does not, |
c40a57e5 AB |
141 | * -1 on failure |
142 | */ | |
143 | static inline | |
d5ddf820 | 144 | int bin_info_has_address(struct bin_info *bin, uint64_t addr) |
c40a57e5 | 145 | { |
d5ddf820 | 146 | if (!bin) { |
c40a57e5 AB |
147 | return -1; |
148 | } | |
149 | ||
d5ddf820 | 150 | return addr >= bin->low_addr && addr < bin->high_addr; |
c40a57e5 AB |
151 | } |
152 | ||
153 | /** | |
154 | * Get the name of the function containing a given address within an | |
155 | * executable. | |
156 | * | |
157 | * If no DWARF info is available, the function falls back to ELF | |
158 | * symbols and the "function name" is in fact the name of the closest | |
159 | * symbol, followed by the offset between the symbol and the address. | |
160 | * | |
161 | * On success, if found, the out parameter `func_name` is set. The ownership | |
162 | * of `func_name` is passed to the caller. On failure, `func_name` remains | |
163 | * unchanged. | |
164 | * | |
d5ddf820 | 165 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
166 | * the address |
167 | * @param addr Virtual memory address for which to find the | |
168 | * function name | |
169 | * @param func_name Out parameter, the function name. | |
170 | * @returns 0 on success, -1 on failure | |
171 | */ | |
d5ddf820 | 172 | int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
173 | char **func_name); |
174 | ||
175 | /** | |
176 | * Get the source location (file name and line number) for a given | |
177 | * address within an executable. | |
178 | * | |
179 | * If no DWARF info is available, the source location cannot be found | |
4caab45b | 180 | * and the function will return unsuccessfully. |
c40a57e5 AB |
181 | * |
182 | * On success, if found, the out parameter `src_loc` is set. The ownership | |
183 | * of `src_loc` is passed to the caller. On failure, `src_loc` remains | |
184 | * unchanged. | |
185 | * | |
d5ddf820 | 186 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
187 | * the address |
188 | * @param addr Virtual memory address for which to find the | |
189 | * source location | |
190 | * @param src_loc Out parameter, the source location | |
191 | * @returns 0 on success, -1 on failure | |
192 | */ | |
d5ddf820 | 193 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
c40a57e5 | 194 | struct source_location **src_loc); |
36ae9941 AB |
195 | /** |
196 | * Get a string representing the location within the binary of a given | |
197 | * address. | |
198 | * | |
199 | * In the case of a PIC binary, the location is relative (+0x1234). | |
200 | * For a non-PIC binary, the location is absolute (@0x1234) | |
201 | * | |
202 | * On success, the out parameter `bin_loc` is set. The ownership is | |
203 | * passed to the caller. On failure, `bin_loc` remains unchanged. | |
204 | * | |
d5ddf820 | 205 | * @param bin bin_info instance for the executable containing |
36ae9941 AB |
206 | * the address |
207 | * @param addr Virtual memory address for which to find the | |
208 | * binary location | |
209 | * @param bin_loc Out parameter, the binary location | |
210 | * @returns 0 on success, -1 on failure | |
211 | */ | |
d5ddf820 | 212 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc); |
c40a57e5 AB |
213 | |
214 | /** | |
215 | * Destroy the given source_location instance | |
216 | * | |
217 | * @param src_loc source_location instance to destroy | |
218 | */ | |
c40a57e5 AB |
219 | void source_location_destroy(struct source_location *src_loc); |
220 | ||
d5ddf820 | 221 | #endif /* _BABELTRACE_BIN_INFO_H */ |