Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move mc:api::get_remote_heap_bytes() to RemoteProcess
[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 Checker perspective
54  *
55  *  Responsibilities:
56  *
57  *  - reading from the process memory (`AddressSpace`);
58  *  - accessing the system state of the process (heap, …);
59  *  - privatization;
60  *  - stack unwinding;
61  *  - etc.
62  */
63 class RemoteProcess final : public AddressSpace {
64 private:
65   // Those flags are used to track down which cached information
66   // is still up to date and which information needs to be updated.
67   static constexpr int cache_none            = 0;
68   static constexpr int cache_heap            = 1;
69   static constexpr int cache_malloc          = 2;
70
71 public:
72   explicit RemoteProcess(pid_t pid);
73   ~RemoteProcess() override;
74   void init(xbt_mheap_t mmalloc_default_mdp, unsigned long* maxpid);
75
76   RemoteProcess(RemoteProcess const&) = delete;
77   RemoteProcess(RemoteProcess&&)      = delete;
78   RemoteProcess& operator=(RemoteProcess const&) = delete;
79   RemoteProcess& operator=(RemoteProcess&&) = delete;
80
81   /* ************* */
82   /* Low-level API */
83   /* ************* */
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) const;
106   void clear_bytes(RemotePtr<void> address, size_t len) const;
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_ & RemoteProcess::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_ & RemoteProcess::cache_malloc))
125       this->refresh_malloc_info();
126     return this->heap_info.data();
127   }
128   /* Get the amount of memory mallocated in the remote process (requires mmalloc) */
129   std::size_t get_remote_heap_bytes();
130
131   void clear_cache() { this->cache_flags_ = RemoteProcess::cache_none; }
132
133   std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
134   void ignore_region(std::uint64_t address, std::size_t size);
135
136   pid_t pid() const { return pid_; }
137
138   bool in_maestro_stack(RemotePtr<void> p) const
139   {
140     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
141   }
142
143   bool running() const { return running_; }
144
145   void terminate() { running_ = false; }
146
147   void ignore_global_variable(const char* name) const
148   {
149     for (std::shared_ptr<ObjectInformation> const& info : this->object_infos)
150       info->remove_global_variable(name);
151   }
152
153   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
154   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
155
156   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
157   void ignore_heap(IgnoredHeapRegion const& region);
158   void unignore_heap(void* address, size_t size);
159
160   void ignore_local_variable(const char* var_name, const char* frame_name) const;
161
162   /* ***************** */
163   /* SIMIX-related API */
164   /* ***************** */
165 private:
166   // Cache the address of the variables we read directly in the memory of remote
167   RemotePtr<unsigned long> maxpid_addr_;
168
169 public:
170   unsigned long get_maxpid() const { return this->read(maxpid_addr_); }
171
172   void dump_stack() const;
173
174 private:
175   void init_memory_map_info();
176   void refresh_heap();
177   void refresh_malloc_info();
178
179   pid_t pid_    = -1;
180   bool running_ = false;
181   std::vector<xbt::VmMap> memory_map_;
182   RemotePtr<void> maestro_stack_start_;
183   RemotePtr<void> maestro_stack_end_;
184   int memory_file = -1;
185   std::vector<IgnoredRegion> ignored_regions_;
186   std::vector<s_stack_region_t> stack_areas_;
187   std::vector<IgnoredHeapRegion> ignored_heap_;
188
189   /** State of the cache (which variables are up to date) */
190   int cache_flags_ = RemoteProcess::cache_none;
191
192 public:
193   // object info
194   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
195   std::vector<std::shared_ptr<ObjectInformation>> object_infos;
196   std::shared_ptr<ObjectInformation> binary_info;
197
198   /** Address of the heap structure in the MCed process. */
199   RemotePtr<s_xbt_mheap_t> heap_address;
200
201   /** Copy of the heap structure of the process
202    *
203    *  This is refreshed with the `MC_process_refresh` call.
204    *  This is not used if the process is the current one:
205    *  use `get_heap_info()` in order to use it.
206    */
207   std::unique_ptr<s_xbt_mheap_t> heap;
208
209   /** Copy of the allocation info structure
210    *
211    *  This is refreshed with the `MC_process_refresh` call.
212    *  This is not used if the process is the current one:
213    *  use `get_malloc_info()` in order to use it.
214    */
215   std::vector<malloc_info> heap_info;
216
217   // Libunwind-data
218   /** Full-featured MC-aware libunwind address space for the process
219    *
220    *  This address space is using a simgrid::mc::UnwindContext*
221    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
222    *  and unw_context_t).
223    */
224   unw_addr_space_t unw_addr_space = nullptr;
225
226   /** Underlying libunwind address-space
227    *
228    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
229    *  operations of the native MC address space is currently delegated
230    *  to this address space (either the local or a ptrace unwinder).
231    */
232   unw_addr_space_t unw_underlying_addr_space = nullptr;
233
234   /** The corresponding context
235    */
236   void* unw_underlying_context = nullptr;
237 };
238
239 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
240 XBT_PRIVATE int open_vm(pid_t pid, int flags);
241 } // namespace simgrid::mc
242
243 #endif