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