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