Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6216dd19bb0b5fd864d3b372921b0c6f6a06e8a1
[simgrid.git] / src / mc / remote / RemoteClientMemory.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 "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_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 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 RemoteClientMemory 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   RemoteClientMemory(pid_t pid);
77   ~RemoteClientMemory();
78   void init();
79
80   RemoteClientMemory(RemoteClientMemory const&) = delete;
81   RemoteClientMemory(RemoteClientMemory&&)      = delete;
82   RemoteClientMemory& operator=(RemoteClientMemory const&) = delete;
83   RemoteClientMemory& operator=(RemoteClientMemory&&) = delete;
84
85   // Read memory:
86   void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
87                    ReadOptions options = ReadOptions::none()) const override;
88
89   void read_variable(const char* name, void* target, size_t size) const;
90   template <class T> void read_variable(const char* name, T* target) const
91   {
92     read_variable(name, target, sizeof(*target));
93   }
94   template <class T> Remote<T> read_variable(const char* name) const
95   {
96     Remote<T> res;
97     read_variable(name, res.get_buffer(), sizeof(T));
98     return res;
99   }
100
101   std::string read_string(RemotePtr<char> address) const;
102   using AddressSpace::read_string;
103
104   // Write memory:
105   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
106   void clear_bytes(RemotePtr<void> address, size_t len);
107
108   // Debug information:
109   std::shared_ptr<ObjectInformation> find_object_info(RemotePtr<void> addr) const;
110   std::shared_ptr<ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
111   std::shared_ptr<ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
112   Frame* find_function(RemotePtr<void> ip) const;
113   const Variable* find_variable(const char* name) const;
114
115   // Heap access:
116   xbt_mheap_t get_heap()
117   {
118     if (not(this->cache_flags_ & RemoteClientMemory::cache_heap))
119       this->refresh_heap();
120     return this->heap.get();
121   }
122   const malloc_info* get_malloc_info()
123   {
124     if (not(this->cache_flags_ & RemoteClientMemory::cache_malloc))
125       this->refresh_malloc_info();
126     return this->heap_info.data();
127   }
128
129   void clear_cache() { this->cache_flags_ = RemoteClientMemory::cache_none; }
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<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<ActorInformation>& actors();
160   std::vector<ActorInformation>& dead_actors();
161
162   /** Get a local description of a remote SIMIX actor */
163   ActorInformation* resolve_actor_info(RemotePtr<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   kernel::actor::ActorImpl* resolve_actor(RemotePtr<kernel::actor::ActorImpl> process)
180   {
181     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   bool running_ = false;
198   std::vector<xbt::VmMap> memory_map_;
199   RemotePtr<void> maestro_stack_start_;
200   RemotePtr<void> maestro_stack_end_;
201   int memory_file = -1;
202   std::vector<IgnoredRegion> ignored_regions_;
203   std::vector<s_stack_region_t> stack_areas_;
204   std::vector<IgnoredHeapRegion> ignored_heap_;
205
206 public:
207   // object info
208   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
209   std::vector<std::shared_ptr<ObjectInformation>> object_infos;
210   std::shared_ptr<ObjectInformation> libsimgrid_info;
211   std::shared_ptr<ObjectInformation> binary_info;
212
213   // Copies of MCed SMX data structures
214   /** Copy of `simix_global->process_list`
215    *
216    *  See mc_smx.c.
217    */
218   std::vector<ActorInformation> smx_actors_infos;
219
220   /** Copy of `simix_global->actors_to_destroy`
221    *
222    *  See mc_smx.c.
223    */
224   std::vector<ActorInformation> smx_dead_actors_infos;
225
226 private:
227   /** State of the cache (which variables are up to date) */
228   int cache_flags_ = RemoteClientMemory::cache_none;
229
230 public:
231   /** Address of the heap structure in the MCed process. */
232   void* heap_address;
233
234   /** Copy of the heap structure of the process
235    *
236    *  This is refreshed with the `MC_process_refresh` call.
237    *  This is not used if the process is the current one:
238    *  use `get_heap_info()` in order to use it.
239    */
240   std::unique_ptr<s_xbt_mheap_t> heap;
241
242   /** Copy of the allocation info structure
243    *
244    *  This is refreshed with the `MC_process_refresh` call.
245    *  This is not used if the process is the current one:
246    *  use `get_malloc_info()` in order to use it.
247    */
248   std::vector<malloc_info> heap_info;
249
250   // Libunwind-data
251   /** Full-featured MC-aware libunwind address space for the process
252    *
253    *  This address space is using a simgrid::mc::UnwindContext*
254    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
255    *  and unw_context_t).
256    */
257   unw_addr_space_t unw_addr_space;
258
259   /** Underlying libunwind address-space
260    *
261    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
262    *  operations of the native MC address space is currently delegated
263    *  to this address space (either the local or a ptrace unwinder).
264    */
265   unw_addr_space_t unw_underlying_addr_space;
266
267   /** The corresponding context
268    */
269   void* unw_underlying_context;
270 };
271
272 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
273 XBT_PRIVATE int open_vm(pid_t pid, int flags);
274 } // namespace mc
275 } // namespace simgrid
276
277 #endif