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 | ||
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 | ||
d5ddf820 | 41 | struct bin_info { |
c40a57e5 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; | |
9d325e17 PP |
63 | /* Configuration. */ |
64 | char *debug_info_dir; | |
c40a57e5 AB |
65 | /* Denotes whether the executable is position independent code. */ |
66 | bool is_pic:1; | |
d5ddf820 AB |
67 | /* |
68 | * Denotes whether the executable only has ELF symbols and no | |
69 | * DWARF info. | |
70 | */ | |
c40a57e5 AB |
71 | bool is_elf_only:1; |
72 | }; | |
73 | ||
74 | struct source_location { | |
75 | uint64_t line_no; | |
76 | char *filename; | |
77 | }; | |
78 | ||
79 | /** | |
d5ddf820 | 80 | * Initializes the bin_info framework. Call this before calling |
c40a57e5 AB |
81 | * anything else. |
82 | * | |
83 | * @returns 0 on success, -1 on failure | |
84 | */ | |
85 | BT_HIDDEN | |
d5ddf820 | 86 | int bin_info_init(void); |
c40a57e5 AB |
87 | |
88 | /** | |
89 | * Instantiate a structure representing an ELF executable, possibly | |
90 | * with DWARF info, located at the given path. | |
91 | * | |
92 | * @param path Path to the ELF file | |
93 | * @param low_addr Base address of the executable | |
94 | * @param memsz In-memory size of the executable | |
9f2b13ca AB |
95 | * @param is_pic Whether the executable is position independent |
96 | * code (PIC) | |
9d325e17 PP |
97 | * @param debug_info_dir Directory containing debug info or NULL. |
98 | * @param target_prefix Path to the root file system of the target | |
99 | * or NULL. | |
d5ddf820 | 100 | * @returns Pointer to the new bin_info on success, |
c40a57e5 AB |
101 | * NULL on failure. |
102 | */ | |
103 | BT_HIDDEN | |
d5ddf820 | 104 | struct bin_info *bin_info_create(const char *path, uint64_t low_addr, |
9d325e17 PP |
105 | uint64_t memsz, bool is_pic, const char *debug_info_dir, |
106 | const char *target_prefix); | |
c40a57e5 AB |
107 | |
108 | /** | |
d5ddf820 | 109 | * Destroy the given bin_info instance |
c40a57e5 | 110 | * |
d5ddf820 | 111 | * @param bin bin_info instance to destroy |
c40a57e5 AB |
112 | */ |
113 | BT_HIDDEN | |
d5ddf820 | 114 | void bin_info_destroy(struct bin_info *bin); |
c40a57e5 AB |
115 | |
116 | /** | |
d5ddf820 | 117 | * Sets the build ID information for a given bin_info instance. |
c40a57e5 | 118 | * |
d5ddf820 | 119 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
120 | * the build ID |
121 | * @param build_id Array of bytes containing the actual ID | |
122 | * @param build_id_len Length in bytes of the build_id | |
123 | * @returns 0 on success, -1 on failure | |
124 | */ | |
125 | BT_HIDDEN | |
d5ddf820 | 126 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
c40a57e5 AB |
127 | size_t build_id_len); |
128 | ||
129 | /** | |
d5ddf820 | 130 | * Sets the debug link information for a given bin_info instance. |
c40a57e5 | 131 | * |
d5ddf820 | 132 | * @param bin The bin_info instance for which to set |
c40a57e5 AB |
133 | * the debug link |
134 | * @param filename Name of the separate debug info file | |
135 | * @param crc Checksum for the debug info file | |
136 | * @returns 0 on success, -1 on failure | |
137 | */ | |
138 | BT_HIDDEN | |
4f45f9bb JD |
139 | int bin_info_set_debug_link(struct bin_info *bin, const char *filename, |
140 | uint32_t crc); | |
c40a57e5 AB |
141 | |
142 | /** | |
d5ddf820 AB |
143 | * Returns whether or not the given bin info \p bin contains the |
144 | * address \p addr. | |
c40a57e5 | 145 | * |
d5ddf820 | 146 | * @param bin bin_info instance |
c40a57e5 | 147 | * @param addr Address to lookup |
d5ddf820 | 148 | * @returns 1 if \p bin contains \p addr, 0 if it does not, |
c40a57e5 AB |
149 | * -1 on failure |
150 | */ | |
151 | static inline | |
d5ddf820 | 152 | int bin_info_has_address(struct bin_info *bin, uint64_t addr) |
c40a57e5 | 153 | { |
d5ddf820 | 154 | if (!bin) { |
c40a57e5 AB |
155 | return -1; |
156 | } | |
157 | ||
d5ddf820 | 158 | return addr >= bin->low_addr && addr < bin->high_addr; |
c40a57e5 AB |
159 | } |
160 | ||
161 | /** | |
162 | * Get the name of the function containing a given address within an | |
163 | * executable. | |
164 | * | |
165 | * If no DWARF info is available, the function falls back to ELF | |
166 | * symbols and the "function name" is in fact the name of the closest | |
167 | * symbol, followed by the offset between the symbol and the address. | |
168 | * | |
169 | * On success, if found, the out parameter `func_name` is set. The ownership | |
170 | * of `func_name` is passed to the caller. On failure, `func_name` remains | |
171 | * unchanged. | |
172 | * | |
d5ddf820 | 173 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
174 | * the address |
175 | * @param addr Virtual memory address for which to find the | |
176 | * function name | |
177 | * @param func_name Out parameter, the function name. | |
178 | * @returns 0 on success, -1 on failure | |
179 | */ | |
180 | BT_HIDDEN | |
d5ddf820 | 181 | int bin_info_lookup_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
182 | char **func_name); |
183 | ||
184 | /** | |
185 | * Get the source location (file name and line number) for a given | |
186 | * address within an executable. | |
187 | * | |
188 | * If no DWARF info is available, the source location cannot be found | |
4caab45b | 189 | * and the function will return unsuccessfully. |
c40a57e5 AB |
190 | * |
191 | * On success, if found, the out parameter `src_loc` is set. The ownership | |
192 | * of `src_loc` is passed to the caller. On failure, `src_loc` remains | |
193 | * unchanged. | |
194 | * | |
d5ddf820 | 195 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
196 | * the address |
197 | * @param addr Virtual memory address for which to find the | |
198 | * source location | |
199 | * @param src_loc Out parameter, the source location | |
200 | * @returns 0 on success, -1 on failure | |
201 | */ | |
202 | BT_HIDDEN | |
d5ddf820 | 203 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
c40a57e5 | 204 | struct source_location **src_loc); |
36ae9941 AB |
205 | /** |
206 | * Get a string representing the location within the binary of a given | |
207 | * address. | |
208 | * | |
209 | * In the case of a PIC binary, the location is relative (+0x1234). | |
210 | * For a non-PIC binary, the location is absolute (@0x1234) | |
211 | * | |
212 | * On success, the out parameter `bin_loc` is set. The ownership is | |
213 | * passed to the caller. On failure, `bin_loc` remains unchanged. | |
214 | * | |
d5ddf820 | 215 | * @param bin bin_info instance for the executable containing |
36ae9941 AB |
216 | * the address |
217 | * @param addr Virtual memory address for which to find the | |
218 | * binary location | |
219 | * @param bin_loc Out parameter, the binary location | |
220 | * @returns 0 on success, -1 on failure | |
221 | */ | |
222 | BT_HIDDEN | |
d5ddf820 | 223 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc); |
c40a57e5 AB |
224 | |
225 | /** | |
226 | * Destroy the given source_location instance | |
227 | * | |
228 | * @param src_loc source_location instance to destroy | |
229 | */ | |
230 | BT_HIDDEN | |
231 | void source_location_destroy(struct source_location *src_loc); | |
232 | ||
d5ddf820 | 233 | #endif /* _BABELTRACE_BIN_INFO_H */ |