1 /* mc::RemoteClient: representative of the Client memory on the MC side */
3 /* Copyright (c) 2008-2022. The SimGrid Team. All rights reserved. */
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. */
8 #ifndef SIMGRID_MC_PROCESS_H
9 #define SIMGRID_MC_PROCESS_H
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"
18 #include <libunwind.h>
21 namespace simgrid::mc {
23 class ActorInformation {
25 /** MCed address of the process */
26 RemotePtr<kernel::actor::ActorImpl> address{nullptr};
27 Remote<kernel::actor::ActorImpl> copy;
29 /** Hostname (owned by `mc_model_checker->hostnames_`) */
30 const xbt::string* hostname = nullptr;
41 struct IgnoredRegion {
46 struct IgnoredHeapRegion {
53 /** The Application's process memory, seen from the MCer perspective
55 * This class is mixing a lot of different responsibilities and is tied
56 * to SIMIX. It should probably be split into different classes.
60 * - reading from the process memory (`AddressSpace`);
61 * - accessing the system state of the process (heap, …);
62 * - storing the SIMIX state of the process;
67 class RemoteProcess final : public AddressSpace {
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;
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);
81 RemoteProcess(RemoteProcess const&) = delete;
82 RemoteProcess(RemoteProcess&&) = delete;
83 RemoteProcess& operator=(RemoteProcess const&) = delete;
84 RemoteProcess& operator=(RemoteProcess&&) = delete;
91 void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
92 ReadOptions options = ReadOptions::none()) const override;
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
97 read_variable(name, target, sizeof(*target));
99 template <class T> Remote<T> read_variable(const char* name) const
102 read_variable(name, res.get_buffer(), sizeof(T));
106 std::string read_string(RemotePtr<char> address) const;
107 using AddressSpace::read_string;
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;
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;
121 xbt_mheap_t get_heap()
123 if (not(this->cache_flags_ & RemoteProcess::cache_heap))
124 this->refresh_heap();
125 return this->heap.get();
127 const malloc_info* get_malloc_info()
129 if (not(this->cache_flags_ & RemoteProcess::cache_malloc))
130 this->refresh_malloc_info();
131 return this->heap_info.data();
134 void clear_cache() { this->cache_flags_ = RemoteProcess::cache_none; }
136 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
137 void ignore_region(std::uint64_t address, std::size_t size);
139 pid_t pid() const { return pid_; }
141 bool in_maestro_stack(RemotePtr<void> p) const
143 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
146 bool running() const { return running_; }
148 void terminate() { running_ = false; }
150 void ignore_global_variable(const char* name) const
152 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos)
153 info->remove_global_variable(name);
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_; }
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);
163 void ignore_local_variable(const char* var_name, const char* frame_name) const;
165 /* ***************** */
166 /* SIMIX-related API */
167 /* ***************** */
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_;
174 std::vector<ActorInformation>& actors();
176 unsigned long get_maxpid() const { return this->read(maxpid_addr_); }
178 void dump_stack() const;
181 void init_memory_map_info();
183 void refresh_malloc_info();
186 bool running_ = false;
187 std::vector<xbt::VmMap> memory_map_;
188 RemotePtr<void> maestro_stack_start_;
189 RemotePtr<void> maestro_stack_end_;
190 int memory_file = -1;
191 std::vector<IgnoredRegion> ignored_regions_;
192 std::vector<s_stack_region_t> stack_areas_;
193 std::vector<IgnoredHeapRegion> ignored_heap_;
195 // Copies of MCed SMX data structures
196 /** Copy of `EngineImpl::actor_list_`
200 std::vector<ActorInformation> smx_actors_infos;
202 /** State of the cache (which variables are up to date) */
203 int cache_flags_ = RemoteProcess::cache_none;
207 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
208 std::vector<std::shared_ptr<ObjectInformation>> object_infos;
209 std::shared_ptr<ObjectInformation> binary_info;
211 /** Address of the heap structure in the MCed process. */
212 RemotePtr<s_xbt_mheap_t> heap_address;
214 /** Copy of the heap structure of the process
216 * This is refreshed with the `MC_process_refresh` call.
217 * This is not used if the process is the current one:
218 * use `get_heap_info()` in order to use it.
220 std::unique_ptr<s_xbt_mheap_t> heap;
222 /** Copy of the allocation info structure
224 * This is refreshed with the `MC_process_refresh` call.
225 * This is not used if the process is the current one:
226 * use `get_malloc_info()` in order to use it.
228 std::vector<malloc_info> heap_info;
231 /** Full-featured MC-aware libunwind address space for the process
233 * This address space is using a simgrid::mc::UnwindContext*
234 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
235 * and unw_context_t).
237 unw_addr_space_t unw_addr_space = nullptr;
239 /** Underlying libunwind address-space
241 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
242 * operations of the native MC address space is currently delegated
243 * to this address space (either the local or a ptrace unwinder).
245 unw_addr_space_t unw_underlying_addr_space = nullptr;
247 /** The corresponding context
249 void* unw_underlying_context = nullptr;
252 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
253 XBT_PRIVATE int open_vm(pid_t pid, int flags);
254 } // namespace simgrid::mc