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.hpp"
30 #include "src/simix/smx_private.hpp"
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 using AddressSpace::read_string;
128 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
129 void clear_bytes(RemotePtr<void> address, size_t len);
131 // Debug information:
132 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
133 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
134 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
135 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
136 simgrid::mc::Variable* find_variable(const char* name) const;
139 xbt_mheap_t get_heap()
141 if (not(this->cache_flags_ & RemoteClient::cache_heap))
142 this->refresh_heap();
143 return this->heap.get();
145 const malloc_info* get_malloc_info()
147 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
148 this->refresh_malloc_info();
149 return this->heap_info.data();
152 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
154 Channel const& getChannel() const { return channel_; }
155 Channel& getChannel() { return channel_; }
157 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
158 void ignore_region(std::uint64_t address, std::size_t size);
160 pid_t pid() const { return pid_; }
162 bool in_maestro_stack(RemotePtr<void> p) const
164 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
167 bool running() const { return running_; }
169 void terminate() { running_ = false; }
171 bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
172 bool privatized() const { return privatized_; }
173 void privatized(bool privatized) { privatized_ = privatized; }
175 void ignore_global_variable(const char* name)
177 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
178 info->remove_global_variable(name);
181 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
182 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
184 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
185 void ignore_heap(IgnoredHeapRegion const& region);
186 void unignore_heap(void* address, size_t size);
188 void ignore_local_variable(const char* var_name, const char* frame_name);
189 std::vector<simgrid::mc::ActorInformation>& actors();
190 std::vector<simgrid::mc::ActorInformation>& dead_actors();
192 /** Get a local description of a remote SIMIX actor */
193 simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> actor)
195 xbt_assert(mc_model_checker != nullptr);
198 this->refresh_simix();
199 for (auto& actor_info : this->smx_actors_infos)
200 if (actor_info.address == actor)
202 for (auto& actor_info : this->smx_dead_actors_infos)
203 if (actor_info.address == actor)
208 /** Get a local copy of the SIMIX actor structure */
209 simgrid::simix::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> process)
211 simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
213 return actor_info->copy.getBuffer();
221 void init_memory_map_info();
223 void refresh_malloc_info();
224 void refresh_simix();
228 bool running_ = false;
229 std::vector<simgrid::xbt::VmMap> memory_map_;
230 RemotePtr<void> maestro_stack_start_;
231 RemotePtr<void> maestro_stack_end_;
232 int memory_file = -1;
233 std::vector<IgnoredRegion> ignored_regions_;
234 bool privatized_ = false;
235 std::vector<s_stack_region_t> stack_areas_;
236 std::vector<IgnoredHeapRegion> ignored_heap_;
240 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
241 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
242 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
243 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
245 // Copies of MCed SMX data structures
246 /** Copy of `simix_global->process_list`
250 std::vector<ActorInformation> smx_actors_infos;
252 /** Copy of `simix_global->process_to_destroy`
256 std::vector<ActorInformation> smx_dead_actors_infos;
259 /** State of the cache (which variables are up to date) */
260 int cache_flags_ = RemoteClient::cache_none;
263 /** Address of the heap structure in the MCed process. */
266 /** Copy of the heap structure of the process
268 * This is refreshed with the `MC_process_refresh` call.
269 * This is not used if the process is the current one:
270 * use `get_heap_info()` in order to use it.
272 std::unique_ptr<s_xbt_mheap_t> heap;
274 /** Copy of the allocation info structure
276 * This is refreshed with the `MC_process_refresh` call.
277 * This is not used if the process is the current one:
278 * use `get_malloc_info()` in order to use it.
280 std::vector<malloc_info> heap_info;
283 /** Full-featured MC-aware libunwind address space for the process
285 * This address space is using a simgrid::mc::UnwindContext*
286 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
287 * and unw_context_t).
289 unw_addr_space_t unw_addr_space;
291 /** Underlying libunwind address-space
293 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
294 * operations of the native MC address space is currently delegated
295 * to this address space (either the local or a ptrace unwinder).
297 unw_addr_space_t unw_underlying_addr_space;
299 /** The corresponding context
301 void* unw_underlying_context;
303 /* Check whether the given actor is enabled */
304 bool actor_is_enabled(aid_t pid);
307 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
309 XBT_PRIVATE int open_vm(pid_t pid, int flags);