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