1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include <libunwind.h>
12 #include <libunwind-ptrace.h>
14 #include "src/mc/mc_unw.h"
19 * Libunwind namespace implementation using process_vm_readv.
24 /** Partial structure of libunwind-ptrace context in order to get the PID
26 * The context type for libunwind-race is an opaque type. We need to get the
27 * PID which is the first field. This is a hack which might break if the
28 * libunwind-ptrace structure changes.
35 /** Get the PID of a `libunwind-ptrace` context
38 pid_t _UPT_getpid(void* arg)
40 struct _UPT_info* info = (_UPT_info*) arg;
44 /** Read from the memory, avoid using `ptrace` (libunwind method)
46 static int access_mem(const unw_addr_space_t as,
47 const unw_word_t addr, unw_word_t* const valp,
48 const int write, void* const arg)
52 pid_t pid = _UPT_getpid(arg);
53 size_t size = sizeof(unw_word_t);
55 #if HAVE_PROCESS_VM_READV
56 // process_vm_read implementation.
57 // This is only available since Linux 3.2.
59 struct iovec local = { valp, size };
60 struct iovec remote = { (void*) addr, size };
61 ssize_t s = process_vm_readv(pid, &local, 1, &remote, 1, 0);
63 if ((size_t) s != size)
68 if (s < 0 && errno != ENOSYS)
72 // /proc/${pid}/mem implementation.
73 // On recent kernels, we do not need to ptrace the target process.
74 // On older kernels, it is necessary to ptrace the target process.
76 off_t off = (off_t) addr;
77 char* buf = (char*) valp;
78 int fd = simgrid::mc::open_vm(pid, O_RDONLY);
82 ssize_t s = pread(fd, buf, count, off);
99 // ptrace implementation.
100 // We need to have PTRACE_ATTACH-ed it before.
101 return _UPT_access_mem(as, addr, valp, write, arg);
104 unw_accessors_t mc_unw_vmread_accessors =
106 &_UPT_find_proc_info,
107 &_UPT_put_unwind_info,
108 &_UPT_get_dyn_info_list_addr,