Logo AND Algorithmique Numérique Distribuée

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