Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remote unwinding support
[simgrid.git] / src / mc / mc_unw_vmread.c
1 #define _GNU_SOURCE
2
3 #include <sys/types.h>
4 #include <sys/uio.h>
5
6 #include <libunwind.h>
7 #include <libunwind-ptrace.h>
8
9 #include "mc_unw.h"
10
11 /** \file
12  *  Libunwind namespace implementation using process_vm_readv.
13  *.
14  *  This implem
15  */
16
17 /** Partial structure of libunwind-ptrace context in order to get the PID
18  *
19  *  The context type for libunwind-race is an opaque type. We need to get the
20  *  PID which is the first field. This is a hack which might break if the
21  *  libunwind-ptrace structure changes.
22  */
23 struct _UPT_info {
24   pid_t pid;
25   // Other things;
26 };
27
28 /** Get the PID of a `libunwind-ptrace` context
29  */
30 static inline
31 pid_t _UPT_getpid(void* arg)
32 {
33   struct _UPT_info* info = arg;
34   return info->pid;
35 }
36
37 /** Read from the memory, avoid using `ptrace` (libunwind method)
38  */
39 static int access_mem(const unw_addr_space_t as,
40               const unw_word_t addr, unw_word_t* const  valp,
41               const int write, void* const arg)
42 {
43   if (write)
44     return - UNW_EINVAL;
45   ssize_t s;
46   pid_t pid = _UPT_getpid(arg);
47   size_t size = sizeof(unw_word_t);
48
49 #ifdef HAVE_PROCESS_VM_READV
50   // process_vm_read implementation.
51   // This is only available since Linux 3.2.
52
53   struct iovec local = { valp, size };
54   struct iovec remote = { (void*) addr, size };
55   s = process_vm_readv(pid, &local, 1, &remote, 1, 0);
56   if (s >= 0) {
57     if (s != size)
58       return - UNW_EINVAL;
59     else
60       return 0;
61   }
62   if (s < 0 && errno != ENOSYS)
63     return - UNW_EINVAL;
64 #endif
65
66   // /proc/${pid}/mem implementation.
67   // On recent kernels, we do not need to ptrace the target process.
68   // On older kernels, it is necessary to ptrace the target process.
69   size_t count = size;
70   off_t off = (off_t) addr;
71   char* buf = (char*) valp;
72   int fd = MC_process_vm_open(pid, O_RDONLY);
73   if (fd < 0)
74     return - UNW_EINVAL;
75   while (1) {
76     ssize_t s = pread(fd, buf, count, off);
77     if (s == 0) {
78       close(fd);
79       return - UNW_EINVAL;
80     }
81     if (s == -1)
82       break;
83     count -= s;
84     buf += s;
85     off += s;
86     if (count == 0) {
87       close(fd);
88       return 0;
89     }
90   }
91   close(fd);
92
93   // ptrace implementation.
94   // We need to have PTRACE_ATTACH-ed it before.
95   return _UPT_access_mem(as, addr, valp, write, arg);
96 }
97
98 unw_accessors_t mc_unw_vmread_accessors =
99   {
100     .find_proc_info             = &_UPT_find_proc_info,
101     .put_unwind_info            = &_UPT_put_unwind_info,
102     .get_dyn_info_list_addr     = &_UPT_get_dyn_info_list_addr,
103     .access_mem                 = &access_mem,
104     .access_reg                 = &_UPT_access_reg,
105     .access_fpreg               = &_UPT_access_fpreg,
106     .resume                     = &_UPT_resume,
107     .get_proc_name              = &_UPT_get_proc_name
108   };