Commit | Line | Data |
---|---|---|
d5ddf820 AB |
1 | #ifndef _BABELTRACE_BIN_INFO_H |
2 | #define _BABELTRACE_BIN_INFO_H | |
c40a57e5 AB |
3 | |
4 | /* | |
5 | * Babeltrace - Executable and Shared Object Debug Info Reader | |
6 | * | |
7 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
8 | * | |
9 | * Author: Antoine Busque <abusque@efficios.com> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
27 | * SOFTWARE. | |
28 | */ | |
29 | ||
3a3d15f3 | 30 | #include <babeltrace2/logging.h> |
c40a57e5 AB |
31 | #include <stdint.h> |
32 | #include <stdbool.h> | |
33 | #include <gelf.h> | |
34 | #include <elfutils/libdw.h> | |
91d81473 | 35 | #include "common/macros.h" |
578e048b | 36 | #include "fd-cache/fd-cache.h" |
c40a57e5 AB |
37 | |
38 | #define DEFAULT_DEBUG_DIR "/usr/lib/debug" | |
73485cf9 MJ |
39 | #define DEBUG_SUBDIR ".debug" |
40 | #define BUILD_ID_SUBDIR ".build-id" | |
c40a57e5 | 41 | #define BUILD_ID_SUFFIX ".debug" |
73485cf9 | 42 | #define BUILD_ID_PREFIX_DIR_LEN 2 |
c40a57e5 | 43 | |
d5ddf820 | 44 | struct bin_info { |
3a3d15f3 PP |
45 | bt_logging_level log_level; |
46 | ||
c40a57e5 AB |
47 | /* Base virtual memory address. */ |
48 | uint64_t low_addr; | |
49 | /* Upper bound of exec address space. */ | |
50 | uint64_t high_addr; | |
51 | /* Size of exec address space. */ | |
52 | uint64_t memsz; | |
53 | /* Paths to ELF and DWARF files. */ | |
06d1cf5d FD |
54 | gchar *elf_path; |
55 | gchar *dwarf_path; | |
c40a57e5 AB |
56 | /* libelf and libdw objects representing the files. */ |
57 | Elf *elf_file; | |
58 | Dwarf *dwarf_info; | |
59 | /* Optional build ID info. */ | |
60 | uint8_t *build_id; | |
61 | size_t build_id_len; | |
ca9f27f3 | 62 | |
c40a57e5 | 63 | /* Optional debug link info. */ |
06d1cf5d | 64 | gchar *dbg_link_filename; |
c40a57e5 | 65 | uint32_t dbg_link_crc; |
1e638f98 FD |
66 | /* fd cache handles to ELF and DWARF files. */ |
67 | struct bt_fd_cache_handle *elf_handle; | |
68 | struct bt_fd_cache_handle *dwarf_handle; | |
9d325e17 | 69 | /* Configuration. */ |
06d1cf5d | 70 | gchar *debug_info_dir; |
c40a57e5 AB |
71 | /* Denotes whether the executable is position independent code. */ |
72 | bool is_pic:1; | |
af54bd18 | 73 | /* Denotes whether the build id in the trace matches to one on disk. */ |
ca9f27f3 | 74 | bool file_build_id_matches:1; |
d5ddf820 AB |
75 | /* |
76 | * Denotes whether the executable only has ELF symbols and no | |
77 | * DWARF info. | |
78 | */ | |
c40a57e5 | 79 | bool is_elf_only:1; |
1e638f98 FD |
80 | /* Weak ref. Owned by the iterator. */ |
81 | struct bt_fd_cache *fd_cache; | |
c40a57e5 AB |
82 | }; |
83 | ||
84 | struct source_location { | |
85 | uint64_t line_no; | |
06d1cf5d | 86 | gchar *filename; |
c40a57e5 AB |
87 | }; |
88 | ||
89 | /** | |
d5ddf820 | 90 | * Initializes the bin_info framework. Call this before calling |
c40a57e5 AB |
91 | * anything else. |
92 | * | |
93 | * @returns 0 on success, -1 on failure | |
94 | */ | |
95 | BT_HIDDEN | |
3a3d15f3 | 96 | int bin_info_init(bt_logging_level log_level); |
c40a57e5 AB |
97 | |
98 | /** | |
99 | * Instantiate a structure representing an ELF executable, possibly | |
100 | * with DWARF info, located at the given path. | |
101 | * | |
102 | * @param path Path to the ELF file | |
103 | * @param low_addr Base address of the executable | |
104 | * @param memsz In-memory size of the executable | |
9f2b13ca AB |
105 | * @param is_pic Whether the executable is position independent |
106 | * code (PIC) | |
9d325e17 PP |
107 | * @param debug_info_dir Directory containing debug info or NULL. |
108 | * @param target_prefix Path to the root file system of the target | |
109 | * or NULL. | |
d5ddf820 | 110 | * @returns Pointer to the new bin_info on success, |
c40a57e5 AB |
111 | * NULL on failure. |
112 | */ | |
113 | BT_HIDDEN | |
1e638f98 FD |
114 | struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path, |
115 | uint64_t low_addr, uint64_t memsz, bool is_pic, | |
3a3d15f3 PP |
116 | const char *debug_info_dir, const char *target_prefix, |
117 | bt_logging_level log_level); | |
c40a57e5 AB |
118 | |
119 | /** | |
d5ddf820 | 120 | * Destroy the given bin_info instance |
c40a57e5 | 121 | * |
d5ddf820 | 122 | * @param bin bin_info instance to destroy |
c40a57e5 AB |
123 | */ |
124 | BT_HIDDEN | |
d5ddf820 | 125 | void bin_info_destroy(struct bin_info *bin); |
c40a57e5 AB |
126 | |
127 | /** | |
d5ddf820 | 128 | * Sets the build ID information for a given bin_info instance. |
c40a57e5 | 129 | * |
d5ddf820 | 130 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
131 | * the build ID |
132 | * @param build_id Array of bytes containing the actual ID | |
133 | * @param build_id_len Length in bytes of the build_id | |
134 | * @returns 0 on success, -1 on failure | |
135 | */ | |
136 | BT_HIDDEN | |
d5ddf820 | 137 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
c40a57e5 AB |
138 | size_t build_id_len); |
139 | ||
140 | /** | |
d5ddf820 | 141 | * Sets the debug link information for a given bin_info instance. |
c40a57e5 | 142 | * |
d5ddf820 | 143 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
144 | * the debug link |
145 | * @param filename Name of the separate debug info file | |
146 | * @param crc Checksum for the debug info file | |
147 | * @returns 0 on success, -1 on failure | |
148 | */ | |
149 | BT_HIDDEN | |
4f45f9bb JD |
150 | int bin_info_set_debug_link(struct bin_info *bin, const char *filename, |
151 | uint32_t crc); | |
c40a57e5 AB |
152 | |
153 | /** | |
d5ddf820 AB |
154 | * Returns whether or not the given bin info \p bin contains the |
155 | * address \p addr. | |
c40a57e5 | 156 | * |
d5ddf820 | 157 | * @param bin bin_info instance |
c40a57e5 | 158 | * @param addr Address to lookup |
d5ddf820 | 159 | * @returns 1 if \p bin contains \p addr, 0 if it does not, |
c40a57e5 AB |
160 | * -1 on failure |
161 | */ | |
162 | static inline | |
d5ddf820 | 163 | int bin_info_has_address(struct bin_info *bin, uint64_t addr) |
c40a57e5 | 164 | { |
d5ddf820 | 165 | if (!bin) { |
c40a57e5 AB |
166 | return -1; |
167 | } | |
168 | ||
d5ddf820 | 169 | return addr >= bin->low_addr && addr < bin->high_addr; |
c40a57e5 AB |
170 | } |
171 | ||
172 | /** | |
173 | * Get the name of the function containing a given address within an | |
174 | * executable. | |
175 | * | |
176 | * If no DWARF info is available, the function falls back to ELF | |
177 | * symbols and the "function name" is in fact the name of the closest | |
178 | * symbol, followed by the offset between the symbol and the address. | |
179 | * | |
180 | * On success, if found, the out parameter `func_name` is set. The ownership | |
181 | * of `func_name` is passed to the caller. On failure, `func_name` remains | |
182 | * unchanged. | |
183 | * | |
d5ddf820 | 184 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
185 | * the address |
186 | * @param addr Virtual memory address for which to find the | |
187 | * function name | |
188 | * @param func_name Out parameter, the function name. | |
189 | * @returns 0 on success, -1 on failure | |
190 | */ | |
191 | BT_HIDDEN | |
d5ddf820 | 192 | int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
193 | char **func_name); |
194 | ||
195 | /** | |
196 | * Get the source location (file name and line number) for a given | |
197 | * address within an executable. | |
198 | * | |
199 | * If no DWARF info is available, the source location cannot be found | |
4caab45b | 200 | * and the function will return unsuccessfully. |
c40a57e5 AB |
201 | * |
202 | * On success, if found, the out parameter `src_loc` is set. The ownership | |
203 | * of `src_loc` is passed to the caller. On failure, `src_loc` remains | |
204 | * unchanged. | |
205 | * | |
d5ddf820 | 206 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
207 | * the address |
208 | * @param addr Virtual memory address for which to find the | |
209 | * source location | |
210 | * @param src_loc Out parameter, the source location | |
211 | * @returns 0 on success, -1 on failure | |
212 | */ | |
213 | BT_HIDDEN | |
d5ddf820 | 214 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
c40a57e5 | 215 | struct source_location **src_loc); |
36ae9941 AB |
216 | /** |
217 | * Get a string representing the location within the binary of a given | |
218 | * address. | |
219 | * | |
220 | * In the case of a PIC binary, the location is relative (+0x1234). | |
221 | * For a non-PIC binary, the location is absolute (@0x1234) | |
222 | * | |
223 | * On success, the out parameter `bin_loc` is set. The ownership is | |
224 | * passed to the caller. On failure, `bin_loc` remains unchanged. | |
225 | * | |
d5ddf820 | 226 | * @param bin bin_info instance for the executable containing |
36ae9941 AB |
227 | * the address |
228 | * @param addr Virtual memory address for which to find the | |
229 | * binary location | |
230 | * @param bin_loc Out parameter, the binary location | |
231 | * @returns 0 on success, -1 on failure | |
232 | */ | |
233 | BT_HIDDEN | |
d5ddf820 | 234 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc); |
c40a57e5 AB |
235 | |
236 | /** | |
237 | * Destroy the given source_location instance | |
238 | * | |
239 | * @param src_loc source_location instance to destroy | |
240 | */ | |
241 | BT_HIDDEN | |
242 | void source_location_destroy(struct source_location *src_loc); | |
243 | ||
d5ddf820 | 244 | #endif /* _BABELTRACE_BIN_INFO_H */ |