Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[simgrid.git] / src / mc / remote / RemoteClient.hpp
1 /* Copyright (c) 2008-2017. 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 #ifndef SIMGRID_MC_PROCESS_H
7 #define SIMGRID_MC_PROCESS_H
8
9 #include <cstddef>
10 #include <cstdint>
11
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16
17 #include <sys/types.h>
18
19 #include <simgrid_config.h>
20
21 #include "xbt/base.h"
22 #include <xbt/mmalloc.h>
23
24 #include "src/xbt/mmalloc/mmprivate.h"
25
26 #include "src/mc/remote/Channel.hpp"
27 #include "src/mc/remote/RemotePtr.hpp"
28
29 #include "src/simix/popping_private.hpp"
30 #include "src/simix/smx_private.hpp"
31 #include <simgrid/simix.h>
32
33 #include "src/xbt/memory_map.hpp"
34
35 #include "src/mc/AddressSpace.hpp"
36 #include "src/mc/ObjectInformation.hpp"
37 #include "src/mc/mc_base.h"
38 #include "src/mc/mc_forward.hpp"
39 #include "src/mc/remote/mc_protocol.h"
40
41 namespace simgrid {
42 namespace mc {
43
44 class ActorInformation {
45 public:
46   /** MCed address of the process */
47   RemotePtr<simgrid::simix::ActorImpl> address = nullptr;
48   Remote<simgrid::simix::ActorImpl> copy;
49
50   /** Hostname (owned by `mc_modelchecker->hostnames`) */
51   const char* hostname = nullptr;
52   std::string name;
53
54   void clear()
55   {
56     name.clear();
57     address  = nullptr;
58     hostname = nullptr;
59   }
60 };
61
62 struct IgnoredRegion {
63   std::uint64_t addr;
64   std::size_t size;
65 };
66
67 struct IgnoredHeapRegion {
68   int block;
69   int fragment;
70   void* address;
71   std::size_t size;
72 };
73
74 /** The Model-Checked process, seen from the MCer perspective
75  *
76  *  This class is mixing a lot of different responsibilities and is tied
77  *  to SIMIX. It should probably be split into different classes.
78  *
79  *  Responsibilities:
80  *
81  *  - reading from the process memory (`AddressSpace`);
82  *  - accessing the system state of the process (heap, …);
83  *  - storing the SIMIX state of the process;
84  *  - privatization;
85  *  - communication with the model-checked process;
86  *  - stack unwinding;
87  *  - etc.
88  */
89 class RemoteClient final : public AddressSpace {
90 private:
91   // Those flags are used to track down which cached information
92   // is still up to date and which information needs to be updated.
93   static constexpr int cache_none            = 0;
94   static constexpr int cache_heap            = 1;
95   static constexpr int cache_malloc          = 2;
96   static constexpr int cache_simix_processes = 4;
97
98 public:
99   RemoteClient(pid_t pid, int sockfd);
100   ~RemoteClient();
101   void init();
102
103   RemoteClient(RemoteClient const&) = delete;
104   RemoteClient(RemoteClient&&)      = delete;
105   RemoteClient& operator=(RemoteClient const&) = delete;
106   RemoteClient& operator=(RemoteClient&&) = delete;
107
108   // Read memory:
109   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
110                          ReadOptions options = ReadOptions::none()) const override;
111
112   void read_variable(const char* name, void* target, size_t size) const;
113   template <class T> void read_variable(const char* name, T* target) const
114   {
115     read_variable(name, target, sizeof(*target));
116   }
117   template <class T> Remote<T> read_variable(const char* name) const
118   {
119     Remote<T> res;
120     read_variable(name, res.getBuffer(), sizeof(T));
121     return res;
122   }
123
124   std::string read_string(RemotePtr<char> address) const;
125   using AddressSpace::read_string;
126
127   // Write memory:
128   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
129   void clear_bytes(RemotePtr<void> address, size_t len);
130
131   // Debug information:
132   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
133   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
134   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
135   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
136   simgrid::mc::Variable* find_variable(const char* name) const;
137
138   // Heap access:
139   xbt_mheap_t get_heap()
140   {
141     if (not(this->cache_flags_ & RemoteClient::cache_heap))
142       this->refresh_heap();
143     return this->heap.get();
144   }
145   const malloc_info* get_malloc_info()
146   {
147     if (not(this->cache_flags_ & RemoteClient::cache_malloc))
148       this->refresh_malloc_info();
149     return this->heap_info.data();
150   }
151
152   void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
153
154   Channel const& getChannel() const { return channel_; }
155   Channel& getChannel() { return channel_; }
156
157   std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
158   void ignore_region(std::uint64_t address, std::size_t size);
159
160   pid_t pid() const { return pid_; }
161
162   bool in_maestro_stack(RemotePtr<void> p) const
163   {
164     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
165   }
166
167   bool running() const { return running_; }
168
169   void terminate() { running_ = false; }
170
171   bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
172   bool privatized() const { return privatized_; }
173   void privatized(bool privatized) { privatized_ = privatized; }
174
175   void ignore_global_variable(const char* name)
176   {
177     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
178       info->remove_global_variable(name);
179   }
180
181   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
182   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
183
184   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
185   void ignore_heap(IgnoredHeapRegion const& region);
186   void unignore_heap(void* address, size_t size);
187
188   void ignore_local_variable(const char* var_name, const char* frame_name);
189   std::vector<simgrid::mc::ActorInformation>& actors();
190   std::vector<simgrid::mc::ActorInformation>& dead_actors();
191
192   /** Get a local description of a remote SIMIX actor */
193   simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> actor)
194   {
195     xbt_assert(mc_model_checker != nullptr);
196     if (not actor)
197       return nullptr;
198     this->refresh_simix();
199     for (auto& actor_info : this->smx_actors_infos)
200       if (actor_info.address == actor)
201         return &actor_info;
202     for (auto& actor_info : this->smx_dead_actors_infos)
203       if (actor_info.address == actor)
204         return &actor_info;
205     return nullptr;
206   }
207
208   /** Get a local copy of the SIMIX actor structure */
209   simgrid::simix::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> process)
210   {
211     simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
212     if (actor_info)
213       return actor_info->copy.getBuffer();
214     else
215       return nullptr;
216   }
217
218   void dumpStack();
219
220 private:
221   void init_memory_map_info();
222   void refresh_heap();
223   void refresh_malloc_info();
224   void refresh_simix();
225
226   pid_t pid_ = -1;
227   Channel channel_;
228   bool running_ = false;
229   std::vector<simgrid::xbt::VmMap> memory_map_;
230   RemotePtr<void> maestro_stack_start_;
231   RemotePtr<void> maestro_stack_end_;
232   int memory_file = -1;
233   std::vector<IgnoredRegion> ignored_regions_;
234   bool privatized_ = false;
235   std::vector<s_stack_region_t> stack_areas_;
236   std::vector<IgnoredHeapRegion> ignored_heap_;
237
238 public:
239   // object info
240   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
241   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
242   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
243   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
244
245   // Copies of MCed SMX data structures
246   /** Copy of `simix_global->process_list`
247    *
248    *  See mc_smx.c.
249    */
250   std::vector<ActorInformation> smx_actors_infos;
251
252   /** Copy of `simix_global->process_to_destroy`
253    *
254    *  See mc_smx.c.
255    */
256   std::vector<ActorInformation> smx_dead_actors_infos;
257
258 private:
259   /** State of the cache (which variables are up to date) */
260   int cache_flags_ = RemoteClient::cache_none;
261
262 public:
263   /** Address of the heap structure in the MCed process. */
264   void* heap_address;
265
266   /** Copy of the heap structure of the process
267    *
268    *  This is refreshed with the `MC_process_refresh` call.
269    *  This is not used if the process is the current one:
270    *  use `get_heap_info()` in order to use it.
271    */
272   std::unique_ptr<s_xbt_mheap_t> heap;
273
274   /** Copy of the allocation info structure
275    *
276    *  This is refreshed with the `MC_process_refresh` call.
277    *  This is not used if the process is the current one:
278    *  use `get_malloc_info()` in order to use it.
279    */
280   std::vector<malloc_info> heap_info;
281
282   // Libunwind-data
283   /** Full-featured MC-aware libunwind address space for the process
284    *
285    *  This address space is using a simgrid::mc::UnwindContext*
286    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
287    *  and unw_context_t).
288    */
289   unw_addr_space_t unw_addr_space;
290
291   /** Underlying libunwind address-space
292    *
293    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
294    *  operations of the native MC address space is currently delegated
295    *  to this address space (either the local or a ptrace unwinder).
296    */
297   unw_addr_space_t unw_underlying_addr_space;
298
299   /** The corresponding context
300    */
301   void* unw_underlying_context;
302
303   /* Check whether the given actor is enabled */
304   bool actor_is_enabled(aid_t pid);
305 };
306
307 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
308  */
309 XBT_PRIVATE int open_vm(pid_t pid, int flags);
310 }
311 }
312
313 #endif