Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
One usage of mc_model_checker less
[simgrid.git] / src / mc / sosp / RemoteProcessMemory.hpp
1 /* mc::RemoteClient: representative of the Client memory on the MC side */
2
3 /* Copyright (c) 2008-2023. 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 "src/mc/AddressSpace.hpp"
12 #include "src/mc/datatypes.h"
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 std::string* hostname = nullptr;
31   std::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  *  - stack unwinding;
60  *  - etc.
61  */
62 class RemoteProcessMemory final : public AddressSpace {
63 private:
64   // Those flags are used to track down which cached information
65   // is still up to date and which information needs to be updated.
66   static constexpr int cache_none   = 0;
67   static constexpr int cache_heap   = 1;
68   static constexpr int cache_malloc = 2;
69
70 public:
71   explicit RemoteProcessMemory(pid_t pid);
72   ~RemoteProcessMemory() override;
73   void init(xbt_mheap_t mmalloc_default_mdp);
74
75   RemoteProcessMemory(RemoteProcessMemory const&)            = delete;
76   RemoteProcessMemory(RemoteProcessMemory&&)                 = delete;
77   RemoteProcessMemory& operator=(RemoteProcessMemory const&) = delete;
78   RemoteProcessMemory& operator=(RemoteProcessMemory&&)      = delete;
79
80   /* ************* */
81   /* Low-level API */
82   /* ************* */
83
84   // Read memory:
85   void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
86                    ReadOptions options = ReadOptions::none()) const override;
87
88   void read_variable(const char* name, void* target, size_t size) const;
89   template <class T> void read_variable(const char* name, T* target) const
90   {
91     read_variable(name, target, sizeof(*target));
92   }
93   template <class T> Remote<T> read_variable(const char* name) const
94   {
95     Remote<T> res;
96     read_variable(name, res.get_buffer(), sizeof(T));
97     return res;
98   }
99
100   std::string read_string(RemotePtr<char> address) const;
101   using AddressSpace::read_string;
102
103   // Write memory:
104   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address) const;
105   void clear_bytes(RemotePtr<void> address, size_t len) const;
106
107   // Debug information:
108   std::shared_ptr<ObjectInformation> find_object_info(RemotePtr<void> addr) const;
109   std::shared_ptr<ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
110   std::shared_ptr<ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
111   Frame* find_function(RemotePtr<void> ip) const;
112   const Variable* find_variable(const char* name) const;
113
114   // Heap access:
115   xbt_mheap_t get_heap()
116   {
117     if (not(cache_flags_ & RemoteProcessMemory::cache_heap))
118       refresh_heap();
119     return this->heap.get();
120   }
121   const malloc_info* get_malloc_info()
122   {
123     if (not(this->cache_flags_ & RemoteProcessMemory::cache_malloc))
124       this->refresh_malloc_info();
125     return this->heap_info.data();
126   }
127   /* Get the amount of memory mallocated in the remote process (requires mmalloc) */
128   std::size_t get_remote_heap_bytes();
129
130   void clear_cache() { this->cache_flags_ = RemoteProcessMemory::cache_none; }
131
132   std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
133   void ignore_region(std::uint64_t address, std::size_t size);
134
135   pid_t pid() const { return pid_; }
136
137   bool in_maestro_stack(RemotePtr<void> p) const
138   {
139     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
140   }
141
142   bool running() const { return running_; }
143
144   void terminate() { running_ = false; }
145
146   void ignore_global_variable(const char* name) const
147   {
148     for (std::shared_ptr<ObjectInformation> const& info : this->object_infos)
149       info->remove_global_variable(name);
150   }
151
152   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
153   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
154
155   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
156   void ignore_heap(IgnoredHeapRegion const& region);
157   void unignore_heap(void* address, size_t size);
158
159   void ignore_local_variable(const char* var_name, const char* frame_name) const;
160
161   void dump_stack() const;
162
163 private:
164   void init_memory_map_info();
165   void refresh_heap();
166   void refresh_malloc_info();
167
168   pid_t pid_    = -1;
169   bool running_ = false;
170   std::vector<xbt::VmMap> memory_map_;
171   RemotePtr<void> maestro_stack_start_;
172   RemotePtr<void> maestro_stack_end_;
173   int memory_file = -1;
174   std::vector<IgnoredRegion> ignored_regions_;
175   std::vector<s_stack_region_t> stack_areas_;
176   std::vector<IgnoredHeapRegion> ignored_heap_;
177
178   /** State of the cache (which variables are up to date) */
179   int cache_flags_ = RemoteProcessMemory::cache_none;
180
181 public:
182   // object info
183   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
184   std::vector<std::shared_ptr<ObjectInformation>> object_infos;
185   std::shared_ptr<ObjectInformation> binary_info;
186
187   /** Address of the heap structure in the MCed process. */
188   RemotePtr<s_xbt_mheap_t> heap_address;
189
190   /** Copy of the heap structure of the process
191    *
192    *  This is refreshed with the `MC_process_refresh` call.
193    *  This is not used if the process is the current one:
194    *  use `get_heap_info()` in order to use it.
195    */
196   std::unique_ptr<s_xbt_mheap_t> heap = std::make_unique<s_xbt_mheap_t>();
197
198   /** Copy of the allocation info structure
199    *
200    *  This is refreshed with the `MC_process_refresh` call.
201    *  This is not used if the process is the current one:
202    *  use `get_malloc_info()` in order to use it.
203    */
204   std::vector<malloc_info> heap_info;
205
206   // Libunwind-data
207   /** Full-featured MC-aware libunwind address space for the process
208    *
209    *  This address space is using a simgrid::mc::UnwindContext*
210    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
211    *  and unw_context_t).
212    */
213   unw_addr_space_t unw_addr_space = nullptr;
214
215   /** Underlying libunwind address-space
216    *
217    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
218    *  operations of the native MC address space is currently delegated
219    *  to this address space (either the local or a ptrace unwinder).
220    */
221   unw_addr_space_t unw_underlying_addr_space = nullptr;
222
223   /** The corresponding context
224    */
225   void* unw_underlying_context = nullptr;
226 };
227
228 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
229 XBT_PRIVATE int open_vm(pid_t pid, int flags);
230 } // namespace simgrid::mc
231
232 #endif