Commit | Line | Data |
---|---|---|
c40a57e5 | 1 | /* |
d5ddf820 | 2 | * bin-info.c |
c40a57e5 AB |
3 | * |
4 | * Babeltrace - Executable and Shared Object Debug Info Reader | |
5 | * | |
6 | * Copyright 2015 Antoine Busque <abusque@efficios.com> | |
7 | * | |
8 | * Author: Antoine Busque <abusque@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
91bc8451 | 29 | #define BT_COMP_LOG_SELF_COMP (bin->self_comp) |
3a3d15f3 | 30 | #define BT_LOG_OUTPUT_LEVEL (bin->log_level) |
350ad6c1 | 31 | #define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO/BIN-INFO" |
d9c39b0a | 32 | #include "logging/comp-logging.h" |
b4565e8b | 33 | |
3a3d15f3 | 34 | #include <babeltrace2/logging.h> |
1e638f98 FD |
35 | #include <dwarf.h> |
36 | #include <errno.h> | |
c40a57e5 | 37 | #include <fcntl.h> |
1e638f98 | 38 | #include <inttypes.h> |
c40a57e5 | 39 | #include <libgen.h> |
1e638f98 | 40 | #include <math.h> |
c4f23e30 | 41 | #include <stdbool.h> |
c40a57e5 | 42 | #include <stdio.h> |
c40a57e5 AB |
43 | #include <stdlib.h> |
44 | #include <string.h> | |
45 | #include <unistd.h> | |
1e638f98 | 46 | |
c40a57e5 | 47 | #include <glib.h> |
1e638f98 | 48 | |
578e048b | 49 | #include "common/common.h" |
af54bd18 | 50 | |
4f45f9bb JD |
51 | #include "bin-info.h" |
52 | #include "crc32.h" | |
1e638f98 | 53 | #include "dwarf.h" |
4f45f9bb | 54 | #include "utils.h" |
c40a57e5 AB |
55 | |
56 | /* | |
57 | * An address printed in hex is at most 20 bytes (16 for 64-bits + | |
58 | * leading 0x + optional leading '+' if addr is an offset + null | |
59 | * character). | |
60 | */ | |
61 | #define ADDR_STR_LEN 20 | |
ca9f27f3 | 62 | #define BUILD_ID_NOTE_NAME "GNU" |
c40a57e5 AB |
63 | |
64 | BT_HIDDEN | |
91bc8451 | 65 | int bin_info_init(bt_logging_level log_level, bt_self_component *self_comp) |
c40a57e5 AB |
66 | { |
67 | int ret = 0; | |
68 | ||
69 | if (elf_version(EV_CURRENT) == EV_NONE) { | |
91bc8451 | 70 | BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp, |
3a3d15f3 | 71 | "ELF library initialization failed: %s.", |
b4565e8b | 72 | elf_errmsg(-1)); |
c40a57e5 AB |
73 | ret = -1; |
74 | } | |
75 | ||
76 | return ret; | |
77 | } | |
78 | ||
79 | BT_HIDDEN | |
1e638f98 FD |
80 | struct bin_info *bin_info_create(struct bt_fd_cache *fdc, const char *path, |
81 | uint64_t low_addr, uint64_t memsz, bool is_pic, | |
3a3d15f3 | 82 | const char *debug_info_dir, const char *target_prefix, |
91bc8451 | 83 | bt_logging_level log_level, bt_self_component *self_comp) |
c40a57e5 | 84 | { |
d5ddf820 | 85 | struct bin_info *bin = NULL; |
c40a57e5 | 86 | |
1e638f98 FD |
87 | BT_ASSERT(fdc); |
88 | ||
c40a57e5 AB |
89 | if (!path) { |
90 | goto error; | |
91 | } | |
92 | ||
d5ddf820 AB |
93 | bin = g_new0(struct bin_info, 1); |
94 | if (!bin) { | |
c40a57e5 AB |
95 | goto error; |
96 | } | |
97 | ||
3a3d15f3 | 98 | bin->log_level = log_level; |
91bc8451 | 99 | bin->self_comp = self_comp; |
9d325e17 | 100 | if (target_prefix) { |
0cf9d412 | 101 | bin->elf_path = g_build_filename(target_prefix, path, NULL); |
5cde0dc1 | 102 | } else { |
ca9f27f3 | 103 | bin->elf_path = g_strdup(path); |
5cde0dc1 AB |
104 | } |
105 | ||
d5ddf820 | 106 | if (!bin->elf_path) { |
c40a57e5 AB |
107 | goto error; |
108 | } | |
109 | ||
9d325e17 | 110 | if (debug_info_dir) { |
06d1cf5d | 111 | bin->debug_info_dir = g_strdup(debug_info_dir); |
9d325e17 PP |
112 | if (!bin->debug_info_dir) { |
113 | goto error; | |
114 | } | |
115 | } | |
116 | ||
d5ddf820 AB |
117 | bin->is_pic = is_pic; |
118 | bin->memsz = memsz; | |
119 | bin->low_addr = low_addr; | |
120 | bin->high_addr = bin->low_addr + bin->memsz; | |
ca9f27f3 FD |
121 | bin->build_id = NULL; |
122 | bin->build_id_len = 0; | |
123 | bin->file_build_id_matches = false; | |
1e638f98 | 124 | bin->fd_cache = fdc; |
c40a57e5 | 125 | |
d5ddf820 | 126 | return bin; |
c40a57e5 AB |
127 | |
128 | error: | |
d5ddf820 | 129 | bin_info_destroy(bin); |
c40a57e5 AB |
130 | return NULL; |
131 | } | |
132 | ||
133 | BT_HIDDEN | |
d5ddf820 | 134 | void bin_info_destroy(struct bin_info *bin) |
c40a57e5 | 135 | { |
d5ddf820 | 136 | if (!bin) { |
c40a57e5 AB |
137 | return; |
138 | } | |
139 | ||
d5ddf820 | 140 | dwarf_end(bin->dwarf_info); |
c40a57e5 | 141 | |
06d1cf5d FD |
142 | g_free(bin->debug_info_dir); |
143 | g_free(bin->elf_path); | |
144 | g_free(bin->dwarf_path); | |
4f45f9bb | 145 | g_free(bin->build_id); |
06d1cf5d | 146 | g_free(bin->dbg_link_filename); |
c40a57e5 | 147 | |
d5ddf820 | 148 | elf_end(bin->elf_file); |
c40a57e5 | 149 | |
1e638f98 FD |
150 | bt_fd_cache_put_handle(bin->fd_cache, bin->elf_handle); |
151 | bt_fd_cache_put_handle(bin->fd_cache, bin->dwarf_handle); | |
c40a57e5 | 152 | |
d5ddf820 | 153 | g_free(bin); |
c40a57e5 AB |
154 | } |
155 | ||
ca9f27f3 FD |
156 | /** |
157 | * Initialize the ELF file for a given executable. | |
158 | * | |
159 | * @param bin bin_info instance | |
160 | * @returns 0 on success, negative value on error. | |
161 | */ | |
162 | static | |
163 | int bin_info_set_elf_file(struct bin_info *bin) | |
164 | { | |
1e638f98 | 165 | struct bt_fd_cache_handle *elf_handle = NULL; |
ca9f27f3 | 166 | Elf *elf_file = NULL; |
9d3a71fc | 167 | int ret; |
ca9f27f3 | 168 | |
9d3a71fc | 169 | BT_ASSERT(bin); |
ca9f27f3 | 170 | |
1e638f98 FD |
171 | elf_handle = bt_fd_cache_get_handle(bin->fd_cache, bin->elf_path); |
172 | if (!elf_handle) { | |
91bc8451 | 173 | BT_COMP_LOGI("Failed to open %s", bin->elf_path); |
ca9f27f3 FD |
174 | goto error; |
175 | } | |
af54bd18 FD |
176 | bin->elf_handle = elf_handle; |
177 | ||
af54bd18 | 178 | elf_file = elf_begin(bt_fd_cache_handle_get_fd(bin->elf_handle), |
1e638f98 | 179 | ELF_C_READ, NULL); |
ca9f27f3 | 180 | if (!elf_file) { |
3b34b490 FD |
181 | BT_COMP_LOGE_APPEND_CAUSE(bin->self_comp, |
182 | "elf_begin failed: %s", elf_errmsg(-1)); | |
ca9f27f3 FD |
183 | goto error; |
184 | } | |
185 | ||
af54bd18 FD |
186 | bin->elf_file = elf_file; |
187 | ||
ca9f27f3 | 188 | if (elf_kind(elf_file) != ELF_K_ELF) { |
3b34b490 FD |
189 | BT_COMP_LOGE_APPEND_CAUSE(bin->self_comp, |
190 | "Error: %s is not an ELF object", bin->elf_path); | |
ca9f27f3 FD |
191 | goto error; |
192 | } | |
193 | ||
9d3a71fc FD |
194 | |
195 | ret = 0; | |
196 | goto end; | |
ca9f27f3 FD |
197 | |
198 | error: | |
1e638f98 | 199 | bt_fd_cache_put_handle(bin->fd_cache, elf_handle); |
ca9f27f3 | 200 | elf_end(elf_file); |
9d3a71fc FD |
201 | ret = -1; |
202 | ||
203 | end: | |
204 | return ret; | |
ca9f27f3 FD |
205 | } |
206 | ||
207 | /** | |
9b1d6816 | 208 | * From a note section data struct, check if it is a build id note. |
ca9f27f3 | 209 | * |
9b1d6816 | 210 | * @param note_data Pointer to a note section |
ca9f27f3 FD |
211 | * |
212 | * @returns 1 on match, 0 if `buf` does not contain a | |
213 | * valid build id note | |
214 | */ | |
215 | static | |
9b1d6816 | 216 | int is_build_id_note_section(Elf_Data *note_data) |
ca9f27f3 | 217 | { |
9b1d6816 FD |
218 | size_t name_offset, desc_offset; |
219 | GElf_Nhdr note_header; | |
ca9f27f3 | 220 | int ret = 0; |
ca9f27f3 | 221 | |
9b1d6816 FD |
222 | /* |
223 | * Discard the return value as it contains the size of the note section | |
224 | * and we don't need it. | |
ca9f27f3 | 225 | */ |
9b1d6816 FD |
226 | (void) gelf_getnote(note_data, 0, ¬e_header, &name_offset, |
227 | &desc_offset); | |
233b228c FD |
228 | |
229 | /* | |
230 | * Check the note name length. The name_sz field includes the | |
231 | * terminating null byte. | |
232 | */ | |
9b1d6816 | 233 | if (note_header.n_namesz != sizeof(BUILD_ID_NOTE_NAME)) { |
233b228c FD |
234 | goto invalid; |
235 | } | |
236 | ||
ca9f27f3 | 237 | /* Check the note type. */ |
9b1d6816 | 238 | if (note_header.n_type != NT_GNU_BUILD_ID) { |
ca9f27f3 FD |
239 | goto invalid; |
240 | } | |
241 | ||
242 | /* Check the note name. */ | |
9b1d6816 FD |
243 | if (memcmp(note_data->d_buf + name_offset, BUILD_ID_NOTE_NAME, |
244 | note_header.n_namesz) != 0) { | |
ca9f27f3 FD |
245 | goto invalid; |
246 | } | |
247 | ||
248 | ret = 1; | |
249 | ||
250 | invalid: | |
251 | return ret; | |
252 | } | |
253 | ||
254 | /** | |
9b1d6816 | 255 | * From a build id note section data struct, check if the build id it contains |
ca9f27f3 FD |
256 | * is identical to the build id passed as parameter. |
257 | * | |
9b1d6816 | 258 | * @param note_data Pointer to the file build id note section. |
ca9f27f3 FD |
259 | * @param build_id Pointer to a build id to compare to. |
260 | * @param build_id_len length of the build id. | |
261 | * | |
262 | * @returns 1 on match, 0 otherwise. | |
263 | */ | |
264 | static | |
9b1d6816 | 265 | int is_build_id_note_section_matching(Elf_Data *note_data, |
ca9f27f3 FD |
266 | uint8_t *build_id, size_t build_id_len) |
267 | { | |
9b1d6816 FD |
268 | size_t name_offset, desc_offset; |
269 | GElf_Nhdr note_header; | |
ca9f27f3 FD |
270 | |
271 | if (build_id_len <= 0) { | |
272 | goto end; | |
273 | } | |
274 | ||
ca9f27f3 | 275 | /* |
9b1d6816 FD |
276 | * Discard the return value as it contains the size of the note section |
277 | * and we don't need it. | |
ca9f27f3 | 278 | */ |
9b1d6816 FD |
279 | (void) gelf_getnote(note_data, 0, ¬e_header, &name_offset, |
280 | &desc_offset); | |
ca9f27f3 FD |
281 | |
282 | /* | |
283 | * Compare the binary build id with the supplied build id. | |
284 | */ | |
9b1d6816 FD |
285 | if (memcmp(build_id, note_data->d_buf + desc_offset, |
286 | build_id_len) == 0) { | |
ca9f27f3 FD |
287 | return 1; |
288 | } | |
289 | end: | |
290 | return 0; | |
291 | } | |
292 | ||
293 | /** | |
294 | * Checks if the build id stored in `bin` (bin->build_id) is matching the build | |
295 | * id of the ondisk file (bin->elf_file). | |
296 | * | |
297 | * @param bin bin_info instance | |
298 | * @param build_id build id to compare ot the on disk file | |
299 | * @param build_id_len length of the build id | |
300 | * | |
301 | * @returns 1 on if the build id of stored in `bin` matches | |
302 | * the build id of the ondisk file. | |
303 | * 0 on if they are different or an error occured. | |
304 | */ | |
305 | static | |
306 | int is_build_id_matching(struct bin_info *bin) | |
307 | { | |
308 | int ret, is_build_id, is_matching = 0; | |
309 | Elf_Scn *curr_section = NULL, *next_section = NULL; | |
bfe42dac | 310 | GElf_Shdr curr_section_hdr; |
ca9f27f3 FD |
311 | |
312 | if (!bin->build_id) { | |
313 | goto error; | |
314 | } | |
315 | ||
316 | /* Set ELF file if it hasn't been accessed yet. */ | |
317 | if (!bin->elf_file) { | |
318 | ret = bin_info_set_elf_file(bin); | |
319 | if (ret) { | |
320 | /* Failed to set ELF file. */ | |
321 | goto error; | |
322 | } | |
323 | } | |
324 | ||
ca9f27f3 FD |
325 | next_section = elf_nextscn(bin->elf_file, curr_section); |
326 | if (!next_section) { | |
327 | goto error; | |
328 | } | |
329 | ||
330 | while (next_section) { | |
9b1d6816 | 331 | Elf_Data *note_data = NULL; |
af54bd18 | 332 | |
ca9f27f3 FD |
333 | curr_section = next_section; |
334 | next_section = elf_nextscn(bin->elf_file, curr_section); | |
335 | ||
bfe42dac | 336 | if (!gelf_getshdr(curr_section, &curr_section_hdr)) { |
ca9f27f3 FD |
337 | goto error; |
338 | } | |
339 | ||
bfe42dac | 340 | if (curr_section_hdr.sh_type != SHT_NOTE) { |
ca9f27f3 FD |
341 | continue; |
342 | } | |
343 | ||
af54bd18 | 344 | /* |
9b1d6816 | 345 | * elf_getdata() translates the data to native byte order. |
af54bd18 | 346 | */ |
9b1d6816 FD |
347 | note_data = elf_getdata(curr_section, NULL); |
348 | if (!note_data) { | |
349 | goto error; | |
350 | } | |
af54bd18 | 351 | |
ca9f27f3 | 352 | /* Check if the note is of the build-id type. */ |
9b1d6816 | 353 | is_build_id = is_build_id_note_section(note_data); |
ca9f27f3 FD |
354 | if (!is_build_id) { |
355 | continue; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Compare the build id of the on-disk file and | |
360 | * the build id recorded in the trace. | |
361 | */ | |
af54bd18 | 362 | is_matching = is_build_id_note_section_matching( |
9b1d6816 | 363 | note_data, bin->build_id, bin->build_id_len); |
ca9f27f3 FD |
364 | if (!is_matching) { |
365 | break; | |
366 | } | |
367 | } | |
368 | error: | |
ca9f27f3 FD |
369 | return is_matching; |
370 | } | |
371 | ||
c40a57e5 | 372 | BT_HIDDEN |
d5ddf820 | 373 | int bin_info_set_build_id(struct bin_info *bin, uint8_t *build_id, |
c40a57e5 AB |
374 | size_t build_id_len) |
375 | { | |
d5ddf820 | 376 | if (!bin || !build_id) { |
c40a57e5 AB |
377 | goto error; |
378 | } | |
379 | ||
ca9f27f3 FD |
380 | /* Set the build id. */ |
381 | bin->build_id = g_new0(uint8_t, build_id_len); | |
d5ddf820 | 382 | if (!bin->build_id) { |
c40a57e5 AB |
383 | goto error; |
384 | } | |
385 | ||
d5ddf820 AB |
386 | memcpy(bin->build_id, build_id, build_id_len); |
387 | bin->build_id_len = build_id_len; | |
c40a57e5 | 388 | |
ca9f27f3 FD |
389 | /* |
390 | * Check if the file found on the file system has the same build id | |
391 | * that what was recorded in the trace. | |
392 | */ | |
393 | bin->file_build_id_matches = is_build_id_matching(bin); | |
394 | if (!bin->file_build_id_matches) { | |
91bc8451 | 395 | BT_COMP_LOGI_STR("Supplied Build ID does not match Build ID of the " |
ca9f27f3 FD |
396 | "binary or library found on the file system."); |
397 | goto error; | |
398 | } | |
399 | ||
c40a57e5 AB |
400 | /* |
401 | * Reset the is_elf_only flag in case it had been set | |
402 | * previously, because we might find separate debug info using | |
403 | * the new build id information. | |
404 | */ | |
d5ddf820 | 405 | bin->is_elf_only = false; |
c40a57e5 AB |
406 | |
407 | return 0; | |
408 | ||
409 | error: | |
c40a57e5 AB |
410 | return -1; |
411 | } | |
412 | ||
413 | BT_HIDDEN | |
4f45f9bb JD |
414 | int bin_info_set_debug_link(struct bin_info *bin, const char *filename, |
415 | uint32_t crc) | |
c40a57e5 | 416 | { |
d5ddf820 | 417 | if (!bin || !filename) { |
c40a57e5 AB |
418 | goto error; |
419 | } | |
420 | ||
06d1cf5d | 421 | bin->dbg_link_filename = g_strdup(filename); |
d5ddf820 | 422 | if (!bin->dbg_link_filename) { |
c40a57e5 AB |
423 | goto error; |
424 | } | |
425 | ||
d5ddf820 | 426 | bin->dbg_link_crc = crc; |
c40a57e5 AB |
427 | |
428 | /* | |
429 | * Reset the is_elf_only flag in case it had been set | |
430 | * previously, because we might find separate debug info using | |
431 | * the new build id information. | |
432 | */ | |
d5ddf820 | 433 | bin->is_elf_only = false; |
c40a57e5 AB |
434 | |
435 | return 0; | |
436 | ||
437 | error: | |
438 | ||
439 | return -1; | |
440 | } | |
441 | ||
442 | /** | |
443 | * Tries to read DWARF info from the location given by path, and | |
d5ddf820 | 444 | * attach it to the given bin_info instance if it exists. |
c40a57e5 | 445 | * |
d5ddf820 | 446 | * @param bin bin_info instance for which to set DWARF info |
c40a57e5 | 447 | * @param path Presumed location of the DWARF info |
545e1e92 | 448 | * @returns 0 on success, negative value on failure |
c40a57e5 AB |
449 | */ |
450 | static | |
d5ddf820 | 451 | int bin_info_set_dwarf_info_from_path(struct bin_info *bin, char *path) |
c40a57e5 | 452 | { |
1e638f98 FD |
453 | int ret = 0; |
454 | struct bt_fd_cache_handle *dwarf_handle = NULL; | |
c40a57e5 AB |
455 | struct bt_dwarf_cu *cu = NULL; |
456 | Dwarf *dwarf_info = NULL; | |
457 | ||
d5ddf820 | 458 | if (!bin || !path) { |
c40a57e5 AB |
459 | goto error; |
460 | } | |
461 | ||
1e638f98 FD |
462 | dwarf_handle = bt_fd_cache_get_handle(bin->fd_cache, path); |
463 | if (!dwarf_handle) { | |
c40a57e5 AB |
464 | goto error; |
465 | } | |
466 | ||
1e638f98 FD |
467 | dwarf_info = dwarf_begin(bt_fd_cache_handle_get_fd(dwarf_handle), |
468 | DWARF_C_READ); | |
c40a57e5 AB |
469 | if (!dwarf_info) { |
470 | goto error; | |
471 | } | |
472 | ||
473 | /* | |
d5ddf820 AB |
474 | * Check if the dwarf info has any CU. If not, the |
475 | * executable's object file contains no DWARF info. | |
c40a57e5 AB |
476 | */ |
477 | cu = bt_dwarf_cu_create(dwarf_info); | |
478 | if (!cu) { | |
479 | goto error; | |
480 | } | |
481 | ||
482 | ret = bt_dwarf_cu_next(cu); | |
483 | if (ret) { | |
484 | goto error; | |
485 | } | |
486 | ||
1e638f98 | 487 | bin->dwarf_handle = dwarf_handle; |
06d1cf5d | 488 | bin->dwarf_path = g_strdup(path); |
d5ddf820 | 489 | if (!bin->dwarf_path) { |
c40a57e5 AB |
490 | goto error; |
491 | } | |
d5ddf820 | 492 | bin->dwarf_info = dwarf_info; |
c40a57e5 AB |
493 | free(cu); |
494 | ||
495 | return 0; | |
496 | ||
497 | error: | |
c449a5c3 FD |
498 | if (bin) { |
499 | bt_fd_cache_put_handle(bin->fd_cache, dwarf_handle); | |
500 | } | |
c40a57e5 AB |
501 | dwarf_end(dwarf_info); |
502 | g_free(dwarf_info); | |
503 | free(cu); | |
504 | ||
1e638f98 | 505 | return -1; |
c40a57e5 AB |
506 | } |
507 | ||
508 | /** | |
d5ddf820 | 509 | * Try to set the dwarf_info for a given bin_info instance via the |
c40a57e5 AB |
510 | * build ID method. |
511 | * | |
d5ddf820 | 512 | * @param bin bin_info instance for which to retrieve the |
c40a57e5 AB |
513 | * DWARF info via build ID |
514 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
515 | */ | |
516 | static | |
d5ddf820 | 517 | int bin_info_set_dwarf_info_build_id(struct bin_info *bin) |
c40a57e5 | 518 | { |
f06ce5e5 | 519 | int i = 0, ret = 0; |
73485cf9 | 520 | char *path = NULL, *build_id_prefix_dir = NULL, *build_id_file = NULL; |
c40a57e5 | 521 | const char *dbg_dir = NULL; |
8486e87a | 522 | size_t build_id_char_len, build_id_suffix_char_len, build_id_file_len; |
c40a57e5 | 523 | |
d5ddf820 | 524 | if (!bin || !bin->build_id) { |
c40a57e5 AB |
525 | goto error; |
526 | } | |
527 | ||
06d1cf5d | 528 | dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR; |
c40a57e5 | 529 | |
73485cf9 MJ |
530 | /* |
531 | * The prefix dir is the first byte of the build id, represented in | |
532 | * lowercase hex as two characters per byte, +1 for '\0'. | |
533 | */ | |
534 | build_id_prefix_dir = g_new0(gchar, BUILD_ID_PREFIX_DIR_LEN + 1); | |
535 | if (!build_id_prefix_dir) { | |
536 | goto error; | |
537 | } | |
538 | g_snprintf(build_id_prefix_dir, BUILD_ID_PREFIX_DIR_LEN + 1, "%02x", bin->build_id[0]); | |
539 | ||
540 | /* | |
541 | * The build id file is the remaining bytes of the build id, | |
542 | * represented in lowercase hex, as two characters per byte. | |
543 | */ | |
544 | build_id_char_len = (2 * (bin->build_id_len - 1)); | |
545 | ||
546 | /* To which the build id suffix is added, +1 for '\0'. */ | |
8486e87a | 547 | build_id_suffix_char_len = strlen(BUILD_ID_SUFFIX) + 1; |
73485cf9 MJ |
548 | |
549 | /* | |
550 | * The resulting filename string is the concatenation of the | |
551 | * hex build id and the suffix. | |
552 | */ | |
8486e87a | 553 | build_id_file_len = build_id_char_len + build_id_suffix_char_len; |
06d1cf5d | 554 | build_id_file = g_new0(gchar, build_id_file_len); |
c40a57e5 AB |
555 | if (!build_id_file) { |
556 | goto error; | |
557 | } | |
558 | ||
73485cf9 MJ |
559 | /* |
560 | * For each byte, starting at offset 1, append two characters | |
561 | * in lowercase hex. | |
562 | */ | |
d5ddf820 | 563 | for (i = 1; i < bin->build_id_len; ++i) { |
73485cf9 | 564 | int path_idx = 2 * (i - 1); |
c40a57e5 | 565 | |
06d1cf5d | 566 | g_snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]); |
c40a57e5 | 567 | } |
73485cf9 | 568 | /* Append the suffix to the generated string, including the '\0'. */ |
8486e87a FD |
569 | g_snprintf(&build_id_file[build_id_char_len], build_id_suffix_char_len, |
570 | BUILD_ID_SUFFIX); | |
c40a57e5 | 571 | |
73485cf9 | 572 | path = g_build_filename(dbg_dir, BUILD_ID_SUBDIR, build_id_prefix_dir, build_id_file, NULL); |
c40a57e5 AB |
573 | if (!path) { |
574 | goto error; | |
575 | } | |
576 | ||
d5ddf820 | 577 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
c40a57e5 AB |
578 | if (ret) { |
579 | goto error; | |
580 | } | |
581 | ||
582 | goto end; | |
583 | ||
584 | error: | |
585 | ret = -1; | |
586 | end: | |
73485cf9 MJ |
587 | g_free(build_id_prefix_dir); |
588 | g_free(build_id_file); | |
589 | g_free(path); | |
c40a57e5 AB |
590 | |
591 | return ret; | |
592 | } | |
593 | ||
594 | /** | |
595 | * Tests whether the file located at path exists and has the expected | |
596 | * checksum. | |
597 | * | |
598 | * This predicate is used when looking up separate debug info via the | |
599 | * GNU debuglink method. The expected crc can be found .gnu_debuglink | |
600 | * section in the original ELF file, along with the filename for the | |
601 | * file containing the debug info. | |
602 | * | |
603 | * @param path Full path at which to look for the debug file | |
604 | * @param crc Expected checksum for the debug file | |
605 | * @returns 1 if the file exists and has the correct checksum, | |
606 | * 0 otherwise | |
607 | */ | |
608 | static | |
1e638f98 | 609 | int is_valid_debug_file(struct bin_info *bin, char *path, uint32_t crc) |
c40a57e5 | 610 | { |
1e638f98 FD |
611 | int ret = 0; |
612 | struct bt_fd_cache_handle *debug_handle = NULL; | |
c40a57e5 AB |
613 | uint32_t _crc = 0; |
614 | ||
615 | if (!path) { | |
1e638f98 | 616 | goto end; |
c40a57e5 AB |
617 | } |
618 | ||
1e638f98 FD |
619 | debug_handle = bt_fd_cache_get_handle(bin->fd_cache, path); |
620 | if (!debug_handle) { | |
621 | goto end; | |
c40a57e5 AB |
622 | } |
623 | ||
1e638f98 | 624 | ret = crc32(bt_fd_cache_handle_get_fd(debug_handle), &_crc); |
c40a57e5 AB |
625 | if (ret) { |
626 | ret = 0; | |
627 | goto end; | |
628 | } | |
629 | ||
630 | ret = (crc == _crc); | |
631 | ||
632 | end: | |
1e638f98 | 633 | bt_fd_cache_put_handle(bin->fd_cache, debug_handle); |
c40a57e5 AB |
634 | return ret; |
635 | } | |
636 | ||
637 | /** | |
d5ddf820 | 638 | * Try to set the dwarf_info for a given bin_info instance via the |
ca9f27f3 | 639 | * debug-link method. |
c40a57e5 | 640 | * |
d5ddf820 | 641 | * @param bin bin_info instance for which to retrieve the |
c40a57e5 AB |
642 | * DWARF info via debug link |
643 | * @returns 0 on success (i.e. dwarf_info set), -1 on failure | |
644 | */ | |
645 | static | |
d5ddf820 | 646 | int bin_info_set_dwarf_info_debug_link(struct bin_info *bin) |
c40a57e5 AB |
647 | { |
648 | int ret = 0; | |
06d1cf5d | 649 | const gchar *dbg_dir = NULL; |
73485cf9 | 650 | gchar *bin_dir = NULL, *path = NULL; |
c40a57e5 | 651 | |
d5ddf820 | 652 | if (!bin || !bin->dbg_link_filename) { |
c40a57e5 AB |
653 | goto error; |
654 | } | |
655 | ||
06d1cf5d | 656 | dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR; |
73485cf9 | 657 | bin_dir = g_path_get_dirname(bin->elf_path); |
c40a57e5 | 658 | |
d5ddf820 | 659 | /* First look in the executable's dir */ |
73485cf9 | 660 | path = g_build_filename(bin_dir, bin->dbg_link_filename, NULL); |
c40a57e5 | 661 | |
1e638f98 | 662 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
663 | goto found; |
664 | } | |
665 | ||
666 | /* If not found, look in .debug subdir */ | |
06d1cf5d | 667 | g_free(path); |
73485cf9 | 668 | path = g_build_filename(bin_dir, DEBUG_SUBDIR, bin->dbg_link_filename, NULL); |
c40a57e5 | 669 | |
1e638f98 | 670 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
671 | goto found; |
672 | } | |
673 | ||
674 | /* Lastly, look under the global debug directory */ | |
06d1cf5d | 675 | g_free(path); |
c40a57e5 | 676 | |
73485cf9 | 677 | path = g_build_filename(dbg_dir, bin_dir, bin->dbg_link_filename, NULL); |
1e638f98 | 678 | if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) { |
c40a57e5 AB |
679 | goto found; |
680 | } | |
681 | ||
682 | error: | |
683 | ret = -1; | |
684 | end: | |
39588a20 | 685 | g_free(bin_dir); |
85cf9985 | 686 | g_free(path); |
c40a57e5 AB |
687 | |
688 | return ret; | |
689 | ||
690 | found: | |
d5ddf820 | 691 | ret = bin_info_set_dwarf_info_from_path(bin, path); |
c40a57e5 AB |
692 | if (ret) { |
693 | goto error; | |
694 | } | |
695 | ||
696 | goto end; | |
697 | } | |
698 | ||
699 | /** | |
700 | * Initialize the DWARF info for a given executable. | |
701 | * | |
d5ddf820 | 702 | * @param bin bin_info instance |
545e1e92 | 703 | * @returns 0 on success, negative value on failure |
c40a57e5 AB |
704 | */ |
705 | static | |
d5ddf820 | 706 | int bin_info_set_dwarf_info(struct bin_info *bin) |
c40a57e5 AB |
707 | { |
708 | int ret = 0; | |
709 | ||
d5ddf820 | 710 | if (!bin) { |
545e1e92 JG |
711 | ret = -1; |
712 | goto end; | |
c40a57e5 AB |
713 | } |
714 | ||
715 | /* First try to set the DWARF info from the ELF file */ | |
d5ddf820 | 716 | ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path); |
c40a57e5 AB |
717 | if (!ret) { |
718 | goto end; | |
719 | } | |
720 | ||
721 | /* | |
722 | * If that fails, try to find separate debug info via build ID | |
723 | * and debug link. | |
724 | */ | |
d5ddf820 | 725 | ret = bin_info_set_dwarf_info_build_id(bin); |
c40a57e5 AB |
726 | if (!ret) { |
727 | goto end; | |
728 | } | |
729 | ||
d5ddf820 | 730 | ret = bin_info_set_dwarf_info_debug_link(bin); |
c40a57e5 AB |
731 | if (!ret) { |
732 | goto end; | |
733 | } | |
734 | ||
c40a57e5 AB |
735 | end: |
736 | return ret; | |
737 | } | |
738 | ||
739 | BT_HIDDEN | |
740 | void source_location_destroy(struct source_location *src_loc) | |
741 | { | |
742 | if (!src_loc) { | |
743 | return; | |
744 | } | |
745 | ||
746 | free(src_loc->filename); | |
747 | g_free(src_loc); | |
748 | } | |
d6d3f4e8 | 749 | |
d2ac1099 AB |
750 | /** |
751 | * Append a string representation of an address offset to an existing | |
752 | * string. | |
753 | * | |
754 | * On success, the out parameter `result` will contain the base string | |
755 | * followed by the offset string of the form "+0x1234". On failure, | |
756 | * `result` remains unchanged. | |
757 | * | |
758 | * @param base_str The string to which to append an offset string | |
759 | * @param low_addr The lower virtual memory address, the base from | |
760 | * which the offset is computed | |
761 | * @param high_addr The higher virtual memory address | |
762 | * @param result Out parameter, the base string followed by the | |
763 | * offset string | |
764 | * @returns 0 on success, -1 on failure | |
765 | */ | |
766 | static | |
d5ddf820 | 767 | int bin_info_append_offset_str(const char *base_str, uint64_t low_addr, |
d2ac1099 AB |
768 | uint64_t high_addr, char **result) |
769 | { | |
d2ac1099 AB |
770 | uint64_t offset; |
771 | char *_result = NULL; | |
06d1cf5d | 772 | |
d2ac1099 AB |
773 | if (!base_str || !result) { |
774 | goto error; | |
775 | } | |
776 | ||
777 | offset = high_addr - low_addr; | |
778 | ||
06d1cf5d | 779 | _result = g_strdup_printf("%s+%#0" PRIx64, base_str, offset); |
d2ac1099 AB |
780 | if (!_result) { |
781 | goto error; | |
782 | } | |
d2ac1099 AB |
783 | *result = _result; |
784 | ||
785 | return 0; | |
786 | ||
787 | error: | |
788 | free(_result); | |
789 | return -1; | |
790 | } | |
c40a57e5 AB |
791 | |
792 | /** | |
793 | * Try to find the symbol closest to an address within a given ELF | |
794 | * section. | |
795 | * | |
796 | * Only function symbols are taken into account. The symbol's address | |
797 | * must precede `addr`. A symbol with a closer address might exist | |
798 | * after `addr` but is irrelevant because it cannot encompass `addr`. | |
799 | * | |
800 | * On success, if found, the out parameters `sym` and `shdr` are | |
801 | * set. On failure or if none are found, they remain unchanged. | |
802 | * | |
803 | * @param scn ELF section in which to look for the address | |
804 | * @param addr Virtual memory address for which to find the | |
805 | * nearest function symbol | |
806 | * @param sym Out parameter, the nearest function symbol | |
807 | * @param shdr Out parameter, the section header for scn | |
808 | * @returns 0 on success, -1 on failure | |
809 | */ | |
810 | static | |
d5ddf820 | 811 | int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr, |
c40a57e5 AB |
812 | GElf_Sym **sym, GElf_Shdr **shdr) |
813 | { | |
814 | int i; | |
815 | size_t symbol_count; | |
816 | Elf_Data *data = NULL; | |
817 | GElf_Shdr *_shdr = NULL; | |
818 | GElf_Sym *nearest_sym = NULL; | |
819 | ||
820 | if (!scn || !sym || !shdr) { | |
821 | goto error; | |
822 | } | |
823 | ||
824 | _shdr = g_new0(GElf_Shdr, 1); | |
825 | if (!_shdr) { | |
826 | goto error; | |
827 | } | |
828 | ||
829 | _shdr = gelf_getshdr(scn, _shdr); | |
830 | if (!_shdr) { | |
831 | goto error; | |
832 | } | |
833 | ||
834 | if (_shdr->sh_type != SHT_SYMTAB) { | |
835 | /* | |
836 | * We are only interested in symbol table (symtab) | |
837 | * sections, skip this one. | |
838 | */ | |
839 | goto end; | |
840 | } | |
841 | ||
842 | data = elf_getdata(scn, NULL); | |
843 | if (!data) { | |
844 | goto error; | |
845 | } | |
846 | ||
847 | symbol_count = _shdr->sh_size / _shdr->sh_entsize; | |
848 | ||
849 | for (i = 0; i < symbol_count; ++i) { | |
850 | GElf_Sym *cur_sym = NULL; | |
851 | ||
852 | cur_sym = g_new0(GElf_Sym, 1); | |
853 | if (!cur_sym) { | |
854 | goto error; | |
855 | } | |
856 | cur_sym = gelf_getsym(data, i, cur_sym); | |
857 | if (!cur_sym) { | |
858 | goto error; | |
859 | } | |
860 | if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) { | |
861 | /* We're only interested in the functions. */ | |
862 | g_free(cur_sym); | |
863 | continue; | |
864 | } | |
865 | ||
866 | if (cur_sym->st_value <= addr && | |
867 | (!nearest_sym || | |
868 | cur_sym->st_value > nearest_sym->st_value)) { | |
869 | g_free(nearest_sym); | |
870 | nearest_sym = cur_sym; | |
871 | } else { | |
872 | g_free(cur_sym); | |
873 | } | |
874 | } | |
875 | ||
876 | end: | |
877 | if (nearest_sym) { | |
878 | *sym = nearest_sym; | |
879 | *shdr = _shdr; | |
880 | } else { | |
881 | g_free(_shdr); | |
882 | } | |
883 | ||
884 | return 0; | |
885 | ||
886 | error: | |
887 | g_free(nearest_sym); | |
888 | g_free(_shdr); | |
889 | return -1; | |
890 | } | |
891 | ||
892 | /** | |
893 | * Get the name of the function containing a given address within an | |
894 | * executable using ELF symbols. | |
895 | * | |
896 | * The function name is in fact the name of the nearest ELF symbol, | |
897 | * followed by the offset in bytes between the address and the symbol | |
898 | * (in hex), separated by a '+' character. | |
899 | * | |
900 | * If found, the out parameter `func_name` is set on success. On failure, | |
901 | * it remains unchanged. | |
902 | * | |
d5ddf820 | 903 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
904 | * the address |
905 | * @param addr Virtual memory address for which to find the | |
906 | * function name | |
907 | * @param func_name Out parameter, the function name | |
908 | * @returns 0 on success, -1 on failure | |
909 | */ | |
910 | static | |
d5ddf820 | 911 | int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
912 | char **func_name) |
913 | { | |
914 | /* | |
915 | * TODO (possible optimisation): if an ELF has no symtab | |
916 | * section, it has been stripped. Therefore, it would be wise | |
917 | * to store a flag indicating the stripped status after the | |
918 | * first iteration to prevent subsequent ones. | |
919 | */ | |
920 | int ret = 0; | |
921 | Elf_Scn *scn = NULL; | |
922 | GElf_Sym *sym = NULL; | |
923 | GElf_Shdr *shdr = NULL; | |
924 | char *sym_name = NULL; | |
c40a57e5 | 925 | |
49824faa | 926 | /* Set ELF file if it hasn't been accessed yet. */ |
d5ddf820 AB |
927 | if (!bin->elf_file) { |
928 | ret = bin_info_set_elf_file(bin); | |
49824faa AB |
929 | if (ret) { |
930 | /* Failed to set ELF file. */ | |
931 | goto error; | |
932 | } | |
933 | } | |
934 | ||
d5ddf820 | 935 | scn = elf_nextscn(bin->elf_file, scn); |
c40a57e5 AB |
936 | if (!scn) { |
937 | goto error; | |
938 | } | |
939 | ||
940 | while (scn && !sym) { | |
d5ddf820 | 941 | ret = bin_info_get_nearest_symbol_from_section( |
c40a57e5 AB |
942 | scn, addr, &sym, &shdr); |
943 | if (ret) { | |
944 | goto error; | |
945 | } | |
946 | ||
d5ddf820 | 947 | scn = elf_nextscn(bin->elf_file, scn); |
c40a57e5 AB |
948 | } |
949 | ||
950 | if (sym) { | |
d5ddf820 | 951 | sym_name = elf_strptr(bin->elf_file, shdr->sh_link, |
c40a57e5 AB |
952 | sym->st_name); |
953 | if (!sym_name) { | |
954 | goto error; | |
955 | } | |
956 | ||
d5ddf820 | 957 | ret = bin_info_append_offset_str(sym_name, sym->st_value, addr, |
d2ac1099 AB |
958 | func_name); |
959 | if (ret) { | |
c40a57e5 AB |
960 | goto error; |
961 | } | |
c40a57e5 AB |
962 | } |
963 | ||
964 | g_free(shdr); | |
965 | g_free(sym); | |
966 | return 0; | |
967 | ||
968 | error: | |
969 | g_free(shdr); | |
970 | g_free(sym); | |
545e1e92 | 971 | return ret; |
c40a57e5 AB |
972 | } |
973 | ||
974 | /** | |
975 | * Get the name of the function containing a given address within a | |
976 | * given compile unit (CU). | |
977 | * | |
978 | * If found, the out parameter `func_name` is set on success. On | |
979 | * failure, it remains unchanged. | |
980 | * | |
981 | * @param cu bt_dwarf_cu instance which may contain the address | |
982 | * @param addr Virtual memory address for which to find the | |
983 | * function name | |
984 | * @param func_name Out parameter, the function name | |
985 | * @returns 0 on success, -1 on failure | |
986 | */ | |
987 | static | |
d5ddf820 | 988 | int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
989 | char **func_name) |
990 | { | |
a54aa699 AB |
991 | int ret = 0; |
992 | bool found = false; | |
c40a57e5 AB |
993 | struct bt_dwarf_die *die = NULL; |
994 | ||
995 | if (!cu || !func_name) { | |
996 | goto error; | |
997 | } | |
998 | ||
999 | die = bt_dwarf_die_create(cu); | |
1000 | if (!die) { | |
1001 | goto error; | |
1002 | } | |
1003 | ||
1004 | while (bt_dwarf_die_next(die) == 0) { | |
1005 | int tag; | |
1006 | ||
1007 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1008 | if (ret) { | |
1009 | goto error; | |
1010 | } | |
1011 | ||
1012 | if (tag == DW_TAG_subprogram) { | |
1013 | ret = bt_dwarf_die_contains_addr(die, addr, &found); | |
1014 | if (ret) { | |
1015 | goto error; | |
1016 | } | |
1017 | ||
1018 | if (found) { | |
1019 | break; | |
1020 | } | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | if (found) { | |
d2ac1099 AB |
1025 | uint64_t low_addr = 0; |
1026 | char *die_name = NULL; | |
1027 | ||
7935ee7a AB |
1028 | ret = bt_dwarf_die_get_name(die, &die_name); |
1029 | if (ret) { | |
1030 | goto error; | |
1031 | } | |
1032 | ||
1033 | ret = dwarf_lowpc(die->dwarf_die, &low_addr); | |
c40a57e5 | 1034 | if (ret) { |
7d41a84b | 1035 | free(die_name); |
c40a57e5 AB |
1036 | goto error; |
1037 | } | |
1038 | ||
d5ddf820 | 1039 | ret = bin_info_append_offset_str(die_name, low_addr, addr, |
d2ac1099 | 1040 | func_name); |
7d41a84b | 1041 | free(die_name); |
d2ac1099 | 1042 | if (ret) { |
7935ee7a AB |
1043 | goto error; |
1044 | } | |
c40a57e5 AB |
1045 | } |
1046 | ||
1047 | bt_dwarf_die_destroy(die); | |
1048 | return 0; | |
1049 | ||
1050 | error: | |
1051 | bt_dwarf_die_destroy(die); | |
1052 | return -1; | |
1053 | } | |
1054 | ||
1055 | /** | |
1056 | * Get the name of the function containing a given address within an | |
1057 | * executable using DWARF debug info. | |
1058 | * | |
1059 | * If found, the out parameter `func_name` is set on success. On | |
1060 | * failure, it remains unchanged. | |
1061 | * | |
d5ddf820 | 1062 | * @param bin bin_info instance for the executable containing |
c40a57e5 AB |
1063 | * the address |
1064 | * @param addr Virtual memory address for which to find the | |
1065 | * function name | |
1066 | * @param func_name Out parameter, the function name | |
1067 | * @returns 0 on success, -1 on failure | |
1068 | */ | |
1069 | static | |
d5ddf820 | 1070 | int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
1071 | char **func_name) |
1072 | { | |
1073 | int ret = 0; | |
1074 | char *_func_name = NULL; | |
1075 | struct bt_dwarf_cu *cu = NULL; | |
1076 | ||
d5ddf820 | 1077 | if (!bin || !func_name) { |
c40a57e5 AB |
1078 | goto error; |
1079 | } | |
1080 | ||
d5ddf820 | 1081 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
c40a57e5 AB |
1082 | if (!cu) { |
1083 | goto error; | |
1084 | } | |
1085 | ||
1086 | while (bt_dwarf_cu_next(cu) == 0) { | |
d5ddf820 | 1087 | ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name); |
c40a57e5 AB |
1088 | if (ret) { |
1089 | goto error; | |
1090 | } | |
1091 | ||
1092 | if (_func_name) { | |
1093 | break; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | if (_func_name) { | |
1098 | *func_name = _func_name; | |
93d65223 JG |
1099 | } else { |
1100 | goto error; | |
c40a57e5 AB |
1101 | } |
1102 | ||
1103 | bt_dwarf_cu_destroy(cu); | |
1104 | return 0; | |
1105 | ||
1106 | error: | |
1107 | bt_dwarf_cu_destroy(cu); | |
1108 | return -1; | |
1109 | } | |
1110 | ||
1111 | BT_HIDDEN | |
9d325e17 PP |
1112 | int bin_info_lookup_function_name(struct bin_info *bin, |
1113 | uint64_t addr, char **func_name) | |
c40a57e5 AB |
1114 | { |
1115 | int ret = 0; | |
1116 | char *_func_name = NULL; | |
1117 | ||
d5ddf820 | 1118 | if (!bin || !func_name) { |
c40a57e5 AB |
1119 | goto error; |
1120 | } | |
1121 | ||
ca9f27f3 FD |
1122 | /* |
1123 | * If the bin_info has a build id but it does not match the build id | |
1124 | * that was found on the file system, return an error. | |
1125 | */ | |
1126 | if (bin->build_id && !bin->file_build_id_matches) { | |
1127 | goto error; | |
1128 | } | |
1129 | ||
c40a57e5 | 1130 | /* Set DWARF info if it hasn't been accessed yet. */ |
d5ddf820 AB |
1131 | if (!bin->dwarf_info && !bin->is_elf_only) { |
1132 | ret = bin_info_set_dwarf_info(bin); | |
c40a57e5 | 1133 | if (ret) { |
91bc8451 | 1134 | BT_COMP_LOGI_STR("Failed to set bin dwarf info, falling " |
1e638f98 | 1135 | "back to ELF lookup."); |
c40a57e5 | 1136 | /* Failed to set DWARF info, fallback to ELF. */ |
d5ddf820 | 1137 | bin->is_elf_only = true; |
c40a57e5 AB |
1138 | } |
1139 | } | |
1140 | ||
d5ddf820 | 1141 | if (!bin_info_has_address(bin, addr)) { |
c40a57e5 AB |
1142 | goto error; |
1143 | } | |
1144 | ||
1145 | /* | |
1146 | * Addresses in ELF and DWARF are relative to base address for | |
1147 | * PIC, so make the address argument relative too if needed. | |
1148 | */ | |
d5ddf820 AB |
1149 | if (bin->is_pic) { |
1150 | addr -= bin->low_addr; | |
36ae9941 AB |
1151 | } |
1152 | ||
d5ddf820 | 1153 | if (bin->is_elf_only) { |
b223415b FD |
1154 | ret = bin_info_lookup_elf_function_name(bin, addr, |
1155 | &_func_name); | |
1156 | if (ret) { | |
91bc8451 | 1157 | BT_COMP_LOGI("Failed to lookup function name (ELF): " |
b223415b FD |
1158 | "ret=%d", ret); |
1159 | } | |
c40a57e5 | 1160 | } else { |
b223415b FD |
1161 | ret = bin_info_lookup_dwarf_function_name(bin, addr, |
1162 | &_func_name); | |
1163 | if (ret) { | |
91bc8451 | 1164 | BT_COMP_LOGI("Failed to lookup function name (DWARF): " |
b223415b FD |
1165 | "ret=%d", ret); |
1166 | } | |
c40a57e5 AB |
1167 | } |
1168 | ||
36ae9941 AB |
1169 | *func_name = _func_name; |
1170 | return 0; | |
55cd033d | 1171 | |
36ae9941 AB |
1172 | error: |
1173 | return -1; | |
1174 | } | |
1175 | ||
1176 | BT_HIDDEN | |
d5ddf820 | 1177 | int bin_info_get_bin_loc(struct bin_info *bin, uint64_t addr, char **bin_loc) |
36ae9941 | 1178 | { |
06d1cf5d | 1179 | gchar *_bin_loc = NULL; |
36ae9941 | 1180 | |
d5ddf820 | 1181 | if (!bin || !bin_loc) { |
36ae9941 | 1182 | goto error; |
c40a57e5 AB |
1183 | } |
1184 | ||
ca9f27f3 FD |
1185 | /* |
1186 | * If the bin_info has a build id but it does not match the build id | |
1187 | * that was found on the file system, return an error. | |
1188 | */ | |
1189 | if (bin->build_id && !bin->file_build_id_matches) { | |
1190 | goto error; | |
1191 | } | |
1192 | ||
d5ddf820 AB |
1193 | if (bin->is_pic) { |
1194 | addr -= bin->low_addr; | |
06d1cf5d | 1195 | _bin_loc = g_strdup_printf("+%#0" PRIx64, addr); |
36ae9941 | 1196 | } else { |
06d1cf5d | 1197 | _bin_loc = g_strdup_printf("@%#0" PRIx64, addr); |
36ae9941 AB |
1198 | } |
1199 | ||
06d1cf5d | 1200 | if (!_bin_loc) { |
36ae9941 AB |
1201 | goto error; |
1202 | } | |
1203 | ||
1204 | *bin_loc = _bin_loc; | |
c40a57e5 AB |
1205 | return 0; |
1206 | ||
1207 | error: | |
1208 | return -1; | |
1209 | } | |
1210 | ||
1211 | /** | |
1212 | * Predicate used to determine whether the children of a given DIE | |
1213 | * contain a specific address. | |
1214 | * | |
1215 | * More specifically, the parameter `die` is expected to be a | |
1216 | * subprogram (function) DIE, and this predicate tells whether any | |
1217 | * subroutines are inlined within this function and would contain | |
1218 | * `addr`. | |
1219 | * | |
a54aa699 AB |
1220 | * On success, the out parameter `contains` is set with the boolean |
1221 | * value indicating whether the DIE's range covers `addr`. On failure, | |
1222 | * it remains unchanged. | |
1223 | * | |
c40a57e5 AB |
1224 | * Do note that this function advances the position of `die`. If the |
1225 | * address is found within one of its children, `die` will be pointing | |
1226 | * to that child upon returning from the function, allowing to extract | |
1227 | * the information deemed necessary. | |
1228 | * | |
a54aa699 AB |
1229 | * @param die The parent DIE in whose children the address will be |
1230 | * looked for | |
1231 | * @param addr The address for which to look for in the DIEs | |
1232 | * @param contains Out parameter, true if addr is contained, | |
1233 | * false if not | |
1234 | * @returns Returns 0 on success, -1 on failure | |
c40a57e5 AB |
1235 | */ |
1236 | static | |
a54aa699 | 1237 | int bin_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr, bool *contains) |
c40a57e5 | 1238 | { |
a54aa699 AB |
1239 | int ret = 0; |
1240 | bool _contains = false; | |
c40a57e5 AB |
1241 | |
1242 | if (!die) { | |
1243 | goto error; | |
1244 | } | |
1245 | ||
1246 | ret = bt_dwarf_die_child(die); | |
1247 | if (ret) { | |
1248 | goto error; | |
1249 | } | |
1250 | ||
1251 | do { | |
e6365242 | 1252 | ret = bt_dwarf_die_contains_addr(die, addr, &_contains); |
c40a57e5 AB |
1253 | if (ret) { |
1254 | goto error; | |
1255 | } | |
1256 | ||
e6365242 FD |
1257 | if (_contains) { |
1258 | /* | |
1259 | * The address is within the range of the current DIE | |
1260 | * or its children. | |
1261 | */ | |
1262 | int tag; | |
1263 | ||
1264 | ret = bt_dwarf_die_get_tag(die, &tag); | |
c40a57e5 AB |
1265 | if (ret) { |
1266 | goto error; | |
1267 | } | |
1268 | ||
e6365242 FD |
1269 | if (tag == DW_TAG_inlined_subroutine) { |
1270 | /* Found the tracepoint. */ | |
c40a57e5 AB |
1271 | goto end; |
1272 | } | |
e6365242 FD |
1273 | |
1274 | if (bt_dwarf_die_has_children(die)) { | |
1275 | /* | |
1276 | * Look for the address in the children DIEs. | |
1277 | */ | |
1278 | ret = bt_dwarf_die_child(die); | |
1279 | if (ret) { | |
1280 | goto error; | |
1281 | } | |
1282 | } | |
c40a57e5 AB |
1283 | } |
1284 | } while (bt_dwarf_die_next(die) == 0); | |
1285 | ||
1286 | end: | |
a54aa699 AB |
1287 | *contains = _contains; |
1288 | return 0; | |
c40a57e5 AB |
1289 | |
1290 | error: | |
a54aa699 | 1291 | return -1; |
c40a57e5 AB |
1292 | } |
1293 | ||
1294 | /** | |
1295 | * Lookup the source location for a given address within a CU, making | |
1296 | * the assumption that it is contained within an inline routine in a | |
1297 | * function. | |
1298 | * | |
1299 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1300 | * @param addr The address for which to look for | |
1301 | * @param src_loc Out parameter, the source location (filename and | |
1302 | * line number) for the address | |
1303 | * @returns 0 on success, -1 on failure | |
1304 | */ | |
1305 | static | |
d5ddf820 | 1306 | int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1307 | struct source_location **src_loc) |
1308 | { | |
a54aa699 AB |
1309 | int ret = 0; |
1310 | bool found = false; | |
c40a57e5 AB |
1311 | struct bt_dwarf_die *die = NULL; |
1312 | struct source_location *_src_loc = NULL; | |
1313 | ||
1314 | if (!cu || !src_loc) { | |
1315 | goto error; | |
1316 | } | |
1317 | ||
1318 | die = bt_dwarf_die_create(cu); | |
1319 | if (!die) { | |
1320 | goto error; | |
1321 | } | |
1322 | ||
1323 | while (bt_dwarf_die_next(die) == 0) { | |
1324 | int tag; | |
1325 | ||
1326 | ret = bt_dwarf_die_get_tag(die, &tag); | |
1327 | if (ret) { | |
1328 | goto error; | |
1329 | } | |
1330 | ||
1331 | if (tag == DW_TAG_subprogram) { | |
a54aa699 | 1332 | bool contains = false; |
c40a57e5 AB |
1333 | |
1334 | ret = bt_dwarf_die_contains_addr(die, addr, &contains); | |
1335 | if (ret) { | |
1336 | goto error; | |
1337 | } | |
1338 | ||
1339 | if (contains) { | |
1340 | /* | |
1341 | * Try to find an inlined subroutine | |
1342 | * child of this DIE containing addr. | |
1343 | */ | |
a54aa699 AB |
1344 | ret = bin_info_child_die_has_address(die, addr, |
1345 | &found); | |
1346 | if(ret) { | |
1347 | goto error; | |
1348 | } | |
1349 | ||
c40a57e5 AB |
1350 | goto end; |
1351 | } | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | end: | |
1356 | if (found) { | |
1357 | char *filename = NULL; | |
1358 | uint64_t line_no; | |
1359 | ||
1360 | _src_loc = g_new0(struct source_location, 1); | |
1361 | if (!_src_loc) { | |
1362 | goto error; | |
1363 | } | |
1364 | ||
1365 | ret = bt_dwarf_die_get_call_file(die, &filename); | |
1366 | if (ret) { | |
1367 | goto error; | |
1368 | } | |
1369 | ret = bt_dwarf_die_get_call_line(die, &line_no); | |
1370 | if (ret) { | |
1371 | free(filename); | |
1372 | goto error; | |
1373 | } | |
1374 | ||
1375 | _src_loc->filename = filename; | |
1376 | _src_loc->line_no = line_no; | |
1377 | *src_loc = _src_loc; | |
1378 | } | |
1379 | ||
1380 | bt_dwarf_die_destroy(die); | |
1381 | return 0; | |
1382 | ||
1383 | error: | |
1384 | source_location_destroy(_src_loc); | |
1385 | bt_dwarf_die_destroy(die); | |
1386 | return -1; | |
1387 | } | |
1388 | ||
1389 | /** | |
1390 | * Lookup the source location for a given address within a CU, | |
1391 | * assuming that it is contained within an inlined function. | |
1392 | * | |
1393 | * A source location can be found regardless of inlining status for | |
1394 | * this method, but in the case of an inlined function, the returned | |
1395 | * source location will point not to the callsite but rather to the | |
1396 | * definition site of the inline function. | |
1397 | * | |
1398 | * @param cu bt_dwarf_cu instance in which to look for the address | |
1399 | * @param addr The address for which to look for | |
1400 | * @param src_loc Out parameter, the source location (filename and | |
1ed6d0cc FD |
1401 | * line number) for the address. Set only if the address |
1402 | * is found and resolved successfully | |
1403 | * | |
c40a57e5 AB |
1404 | * @returns 0 on success, -1 on failure |
1405 | */ | |
1406 | static | |
d5ddf820 | 1407 | int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1408 | struct source_location **src_loc) |
1409 | { | |
1410 | struct source_location *_src_loc = NULL; | |
1411 | struct bt_dwarf_die *die = NULL; | |
1412 | const char *filename = NULL; | |
1413 | Dwarf_Line *line = NULL; | |
1414 | Dwarf_Addr line_addr; | |
f06a3e47 | 1415 | int ret = 0, line_no; |
c40a57e5 AB |
1416 | |
1417 | if (!cu || !src_loc) { | |
1418 | goto error; | |
1419 | } | |
1420 | ||
1421 | die = bt_dwarf_die_create(cu); | |
1422 | if (!die) { | |
1423 | goto error; | |
1424 | } | |
1425 | ||
1426 | line = dwarf_getsrc_die(die->dwarf_die, addr); | |
1427 | if (!line) { | |
1ed6d0cc FD |
1428 | /* This is not an error. The caller needs to keep looking. */ |
1429 | goto end; | |
c40a57e5 AB |
1430 | } |
1431 | ||
1432 | ret = dwarf_lineaddr(line, &line_addr); | |
1433 | if (ret) { | |
1434 | goto error; | |
1435 | } | |
1436 | ||
1437 | filename = dwarf_linesrc(line, NULL, NULL); | |
1438 | if (!filename) { | |
1439 | goto error; | |
1440 | } | |
1441 | ||
1442 | if (addr == line_addr) { | |
1443 | _src_loc = g_new0(struct source_location, 1); | |
1444 | if (!_src_loc) { | |
1445 | goto error; | |
1446 | } | |
1447 | ||
1448 | ret = dwarf_lineno(line, &line_no); | |
1449 | if (ret) { | |
1450 | goto error; | |
1451 | } | |
1452 | ||
1453 | _src_loc->line_no = line_no; | |
06d1cf5d | 1454 | _src_loc->filename = g_strdup(filename); |
c40a57e5 AB |
1455 | } |
1456 | ||
c40a57e5 AB |
1457 | if (_src_loc) { |
1458 | *src_loc = _src_loc; | |
1459 | } | |
1460 | ||
f06a3e47 | 1461 | goto end; |
c40a57e5 AB |
1462 | |
1463 | error: | |
1464 | source_location_destroy(_src_loc); | |
f06a3e47 FD |
1465 | ret = -1; |
1466 | end: | |
c40a57e5 | 1467 | bt_dwarf_die_destroy(die); |
f06a3e47 | 1468 | return ret; |
c40a57e5 AB |
1469 | } |
1470 | ||
1471 | /** | |
1472 | * Get the source location (file name and line number) for a given | |
1473 | * address within a compile unit (CU). | |
1474 | * | |
1475 | * On success, the out parameter `src_loc` is set if found. On | |
1476 | * failure, it remains unchanged. | |
1477 | * | |
d5ddf820 | 1478 | * @param cu bt_dwarf_cu instance for the compile unit which |
c40a57e5 AB |
1479 | * may contain the address |
1480 | * @param addr Virtual memory address for which to find the | |
1481 | * source location | |
1482 | * @param src_loc Out parameter, the source location | |
1483 | * @returns 0 on success, -1 on failure | |
1484 | */ | |
1485 | static | |
d5ddf820 | 1486 | int bin_info_lookup_cu_src_loc(struct bt_dwarf_cu *cu, uint64_t addr, |
c40a57e5 AB |
1487 | struct source_location **src_loc) |
1488 | { | |
1489 | int ret = 0; | |
1490 | struct source_location *_src_loc = NULL; | |
1491 | ||
1492 | if (!cu || !src_loc) { | |
1493 | goto error; | |
1494 | } | |
1495 | ||
d5ddf820 | 1496 | ret = bin_info_lookup_cu_src_loc_inl(cu, addr, &_src_loc); |
c40a57e5 AB |
1497 | if (ret) { |
1498 | goto error; | |
1499 | } | |
1500 | ||
1501 | if (_src_loc) { | |
1502 | goto end; | |
1503 | } | |
1504 | ||
d5ddf820 | 1505 | ret = bin_info_lookup_cu_src_loc_no_inl(cu, addr, &_src_loc); |
c40a57e5 AB |
1506 | if (ret) { |
1507 | goto error; | |
1508 | } | |
1509 | ||
1510 | if (_src_loc) { | |
1511 | goto end; | |
1512 | } | |
1513 | ||
1514 | end: | |
1515 | if (_src_loc) { | |
1516 | *src_loc = _src_loc; | |
1517 | } | |
1518 | ||
1519 | return 0; | |
1520 | ||
1521 | error: | |
1522 | source_location_destroy(_src_loc); | |
1523 | return -1; | |
1524 | } | |
1525 | ||
1526 | BT_HIDDEN | |
d5ddf820 | 1527 | int bin_info_lookup_source_location(struct bin_info *bin, uint64_t addr, |
c40a57e5 AB |
1528 | struct source_location **src_loc) |
1529 | { | |
1530 | struct bt_dwarf_cu *cu = NULL; | |
1531 | struct source_location *_src_loc = NULL; | |
1532 | ||
d5ddf820 | 1533 | if (!bin || !src_loc) { |
c40a57e5 AB |
1534 | goto error; |
1535 | } | |
1536 | ||
ca9f27f3 FD |
1537 | /* |
1538 | * If the bin_info has a build id but it does not match the build id | |
1539 | * that was found on the file system, return an error. | |
1540 | */ | |
1541 | if (bin->build_id && !bin->file_build_id_matches) { | |
1542 | goto error; | |
1543 | } | |
1544 | ||
c40a57e5 | 1545 | /* Set DWARF info if it hasn't been accessed yet. */ |
d5ddf820 AB |
1546 | if (!bin->dwarf_info && !bin->is_elf_only) { |
1547 | if (bin_info_set_dwarf_info(bin)) { | |
c40a57e5 | 1548 | /* Failed to set DWARF info. */ |
d5ddf820 | 1549 | bin->is_elf_only = true; |
c40a57e5 AB |
1550 | } |
1551 | } | |
1552 | ||
d5ddf820 | 1553 | if (bin->is_elf_only) { |
c40a57e5 AB |
1554 | /* We cannot lookup source location without DWARF info. */ |
1555 | goto error; | |
1556 | } | |
1557 | ||
d5ddf820 | 1558 | if (!bin_info_has_address(bin, addr)) { |
c40a57e5 AB |
1559 | goto error; |
1560 | } | |
1561 | ||
1562 | /* | |
1563 | * Addresses in ELF and DWARF are relative to base address for | |
1564 | * PIC, so make the address argument relative too if needed. | |
1565 | */ | |
d5ddf820 AB |
1566 | if (bin->is_pic) { |
1567 | addr -= bin->low_addr; | |
c40a57e5 AB |
1568 | } |
1569 | ||
d5ddf820 | 1570 | cu = bt_dwarf_cu_create(bin->dwarf_info); |
c40a57e5 AB |
1571 | if (!cu) { |
1572 | goto error; | |
1573 | } | |
1574 | ||
1575 | while (bt_dwarf_cu_next(cu) == 0) { | |
1576 | int ret; | |
1577 | ||
d5ddf820 | 1578 | ret = bin_info_lookup_cu_src_loc(cu, addr, &_src_loc); |
c40a57e5 AB |
1579 | if (ret) { |
1580 | goto error; | |
1581 | } | |
1582 | ||
1583 | if (_src_loc) { | |
1584 | break; | |
1585 | } | |
1586 | } | |
1587 | ||
1588 | bt_dwarf_cu_destroy(cu); | |
1589 | if (_src_loc) { | |
1590 | *src_loc = _src_loc; | |
1591 | } | |
1592 | ||
1593 | return 0; | |
1594 | ||
1595 | error: | |
1596 | source_location_destroy(_src_loc); | |
1597 | bt_dwarf_cu_destroy(cu); | |
1598 | return -1; | |
1599 | } |