1 /* mc::RemoteClient: representative of the Client memory on the MC side */
3 /* Copyright (c) 2008-2021. 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/mmalloc/mmprivate.h"
22 class ActorInformation {
24 /** MCed address of the process */
25 RemotePtr<kernel::actor::ActorImpl> address{nullptr};
26 Remote<kernel::actor::ActorImpl> copy;
28 /** Hostname (owned by `mc_model_checker->hostnames_`) */
29 const xbt::string* hostname = nullptr;
40 struct IgnoredRegion {
45 struct IgnoredHeapRegion {
52 /** The Application's process memory, seen from the MCer perspective
54 * This class is mixing a lot of different responsibilities and is tied
55 * to SIMIX. It should probably be split into different classes.
59 * - reading from the process memory (`AddressSpace`);
60 * - accessing the system state of the process (heap, …);
61 * - storing the SIMIX state of the process;
66 class RemoteProcess final : public AddressSpace {
68 // Those flags are used to track down which cached information
69 // is still up to date and which information needs to be updated.
70 static constexpr int cache_none = 0;
71 static constexpr int cache_heap = 1;
72 static constexpr int cache_malloc = 2;
73 static constexpr int cache_simix_processes = 4;
76 explicit RemoteProcess(pid_t pid);
77 ~RemoteProcess() override;
78 void init(void* mmalloc_default_mdp, void* maxpid, void* actors, void* dead_actors);
80 RemoteProcess(RemoteProcess const&) = delete;
81 RemoteProcess(RemoteProcess&&) = delete;
82 RemoteProcess& operator=(RemoteProcess const&) = delete;
83 RemoteProcess& operator=(RemoteProcess&&) = delete;
90 void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
91 ReadOptions options = ReadOptions::none()) const override;
93 void read_variable(const char* name, void* target, size_t size) const;
94 template <class T> void read_variable(const char* name, T* target) const
96 read_variable(name, target, sizeof(*target));
98 template <class T> Remote<T> read_variable(const char* name) const
101 read_variable(name, res.get_buffer(), sizeof(T));
105 std::string read_string(RemotePtr<char> address) const;
106 using AddressSpace::read_string;
109 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address) const;
110 void clear_bytes(RemotePtr<void> address, size_t len) const;
112 // Debug information:
113 std::shared_ptr<ObjectInformation> find_object_info(RemotePtr<void> addr) const;
114 std::shared_ptr<ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
115 std::shared_ptr<ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
116 Frame* find_function(RemotePtr<void> ip) const;
117 const Variable* find_variable(const char* name) const;
120 xbt_mheap_t get_heap()
122 if (not(this->cache_flags_ & RemoteProcess::cache_heap))
123 this->refresh_heap();
124 return this->heap.get();
126 const malloc_info* get_malloc_info()
128 if (not(this->cache_flags_ & RemoteProcess::cache_malloc))
129 this->refresh_malloc_info();
130 return this->heap_info.data();
133 void clear_cache() { this->cache_flags_ = RemoteProcess::cache_none; }
135 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
136 void ignore_region(std::uint64_t address, std::size_t size);
138 pid_t pid() const { return pid_; }
140 bool in_maestro_stack(RemotePtr<void> p) const
142 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
145 bool running() const { return running_; }
147 void terminate() { running_ = false; }
149 void ignore_global_variable(const char* name) const
151 for (std::shared_ptr<ObjectInformation> const& info : this->object_infos)
152 info->remove_global_variable(name);
155 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
156 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
158 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
159 void ignore_heap(IgnoredHeapRegion const& region);
160 void unignore_heap(void* address, size_t size);
162 void ignore_local_variable(const char* var_name, const char* frame_name) const;
164 /* ***************** */
165 /* SIMIX-related API */
166 /* ***************** */
168 // Cache the address of the variables we read directly in the memory of remote
171 void* dead_actors_addr_;
174 std::vector<ActorInformation>& actors();
175 std::vector<ActorInformation>& dead_actors();
177 /** Get a local description of a remote SIMIX actor */
178 ActorInformation* resolve_actor_info(RemotePtr<kernel::actor::ActorImpl> actor)
180 xbt_assert(mc_model_checker != nullptr);
183 this->refresh_simix();
184 for (auto& actor_info : this->smx_actors_infos)
185 if (actor_info.address == actor)
187 for (auto& actor_info : this->smx_dead_actors_infos)
188 if (actor_info.address == actor)
193 /** Get a local copy of the SIMIX actor structure */
194 kernel::actor::ActorImpl* resolve_actor(RemotePtr<kernel::actor::ActorImpl> process)
196 ActorInformation* actor_info = this->resolve_actor_info(process);
198 return actor_info->copy.get_buffer();
203 unsigned long get_maxpid() const;
204 void get_actor_vectors(RemotePtr<s_xbt_dynar_t>& actors, RemotePtr<s_xbt_dynar_t>& dead_actors);
206 void dump_stack() const;
209 void init_memory_map_info();
211 void refresh_malloc_info();
212 void refresh_simix();
215 bool running_ = false;
216 std::vector<xbt::VmMap> memory_map_;
217 RemotePtr<void> maestro_stack_start_;
218 RemotePtr<void> maestro_stack_end_;
219 int memory_file = -1;
220 std::vector<IgnoredRegion> ignored_regions_;
221 std::vector<s_stack_region_t> stack_areas_;
222 std::vector<IgnoredHeapRegion> ignored_heap_;
226 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
227 std::vector<std::shared_ptr<ObjectInformation>> object_infos;
228 std::shared_ptr<ObjectInformation> binary_info;
230 // Copies of MCed SMX data structures
231 /** Copy of `simix_global->process_list`
235 std::vector<ActorInformation> smx_actors_infos;
237 /** Copy of `simix_global->actors_to_destroy`
241 std::vector<ActorInformation> smx_dead_actors_infos;
244 /** State of the cache (which variables are up to date) */
245 int cache_flags_ = RemoteProcess::cache_none;
248 /** Address of the heap structure in the MCed process. */
251 /** Copy of the heap structure of the process
253 * This is refreshed with the `MC_process_refresh` call.
254 * This is not used if the process is the current one:
255 * use `get_heap_info()` in order to use it.
257 std::unique_ptr<s_xbt_mheap_t> heap;
259 /** Copy of the allocation info structure
261 * This is refreshed with the `MC_process_refresh` call.
262 * This is not used if the process is the current one:
263 * use `get_malloc_info()` in order to use it.
265 std::vector<malloc_info> heap_info;
268 /** Full-featured MC-aware libunwind address space for the process
270 * This address space is using a simgrid::mc::UnwindContext*
271 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
272 * and unw_context_t).
274 unw_addr_space_t unw_addr_space;
276 /** Underlying libunwind address-space
278 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
279 * operations of the native MC address space is currently delegated
280 * to this address space (either the local or a ptrace unwinder).
282 unw_addr_space_t unw_underlying_addr_space;
284 /** The corresponding context
286 void* unw_underlying_context;
289 /** Open a FD to a remote process memory (`/dev/$pid/mem`) */
290 XBT_PRIVATE int open_vm(pid_t pid, int flags);
292 } // namespace simgrid