1 /* Copyright (c) 2008-2018. 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/xbt/mmalloc/mmprivate.h"
10 #include "src/mc/remote/Channel.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "src/mc/ObjectInformation.hpp"
19 class ActorInformation {
21 /** MCed address of the process */
22 RemotePtr<simgrid::kernel::actor::ActorImpl> address{nullptr};
23 Remote<simgrid::kernel::actor::ActorImpl> copy;
25 /** Hostname (owned by `mc_modelchecker->hostnames`) */
26 const char* hostname = nullptr;
37 struct IgnoredRegion {
42 struct IgnoredHeapRegion {
49 /** The Model-Checked process, seen from the MCer perspective
51 * This class is mixing a lot of different responsibilities and is tied
52 * to SIMIX. It should probably be split into different classes.
56 * - reading from the process memory (`AddressSpace`);
57 * - accessing the system state of the process (heap, …);
58 * - storing the SIMIX state of the process;
60 * - communication with the model-checked process;
64 class RemoteClient final : public AddressSpace {
66 // Those flags are used to track down which cached information
67 // is still up to date and which information needs to be updated.
68 static constexpr int cache_none = 0;
69 static constexpr int cache_heap = 1;
70 static constexpr int cache_malloc = 2;
71 static constexpr int cache_simix_processes = 4;
74 RemoteClient(pid_t pid, int sockfd);
78 RemoteClient(RemoteClient const&) = delete;
79 RemoteClient(RemoteClient&&) = delete;
80 RemoteClient& operator=(RemoteClient const&) = delete;
81 RemoteClient& operator=(RemoteClient&&) = delete;
84 const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
85 ReadOptions options = ReadOptions::none()) const override;
87 void read_variable(const char* name, void* target, size_t size) const;
88 template <class T> void read_variable(const char* name, T* target) const
90 read_variable(name, target, sizeof(*target));
92 template <class T> Remote<T> read_variable(const char* name) const
95 read_variable(name, res.getBuffer(), sizeof(T));
99 std::string read_string(RemotePtr<char> address) const;
100 using AddressSpace::read_string;
103 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
104 void clear_bytes(RemotePtr<void> address, size_t len);
106 // Debug information:
107 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
108 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
109 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
110 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
111 simgrid::mc::Variable* find_variable(const char* name) const;
114 xbt_mheap_t get_heap()
116 if (not(this->cache_flags_ & RemoteClient::cache_heap))
117 this->refresh_heap();
118 return this->heap.get();
120 const malloc_info* get_malloc_info()
122 if (not(this->cache_flags_ & RemoteClient::cache_malloc))
123 this->refresh_malloc_info();
124 return this->heap_info.data();
127 void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
129 Channel const& getChannel() const { return channel_; }
130 Channel& getChannel() { return channel_; }
132 std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
133 void ignore_region(std::uint64_t address, std::size_t size);
135 pid_t pid() const { return pid_; }
137 bool in_maestro_stack(RemotePtr<void> p) const
139 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
142 bool running() const { return running_; }
144 void terminate() { running_ = false; }
146 bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
147 bool privatized() const { return privatized_; }
148 void privatized(bool privatized) { privatized_ = privatized; }
150 void ignore_global_variable(const char* name)
152 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
153 info->remove_global_variable(name);
156 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
157 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
159 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
160 void ignore_heap(IgnoredHeapRegion const& region);
161 void unignore_heap(void* address, size_t size);
163 void ignore_local_variable(const char* var_name, const char* frame_name);
164 std::vector<simgrid::mc::ActorInformation>& actors();
165 std::vector<simgrid::mc::ActorInformation>& dead_actors();
167 /** Get a local description of a remote SIMIX actor */
168 simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
170 xbt_assert(mc_model_checker != nullptr);
173 this->refresh_simix();
174 for (auto& actor_info : this->smx_actors_infos)
175 if (actor_info.address == actor)
177 for (auto& actor_info : this->smx_dead_actors_infos)
178 if (actor_info.address == actor)
183 /** Get a local copy of the SIMIX actor structure */
184 simgrid::kernel::actor::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
186 simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
188 return actor_info->copy.getBuffer();
196 void init_memory_map_info();
198 void refresh_malloc_info();
199 void refresh_simix();
203 bool running_ = false;
204 std::vector<simgrid::xbt::VmMap> memory_map_;
205 RemotePtr<void> maestro_stack_start_;
206 RemotePtr<void> maestro_stack_end_;
207 int memory_file = -1;
208 std::vector<IgnoredRegion> ignored_regions_;
209 bool privatized_ = false;
210 std::vector<s_stack_region_t> stack_areas_;
211 std::vector<IgnoredHeapRegion> ignored_heap_;
215 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
216 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
217 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
218 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
220 // Copies of MCed SMX data structures
221 /** Copy of `simix_global->process_list`
225 std::vector<ActorInformation> smx_actors_infos;
227 /** Copy of `simix_global->process_to_destroy`
231 std::vector<ActorInformation> smx_dead_actors_infos;
234 /** State of the cache (which variables are up to date) */
235 int cache_flags_ = RemoteClient::cache_none;
238 /** Address of the heap structure in the MCed process. */
241 /** Copy of the heap structure of the process
243 * This is refreshed with the `MC_process_refresh` call.
244 * This is not used if the process is the current one:
245 * use `get_heap_info()` in order to use it.
247 std::unique_ptr<s_xbt_mheap_t> heap;
249 /** Copy of the allocation info structure
251 * This is refreshed with the `MC_process_refresh` call.
252 * This is not used if the process is the current one:
253 * use `get_malloc_info()` in order to use it.
255 std::vector<malloc_info> heap_info;
258 /** Full-featured MC-aware libunwind address space for the process
260 * This address space is using a simgrid::mc::UnwindContext*
261 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
262 * and unw_context_t).
264 unw_addr_space_t unw_addr_space;
266 /** Underlying libunwind address-space
268 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
269 * operations of the native MC address space is currently delegated
270 * to this address space (either the local or a ptrace unwinder).
272 unw_addr_space_t unw_underlying_addr_space;
274 /** The corresponding context
276 void* unw_underlying_context;
278 /* Check whether the given actor is enabled */
279 bool actor_is_enabled(aid_t pid);
282 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
284 XBT_PRIVATE int open_vm(pid_t pid, int flags);