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>
24 class ActorInformation {
26 /** MCed address of the process */
27 RemotePtr<kernel::actor::ActorImpl> address{nullptr};
28 Remote<kernel::actor::ActorImpl> copy;
30 /** Hostname (owned by `mc_model_checker->hostnames_`) */
31 const xbt::string* hostname = nullptr;
42 struct IgnoredRegion {
47 struct IgnoredHeapRegion {
54 /** The Application's process memory, seen from the MCer perspective
56 * This class is mixing a lot of different responsibilities and is tied
57 * to SIMIX. It should probably be split into different classes.
61 * - reading from the process memory (`AddressSpace`);
62 * - accessing the system state of the process (heap, …);
63 * - storing the SIMIX state of the process;
68 class RemoteProcess final : public AddressSpace {
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;
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, xbt_dynar_t dead_actors);
82 RemoteProcess(RemoteProcess const&) = delete;
83 RemoteProcess(RemoteProcess&&) = delete;
84 RemoteProcess& operator=(RemoteProcess const&) = delete;
85 RemoteProcess& operator=(RemoteProcess&&) = delete;
92 void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
93 ReadOptions options = ReadOptions::none()) const override;
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
98 read_variable(name, target, sizeof(*target));
100 template <class T> Remote<T> read_variable(const char* name) const
103 read_variable(name, res.get_buffer(), sizeof(T));
107 std::string read_string(RemotePtr<char> address) const;
108 using AddressSpace::read_string;
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;
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;
122 xbt_mheap_t get_heap()
124 if (not(this->cache_flags_ & RemoteProcess::cache_heap))
125 this->refresh_heap();
126 return this->heap.get();
128 const malloc_info* get_malloc_info()
130 if (not(this->cache_flags_ & RemoteProcess::cache_malloc))
131 this->refresh_malloc_info();
132 return this->heap_info.data();
135 void clear_cache() { this->cache_flags_ = RemoteProcess::cache_none; }
137 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
138 void ignore_region(std::uint64_t address, std::size_t size);
140 pid_t pid() const { return pid_; }
142 bool in_maestro_stack(RemotePtr<void> p) const
144 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
147 bool running() const { return running_; }
149 void terminate() { running_ = false; }
151 void ignore_global_variable(const char* name) const
153 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos)
154 info->remove_global_variable(name);
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_; }
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);
164 void ignore_local_variable(const char* var_name, const char* frame_name) const;
166 /* ***************** */
167 /* SIMIX-related API */
168 /* ***************** */
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 RemotePtr<s_xbt_dynar_t> dead_actors_addr_;
176 std::vector<ActorInformation>& actors();
177 std::vector<ActorInformation>& dead_actors();
179 /** Get a local description of a remote SIMIX actor */
180 ActorInformation* resolve_actor_info(RemotePtr<kernel::actor::ActorImpl> actor)
182 xbt_assert(mc_model_checker != nullptr);
185 this->refresh_simix();
186 for (auto& actor_info : this->smx_actors_infos)
187 if (actor_info.address == actor)
189 for (auto& actor_info : this->smx_dead_actors_infos)
190 if (actor_info.address == actor)
195 /** Get a local copy of the SIMIX actor structure */
196 kernel::actor::ActorImpl* resolve_actor(RemotePtr<kernel::actor::ActorImpl> process)
198 ActorInformation* actor_info = this->resolve_actor_info(process);
200 return actor_info->copy.get_buffer();
205 unsigned long get_maxpid() const { return this->read(maxpid_addr_); }
207 void dump_stack() const;
210 void init_memory_map_info();
212 void refresh_malloc_info();
213 void refresh_simix();
216 bool running_ = false;
217 std::vector<xbt::VmMap> memory_map_;
218 RemotePtr<void> maestro_stack_start_;
219 RemotePtr<void> maestro_stack_end_;
220 int memory_file = -1;
221 std::vector<IgnoredRegion> ignored_regions_;
222 std::vector<s_stack_region_t> stack_areas_;
223 std::vector<IgnoredHeapRegion> ignored_heap_;
225 // Copies of MCed SMX data structures
226 /** Copy of `EngineImpl::actor_list_`
230 std::vector<ActorInformation> smx_actors_infos;
232 /** Copy of `EngineImpl::actors_to_destroy_`
236 std::vector<ActorInformation> smx_dead_actors_infos;
238 /** State of the cache (which variables are up to date) */
239 int cache_flags_ = RemoteProcess::cache_none;
243 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
244 std::vector<std::shared_ptr<ObjectInformation>> object_infos;
245 std::shared_ptr<ObjectInformation> binary_info;
247 /** Address of the heap structure in the MCed process. */
248 RemotePtr<s_xbt_mheap_t> heap_address;
250 /** Copy of the heap structure of the process
252 * This is refreshed with the `MC_process_refresh` call.
253 * This is not used if the process is the current one:
254 * use `get_heap_info()` in order to use it.
256 std::unique_ptr<s_xbt_mheap_t> heap;
258 /** Copy of the allocation info structure
260 * This is refreshed with the `MC_process_refresh` call.
261 * This is not used if the process is the current one:
262 * use `get_malloc_info()` in order to use it.
264 std::vector<malloc_info> heap_info;
267 /** Full-featured MC-aware libunwind address space for the process
269 * This address space is using a simgrid::mc::UnwindContext*
270 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
271 * and unw_context_t).
273 unw_addr_space_t unw_addr_space = nullptr;
275 /** Underlying libunwind address-space
277 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
278 * operations of the native MC address space is currently delegated
279 * to this address space (either the local or a ptrace unwinder).
281 unw_addr_space_t unw_underlying_addr_space = nullptr;
283 /** The corresponding context
285 void* unw_underlying_context = nullptr;
288 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
289 XBT_PRIVATE int open_vm(pid_t pid, int flags);
291 } // namespace simgrid