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