Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b786aeabe2420a27301f0e23f7443fea502b43cd
[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/mc/inspect/ObjectInformation.hpp"
10 #include "src/mc/remote/Channel.hpp"
11 #include "src/xbt/mmalloc/mmprivate.h"
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,
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.get_buffer(), 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   const 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& get_channel() const { return channel_; }
129   Channel& get_channel() { 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   void ignore_global_variable(const char* name)
146   {
147     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
148       info->remove_global_variable(name);
149   }
150
151   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
152   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
153
154   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
155   void ignore_heap(IgnoredHeapRegion const& region);
156   void unignore_heap(void* address, size_t size);
157
158   void ignore_local_variable(const char* var_name, const char* frame_name);
159   std::vector<simgrid::mc::ActorInformation>& actors();
160   std::vector<simgrid::mc::ActorInformation>& dead_actors();
161
162   /** Get a local description of a remote SIMIX actor */
163   simgrid::mc::ActorInformation* resolve_actor_info(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
164   {
165     xbt_assert(mc_model_checker != nullptr);
166     if (not actor)
167       return nullptr;
168     this->refresh_simix();
169     for (auto& actor_info : this->smx_actors_infos)
170       if (actor_info.address == actor)
171         return &actor_info;
172     for (auto& actor_info : this->smx_dead_actors_infos)
173       if (actor_info.address == actor)
174         return &actor_info;
175     return nullptr;
176   }
177
178   /** Get a local copy of the SIMIX actor structure */
179   simgrid::kernel::actor::ActorImpl* resolve_actor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
180   {
181     simgrid::mc::ActorInformation* actor_info = this->resolve_actor_info(process);
182     if (actor_info)
183       return actor_info->copy.get_buffer();
184     else
185       return nullptr;
186   }
187
188   void dump_stack();
189
190 private:
191   void init_memory_map_info();
192   void refresh_heap();
193   void refresh_malloc_info();
194   void refresh_simix();
195
196   pid_t pid_ = -1;
197   Channel channel_;
198   bool running_ = false;
199   std::vector<simgrid::xbt::VmMap> memory_map_;
200   RemotePtr<void> maestro_stack_start_;
201   RemotePtr<void> maestro_stack_end_;
202   int memory_file = -1;
203   std::vector<IgnoredRegion> ignored_regions_;
204   std::vector<s_stack_region_t> stack_areas_;
205   std::vector<IgnoredHeapRegion> ignored_heap_;
206
207 public:
208   // object info
209   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
210   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
211   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
212   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
213
214   // Copies of MCed SMX data structures
215   /** Copy of `simix_global->process_list`
216    *
217    *  See mc_smx.c.
218    */
219   std::vector<ActorInformation> smx_actors_infos;
220
221   /** Copy of `simix_global->actors_to_destroy`
222    *
223    *  See mc_smx.c.
224    */
225   std::vector<ActorInformation> smx_dead_actors_infos;
226
227 private:
228   /** State of the cache (which variables are up to date) */
229   int cache_flags_ = RemoteClient::cache_none;
230
231 public:
232   /** Address of the heap structure in the MCed process. */
233   void* heap_address;
234
235   /** Copy of the heap structure of the process
236    *
237    *  This is refreshed with the `MC_process_refresh` call.
238    *  This is not used if the process is the current one:
239    *  use `get_heap_info()` in order to use it.
240    */
241   std::unique_ptr<s_xbt_mheap_t> heap;
242
243   /** Copy of the allocation info structure
244    *
245    *  This is refreshed with the `MC_process_refresh` call.
246    *  This is not used if the process is the current one:
247    *  use `get_malloc_info()` in order to use it.
248    */
249   std::vector<malloc_info> heap_info;
250
251   // Libunwind-data
252   /** Full-featured MC-aware libunwind address space for the process
253    *
254    *  This address space is using a simgrid::mc::UnwindContext*
255    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
256    *  and unw_context_t).
257    */
258   unw_addr_space_t unw_addr_space;
259
260   /** Underlying libunwind address-space
261    *
262    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
263    *  operations of the native MC address space is currently delegated
264    *  to this address space (either the local or a ptrace unwinder).
265    */
266   unw_addr_space_t unw_underlying_addr_space;
267
268   /** The corresponding context
269    */
270   void* unw_underlying_context;
271
272   /* Check whether the given actor is enabled */
273   bool actor_is_enabled(aid_t pid);
274 };
275
276 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
277  */
278 XBT_PRIVATE int open_vm(pid_t pid, int flags);
279 }
280 }
281
282 #endif