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/inspect/ObjectInformation.hpp"
10 #include "src/mc/remote/Channel.hpp"
11 #include "src/xbt/mmalloc/mmprivate.h"
18 class ActorInformation {
20 /** MCed address of the process */
21 RemotePtr<simgrid::kernel::actor::ActorImpl> address{nullptr};
22 Remote<simgrid::kernel::actor::ActorImpl> copy;
24 /** Hostname (owned by `mc_modelchecker->hostnames`) */
25 const char* hostname = nullptr;
36 struct IgnoredRegion {
41 struct IgnoredHeapRegion {
48 /** The Model-Checked process, seen from the MCer perspective
50 * This class is mixing a lot of different responsibilities and is tied
51 * to SIMIX. It should probably be split into different classes.
55 * - reading from the process memory (`AddressSpace`);
56 * - accessing the system state of the process (heap, …);
57 * - storing the SIMIX state of the process;
59 * - communication with the model-checked process;
63 class RemoteClient final : public AddressSpace {
65 // Those flags are used to track down which cached information
66 // is still up to date and which information needs to be updated.
67 static constexpr int cache_none = 0;
68 static constexpr int cache_heap = 1;
69 static constexpr int cache_malloc = 2;
70 static constexpr int cache_simix_processes = 4;
73 RemoteClient(pid_t pid, int sockfd);
77 RemoteClient(RemoteClient const&) = delete;
78 RemoteClient(RemoteClient&&) = delete;
79 RemoteClient& operator=(RemoteClient const&) = delete;
80 RemoteClient& operator=(RemoteClient&&) = delete;
83 const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
84 ReadOptions options = ReadOptions::none()) const override;
86 void read_variable(const char* name, void* target, size_t size) const;
87 template <class T> void read_variable(const char* name, T* target) const
89 read_variable(name, target, sizeof(*target));
91 template <class T> Remote<T> read_variable(const char* name) const
94 read_variable(name, res.get_buffer(), sizeof(T));
98 std::string read_string(RemotePtr<char> address) const;
99 using AddressSpace::read_string;
102 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
103 void clear_bytes(RemotePtr<void> address, size_t len);
105 // Debug information:
106 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
107 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
108 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
109 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
110 const simgrid::mc::Variable* find_variable(const char* name) const;
113 xbt_mheap_t get_heap()
115 if (not(this->cache_flags_ & RemoteClient::cache_heap))
116 this->refresh_heap();
117 return this->heap.get();
119 const malloc_info* get_malloc_info()
121 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
122 this->refresh_malloc_info();
123 return this->heap_info.data();
126 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
128 Channel const& get_channel() const { return channel_; }
129 Channel& get_channel() { return channel_; }
131 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
132 void ignore_region(std::uint64_t address, std::size_t size);
134 pid_t pid() const { return pid_; }
136 bool in_maestro_stack(RemotePtr<void> p) const
138 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
141 bool running() const { return running_; }
143 void terminate() { running_ = false; }
145 void ignore_global_variable(const char* name)
147 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
148 info->remove_global_variable(name);
151 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
152 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
154 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
155 void ignore_heap(IgnoredHeapRegion const& region);
156 void unignore_heap(void* address, size_t size);
158 void ignore_local_variable(const char* var_name, const char* frame_name);
159 std::vector<simgrid::mc::ActorInformation>& actors();
160 std::vector<simgrid::mc::ActorInformation>& dead_actors();
162 /** Get a local description of a remote SIMIX actor */
163 simgrid::mc::ActorInformation* resolve_actor_info(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
165 xbt_assert(mc_model_checker != nullptr);
168 this->refresh_simix();
169 for (auto& actor_info : this->smx_actors_infos)
170 if (actor_info.address == actor)
172 for (auto& actor_info : this->smx_dead_actors_infos)
173 if (actor_info.address == actor)
178 /** Get a local copy of the SIMIX actor structure */
179 simgrid::kernel::actor::ActorImpl* resolve_actor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
181 simgrid::mc::ActorInformation* actor_info = this->resolve_actor_info(process);
183 return actor_info->copy.get_buffer();
191 void init_memory_map_info();
193 void refresh_malloc_info();
194 void refresh_simix();
198 bool running_ = false;
199 std::vector<simgrid::xbt::VmMap> memory_map_;
200 RemotePtr<void> maestro_stack_start_;
201 RemotePtr<void> maestro_stack_end_;
202 int memory_file = -1;
203 std::vector<IgnoredRegion> ignored_regions_;
204 std::vector<s_stack_region_t> stack_areas_;
205 std::vector<IgnoredHeapRegion> ignored_heap_;
209 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
210 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
211 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
212 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
214 // Copies of MCed SMX data structures
215 /** Copy of `simix_global->process_list`
219 std::vector<ActorInformation> smx_actors_infos;
221 /** Copy of `simix_global->actors_to_destroy`
225 std::vector<ActorInformation> smx_dead_actors_infos;
228 /** State of the cache (which variables are up to date) */
229 int cache_flags_ = RemoteClient::cache_none;
232 /** Address of the heap structure in the MCed process. */
235 /** Copy of the heap structure of the process
237 * This is refreshed with the `MC_process_refresh` call.
238 * This is not used if the process is the current one:
239 * use `get_heap_info()` in order to use it.
241 std::unique_ptr<s_xbt_mheap_t> heap;
243 /** Copy of the allocation info structure
245 * This is refreshed with the `MC_process_refresh` call.
246 * This is not used if the process is the current one:
247 * use `get_malloc_info()` in order to use it.
249 std::vector<malloc_info> heap_info;
252 /** Full-featured MC-aware libunwind address space for the process
254 * This address space is using a simgrid::mc::UnwindContext*
255 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
256 * and unw_context_t).
258 unw_addr_space_t unw_addr_space;
260 /** Underlying libunwind address-space
262 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
263 * operations of the native MC address space is currently delegated
264 * to this address space (either the local or a ptrace unwinder).
266 unw_addr_space_t unw_underlying_addr_space;
268 /** The corresponding context
270 void* unw_underlying_context;
272 /* Check whether the given actor is enabled */
273 bool actor_is_enabled(aid_t pid);
276 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
278 XBT_PRIVATE int open_vm(pid_t pid, int flags);