Logo AND Algorithmique Numérique Distribuée

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