Logo AND Algorithmique Numérique Distribuée

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