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/xbt/mmalloc/mmprivate.h"
10 #include "src/mc/remote/Channel.hpp"
11 #include "src/mc/ObjectInformation.hpp"
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, int process_index = ProcessIndexAny,
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.getBuffer(), 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 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& getChannel() const { return channel_; }
129 Channel& getChannel() { 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 bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
146 bool privatized() const { return privatized_; }
147 void privatized(bool privatized) { privatized_ = privatized; }
149 void ignore_global_variable(const char* name)
151 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
152 info->remove_global_variable(name);
155 std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
156 std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
158 std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
159 void ignore_heap(IgnoredHeapRegion const& region);
160 void unignore_heap(void* address, size_t size);
162 void ignore_local_variable(const char* var_name, const char* frame_name);
163 std::vector<simgrid::mc::ActorInformation>& actors();
164 std::vector<simgrid::mc::ActorInformation>& dead_actors();
166 /** Get a local description of a remote SIMIX actor */
167 simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
169 xbt_assert(mc_model_checker != nullptr);
172 this->refresh_simix();
173 for (auto& actor_info : this->smx_actors_infos)
174 if (actor_info.address == actor)
176 for (auto& actor_info : this->smx_dead_actors_infos)
177 if (actor_info.address == actor)
182 /** Get a local copy of the SIMIX actor structure */
183 simgrid::kernel::actor::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
185 simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
187 return actor_info->copy.getBuffer();
195 void init_memory_map_info();
197 void refresh_malloc_info();
198 void refresh_simix();
202 bool running_ = false;
203 std::vector<simgrid::xbt::VmMap> memory_map_;
204 RemotePtr<void> maestro_stack_start_;
205 RemotePtr<void> maestro_stack_end_;
206 int memory_file = -1;
207 std::vector<IgnoredRegion> ignored_regions_;
208 bool privatized_ = false;
209 std::vector<s_stack_region_t> stack_areas_;
210 std::vector<IgnoredHeapRegion> ignored_heap_;
214 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
215 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
216 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
217 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
219 // Copies of MCed SMX data structures
220 /** Copy of `simix_global->process_list`
224 std::vector<ActorInformation> smx_actors_infos;
226 /** Copy of `simix_global->process_to_destroy`
230 std::vector<ActorInformation> smx_dead_actors_infos;
233 /** State of the cache (which variables are up to date) */
234 int cache_flags_ = RemoteClient::cache_none;
237 /** Address of the heap structure in the MCed process. */
240 /** Copy of the heap structure of the process
242 * This is refreshed with the `MC_process_refresh` call.
243 * This is not used if the process is the current one:
244 * use `get_heap_info()` in order to use it.
246 std::unique_ptr<s_xbt_mheap_t> heap;
248 /** Copy of the allocation info structure
250 * This is refreshed with the `MC_process_refresh` call.
251 * This is not used if the process is the current one:
252 * use `get_malloc_info()` in order to use it.
254 std::vector<malloc_info> heap_info;
257 /** Full-featured MC-aware libunwind address space for the process
259 * This address space is using a simgrid::mc::UnwindContext*
260 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
261 * and unw_context_t).
263 unw_addr_space_t unw_addr_space;
265 /** Underlying libunwind address-space
267 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
268 * operations of the native MC address space is currently delegated
269 * to this address space (either the local or a ptrace unwinder).
271 unw_addr_space_t unw_underlying_addr_space;
273 /** The corresponding context
275 void* unw_underlying_context;
277 /* Check whether the given actor is enabled */
278 bool actor_is_enabled(aid_t pid);
281 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
283 XBT_PRIVATE int open_vm(pid_t pid, int flags);