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