Logo AND Algorithmique Numérique Distribuée

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