flt.lttng-utils.debug-info: Implement file descriptor cache
[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_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_file_len = (2 * bin->build_id_len) + 1 +
522 strlen(BUILD_ID_SUFFIX) + 1;
523 build_id_file = g_new0(gchar, build_id_file_len);
524 if (!build_id_file) {
525 goto error;
526 }
527
528 g_snprintf(build_id_file, 4, "%02x/", bin->build_id[0]);
529 for (i = 1; i < bin->build_id_len; ++i) {
530 int path_idx = 3 + 2 * (i - 1);
531
532 g_snprintf(&build_id_file[path_idx], 3, "%02x", bin->build_id[i]);
533 }
534 g_strconcat(build_id_file, BUILD_ID_SUFFIX, NULL);
535
536 path = g_build_path("/", dbg_dir, BUILD_ID_SUBDIR, build_id_file, NULL);
537 if (!path) {
538 goto error;
539 }
540
541 ret = bin_info_set_dwarf_info_from_path(bin, path);
542 if (ret) {
543 goto error;
544 }
545
546 goto end;
547
548 error:
549 ret = -1;
550 end:
551 free(build_id_file);
552 free(path);
553
554 return ret;
555 }
556
557 /**
558 * Tests whether the file located at path exists and has the expected
559 * checksum.
560 *
561 * This predicate is used when looking up separate debug info via the
562 * GNU debuglink method. The expected crc can be found .gnu_debuglink
563 * section in the original ELF file, along with the filename for the
564 * file containing the debug info.
565 *
566 * @param path Full path at which to look for the debug file
567 * @param crc Expected checksum for the debug file
568 * @returns 1 if the file exists and has the correct checksum,
569 * 0 otherwise
570 */
571 static
572 int is_valid_debug_file(struct bin_info *bin, char *path, uint32_t crc)
573 {
574 int ret = 0;
575 struct bt_fd_cache_handle *debug_handle = NULL;
576 uint32_t _crc = 0;
577
578 if (!path) {
579 goto end;
580 }
581
582 debug_handle = bt_fd_cache_get_handle(bin->fd_cache, path);
583 if (!debug_handle) {
584 goto end;
585 }
586
587 ret = crc32(bt_fd_cache_handle_get_fd(debug_handle), &_crc);
588 if (ret) {
589 ret = 0;
590 goto end;
591 }
592
593 ret = (crc == _crc);
594
595 end:
596 bt_fd_cache_put_handle(bin->fd_cache, debug_handle);
597 return ret;
598 }
599
600 /**
601 * Try to set the dwarf_info for a given bin_info instance via the
602 * debug-link method.
603 *
604 * @param bin bin_info instance for which to retrieve the
605 * DWARF info via debug link
606 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
607 */
608 static
609 int bin_info_set_dwarf_info_debug_link(struct bin_info *bin)
610 {
611 int ret = 0;
612 const gchar *dbg_dir = NULL;
613 gchar *bin_dir = NULL, *dir_name = NULL, *path = NULL;
614
615 if (!bin || !bin->dbg_link_filename) {
616 goto error;
617 }
618
619 dbg_dir = bin->debug_info_dir ? bin->debug_info_dir : DEFAULT_DEBUG_DIR;
620 dir_name = g_path_get_dirname(bin->elf_path);
621 if (!dir_name) {
622 goto error;
623 }
624
625 bin_dir = g_strconcat(dir_name, "/", NULL);
626
627 /* First look in the executable's dir */
628 path = g_strconcat(bin_dir, bin->dbg_link_filename, NULL);
629
630 if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) {
631 goto found;
632 }
633
634 /* If not found, look in .debug subdir */
635 g_free(path);
636 path = g_strconcat(bin_dir, DEBUG_SUBDIR, bin->dbg_link_filename, NULL);
637
638 if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) {
639 goto found;
640 }
641
642 /* Lastly, look under the global debug directory */
643 g_free(path);
644
645 path = g_strconcat(dbg_dir, bin_dir, bin->dbg_link_filename, NULL);
646 if (is_valid_debug_file(bin, path, bin->dbg_link_crc)) {
647 goto found;
648 }
649
650 error:
651 ret = -1;
652 end:
653 g_free(dir_name);
654 g_free(path);
655
656 return ret;
657
658 found:
659 ret = bin_info_set_dwarf_info_from_path(bin, path);
660 if (ret) {
661 goto error;
662 }
663
664 goto end;
665 }
666
667 /**
668 * Initialize the DWARF info for a given executable.
669 *
670 * @param bin bin_info instance
671 * @returns 0 on success, negative value on failure
672 */
673 static
674 int bin_info_set_dwarf_info(struct bin_info *bin)
675 {
676 int ret = 0;
677
678 if (!bin) {
679 ret = -1;
680 goto end;
681 }
682
683 /* First try to set the DWARF info from the ELF file */
684 ret = bin_info_set_dwarf_info_from_path(bin, bin->elf_path);
685 if (!ret) {
686 goto end;
687 }
688
689 /*
690 * If that fails, try to find separate debug info via build ID
691 * and debug link.
692 */
693 ret = bin_info_set_dwarf_info_build_id(bin);
694 if (!ret) {
695 goto end;
696 }
697
698 ret = bin_info_set_dwarf_info_debug_link(bin);
699 if (!ret) {
700 goto end;
701 }
702
703 end:
704 return ret;
705 }
706
707 BT_HIDDEN
708 void source_location_destroy(struct source_location *src_loc)
709 {
710 if (!src_loc) {
711 return;
712 }
713
714 free(src_loc->filename);
715 g_free(src_loc);
716 }
717
718 /**
719 * Append a string representation of an address offset to an existing
720 * string.
721 *
722 * On success, the out parameter `result` will contain the base string
723 * followed by the offset string of the form "+0x1234". On failure,
724 * `result` remains unchanged.
725 *
726 * @param base_str The string to which to append an offset string
727 * @param low_addr The lower virtual memory address, the base from
728 * which the offset is computed
729 * @param high_addr The higher virtual memory address
730 * @param result Out parameter, the base string followed by the
731 * offset string
732 * @returns 0 on success, -1 on failure
733 */
734 static
735 int bin_info_append_offset_str(const char *base_str, uint64_t low_addr,
736 uint64_t high_addr, char **result)
737 {
738 uint64_t offset;
739 char *_result = NULL;
740
741
742 if (!base_str || !result) {
743 goto error;
744 }
745
746 offset = high_addr - low_addr;
747
748 _result = g_strdup_printf("%s+%#0" PRIx64, base_str, offset);
749 if (!_result) {
750 goto error;
751 }
752 *result = _result;
753
754 return 0;
755
756 error:
757 free(_result);
758 return -1;
759 }
760
761 /**
762 * Try to find the symbol closest to an address within a given ELF
763 * section.
764 *
765 * Only function symbols are taken into account. The symbol's address
766 * must precede `addr`. A symbol with a closer address might exist
767 * after `addr` but is irrelevant because it cannot encompass `addr`.
768 *
769 * On success, if found, the out parameters `sym` and `shdr` are
770 * set. On failure or if none are found, they remain unchanged.
771 *
772 * @param scn ELF section in which to look for the address
773 * @param addr Virtual memory address for which to find the
774 * nearest function symbol
775 * @param sym Out parameter, the nearest function symbol
776 * @param shdr Out parameter, the section header for scn
777 * @returns 0 on success, -1 on failure
778 */
779 static
780 int bin_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr,
781 GElf_Sym **sym, GElf_Shdr **shdr)
782 {
783 int i;
784 size_t symbol_count;
785 Elf_Data *data = NULL;
786 GElf_Shdr *_shdr = NULL;
787 GElf_Sym *nearest_sym = NULL;
788
789 if (!scn || !sym || !shdr) {
790 goto error;
791 }
792
793 _shdr = g_new0(GElf_Shdr, 1);
794 if (!_shdr) {
795 goto error;
796 }
797
798 _shdr = gelf_getshdr(scn, _shdr);
799 if (!_shdr) {
800 goto error;
801 }
802
803 if (_shdr->sh_type != SHT_SYMTAB) {
804 /*
805 * We are only interested in symbol table (symtab)
806 * sections, skip this one.
807 */
808 goto end;
809 }
810
811 data = elf_getdata(scn, NULL);
812 if (!data) {
813 goto error;
814 }
815
816 symbol_count = _shdr->sh_size / _shdr->sh_entsize;
817
818 for (i = 0; i < symbol_count; ++i) {
819 GElf_Sym *cur_sym = NULL;
820
821 cur_sym = g_new0(GElf_Sym, 1);
822 if (!cur_sym) {
823 goto error;
824 }
825 cur_sym = gelf_getsym(data, i, cur_sym);
826 if (!cur_sym) {
827 goto error;
828 }
829 if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) {
830 /* We're only interested in the functions. */
831 g_free(cur_sym);
832 continue;
833 }
834
835 if (cur_sym->st_value <= addr &&
836 (!nearest_sym ||
837 cur_sym->st_value > nearest_sym->st_value)) {
838 g_free(nearest_sym);
839 nearest_sym = cur_sym;
840 } else {
841 g_free(cur_sym);
842 }
843 }
844
845 end:
846 if (nearest_sym) {
847 *sym = nearest_sym;
848 *shdr = _shdr;
849 } else {
850 g_free(_shdr);
851 }
852
853 return 0;
854
855 error:
856 g_free(nearest_sym);
857 g_free(_shdr);
858 return -1;
859 }
860
861 /**
862 * Get the name of the function containing a given address within an
863 * executable using ELF symbols.
864 *
865 * The function name is in fact the name of the nearest ELF symbol,
866 * followed by the offset in bytes between the address and the symbol
867 * (in hex), separated by a '+' character.
868 *
869 * If found, the out parameter `func_name` is set on success. On failure,
870 * it remains unchanged.
871 *
872 * @param bin bin_info instance for the executable containing
873 * the address
874 * @param addr Virtual memory address for which to find the
875 * function name
876 * @param func_name Out parameter, the function name
877 * @returns 0 on success, -1 on failure
878 */
879 static
880 int bin_info_lookup_elf_function_name(struct bin_info *bin, uint64_t addr,
881 char **func_name)
882 {
883 /*
884 * TODO (possible optimisation): if an ELF has no symtab
885 * section, it has been stripped. Therefore, it would be wise
886 * to store a flag indicating the stripped status after the
887 * first iteration to prevent subsequent ones.
888 */
889 int ret = 0;
890 Elf_Scn *scn = NULL;
891 GElf_Sym *sym = NULL;
892 GElf_Shdr *shdr = NULL;
893 char *sym_name = NULL;
894
895 /* Set ELF file if it hasn't been accessed yet. */
896 if (!bin->elf_file) {
897 ret = bin_info_set_elf_file(bin);
898 if (ret) {
899 /* Failed to set ELF file. */
900 goto error;
901 }
902 }
903
904 scn = elf_nextscn(bin->elf_file, scn);
905 if (!scn) {
906 goto error;
907 }
908
909 while (scn && !sym) {
910 ret = bin_info_get_nearest_symbol_from_section(
911 scn, addr, &sym, &shdr);
912 if (ret) {
913 goto error;
914 }
915
916 scn = elf_nextscn(bin->elf_file, scn);
917 }
918
919 if (sym) {
920 sym_name = elf_strptr(bin->elf_file, shdr->sh_link,
921 sym->st_name);
922 if (!sym_name) {
923 goto error;
924 }
925
926 ret = bin_info_append_offset_str(sym_name, sym->st_value, addr,
927 func_name);
928 if (ret) {
929 goto error;
930 }
931 }
932
933 g_free(shdr);
934 g_free(sym);
935 return 0;
936
937 error:
938 g_free(shdr);
939 g_free(sym);
940 return ret;
941 }
942
943 /**
944 * Get the name of the function containing a given address within a
945 * given compile unit (CU).
946 *
947 * If found, the out parameter `func_name` is set on success. On
948 * failure, it remains unchanged.
949 *
950 * @param cu bt_dwarf_cu instance which may contain the address
951 * @param addr Virtual memory address for which to find the
952 * function name
953 * @param func_name Out parameter, the function name
954 * @returns 0 on success, -1 on failure
955 */
956 static
957 int bin_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr,
958 char **func_name)
959 {
960 int ret = 0;
961 bool found = false;
962 struct bt_dwarf_die *die = NULL;
963
964 if (!cu || !func_name) {
965 goto error;
966 }
967
968 die = bt_dwarf_die_create(cu);
969 if (!die) {
970 goto error;
971 }
972
973 while (bt_dwarf_die_next(die) == 0) {
974 int tag;
975
976 ret = bt_dwarf_die_get_tag(die, &tag);
977 if (ret) {
978 goto error;
979 }
980
981 if (tag == DW_TAG_subprogram) {
982 ret = bt_dwarf_die_contains_addr(die, addr, &found);
983 if (ret) {
984 goto error;
985 }
986
987 if (found) {
988 break;
989 }
990 }
991 }
992
993 if (found) {
994 uint64_t low_addr = 0;
995 char *die_name = NULL;
996
997 ret = bt_dwarf_die_get_name(die, &die_name);
998 if (ret) {
999 goto error;
1000 }
1001
1002 ret = dwarf_lowpc(die->dwarf_die, &low_addr);
1003 if (ret) {
1004 free(die_name);
1005 goto error;
1006 }
1007
1008 ret = bin_info_append_offset_str(die_name, low_addr, addr,
1009 func_name);
1010 free(die_name);
1011 if (ret) {
1012 goto error;
1013 }
1014 }
1015
1016 bt_dwarf_die_destroy(die);
1017 return 0;
1018
1019 error:
1020 bt_dwarf_die_destroy(die);
1021 return -1;
1022 }
1023
1024 /**
1025 * Get the name of the function containing a given address within an
1026 * executable using DWARF debug info.
1027 *
1028 * If found, the out parameter `func_name` is set on success. On
1029 * failure, it remains unchanged.
1030 *
1031 * @param bin bin_info instance for the executable containing
1032 * the address
1033 * @param addr Virtual memory address for which to find the
1034 * function name
1035 * @param func_name Out parameter, the function name
1036 * @returns 0 on success, -1 on failure
1037 */
1038 static
1039 int bin_info_lookup_dwarf_function_name(struct bin_info *bin, uint64_t addr,
1040 char **func_name)
1041 {
1042 int ret = 0;
1043 char *_func_name = NULL;
1044 struct bt_dwarf_cu *cu = NULL;
1045
1046 if (!bin || !func_name) {
1047 goto error;
1048 }
1049
1050 cu = bt_dwarf_cu_create(bin->dwarf_info);
1051 if (!cu) {
1052 goto error;
1053 }
1054
1055 while (bt_dwarf_cu_next(cu) == 0) {
1056 ret = bin_info_lookup_cu_function_name(cu, addr, &_func_name);
1057 if (ret) {
1058 goto error;
1059 }
1060
1061 if (_func_name) {
1062 break;
1063 }
1064 }
1065
1066 if (_func_name) {
1067 *func_name = _func_name;
1068 } else {
1069 goto error;
1070 }
1071
1072 bt_dwarf_cu_destroy(cu);
1073 return 0;
1074
1075 error:
1076 bt_dwarf_cu_destroy(cu);
1077 return -1;
1078 }
1079
1080 BT_HIDDEN
1081 int bin_info_lookup_function_name(struct bin_info *bin,
1082 uint64_t addr, char **func_name)
1083 {
1084 int ret = 0;
1085 char *_func_name = NULL;
1086
1087 if (!bin || !func_name) {
1088 goto error;
1089 }
1090
1091 /*
1092 * If the bin_info has a build id but it does not match the build id
1093 * that was found on the file system, return an error.
1094 */
1095 if (bin->build_id && !bin->file_build_id_matches) {
1096 goto error;
1097 }
1098
1099 /* Set DWARF info if it hasn't been accessed yet. */
1100 if (!bin->dwarf_info && !bin->is_elf_only) {
1101 ret = bin_info_set_dwarf_info(bin);
1102 if (ret) {
1103 BT_LOGD_STR("Failed to set bin dwarf info, falling "
1104 "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 ret = bt_dwarf_die_contains_addr(die, addr, &_contains);
1222 if (ret) {
1223 goto error;
1224 }
1225
1226 if (_contains) {
1227 /*
1228 * The address is within the range of the current DIE
1229 * or its children.
1230 */
1231 int tag;
1232
1233 ret = bt_dwarf_die_get_tag(die, &tag);
1234 if (ret) {
1235 goto error;
1236 }
1237
1238 if (tag == DW_TAG_inlined_subroutine) {
1239 /* Found the tracepoint. */
1240 goto end;
1241 }
1242
1243 if (bt_dwarf_die_has_children(die)) {
1244 /*
1245 * Look for the address in the children DIEs.
1246 */
1247 ret = bt_dwarf_die_child(die);
1248 if (ret) {
1249 goto error;
1250 }
1251 }
1252 }
1253 } while (bt_dwarf_die_next(die) == 0);
1254
1255 end:
1256 *contains = _contains;
1257 return 0;
1258
1259 error:
1260 return -1;
1261 }
1262
1263 /**
1264 * Lookup the source location for a given address within a CU, making
1265 * the assumption that it is contained within an inline routine in a
1266 * function.
1267 *
1268 * @param cu bt_dwarf_cu instance in which to look for the address
1269 * @param addr The address for which to look for
1270 * @param src_loc Out parameter, the source location (filename and
1271 * line number) for the address
1272 * @returns 0 on success, -1 on failure
1273 */
1274 static
1275 int bin_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr,
1276 struct source_location **src_loc)
1277 {
1278 int ret = 0;
1279 bool found = false;
1280 struct bt_dwarf_die *die = NULL;
1281 struct source_location *_src_loc = NULL;
1282
1283 if (!cu || !src_loc) {
1284 goto error;
1285 }
1286
1287 die = bt_dwarf_die_create(cu);
1288 if (!die) {
1289 goto error;
1290 }
1291
1292 while (bt_dwarf_die_next(die) == 0) {
1293 int tag;
1294
1295 ret = bt_dwarf_die_get_tag(die, &tag);
1296 if (ret) {
1297 goto error;
1298 }
1299
1300 if (tag == DW_TAG_subprogram) {
1301 bool contains = false;
1302
1303 ret = bt_dwarf_die_contains_addr(die, addr, &contains);
1304 if (ret) {
1305 goto error;
1306 }
1307
1308 if (contains) {
1309 /*
1310 * Try to find an inlined subroutine
1311 * child of this DIE containing addr.
1312 */
1313 ret = bin_info_child_die_has_address(die, addr,
1314 &found);
1315 if(ret) {
1316 goto error;
1317 }
1318
1319 goto end;
1320 }
1321 }
1322 }
1323
1324 end:
1325 if (found) {
1326 char *filename = NULL;
1327 uint64_t line_no;
1328
1329 _src_loc = g_new0(struct source_location, 1);
1330 if (!_src_loc) {
1331 goto error;
1332 }
1333
1334 ret = bt_dwarf_die_get_call_file(die, &filename);
1335 if (ret) {
1336 goto error;
1337 }
1338 ret = bt_dwarf_die_get_call_line(die, &line_no);
1339 if (ret) {
1340 free(filename);
1341 goto error;
1342 }
1343
1344 _src_loc->filename = filename;
1345 _src_loc->line_no = line_no;
1346 *src_loc = _src_loc;
1347 }
1348
1349 bt_dwarf_die_destroy(die);
1350 return 0;
1351
1352 error:
1353 source_location_destroy(_src_loc);
1354 bt_dwarf_die_destroy(die);
1355 return -1;
1356 }
1357
1358 /**
1359 * Lookup the source location for a given address within a CU,
1360 * assuming that it is contained within an inlined function.
1361 *
1362 * A source location can be found regardless of inlining status for
1363 * this method, but in the case of an inlined function, the returned
1364 * source location will point not to the callsite but rather to the
1365 * definition site of the inline function.
1366 *
1367 * @param cu bt_dwarf_cu instance in which to look for the address
1368 * @param addr The address for which to look for
1369 * @param src_loc Out parameter, the source location (filename and
1370 * line number) for the address
1371 * @returns 0 on success, -1 on failure
1372 */
1373 static
1374 int bin_info_lookup_cu_src_loc_no_inl(struct bt_dwarf_cu *cu, uint64_t addr,
1375 struct source_location **src_loc)
1376 {
1377 struct source_location *_src_loc = NULL;
1378 struct bt_dwarf_die *die = NULL;
1379 const char *filename = NULL;
1380 Dwarf_Line *line = NULL;
1381 Dwarf_Addr line_addr;
1382 int ret, line_no;
1383
1384 if (!cu || !src_loc) {
1385 goto error;
1386 }
1387
1388 die = bt_dwarf_die_create(cu);
1389 if (!die) {
1390 goto error;
1391 }
1392
1393 line = dwarf_getsrc_die(die->dwarf_die, addr);
1394 if (!line) {
1395 goto error;
1396 }
1397
1398 ret = dwarf_lineaddr(line, &line_addr);
1399 if (ret) {
1400 goto error;
1401 }
1402
1403 filename = dwarf_linesrc(line, NULL, NULL);
1404 if (!filename) {
1405 goto error;
1406 }
1407
1408 if (addr == line_addr) {
1409 _src_loc = g_new0(struct source_location, 1);
1410 if (!_src_loc) {
1411 goto error;
1412 }
1413
1414 ret = dwarf_lineno(line, &line_no);
1415 if (ret) {
1416 goto error;
1417 }
1418
1419 _src_loc->line_no = line_no;
1420 _src_loc->filename = g_strdup(filename);
1421 }
1422
1423 bt_dwarf_die_destroy(die);
1424
1425 if (_src_loc) {
1426 *src_loc = _src_loc;
1427 }
1428
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.061827 seconds and 4 git commands to generate.