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