Logo AND Algorithmique Numérique Distribuée

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