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