1 /* Copyright (c) 2008-2019. 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
9 #include "src/mc/AddressSpace.hpp"
10 #include "src/mc/inspect/ObjectInformation.hpp"
11 #include "src/mc/remote/Channel.hpp"
12 #include "src/mc/remote/RemotePtr.hpp"
13 #include "src/xbt/mmalloc/mmprivate.h"
20 class ActorInformation {
22 /** MCed address of the process */
23 RemotePtr<simgrid::kernel::actor::ActorImpl> address{nullptr};
24 Remote<simgrid::kernel::actor::ActorImpl> copy;
26 /** Hostname (owned by `mc_modelchecker->hostnames`) */
27 const char* hostname = nullptr;
38 struct IgnoredRegion {
43 struct IgnoredHeapRegion {
50 /** The Model-Checked process, seen from the MCer perspective
52 * This class is mixing a lot of different responsibilities and is tied
53 * to SIMIX. It should probably be split into different classes.
57 * - reading from the process memory (`AddressSpace`);
58 * - accessing the system state of the process (heap, …);
59 * - storing the SIMIX state of the process;
61 * - communication with the model-checked process;
65 class RemoteClient final : public AddressSpace {
67 // Those flags are used to track down which cached information
68 // is still up to date and which information needs to be updated.
69 static constexpr int cache_none = 0;
70 static constexpr int cache_heap = 1;
71 static constexpr int cache_malloc = 2;
72 static constexpr int cache_simix_processes = 4;
75 RemoteClient(pid_t pid, int sockfd);
79 RemoteClient(RemoteClient const&) = delete;
80 RemoteClient(RemoteClient&&) = delete;
81 RemoteClient& operator=(RemoteClient const&) = delete;
82 RemoteClient& operator=(RemoteClient&&) = delete;
85 void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
86 ReadOptions options = ReadOptions::none()) const override;
88 void read_variable(const char* name, void* target, size_t size) const;
89 template <class T> void read_variable(const char* name, T* target) const
91 read_variable(name, target, sizeof(*target));
93 template <class T> Remote<T> read_variable(const char* name) const
96 read_variable(name, res.get_buffer(), sizeof(T));
100 std::string read_string(RemotePtr<char> address) const;
101 using AddressSpace::read_string;
104 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
105 void clear_bytes(RemotePtr<void> address, size_t len);
107 // Debug information:
108 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
109 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
110 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
111 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
112 const simgrid::mc::Variable* find_variable(const char* name) const;
115 xbt_mheap_t get_heap()
117 if (not(this->cache_flags_ & RemoteClient::cache_heap))
118 this->refresh_heap();
119 return this->heap.get();
121 const malloc_info* get_malloc_info()
123 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
124 this->refresh_malloc_info();
125 return this->heap_info.data();
128 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
130 Channel const& get_channel() const { return channel_; }
131 Channel& get_channel() { return channel_; }
133 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
134 void ignore_region(std::uint64_t address, std::size_t size);
136 pid_t pid() const { return pid_; }
138 bool in_maestro_stack(RemotePtr<void> p) const
140 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
143 bool running() const { return running_; }
145 void terminate() { running_ = false; }
147 void ignore_global_variable(const char* name)
149 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
150 info->remove_global_variable(name);
153 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
154 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
156 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
157 void ignore_heap(IgnoredHeapRegion const& region);
158 void unignore_heap(void* address, size_t size);
160 void ignore_local_variable(const char* var_name, const char* frame_name);
161 std::vector<simgrid::mc::ActorInformation>& actors();
162 std::vector<simgrid::mc::ActorInformation>& dead_actors();
164 /** Get a local description of a remote SIMIX actor */
165 simgrid::mc::ActorInformation* resolve_actor_info(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
167 xbt_assert(mc_model_checker != nullptr);
170 this->refresh_simix();
171 for (auto& actor_info : this->smx_actors_infos)
172 if (actor_info.address == actor)
174 for (auto& actor_info : this->smx_dead_actors_infos)
175 if (actor_info.address == actor)
180 /** Get a local copy of the SIMIX actor structure */
181 simgrid::kernel::actor::ActorImpl* resolve_actor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
183 simgrid::mc::ActorInformation* actor_info = this->resolve_actor_info(process);
185 return actor_info->copy.get_buffer();
193 void init_memory_map_info();
195 void refresh_malloc_info();
196 void refresh_simix();
200 bool running_ = false;
201 std::vector<simgrid::xbt::VmMap> memory_map_;
202 RemotePtr<void> maestro_stack_start_;
203 RemotePtr<void> maestro_stack_end_;
204 int memory_file = -1;
205 std::vector<IgnoredRegion> ignored_regions_;
206 std::vector<s_stack_region_t> stack_areas_;
207 std::vector<IgnoredHeapRegion> ignored_heap_;
211 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
212 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
213 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
214 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
216 // Copies of MCed SMX data structures
217 /** Copy of `simix_global->process_list`
221 std::vector<ActorInformation> smx_actors_infos;
223 /** Copy of `simix_global->actors_to_destroy`
227 std::vector<ActorInformation> smx_dead_actors_infos;
230 /** State of the cache (which variables are up to date) */
231 int cache_flags_ = RemoteClient::cache_none;
234 /** Address of the heap structure in the MCed process. */
237 /** Copy of the heap structure of the process
239 * This is refreshed with the `MC_process_refresh` call.
240 * This is not used if the process is the current one:
241 * use `get_heap_info()` in order to use it.
243 std::unique_ptr<s_xbt_mheap_t> heap;
245 /** Copy of the allocation info structure
247 * This is refreshed with the `MC_process_refresh` call.
248 * This is not used if the process is the current one:
249 * use `get_malloc_info()` in order to use it.
251 std::vector<malloc_info> heap_info;
254 /** Full-featured MC-aware libunwind address space for the process
256 * This address space is using a simgrid::mc::UnwindContext*
257 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
258 * and unw_context_t).
260 unw_addr_space_t unw_addr_space;
262 /** Underlying libunwind address-space
264 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
265 * operations of the native MC address space is currently delegated
266 * to this address space (either the local or a ptrace unwinder).
268 unw_addr_space_t unw_underlying_addr_space;
270 /** The corresponding context
272 void* unw_underlying_context;
274 /* Check whether the given actor is enabled */
275 bool actor_is_enabled(aid_t pid);
278 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
280 XBT_PRIVATE int open_vm(pid_t pid, int flags);