Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused private function MC_snapshot_memcmp().
[simgrid.git] / src / mc / mc_unw_vmread.cpp
1 /* Copyright (c) 2015-2017. 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 <fcntl.h>
11 #include <libunwind.h>
12 #include <libunwind-ptrace.h>
13
14 #include "src/mc/mc_unw.hpp"
15 #include "src/mc/remote/RemoteClient.hpp"
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  *  HACK, The context type for libunwind-race is an opaque type.
26  *  We need to get the PID which is the first field. This is a hack
27  *  which might break if the 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   _UPT_info* info = static_cast<_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   pid_t pid = _UPT_getpid(arg);
52   size_t size = sizeof(unw_word_t);
53
54 #if HAVE_PROCESS_VM_READV
55   // process_vm_read implementation.
56   // This is only available since Linux 3.2.
57
58   struct iovec local = { valp, size };
59   struct iovec remote = { (void*) addr, size };
60   ssize_t s = process_vm_readv(pid, &local, 1, &remote, 1, 0);
61   if (s >= 0) {
62     if ((size_t) s != size)
63       return - UNW_EINVAL;
64     else
65       return 0;
66   }
67   if (s < 0 && errno != ENOSYS)
68     return - UNW_EINVAL;
69 #endif
70
71   // /proc/${pid}/mem implementation.
72   // On recent kernels, we do not need to ptrace the target process.
73   // On older kernels, it is necessary to ptrace the target process.
74   size_t count = size;
75   off_t off = (off_t) addr;
76   char* buf = (char*) valp;
77   int fd = simgrid::mc::open_vm(pid, O_RDONLY);
78   if (fd < 0)
79     return - UNW_EINVAL;
80   while (1) {
81     ssize_t s = pread(fd, buf, count, off);
82     if (s == 0) {
83       close(fd);
84       return - UNW_EINVAL;
85     }
86     if (s == -1)
87       break;
88     count -= s;
89     buf += s;
90     off += s;
91     if (count == 0) {
92       close(fd);
93       return 0;
94     }
95   }
96   close(fd);
97
98   // ptrace implementation.
99   // We need to have PTRACE_ATTACH-ed it before.
100   return _UPT_access_mem(as, addr, valp, write, arg);
101 }
102
103 namespace simgrid {
104 namespace unw {
105
106 /** Virtual table for our `libunwind-process_vm_readv` implementation.
107  *
108  *  This implementation reuse most the code of `libunwind-ptrace` but
109  *  does not use ptrace() to read the target process memory by
110  *  `process_vm_readv()` or `/dev/${pid}/mem` if possible.
111  *
112  *  Does not support any MC-specific behaviour (privatization, snapshots)
113  *  and `ucontext_t`.
114  *
115  *  It works with `void*` contexts allocated with `_UPT_create(pid)`.
116  */
117 // TODO, we could get rid of this if we properly stop the model-checked
118 // process before reading the memory.
119 static unw_accessors_t accessors = {
120   &_UPT_find_proc_info,
121   &_UPT_put_unwind_info,
122   &_UPT_get_dyn_info_list_addr,
123   &access_mem,
124   &_UPT_access_reg,
125   &_UPT_access_fpreg,
126   &_UPT_resume,
127   &_UPT_get_proc_name
128 };
129
130 unw_addr_space_t create_addr_space()
131 {
132   return unw_create_addr_space(&accessors, BYTE_ORDER);
133 }
134
135 void* create_context(unw_addr_space_t as, pid_t pid)
136 {
137   return _UPT_create(pid);
138 }
139
140 }
141 }