Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused variable.
[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);
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
174 public:
175   std::vector<ActorInformation>& actors();
176
177   unsigned long get_maxpid() const { return this->read(maxpid_addr_); }
178
179   void dump_stack() const;
180
181 private:
182   void init_memory_map_info();
183   void refresh_heap();
184   void refresh_malloc_info();
185   void refresh_simix();
186
187   pid_t pid_    = -1;
188   bool running_ = false;
189   std::vector<xbt::VmMap> memory_map_;
190   RemotePtr<void> maestro_stack_start_;
191   RemotePtr<void> maestro_stack_end_;
192   int memory_file = -1;
193   std::vector<IgnoredRegion> ignored_regions_;
194   std::vector<s_stack_region_t> stack_areas_;
195   std::vector<IgnoredHeapRegion> ignored_heap_;
196
197   // Copies of MCed SMX data structures
198   /** Copy of `EngineImpl::actor_list_`
199    *
200    *  See mc_smx.cpp.
201    */
202   std::vector<ActorInformation> smx_actors_infos;
203
204   /** State of the cache (which variables are up to date) */
205   int cache_flags_ = RemoteProcess::cache_none;
206
207 public:
208   // object info
209   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
210   std::vector<std::shared_ptr<ObjectInformation>> object_infos;
211   std::shared_ptr<ObjectInformation> binary_info;
212
213   /** Address of the heap structure in the MCed process. */
214   RemotePtr<s_xbt_mheap_t> heap_address;
215
216   /** Copy of the heap structure of the process
217    *
218    *  This is refreshed with the `MC_process_refresh` call.
219    *  This is not used if the process is the current one:
220    *  use `get_heap_info()` in order to use it.
221    */
222   std::unique_ptr<s_xbt_mheap_t> heap;
223
224   /** Copy of the allocation info structure
225    *
226    *  This is refreshed with the `MC_process_refresh` call.
227    *  This is not used if the process is the current one:
228    *  use `get_malloc_info()` in order to use it.
229    */
230   std::vector<malloc_info> heap_info;
231
232   // Libunwind-data
233   /** Full-featured MC-aware libunwind address space for the process
234    *
235    *  This address space is using a simgrid::mc::UnwindContext*
236    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
237    *  and unw_context_t).
238    */
239   unw_addr_space_t unw_addr_space = nullptr;
240
241   /** Underlying libunwind address-space
242    *
243    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
244    *  operations of the native MC address space is currently delegated
245    *  to this address space (either the local or a ptrace unwinder).
246    */
247   unw_addr_space_t unw_underlying_addr_space = nullptr;
248
249   /** The corresponding context
250    */
251   void* unw_underlying_context = nullptr;
252 };
253
254 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
255 XBT_PRIVATE int open_vm(pid_t pid, int flags);
256 } // namespace mc
257 } // namespace simgrid
258
259 #endif