1 /* mc::RemoteClient: representative of the Client memory on the MC side */
3 /* Copyright (c) 2008-2020. 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 "src/mc/AddressSpace.hpp"
12 #include "src/mc/inspect/ObjectInformation.hpp"
13 #include "src/mc/remote/Channel.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_modelchecker->hostnames`) */
29 const char* hostname = nullptr;
40 struct IgnoredRegion {
45 struct IgnoredHeapRegion {
52 /** The Model-Checked process, 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;
63 * - communication with the model-checked process;
67 class RemoteClient 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 RemoteClient(pid_t pid, int sockfd);
81 RemoteClient(RemoteClient const&) = delete;
82 RemoteClient(RemoteClient&&) = delete;
83 RemoteClient& operator=(RemoteClient const&) = delete;
84 RemoteClient& operator=(RemoteClient&&) = delete;
87 void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
88 ReadOptions options = ReadOptions::none()) const override;
90 void read_variable(const char* name, void* target, size_t size) const;
91 template <class T> void read_variable(const char* name, T* target) const
93 read_variable(name, target, sizeof(*target));
95 template <class T> Remote<T> read_variable(const char* name) const
98 read_variable(name, res.get_buffer(), sizeof(T));
102 std::string read_string(RemotePtr<char> address) const;
103 using AddressSpace::read_string;
106 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
107 void clear_bytes(RemotePtr<void> address, size_t len);
109 // Debug information:
110 std::shared_ptr<ObjectInformation> find_object_info(RemotePtr<void> addr) const;
111 std::shared_ptr<ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
112 std::shared_ptr<ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
113 Frame* find_function(RemotePtr<void> ip) const;
114 const Variable* find_variable(const char* name) const;
117 xbt_mheap_t get_heap()
119 if (not(this->cache_flags_ & RemoteClient::cache_heap))
120 this->refresh_heap();
121 return this->heap.get();
123 const malloc_info* get_malloc_info()
125 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
126 this->refresh_malloc_info();
127 return this->heap_info.data();
130 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
132 Channel const& get_channel() const { return channel_; }
133 Channel& get_channel() { return channel_; }
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)
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);
163 std::vector<ActorInformation>& actors();
164 std::vector<ActorInformation>& dead_actors();
166 /** Get a local description of a remote SIMIX actor */
167 ActorInformation* resolve_actor_info(RemotePtr<kernel::actor::ActorImpl> actor)
169 xbt_assert(mc_model_checker != nullptr);
172 this->refresh_simix();
173 for (auto& actor_info : this->smx_actors_infos)
174 if (actor_info.address == actor)
176 for (auto& actor_info : this->smx_dead_actors_infos)
177 if (actor_info.address == actor)
182 /** Get a local copy of the SIMIX actor structure */
183 kernel::actor::ActorImpl* resolve_actor(RemotePtr<kernel::actor::ActorImpl> process)
185 ActorInformation* actor_info = this->resolve_actor_info(process);
187 return actor_info->copy.get_buffer();
195 void init_memory_map_info();
197 void refresh_malloc_info();
198 void refresh_simix();
202 bool running_ = false;
203 std::vector<xbt::VmMap> memory_map_;
204 RemotePtr<void> maestro_stack_start_;
205 RemotePtr<void> maestro_stack_end_;
206 int memory_file = -1;
207 std::vector<IgnoredRegion> ignored_regions_;
208 std::vector<s_stack_region_t> stack_areas_;
209 std::vector<IgnoredHeapRegion> ignored_heap_;
213 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
214 std::vector<std::shared_ptr<ObjectInformation>> object_infos;
215 std::shared_ptr<ObjectInformation> libsimgrid_info;
216 std::shared_ptr<ObjectInformation> binary_info;
218 // Copies of MCed SMX data structures
219 /** Copy of `simix_global->process_list`
223 std::vector<ActorInformation> smx_actors_infos;
225 /** Copy of `simix_global->actors_to_destroy`
229 std::vector<ActorInformation> smx_dead_actors_infos;
232 /** State of the cache (which variables are up to date) */
233 int cache_flags_ = RemoteClient::cache_none;
236 /** Address of the heap structure in the MCed process. */
239 /** Copy of the heap structure of the process
241 * This is refreshed with the `MC_process_refresh` call.
242 * This is not used if the process is the current one:
243 * use `get_heap_info()` in order to use it.
245 std::unique_ptr<s_xbt_mheap_t> heap;
247 /** Copy of the allocation info structure
249 * This is refreshed with the `MC_process_refresh` call.
250 * This is not used if the process is the current one:
251 * use `get_malloc_info()` in order to use it.
253 std::vector<malloc_info> heap_info;
256 /** Full-featured MC-aware libunwind address space for the process
258 * This address space is using a simgrid::mc::UnwindContext*
259 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
260 * and unw_context_t).
262 unw_addr_space_t unw_addr_space;
264 /** Underlying libunwind address-space
266 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
267 * operations of the native MC address space is currently delegated
268 * to this address space (either the local or a ptrace unwinder).
270 unw_addr_space_t unw_underlying_addr_space;
272 /** The corresponding context
274 void* unw_underlying_context;
276 /* Check whether the given actor is enabled */
277 bool actor_is_enabled(aid_t pid);
280 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
282 XBT_PRIVATE int open_vm(pid_t pid, int flags);
284 } // namespace simgrid