1 /* Copyright (c) 2017 Google Inc.
2 * See LICENSE for details.
4 * Utility functions. */
6 #include <parlib/stdio.h>
11 #include <sys/fcntl.h>
12 #include <sys/types.h>
17 ssize_t cat(char *filename, void *where, size_t memsize)
23 fd = open(filename, O_RDONLY);
25 fprintf(stderr, "Can't open %s: %r\n", filename);
29 if (fstat(fd, &buf) < 0) {
30 fprintf(stderr, "Can't stat %s: %r\n", filename);
35 if (buf.st_size > memsize) {
37 "file is %d bytes, but we only have %d bytes to place it\n",
38 buf.st_size, memsize);
44 while (tot < buf.st_size) {
45 amt = read(fd, where, buf.st_size - tot);
60 void backtrace_guest_thread(FILE *f, struct guest_thread *vm_thread)
62 struct vm_trapframe *vm_tf = gth_to_vmtf(vm_thread);
63 uintptr_t guest_pc, guest_fp, host_fp;
66 guest_pc = vm_tf->tf_rip;
67 guest_fp = vm_tf->tf_rbp;
69 /* The BT should work for any code using frame pointers. Most of the
70 * time, this will be vmlinux, and calling it that helps our backtrace.
71 * This spits out the format that is parsed by bt-akaros.sh. */
72 fprintf(f, "Backtracing guest, vmlinux is assumed, but check addrs\n");
73 for (int i = 1; i < 30; i++) {
74 fprintf(f, "#%02d Addr %p is in vmlinux at offset %p\n", i,
78 if (gva2gpa(vm_thread, guest_fp, &host_fp))
80 memcpy(frame, (void*)host_fp, 2 * sizeof(uintptr_t));