1 /* Copyright (c) 2008-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #ifndef SIMGRID_MC_PROCESS_H
7 #define SIMGRID_MC_PROCESS_H
14 #include <type_traits>
17 #include <sys/types.h>
19 #include <simgrid_config.h>
22 #include <xbt/mmalloc.h>
24 #include "src/xbt/mmalloc/mmprivate.h"
26 #include "src/mc/remote/Channel.hpp"
27 #include "src/mc/remote/RemotePtr.hpp"
29 #include "src/simix/popping_private.h"
30 #include "src/simix/smx_private.h"
31 #include <simgrid/simix.h>
33 #include "src/xbt/memory_map.hpp"
35 #include "src/mc/AddressSpace.hpp"
36 #include "src/mc/ObjectInformation.hpp"
37 #include "src/mc/mc_base.h"
38 #include "src/mc/mc_forward.hpp"
39 #include "src/mc/remote/mc_protocol.h"
44 class ActorInformation {
46 /** MCed address of the process */
47 RemotePtr<simgrid::simix::ActorImpl> address = nullptr;
48 Remote<simgrid::simix::ActorImpl> copy;
50 /** Hostname (owned by `mc_modelchecker->hostnames`) */
51 const char* hostname = nullptr;
62 struct IgnoredRegion {
67 struct IgnoredHeapRegion {
74 /** The Model-Checked process, seen from the MCer perspective
76 * This class is mixing a lot of different responsibilities and is tied
77 * to SIMIX. It should probably be split into different classes.
81 * - reading from the process memory (`AddressSpace`);
82 * - accessing the system state of the process (heap, …);
83 * - storing the SIMIX state of the process;
85 * - communication with the model-checked process;
89 class RemoteClient final : public AddressSpace {
91 // Those flags are used to track down which cached information
92 // is still up to date and which information needs to be updated.
93 static constexpr int cache_none = 0;
94 static constexpr int cache_heap = 1;
95 static constexpr int cache_malloc = 2;
96 static constexpr int cache_simix_processes = 4;
99 RemoteClient(pid_t pid, int sockfd);
103 RemoteClient(RemoteClient const&) = delete;
104 RemoteClient(RemoteClient&&) = delete;
105 RemoteClient& operator=(RemoteClient const&) = delete;
106 RemoteClient& operator=(RemoteClient&&) = delete;
109 const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
110 ReadOptions options = ReadOptions::none()) const override;
112 void read_variable(const char* name, void* target, size_t size) const;
113 template <class T> void read_variable(const char* name, T* target) const
115 read_variable(name, target, sizeof(*target));
117 template <class T> Remote<T> read_variable(const char* name) const
120 read_variable(name, res.getBuffer(), sizeof(T));
124 std::string read_string(RemotePtr<char> address) const;
125 std::string read_string(RemotePtr<char> address, std::size_t len) const
127 return AddressSpace::read_string(address, len);
131 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
132 void clear_bytes(RemotePtr<void> address, size_t len);
134 // Debug information:
135 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
136 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
137 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
138 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
139 simgrid::mc::Variable* find_variable(const char* name) const;
142 xbt_mheap_t get_heap()
144 if (not(this->cache_flags_ & RemoteClient::cache_heap))
145 this->refresh_heap();
146 return this->heap.get();
148 const malloc_info* get_malloc_info()
150 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
151 this->refresh_malloc_info();
152 return this->heap_info.data();
155 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
157 Channel const& getChannel() const { return channel_; }
158 Channel& getChannel() { return channel_; }
160 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
161 void ignore_region(std::uint64_t address, std::size_t size);
163 pid_t pid() const { return pid_; }
165 bool in_maestro_stack(RemotePtr<void> p) const
167 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
170 bool running() const { return running_; }
172 void terminate() { running_ = false; }
174 bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
175 bool privatized() const { return privatized_; }
176 void privatized(bool privatized) { privatized_ = privatized; }
178 void ignore_global_variable(const char* name)
180 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
181 info->remove_global_variable(name);
184 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
185 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
187 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
188 void ignore_heap(IgnoredHeapRegion const& region);
189 void unignore_heap(void* address, size_t size);
191 void ignore_local_variable(const char* var_name, const char* frame_name);
192 std::vector<simgrid::mc::ActorInformation>& actors();
193 std::vector<simgrid::mc::ActorInformation>& dead_actors();
195 /** Get a local description of a remote SIMIX actor */
196 simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> actor)
198 xbt_assert(mc_model_checker != nullptr);
201 this->refresh_simix();
202 for (auto& actor_info : this->smx_actors_infos)
203 if (actor_info.address == actor)
205 for (auto& actor_info : this->smx_dead_actors_infos)
206 if (actor_info.address == actor)
211 /** Get a local copy of the SIMIX actor structure */
212 simgrid::simix::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> process)
214 simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
216 return actor_info->copy.getBuffer();
224 void init_memory_map_info();
226 void refresh_malloc_info();
227 void refresh_simix();
232 bool running_ = false;
233 std::vector<simgrid::xbt::VmMap> memory_map_;
234 RemotePtr<void> maestro_stack_start_;
235 RemotePtr<void> maestro_stack_end_;
236 int memory_file = -1;
237 std::vector<IgnoredRegion> ignored_regions_;
238 bool privatized_ = false;
239 std::vector<s_stack_region_t> stack_areas_;
240 std::vector<IgnoredHeapRegion> ignored_heap_;
242 public: // object info
243 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
244 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
245 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
246 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
248 public: // Copies of MCed SMX data structures
249 /** Copy of `simix_global->process_list`
253 std::vector<ActorInformation> smx_actors_infos;
255 /** Copy of `simix_global->process_to_destroy`
259 std::vector<ActorInformation> smx_dead_actors_infos;
262 /** State of the cache (which variables are up to date) */
263 int cache_flags_ = RemoteClient::cache_none;
266 /** Address of the heap structure in the MCed process. */
269 /** Copy of the heap structure of the process
271 * This is refreshed with the `MC_process_refresh` call.
272 * This is not used if the process is the current one:
273 * use `get_heap_info()` in order to use it.
275 std::unique_ptr<s_xbt_mheap_t> heap;
277 /** Copy of the allocation info structure
279 * This is refreshed with the `MC_process_refresh` call.
280 * This is not used if the process is the current one:
281 * use `get_malloc_info()` in order to use it.
283 std::vector<malloc_info> heap_info;
285 public: // Libunwind-data
286 /** Full-featured MC-aware libunwind address space for the process
288 * This address space is using a simgrid::mc::UnwindContext*
289 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
290 * and unw_context_t).
292 unw_addr_space_t unw_addr_space;
294 /** Underlying libunwind address-space
296 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
297 * operations of the native MC address space is currently delegated
298 * to this address space (either the local or a ptrace unwinder).
300 unw_addr_space_t unw_underlying_addr_space;
302 /** The corresponding context
304 void* unw_underlying_context;
306 /* Check whether the given actor is enabled */
307 bool actor_is_enabled(aid_t pid);
310 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
312 XBT_PRIVATE int open_vm(pid_t pid, int flags);