Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve debug messages (code is still not functional).
[simgrid.git] / src / mc / remote / RemoteProcess.hpp
1 /* mc::RemoteClient: representative of the Client memory on the MC side */
2
3 /* Copyright (c) 2008-2021. 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 "mc/datatypes.h"
12 #include "src/mc/AddressSpace.hpp"
13 #include "src/mc/inspect/ObjectInformation.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_model_checker->hostnames_`) */
29   const xbt::string* 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 Application's process memory, 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  *  - stack unwinding;
64  *  - etc.
65  */
66 class RemoteProcess final : public AddressSpace {
67 private:
68   // Those flags are used to track down which cached information
69   // is still up to date and which information needs to be updated.
70   static constexpr int cache_none            = 0;
71   static constexpr int cache_heap            = 1;
72   static constexpr int cache_malloc          = 2;
73   static constexpr int cache_simix_processes = 4;
74
75 public:
76   explicit RemoteProcess(pid_t pid);
77   ~RemoteProcess() override;
78   void init(void* mmalloc_default_mdp, void* maxpid, void* actors, void* dead_actors);
79
80   RemoteProcess(RemoteProcess const&) = delete;
81   RemoteProcess(RemoteProcess&&)      = delete;
82   RemoteProcess& operator=(RemoteProcess const&) = delete;
83   RemoteProcess& operator=(RemoteProcess&&) = delete;
84
85   /* ************* */
86   /* Low-level API */
87   /* ************* */
88
89   // Read memory:
90   void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
91                    ReadOptions options = ReadOptions::none()) const override;
92
93   void read_variable(const char* name, void* target, size_t size) const;
94   template <class T> void read_variable(const char* name, T* target) const
95   {
96     read_variable(name, target, sizeof(*target));
97   }
98   template <class T> Remote<T> read_variable(const char* name) const
99   {
100     Remote<T> res;
101     read_variable(name, res.get_buffer(), sizeof(T));
102     return res;
103   }
104
105   std::string read_string(RemotePtr<char> address) const;
106   using AddressSpace::read_string;
107
108   // Write memory:
109   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address) const;
110   void clear_bytes(RemotePtr<void> address, size_t len) const;
111
112   // Debug information:
113   std::shared_ptr<ObjectInformation> find_object_info(RemotePtr<void> addr) const;
114   std::shared_ptr<ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
115   std::shared_ptr<ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
116   Frame* find_function(RemotePtr<void> ip) const;
117   const Variable* find_variable(const char* name) const;
118
119   // Heap access:
120   xbt_mheap_t get_heap()
121   {
122     if (not(this->cache_flags_ & RemoteProcess::cache_heap))
123       this->refresh_heap();
124     return this->heap.get();
125   }
126   const malloc_info* get_malloc_info()
127   {
128     if (not(this->cache_flags_ & RemoteProcess::cache_malloc))
129       this->refresh_malloc_info();
130     return this->heap_info.data();
131   }
132
133   void clear_cache() { this->cache_flags_ = RemoteProcess::cache_none; }
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) const
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) const;
163
164   /* ***************** */
165   /* SIMIX-related API */
166   /* ***************** */
167 private:
168   // Cache the address of the variables we read directly in the memory of remote
169   void* maxpid_addr_;
170   void* actors_addr_;
171   void* dead_actors_addr_;
172
173 public:
174   std::vector<ActorInformation>& actors();
175   std::vector<ActorInformation>& dead_actors();
176
177   /** Get a local description of a remote SIMIX actor */
178   ActorInformation* resolve_actor_info(RemotePtr<kernel::actor::ActorImpl> actor)
179   {
180     xbt_assert(mc_model_checker != nullptr);
181     if (not actor)
182       return nullptr;
183     this->refresh_simix();
184     for (auto& actor_info : this->smx_actors_infos)
185       if (actor_info.address == actor)
186         return &actor_info;
187     for (auto& actor_info : this->smx_dead_actors_infos)
188       if (actor_info.address == actor)
189         return &actor_info;
190     return nullptr;
191   }
192
193   /** Get a local copy of the SIMIX actor structure */
194   kernel::actor::ActorImpl* resolve_actor(RemotePtr<kernel::actor::ActorImpl> process)
195   {
196     ActorInformation* actor_info = this->resolve_actor_info(process);
197     if (actor_info)
198       return actor_info->copy.get_buffer();
199     else
200       return nullptr;
201   }
202
203   unsigned long get_maxpid() const;
204   void get_actor_vectors(RemotePtr<s_xbt_dynar_t>& actors, RemotePtr<s_xbt_dynar_t>& dead_actors);
205
206   void dump_stack() const;
207
208 private:
209   void init_memory_map_info();
210   void refresh_heap();
211   void refresh_malloc_info();
212   void refresh_simix();
213
214   pid_t pid_    = -1;
215   bool running_ = false;
216   std::vector<xbt::VmMap> memory_map_;
217   RemotePtr<void> maestro_stack_start_;
218   RemotePtr<void> maestro_stack_end_;
219   int memory_file = -1;
220   std::vector<IgnoredRegion> ignored_regions_;
221   std::vector<s_stack_region_t> stack_areas_;
222   std::vector<IgnoredHeapRegion> ignored_heap_;
223
224 public:
225   // object info
226   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
227   std::vector<std::shared_ptr<ObjectInformation>> object_infos;
228   std::shared_ptr<ObjectInformation> binary_info;
229
230   // Copies of MCed SMX data structures
231   /** Copy of `simix_global->process_list`
232    *
233    *  See mc_smx.c.
234    */
235   std::vector<ActorInformation> smx_actors_infos;
236
237   /** Copy of `simix_global->actors_to_destroy`
238    *
239    *  See mc_smx.c.
240    */
241   std::vector<ActorInformation> smx_dead_actors_infos;
242
243 private:
244   /** State of the cache (which variables are up to date) */
245   int cache_flags_ = RemoteProcess::cache_none;
246
247 public:
248   /** Address of the heap structure in the MCed process. */
249   void* heap_address;
250
251   /** Copy of the heap structure of the process
252    *
253    *  This is refreshed with the `MC_process_refresh` call.
254    *  This is not used if the process is the current one:
255    *  use `get_heap_info()` in order to use it.
256    */
257   std::unique_ptr<s_xbt_mheap_t> heap;
258
259   /** Copy of the allocation info structure
260    *
261    *  This is refreshed with the `MC_process_refresh` call.
262    *  This is not used if the process is the current one:
263    *  use `get_malloc_info()` in order to use it.
264    */
265   std::vector<malloc_info> heap_info;
266
267   // Libunwind-data
268   /** Full-featured MC-aware libunwind address space for the process
269    *
270    *  This address space is using a simgrid::mc::UnwindContext*
271    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
272    *  and unw_context_t).
273    */
274   unw_addr_space_t unw_addr_space;
275
276   /** Underlying libunwind address-space
277    *
278    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
279    *  operations of the native MC address space is currently delegated
280    *  to this address space (either the local or a ptrace unwinder).
281    */
282   unw_addr_space_t unw_underlying_addr_space;
283
284   /** The corresponding context
285    */
286   void* unw_underlying_context;
287 };
288
289 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
290 XBT_PRIVATE int open_vm(pid_t pid, int flags);
291 } // namespace mc
292 } // namespace simgrid
293
294 #endif