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