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