Add --debug-info-target-prefix
[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, dbg_dir_trailing_slash = 0;
259 char *path = NULL, *build_id_file = NULL;
260 const char *dbg_dir = NULL;
261 size_t build_id_file_len, path_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 dbg_dir_trailing_slash = dbg_dir[strlen(dbg_dir) - 1] == '/';
270
271 /* 2 characters per byte printed in hex, +2 for '/' and '\0' */
272 build_id_file_len = (2 * so->build_id_len) + 2;
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
285 path_len = strlen(dbg_dir) + strlen(BUILD_ID_SUBDIR) +
286 strlen(build_id_file) + strlen(BUILD_ID_SUFFIX) + 1;
287 if (!dbg_dir_trailing_slash) {
288 path_len += 1;
289 }
290
291 path = malloc(path_len);
292 if (!path) {
293 goto error;
294 }
295
296 strcpy(path, dbg_dir);
297 if (!dbg_dir_trailing_slash) {
298 strcat(path, "/");
299 }
300 strcat(path, BUILD_ID_SUBDIR);
301 strcat(path, build_id_file);
302 strcat(path, BUILD_ID_SUFFIX);
303
304 ret = so_info_set_dwarf_info_from_path(so, path);
305 if (ret) {
306 goto error;
307 }
308
309 goto end;
310
311 error:
312 ret = -1;
313 end:
314 free(build_id_file);
315 free(path);
316
317 return ret;
318 }
319
320 /**
321 * Tests whether the file located at path exists and has the expected
322 * checksum.
323 *
324 * This predicate is used when looking up separate debug info via the
325 * GNU debuglink method. The expected crc can be found .gnu_debuglink
326 * section in the original ELF file, along with the filename for the
327 * file containing the debug info.
328 *
329 * @param path Full path at which to look for the debug file
330 * @param crc Expected checksum for the debug file
331 * @returns 1 if the file exists and has the correct checksum,
332 * 0 otherwise
333 */
334 static
335 int is_valid_debug_file(char *path, uint32_t crc)
336 {
337 int ret = 0, fd = -1;
338 uint32_t _crc = 0;
339
340 if (!path) {
341 goto end;
342 }
343
344 fd = open(path, O_RDONLY);
345 if (fd < 0) {
346 goto end;
347 }
348
349 ret = crc32(fd, &_crc);
350 if (ret) {
351 ret = 0;
352 goto end;
353 }
354
355 ret = (crc == _crc);
356
357 end:
358 close(fd);
359 return ret;
360 }
361
362 /**
363 * Try to set the dwarf_info for a given so_info instance via the
364 * build ID method.
365 *
366 * @param so so_info instance for which to retrieve the
367 * DWARF info via debug link
368 * @returns 0 on success (i.e. dwarf_info set), -1 on failure
369 */
370 static
371 int so_info_set_dwarf_info_debug_link(struct so_info *so)
372 {
373 int ret = 0;
374 const char *dbg_dir = NULL;
375 char *dir_name = NULL, *so_dir = NULL, *path = NULL;
376 size_t max_path_len = 0;
377
378 if (!so || !so->dbg_link_filename) {
379 goto error;
380 }
381
382 dbg_dir = opt_debug_info_dir ? : DEFAULT_DEBUG_DIR;
383
384 dir_name = dirname(so->elf_path);
385 if (!dir_name) {
386 goto error;
387 }
388
389 /* so_dir is just dir_name with a trailing slash */
390 so_dir = malloc(strlen(dir_name) + 2);
391 if (!so_dir) {
392 goto error;
393 }
394
395 strcpy(so_dir, dir_name);
396 strcat(so_dir, "/");
397
398 max_path_len = strlen(dbg_dir) + strlen(so_dir) +
399 strlen(DEBUG_SUBDIR) + strlen(so->dbg_link_filename)
400 + 1;
401 path = malloc(max_path_len);
402 if (!path) {
403 goto error;
404 }
405
406 /* First look in the SO's dir */
407 strcpy(path, so_dir);
408 strcat(path, so->dbg_link_filename);
409
410 if (is_valid_debug_file(path, so->dbg_link_crc)) {
411 goto found;
412 }
413
414 /* If not found, look in .debug subdir */
415 strcpy(path, so_dir);
416 strcat(path, DEBUG_SUBDIR);
417 strcat(path, so->dbg_link_filename);
418
419 if (is_valid_debug_file(path, so->dbg_link_crc)) {
420 goto found;
421 }
422
423 /* Lastly, look under the global debug directory */
424 strcpy(path, dbg_dir);
425 strcat(path, so_dir);
426 strcat(path, so->dbg_link_filename);
427
428 if (is_valid_debug_file(path, so->dbg_link_crc)) {
429 goto found;
430 }
431
432 error:
433 ret = -1;
434 end:
435 free(path);
436 free(so_dir);
437
438 return ret;
439
440 found:
441 ret = so_info_set_dwarf_info_from_path(so, path);
442 if (ret) {
443 goto error;
444 }
445
446 goto end;
447 }
448
449 /**
450 * Initialize the DWARF info for a given executable.
451 *
452 * @param so so_info instance
453 * @returns 0 on success, -1 on failure
454 */
455 static
456 int so_info_set_dwarf_info(struct so_info *so)
457 {
458 int ret = 0;
459
460 if (!so) {
461 goto error;
462 }
463
464 /* First try to set the DWARF info from the ELF file */
465 ret = so_info_set_dwarf_info_from_path(so, so->elf_path);
466 if (!ret) {
467 goto end;
468 }
469
470 /*
471 * If that fails, try to find separate debug info via build ID
472 * and debug link.
473 */
474 ret = so_info_set_dwarf_info_build_id(so);
475 if (!ret) {
476 goto end;
477 }
478
479 ret = so_info_set_dwarf_info_debug_link(so);
480 if (!ret) {
481 goto end;
482 }
483
484 error:
485 ret = -1;
486 end:
487 return ret;
488 }
489
490 /**
491 * Initialize the ELF file for a given executable.
492 *
493 * @param so so_info instance
494 * @returns 0 on success, -1 on failure
495 */
496 static
497 int so_info_set_elf_file(struct so_info *so)
498 {
499 int elf_fd;
500 Elf *elf_file = NULL;
501
502 if (!so) {
503 goto error;
504 }
505
506 elf_fd = open(so->elf_path, O_RDONLY);
507 if (elf_fd < 0) {
508 fprintf(stderr, "Failed to open %s\n", so->elf_path);
509 goto error;
510 }
511
512 elf_file = elf_begin(elf_fd, ELF_C_READ, NULL);
513 if (!elf_file) {
514 fprintf(stderr, "elf_begin failed: %s\n", elf_errmsg(-1));
515 goto error;
516 }
517
518 if (elf_kind(elf_file) != ELF_K_ELF) {
519 fprintf(stderr, "Error: %s is not an ELF object\n",
520 so->elf_path);
521 goto error;
522 }
523
524 so->elf_fd = elf_fd;
525 so->elf_file = elf_file;
526 return 0;
527
528 error:
529 close(elf_fd);
530 elf_end(elf_file);
531 return -1;
532 }
533
534
535 BT_HIDDEN
536 void source_location_destroy(struct source_location *src_loc)
537 {
538 if (!src_loc) {
539 return;
540 }
541
542 free(src_loc->filename);
543 g_free(src_loc);
544 }
545 /**
546 * Append a string representation of an address offset to an existing
547 * string.
548 *
549 * On success, the out parameter `result` will contain the base string
550 * followed by the offset string of the form "+0x1234". On failure,
551 * `result` remains unchanged.
552 *
553 * @param base_str The string to which to append an offset string
554 * @param low_addr The lower virtual memory address, the base from
555 * which the offset is computed
556 * @param high_addr The higher virtual memory address
557 * @param result Out parameter, the base string followed by the
558 * offset string
559 * @returns 0 on success, -1 on failure
560 */
561 static
562 int so_info_append_offset_str(const char *base_str, uint64_t low_addr,
563 uint64_t high_addr, char **result)
564 {
565 int ret;
566 uint64_t offset;
567 char *_result = NULL;
568 char offset_str[ADDR_STR_LEN];
569
570 if (!base_str || !result) {
571 goto error;
572 }
573
574 offset = high_addr - low_addr;
575
576 _result = malloc(strlen(base_str) + ADDR_STR_LEN);
577 if (!_result) {
578 goto error;
579 }
580
581 ret = snprintf(offset_str, ADDR_STR_LEN, "+%#0" PRIx64, offset);
582 if (ret < 0) {
583 goto error;
584 }
585 strcpy(_result, base_str);
586 strcat(_result, offset_str);
587 *result = _result;
588
589 return 0;
590
591 error:
592 free(_result);
593 return -1;
594 }
595
596 /**
597 * Try to find the symbol closest to an address within a given ELF
598 * section.
599 *
600 * Only function symbols are taken into account. The symbol's address
601 * must precede `addr`. A symbol with a closer address might exist
602 * after `addr` but is irrelevant because it cannot encompass `addr`.
603 *
604 * On success, if found, the out parameters `sym` and `shdr` are
605 * set. On failure or if none are found, they remain unchanged.
606 *
607 * @param scn ELF section in which to look for the address
608 * @param addr Virtual memory address for which to find the
609 * nearest function symbol
610 * @param sym Out parameter, the nearest function symbol
611 * @param shdr Out parameter, the section header for scn
612 * @returns 0 on success, -1 on failure
613 */
614 static
615 int so_info_get_nearest_symbol_from_section(Elf_Scn *scn, uint64_t addr,
616 GElf_Sym **sym, GElf_Shdr **shdr)
617 {
618 int i;
619 size_t symbol_count;
620 Elf_Data *data = NULL;
621 GElf_Shdr *_shdr = NULL;
622 GElf_Sym *nearest_sym = NULL;
623
624 if (!scn || !sym || !shdr) {
625 goto error;
626 }
627
628 _shdr = g_new0(GElf_Shdr, 1);
629 if (!_shdr) {
630 goto error;
631 }
632
633 _shdr = gelf_getshdr(scn, _shdr);
634 if (!_shdr) {
635 goto error;
636 }
637
638 if (_shdr->sh_type != SHT_SYMTAB) {
639 /*
640 * We are only interested in symbol table (symtab)
641 * sections, skip this one.
642 */
643 goto end;
644 }
645
646 data = elf_getdata(scn, NULL);
647 if (!data) {
648 goto error;
649 }
650
651 symbol_count = _shdr->sh_size / _shdr->sh_entsize;
652
653 for (i = 0; i < symbol_count; ++i) {
654 GElf_Sym *cur_sym = NULL;
655
656 cur_sym = g_new0(GElf_Sym, 1);
657 if (!cur_sym) {
658 goto error;
659 }
660 cur_sym = gelf_getsym(data, i, cur_sym);
661 if (!cur_sym) {
662 goto error;
663 }
664 if (GELF_ST_TYPE(cur_sym->st_info) != STT_FUNC) {
665 /* We're only interested in the functions. */
666 g_free(cur_sym);
667 continue;
668 }
669
670 if (cur_sym->st_value <= addr &&
671 (!nearest_sym ||
672 cur_sym->st_value > nearest_sym->st_value)) {
673 g_free(nearest_sym);
674 nearest_sym = cur_sym;
675 } else {
676 g_free(cur_sym);
677 }
678 }
679
680 end:
681 if (nearest_sym) {
682 *sym = nearest_sym;
683 *shdr = _shdr;
684 } else {
685 g_free(_shdr);
686 }
687
688 return 0;
689
690 error:
691 g_free(nearest_sym);
692 g_free(_shdr);
693 return -1;
694 }
695
696 /**
697 * Get the name of the function containing a given address within an
698 * executable using ELF symbols.
699 *
700 * The function name is in fact the name of the nearest ELF symbol,
701 * followed by the offset in bytes between the address and the symbol
702 * (in hex), separated by a '+' character.
703 *
704 * If found, the out parameter `func_name` is set on success. On failure,
705 * it remains unchanged.
706 *
707 * @param so so_info instance for the executable containing
708 * the address
709 * @param addr Virtual memory address for which to find the
710 * function name
711 * @param func_name Out parameter, the function name
712 * @returns 0 on success, -1 on failure
713 */
714 static
715 int so_info_lookup_elf_function_name(struct so_info *so, uint64_t addr,
716 char **func_name)
717 {
718 /*
719 * TODO (possible optimisation): if an ELF has no symtab
720 * section, it has been stripped. Therefore, it would be wise
721 * to store a flag indicating the stripped status after the
722 * first iteration to prevent subsequent ones.
723 */
724 int ret = 0;
725 Elf_Scn *scn = NULL;
726 GElf_Sym *sym = NULL;
727 GElf_Shdr *shdr = NULL;
728 char *sym_name = NULL;
729
730 /* Set ELF file if it hasn't been accessed yet. */
731 if (!so->elf_file) {
732 ret = so_info_set_elf_file(so);
733 if (ret) {
734 /* Failed to set ELF file. */
735 goto error;
736 }
737 }
738
739 scn = elf_nextscn(so->elf_file, scn);
740 if (!scn) {
741 goto error;
742 }
743
744 while (scn && !sym) {
745 ret = so_info_get_nearest_symbol_from_section(
746 scn, addr, &sym, &shdr);
747 if (ret) {
748 goto error;
749 }
750
751 scn = elf_nextscn(so->elf_file, scn);
752 }
753
754 if (sym) {
755 sym_name = elf_strptr(so->elf_file, shdr->sh_link,
756 sym->st_name);
757 if (!sym_name) {
758 goto error;
759 }
760
761 ret = so_info_append_offset_str(sym_name, sym->st_value, addr,
762 func_name);
763 if (ret) {
764 goto error;
765 }
766 }
767
768 g_free(shdr);
769 g_free(sym);
770 return 0;
771
772 error:
773 g_free(shdr);
774 g_free(sym);
775 return -1;
776 }
777
778 /**
779 * Get the name of the function containing a given address within a
780 * given compile unit (CU).
781 *
782 * If found, the out parameter `func_name` is set on success. On
783 * failure, it remains unchanged.
784 *
785 * @param cu bt_dwarf_cu instance which may contain the address
786 * @param addr Virtual memory address for which to find the
787 * function name
788 * @param func_name Out parameter, the function name
789 * @returns 0 on success, -1 on failure
790 */
791 static
792 int so_info_lookup_cu_function_name(struct bt_dwarf_cu *cu, uint64_t addr,
793 char **func_name)
794 {
795 int ret = 0, found = 0;
796 struct bt_dwarf_die *die = NULL;
797
798 if (!cu || !func_name) {
799 goto error;
800 }
801
802 die = bt_dwarf_die_create(cu);
803 if (!die) {
804 goto error;
805 }
806
807 while (bt_dwarf_die_next(die) == 0) {
808 int tag;
809
810 ret = bt_dwarf_die_get_tag(die, &tag);
811 if (ret) {
812 goto error;
813 }
814
815 if (tag == DW_TAG_subprogram) {
816 ret = bt_dwarf_die_contains_addr(die, addr, &found);
817 if (ret) {
818 goto error;
819 }
820
821 if (found) {
822 break;
823 }
824 }
825 }
826
827 if (found) {
828 uint64_t low_addr = 0;
829 char *die_name = NULL;
830
831 ret = bt_dwarf_die_get_name(die, &die_name);
832 if (ret) {
833 goto error;
834 }
835
836 ret = dwarf_lowpc(die->dwarf_die, &low_addr);
837 if (ret) {
838 goto error;
839 }
840
841 ret = so_info_append_offset_str(die_name, low_addr, addr,
842 func_name);
843 if (ret) {
844 goto error;
845 }
846 }
847
848 bt_dwarf_die_destroy(die);
849 return 0;
850
851 error:
852 bt_dwarf_die_destroy(die);
853 return -1;
854 }
855
856 /**
857 * Get the name of the function containing a given address within an
858 * executable using DWARF debug info.
859 *
860 * If found, the out parameter `func_name` is set on success. On
861 * failure, it remains unchanged.
862 *
863 * @param so so_info instance for the executable containing
864 * the address
865 * @param addr Virtual memory address for which to find the
866 * function name
867 * @param func_name Out parameter, the function name
868 * @returns 0 on success, -1 on failure
869 */
870 static
871 int so_info_lookup_dwarf_function_name(struct so_info *so, uint64_t addr,
872 char **func_name)
873 {
874 int ret = 0;
875 char *_func_name = NULL;
876 struct bt_dwarf_cu *cu = NULL;
877
878 if (!so || !func_name) {
879 goto error;
880 }
881
882 cu = bt_dwarf_cu_create(so->dwarf_info);
883 if (!cu) {
884 goto error;
885 }
886
887 while (bt_dwarf_cu_next(cu) == 0) {
888 ret = so_info_lookup_cu_function_name(cu, addr, &_func_name);
889 if (ret) {
890 goto error;
891 }
892
893 if (_func_name) {
894 break;
895 }
896 }
897
898 if (_func_name) {
899 *func_name = _func_name;
900 }
901
902 bt_dwarf_cu_destroy(cu);
903 return 0;
904
905 error:
906 bt_dwarf_cu_destroy(cu);
907 return -1;
908 }
909
910 BT_HIDDEN
911 int so_info_lookup_function_name(struct so_info *so, uint64_t addr,
912 char **func_name)
913 {
914 int ret = 0;
915 char *_func_name = NULL;
916
917 if (!so || !func_name) {
918 goto error;
919 }
920
921 /* Set DWARF info if it hasn't been accessed yet. */
922 if (!so->dwarf_info && !so->is_elf_only) {
923 ret = so_info_set_dwarf_info(so);
924 if (ret) {
925 /* Failed to set DWARF info, fallback to ELF. */
926 so->is_elf_only = true;
927 }
928 }
929
930 if (!so_info_has_address(so, addr)) {
931 goto error;
932 }
933
934 /*
935 * Addresses in ELF and DWARF are relative to base address for
936 * PIC, so make the address argument relative too if needed.
937 */
938 if (so->is_pic) {
939 addr -= so->low_addr;
940 }
941
942 if (so->is_elf_only) {
943 ret = so_info_lookup_elf_function_name(so, addr, &_func_name);
944 } else {
945 ret = so_info_lookup_dwarf_function_name(so, addr, &_func_name);
946 }
947
948 if (ret || !_func_name) {
949 goto error;
950 }
951
952 *func_name = _func_name;
953 return 0;
954
955 error:
956 return -1;
957 }
958
959 BT_HIDDEN
960 int so_info_get_bin_loc(struct so_info *so, uint64_t addr, char **bin_loc)
961 {
962 int ret = 0;
963 char *_bin_loc = NULL;
964
965 if (!so || !bin_loc) {
966 goto error;
967 }
968
969 if (so->is_pic) {
970 addr -= so->low_addr;
971 ret = asprintf(&_bin_loc, "+%#0" PRIx64, addr);
972 } else {
973 ret = asprintf(&_bin_loc, "@%#0" PRIx64, addr);
974 }
975
976 if (ret == -1 || !_bin_loc) {
977 goto error;
978 }
979
980 *bin_loc = _bin_loc;
981 return 0;
982
983 error:
984 return -1;
985 }
986
987 /**
988 * Predicate used to determine whether the children of a given DIE
989 * contain a specific address.
990 *
991 * More specifically, the parameter `die` is expected to be a
992 * subprogram (function) DIE, and this predicate tells whether any
993 * subroutines are inlined within this function and would contain
994 * `addr`.
995 *
996 * Do note that this function advances the position of `die`. If the
997 * address is found within one of its children, `die` will be pointing
998 * to that child upon returning from the function, allowing to extract
999 * the information deemed necessary.
1000 *
1001 * @param die The parent DIE in whose children the address will be
1002 * looked for
1003 * @param addr The address for which to look for in the DIEs
1004 * @returns Returns 1 if the address was found, 0 if not
1005 */
1006 static
1007 int so_info_child_die_has_address(struct bt_dwarf_die *die, uint64_t addr)
1008 {
1009 int ret = 0, contains = 0;
1010
1011 if (!die) {
1012 goto error;
1013 }
1014
1015 ret = bt_dwarf_die_child(die);
1016 if (ret) {
1017 goto error;
1018 }
1019
1020 do {
1021 int tag;
1022
1023 ret = bt_dwarf_die_get_tag(die, &tag);
1024 if (ret) {
1025 goto error;
1026 }
1027
1028 if (tag == DW_TAG_inlined_subroutine) {
1029 ret = bt_dwarf_die_contains_addr(die, addr, &contains);
1030 if (ret) {
1031 goto error;
1032 }
1033
1034 if (contains) {
1035 ret = 1;
1036 goto end;
1037 }
1038 }
1039 } while (bt_dwarf_die_next(die) == 0);
1040
1041 end:
1042 return ret;
1043
1044 error:
1045 ret = 0;
1046 goto end;
1047 }
1048
1049 /**
1050 * Lookup the source location for a given address within a CU, making
1051 * the assumption that it is contained within an inline routine in a
1052 * function.
1053 *
1054 * @param cu bt_dwarf_cu instance in which to look for the address
1055 * @param addr The address for which to look for
1056 * @param src_loc Out parameter, the source location (filename and
1057 * line number) for the address
1058 * @returns 0 on success, -1 on failure
1059 */
1060 static
1061 int so_info_lookup_cu_src_loc_inl(struct bt_dwarf_cu *cu, uint64_t addr,
1062 struct source_location **src_loc)
1063 {
1064 int ret = 0, found = 0;
1065 struct bt_dwarf_die *die = NULL;
1066 struct source_location *_src_loc = NULL;
1067
1068 if (!cu || !src_loc) {
1069 goto error;
1070 }
1071
1072 die = bt_dwarf_die_create(cu);
1073 if (!die) {
1074 goto error;
1075 }
1076
1077 while (bt_dwarf_die_next(die) == 0) {
1078 int tag;
1079
1080 ret = bt_dwarf_die_get_tag(die, &tag);
1081 if (ret) {
1082 goto error;
1083 }
1084
1085 if (tag == DW_TAG_subprogram) {
1086 int contains = 0;
1087
1088 ret = bt_dwarf_die_contains_addr(die, addr, &contains);
1089 if (ret) {
1090 goto error;
1091 }
1092
1093 if (contains) {
1094 /*
1095 * Try to find an inlined subroutine
1096 * child of this DIE containing addr.
1097 */
1098 found = so_info_child_die_has_address(
1099 die, addr);
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 so_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 so 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 so_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 = so_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 = so_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 so_info_lookup_source_location(struct so_info *so, 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 (!so || !src_loc) {
1281 goto error;
1282 }
1283
1284 /* Set DWARF info if it hasn't been accessed yet. */
1285 if (!so->dwarf_info && !so->is_elf_only) {
1286 if (so_info_set_dwarf_info(so)) {
1287 /* Failed to set DWARF info. */
1288 so->is_elf_only = true;
1289 }
1290 }
1291
1292 if (so->is_elf_only) {
1293 /* We cannot lookup source location without DWARF info. */
1294 goto error;
1295 }
1296
1297 if (!so_info_has_address(so, 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 (so->is_pic) {
1306 addr -= so->low_addr;
1307 }
1308
1309 cu = bt_dwarf_cu_create(so->dwarf_info);
1310 if (!cu) {
1311 goto error;
1312 }
1313
1314 while (bt_dwarf_cu_next(cu) == 0) {
1315 int ret;
1316
1317 ret = so_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.059803 seconds and 4 git commands to generate.