Commit | Line | Data |
---|---|---|
dae52e76 AB |
1 | #ifndef _BABELTRACE_BIN_INFO_H |
2 | #define _BABELTRACE_BIN_INFO_H | |
b5a8598f 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 | ||
30 | #include <stdint.h> | |
31 | #include <stdbool.h> | |
32 | #include <gelf.h> | |
33 | #include <elfutils/libdw.h> | |
34 | #include <babeltrace/babeltrace-internal.h> | |
35 | ||
36 | #define DEFAULT_DEBUG_DIR "/usr/lib/debug" | |
37 | #define DEBUG_SUBDIR ".debug/" | |
38 | #define BUILD_ID_SUBDIR ".build-id/" | |
39 | #define BUILD_ID_SUFFIX ".debug" | |
40 | ||
dae52e76 | 41 | struct bin_info { |
b5a8598f AB |
42 | /* Base virtual memory address. */ |
43 | uint64_t low_addr; | |
44 | /* Upper bound of exec address space. */ | |
45 | uint64_t high_addr; | |
46 | /* Size of exec address space. */ | |
47 | uint64_t memsz; | |
48 | /* Paths to ELF and DWARF files. */ | |
49 | char *elf_path; | |
50 | char *dwarf_path; | |
51 | /* libelf and libdw objects representing the files. */ | |
52 | Elf *elf_file; | |
53 | Dwarf *dwarf_info; | |
54 | /* Optional build ID info. */ | |
55 | uint8_t *build_id; | |
56 | size_t build_id_len; | |
57 | /* Optional debug link info. */ | |
58 | char *dbg_link_filename; | |
59 | uint32_t dbg_link_crc; | |
60 | /* FDs to ELF and DWARF files. */ | |
61 | int elf_fd; | |
62 | int dwarf_fd; | |
63 | /* Denotes whether the executable is position independent code. */ | |
64 | bool is_pic:1; | |
dae52e76 AB |
65 | /* |
66 | * Denotes whether the executable only has ELF symbols and no | |
67 | * DWARF info. | |
68 | */ | |
b5a8598f AB |
69 | bool is_elf_only:1; |
70 | }; | |
71 | ||
72 | struct source_location { | |
73 | uint64_t line_no; | |
74 | char *filename; | |
75 | }; | |
76 | ||
77 | /** | |
dae52e76 | 78 | * Initializes the bin_info framework. Call this before calling |
b5a8598f AB |
79 | * anything else. |
80 | * | |
81 | * @returns 0 on success, -1 on failure | |
82 | */ | |
83 | BT_HIDDEN | |
dae52e76 | 84 | int bin_info_init(void); |
b5a8598f AB |
85 | |
86 | /** | |
87 | * Instantiate a structure representing an ELF executable, possibly | |
88 | * with DWARF info, located at the given path. | |
89 | * | |
90 | * @param path Path to the ELF file | |
91 | * @param low_addr Base address of the executable | |
92 | * @param memsz In-memory size of the executable | |
1a4a1345 AB |
93 | * @param is_pic Whether the executable is position independent |
94 | * code (PIC) | |
dae52e76 | 95 | * @returns Pointer to the new bin_info on success, |
b5a8598f AB |
96 | * NULL on failure. |
97 | */ | |
98 | BT_HIDDEN | |
dae52e76 | 99 | struct bin_info *bin_info_create(const char *path, uint64_t low_addr, |
1a4a1345 | 100 | uint64_t memsz, bool is_pic); |
b5a8598f AB |
101 | |
102 | /** | |
dae52e76 | 103 | * Destroy the given bin_info instance |
b5a8598f | 104 | * |
dae52e76 | 105 | * @param bin bin_info instance to destroy |
b5a8598f AB |
106 | */ |
107 | BT_HIDDEN | |
dae52e76 | 108 | void bin_info_destroy(struct bin_info *bin); |
b5a8598f AB |
109 | |
110 | /** | |
dae52e76 | 111 | * Sets the build ID information for a given bin_info instance. |
b5a8598f | 112 | * |
dae52e76 | 113 | * @param bin The bin_info instance for which to set |
b5a8598f 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 | */ | |
119 | BT_HIDDEN | |
dae52e76 | 120 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
b5a8598f AB |
121 | size_t build_id_len); |
122 | ||
123 | /** | |
dae52e76 | 124 | * Sets the debug link information for a given bin_info instance. |
b5a8598f | 125 | * |
dae52e76 | 126 | * @param bin The bin_info instance for which to set |
b5a8598f AB |
127 | * the debug link |
128 | * @param filename Name of the separate debug info file | |
129 | * @param crc Checksum for the debug info file | |
130 | * @returns 0 on success, -1 on failure | |
131 | */ | |
132 | BT_HIDDEN | |
dae52e76 | 133 | int bin_info_set_debug_link(struct bin_info *bin, char *filename, uint32_t crc); |
b5a8598f AB |
134 | |
135 | /** | |
dae52e76 AB |
136 | * Returns whether or not the given bin info \p bin contains the |
137 | * address \p addr. | |
b5a8598f | 138 | * |
dae52e76 | 139 | * @param bin bin_info instance |
b5a8598f | 140 | * @param addr Address to lookup |
dae52e76 | 141 | * @returns 1 if \p bin contains \p addr, 0 if it does not, |
b5a8598f AB |
142 | * -1 on failure |
143 | */ | |
144 | static inline | |
dae52e76 | 145 | int bin_info_has_address(struct bin_info *bin, uint64_t addr) |
b5a8598f | 146 | { |
dae52e76 | 147 | if (!bin) { |
b5a8598f AB |
148 | return -1; |
149 | } | |
150 | ||
dae52e76 | 151 | return addr >= bin->low_addr && addr < bin->high_addr; |
b5a8598f AB |
152 | } |
153 | ||
154 | /** | |
155 | * Get the name of the function containing a given address within an | |
156 | * executable. | |
157 | * | |
158 | * If no DWARF info is available, the function falls back to ELF | |
159 | * symbols and the "function name" is in fact the name of the closest | |
160 | * symbol, followed by the offset between the symbol and the address. | |
161 | * | |
162 | * On success, if found, the out parameter `func_name` is set. The ownership | |
163 | * of `func_name` is passed to the caller. On failure, `func_name` remains | |
164 | * unchanged. | |
165 | * | |
dae52e76 | 166 | * @param bin bin_info instance for the executable containing |
b5a8598f AB |
167 | * the address |
168 | * @param addr Virtual memory address for which to find the | |
169 | * function name | |
170 | * @param func_name Out parameter, the function name. | |
171 | * @returns 0 on success, -1 on failure | |
172 | */ | |
173 | BT_HIDDEN | |
dae52e76 | 174 | int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, |
b5a8598f AB |
175 | char **func_name); |
176 | ||
177 | /** | |
178 | * Get the source location (file name and line number) for a given | |
179 | * address within an executable. | |
180 | * | |
181 | * If no DWARF info is available, the source location cannot be found | |
d4832a19 | 182 | * and the function will return unsuccessfully. |
b5a8598f AB |
183 | * |
184 | * On success, if found, the out parameter `src_loc` is set. The ownership | |
185 | * of `src_loc` is passed to the caller. On failure, `src_loc` remains | |
186 | * unchanged. | |
187 | * | |
dae52e76 | 188 | * @param bin bin_info instance for the executable containing |
b5a8598f AB |
189 | * the address |
190 | * @param addr Virtual memory address for which to find the | |
191 | * source location | |
192 | * @param src_loc Out parameter, the source location | |
193 | * @returns 0 on success, -1 on failure | |
194 | */ | |
195 | BT_HIDDEN | |
dae52e76 | 196 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
b5a8598f | 197 | struct source_location **src_loc); |
dc419b6c AB |
198 | /** |
199 | * Get a string representing the location within the binary of a given | |
200 | * address. | |
201 | * | |
202 | * In the case of a PIC binary, the location is relative (+0x1234). | |
203 | * For a non-PIC binary, the location is absolute (@0x1234) | |
204 | * | |
205 | * On success, the out parameter `bin_loc` is set. The ownership is | |
206 | * passed to the caller. On failure, `bin_loc` remains unchanged. | |
207 | * | |
dae52e76 | 208 | * @param bin bin_info instance for the executable containing |
dc419b6c AB |
209 | * the address |
210 | * @param addr Virtual memory address for which to find the | |
211 | * binary location | |
212 | * @param bin_loc Out parameter, the binary location | |
213 | * @returns 0 on success, -1 on failure | |
214 | */ | |
215 | BT_HIDDEN | |
dae52e76 | 216 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc); |
b5a8598f AB |
217 | |
218 | /** | |
219 | * Destroy the given source_location instance | |
220 | * | |
221 | * @param src_loc source_location instance to destroy | |
222 | */ | |
223 | BT_HIDDEN | |
224 | void source_location_destroy(struct source_location *src_loc); | |
225 | ||
dae52e76 | 226 | #endif /* _BABELTRACE_BIN_INFO_H */ |