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