Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ahem. You mean I need to actually compile with MC after moving files?
[simgrid.git] / src / mc / inspect / mc_unw_vmread.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/inspect/mc_unw.hpp"
7 #include "src/mc/remote/RemoteClient.hpp"
8
9 #include <sys/types.h>
10 #include <sys/uio.h>
11
12 #include <fcntl.h>
13 #include <libunwind-ptrace.h>
14 #include <libunwind.h>
15
16 /** \file
17  *  Libunwind namespace implementation using process_vm_readv.
18  */
19
20 /** Partial structure of libunwind-ptrace context in order to get the PID
21  *
22  *  HACK, The context type for libunwind-race is an opaque type.
23  *  We need to get the PID which is the first field. This is a hack
24  *  which might break if the libunwind-ptrace structure changes.
25  */
26 struct _UPT_info {
27   pid_t pid;
28   // Other things...
29 };
30
31 /** Get the PID of a `libunwind-ptrace` context
32  */
33 static inline pid_t _UPT_getpid(void* arg)
34 {
35   _UPT_info* info = static_cast<_UPT_info*>(arg);
36   return info->pid;
37 }
38
39 /** Read from the memory, avoid using `ptrace` (libunwind method)
40  */
41 static int access_mem(const unw_addr_space_t as, const unw_word_t addr, unw_word_t* const valp, const int write,
42                       void* const arg)
43 {
44   if (write)
45     return -UNW_EINVAL;
46   pid_t pid   = _UPT_getpid(arg);
47   size_t size = sizeof(unw_word_t);
48
49 #if 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   ssize_t s           = process_vm_readv(pid, &local, 1, &remote, 1, 0);
56   if (s >= 0) {
57     if ((size_t)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       = simgrid::mc::open_vm(pid, O_RDONLY);
73   if (fd < 0)
74     return -UNW_EINVAL;
75   while (1) {
76     ssize_t nread = pread(fd, buf, count, off);
77     if (nread == 0) {
78       close(fd);
79       return -UNW_EINVAL;
80     }
81     if (nread == -1)
82       break;
83     count -= nread;
84     buf += nread;
85     off += nread;
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 namespace simgrid {
99 namespace unw {
100
101 /** Virtual table for our `libunwind-process_vm_readv` implementation.
102  *
103  *  This implementation reuse most the code of `libunwind-ptrace` but
104  *  does not use ptrace() to read the target process memory by
105  *  `process_vm_readv()` or `/dev/${pid}/mem` if possible.
106  *
107  *  Does not support any MC-specific behaviour (privatization, snapshots)
108  *  and `ucontext_t`.
109  *
110  *  It works with `void*` contexts allocated with `_UPT_create(pid)`.
111  */
112 // TODO, we could get rid of this if we properly stop the model-checked
113 // process before reading the memory.
114 static unw_accessors_t accessors = {&_UPT_find_proc_info, &_UPT_put_unwind_info, &_UPT_get_dyn_info_list_addr,
115                                     &access_mem,          &_UPT_access_reg,      &_UPT_access_fpreg,
116                                     &_UPT_resume,         &_UPT_get_proc_name};
117
118 unw_addr_space_t create_addr_space()
119 {
120   return unw_create_addr_space(&accessors, BYTE_ORDER);
121 }
122
123 void* create_context(unw_addr_space_t /*as*/, pid_t pid)
124 {
125   return _UPT_create(pid);
126 }
127
128 } // namespace unw
129 } // namespace simgrid