Logo AND Algorithmique Numérique Distribuée

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