Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add s4u-cloud-simple to the list of examples
[simgrid.git] / src / mc / inspect / mc_unw_vmread.cpp
1 /* Copyright (c) 2015-2019. 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-ptrace.h>
12 #include <libunwind.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 pid_t _UPT_getpid(void* arg)
37 {
38   _UPT_info* info = static_cast<_UPT_info*>(arg);
39   return info->pid;
40 }
41
42 /** Read from the memory, avoid using `ptrace` (libunwind method)
43  */
44 static int access_mem(const unw_addr_space_t as, const unw_word_t addr, unw_word_t* const valp, const int write,
45                       void* const arg)
46 {
47   if (write)
48     return -UNW_EINVAL;
49   pid_t pid   = _UPT_getpid(arg);
50   size_t size = sizeof(unw_word_t);
51
52 #if HAVE_PROCESS_VM_READV
53   // process_vm_read implementation.
54   // This is only available since Linux 3.2.
55
56   struct iovec local  = {valp, size};
57   struct iovec remote = {(void*)addr, size};
58   ssize_t s           = process_vm_readv(pid, &local, 1, &remote, 1, 0);
59   if (s >= 0) {
60     if ((size_t)s != size)
61       return -UNW_EINVAL;
62     else
63       return 0;
64   }
65   if (s < 0 && errno != ENOSYS)
66     return -UNW_EINVAL;
67 #endif
68
69   // /proc/${pid}/mem implementation.
70   // On recent kernels, we do not need to ptrace the target process.
71   // On older kernels, it is necessary to ptrace the target process.
72   size_t count = size;
73   off_t off    = (off_t)addr;
74   char* buf    = (char*)valp;
75   int fd       = simgrid::mc::open_vm(pid, O_RDONLY);
76   if (fd < 0)
77     return -UNW_EINVAL;
78   while (1) {
79     ssize_t nread = pread(fd, buf, count, off);
80     if (nread == 0) {
81       close(fd);
82       return -UNW_EINVAL;
83     }
84     if (nread == -1)
85       break;
86     count -= nread;
87     buf += nread;
88     off += nread;
89     if (count == 0) {
90       close(fd);
91       return 0;
92     }
93   }
94   close(fd);
95
96   // ptrace implementation.
97   // We need to have PTRACE_ATTACH-ed it before.
98   return _UPT_access_mem(as, addr, valp, write, arg);
99 }
100
101 namespace simgrid {
102 namespace unw {
103
104 /** Virtual table for our `libunwind-process_vm_readv` implementation.
105  *
106  *  This implementation reuse most the code of `libunwind-ptrace` but
107  *  does not use ptrace() to read the target process memory by
108  *  `process_vm_readv()` or `/dev/${pid}/mem` if possible.
109  *
110  *  Does not support any MC-specific behaviour (privatization, snapshots)
111  *  and `ucontext_t`.
112  *
113  *  It works with `void*` contexts allocated with `_UPT_create(pid)`.
114  */
115 // TODO, we could get rid of this if we properly stop the model-checked
116 // process before reading the memory.
117 static unw_accessors_t accessors = {&_UPT_find_proc_info, &_UPT_put_unwind_info, &_UPT_get_dyn_info_list_addr,
118                                     &access_mem,          &_UPT_access_reg,      &_UPT_access_fpreg,
119                                     &_UPT_resume,         &_UPT_get_proc_name};
120
121 unw_addr_space_t create_addr_space()
122 {
123   return unw_create_addr_space(&accessors, BYTE_ORDER);
124 }
125
126 void* create_context(unw_addr_space_t /*as*/, pid_t pid)
127 {
128   return _UPT_create(pid);
129 }
130
131 } // namespace unw
132 } // namespace simgrid