1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_PROCESS_H
8 #define SIMGRID_MC_PROCESS_H
13 #include <type_traits>
18 #include <sys/types.h>
20 #include <simgrid_config.h>
23 #include <xbt/mmalloc.h>
25 #include "src/xbt/mmalloc/mmprivate.h"
26 #include "src/mc/Channel.hpp"
28 #include <simgrid/simix.h>
29 #include "src/simix/popping_private.h"
30 #include "src/simix/smx_private.h"
32 #include "src/xbt/memory_map.hpp"
34 #include "src/mc/mc_forward.hpp"
35 #include "src/mc/mc_base.h"
36 #include "src/mc/AddressSpace.hpp"
37 #include "src/mc/mc_protocol.h"
38 #include "src/mc/ObjectInformation.hpp"
44 class SimixProcessInformation {
46 /** MCed address of the process */
47 void* address = nullptr;
49 /** (Flat) Copy of the process data structure */
50 struct s_smx_process copy;
52 /** Hostname (owned by `mc_modelchecker->hostnames`) */
53 const char* hostname = nullptr;
64 struct IgnoredRegion {
69 struct IgnoredHeapRegion {
76 /** Representation of a process
78 * This class is mixing a lot of differents responsabilities and is tied
79 * to SIMIX. It should probably split into different classes.
83 * - reading from the process memory (`AddressSpace`);
84 * - accessing the system state of the porcess (heap, …);
85 * - storing the SIMIX state of the process;
87 * - communication with the model-checked process;
91 class Process final : public AddressSpace {
93 // Those flags are used to track down which cached information
94 // is still up to date and which information needs to be updated.
95 static constexpr int cache_none = 0;
96 static constexpr int cache_heap = 1;
97 static constexpr int cache_malloc = 2;
98 static constexpr int cache_simix_processes = 4;
100 Process(pid_t pid, int sockfd);
104 Process(Process const&) = delete;
105 Process(Process &&) = delete;
106 Process& operator=(Process const&) = delete;
107 Process& operator=(Process &&) = delete;
110 const void* read_bytes(void* buffer, std::size_t size,
111 RemotePtr<void> address, int process_index = ProcessIndexAny,
112 ReadOptions options = ReadOptions::none()) const override;
113 void read_variable(const char* name, void* target, size_t size) const;
115 T read_variable(const char *name) const
117 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
119 read_variable(name, &res, sizeof(T));
122 std::string read_string(RemotePtr<void> address) const;
125 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
126 void clear_bytes(RemotePtr<void> address, size_t len);
128 // Debug information:
129 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
130 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
131 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
132 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
133 simgrid::mc::Variable* find_variable(const char* name) const;
136 xbt_mheap_t get_heap()
138 if (!(this->cache_flags_ & Process::cache_heap))
139 this->refresh_heap();
140 return this->heap.get();
142 malloc_info* get_malloc_info()
144 if (!(this->cache_flags_ & Process::cache_malloc))
145 this->refresh_malloc_info();
146 return this->heap_info.data();
151 this->cache_flags_ = Process::cache_none;
154 Channel const& getChannel() const { return channel_; }
155 Channel& getChannel() { return channel_; }
157 std::vector<IgnoredRegion> const& ignored_regions() const
159 return ignored_regions_;
161 void ignore_region(std::uint64_t address, std::size_t size);
163 pid_t pid() const { return pid_; }
165 bool in_maestro_stack(RemotePtr<void> p) const
167 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
180 void reset_soft_dirty();
181 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
183 bool privatized(ObjectInformation const& info) const
185 return privatized_ && info.executable();
187 bool privatized() const
191 void privatized(bool privatized) { privatized_ = privatized; }
193 void ignore_global_variable(const char* name)
195 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
197 info->remove_global_variable(name);
200 std::vector<s_stack_region_t>& stack_areas()
204 std::vector<s_stack_region_t> const& stack_areas() const
209 std::vector<IgnoredHeapRegion> const& ignored_heap() const
211 return ignored_heap_;
213 void ignore_heap(IgnoredHeapRegion const& region);
214 void unignore_heap(void *address, size_t size);
216 void ignore_local_variable(const char *var_name, const char *frame_name);
217 std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
218 std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
223 void init_memory_map_info();
225 void refresh_malloc_info();
226 void refresh_simix();
231 bool running_ = false;
232 std::vector<simgrid::xbt::VmMap> memory_map_;
233 RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
234 int memory_file = -1;
235 std::vector<IgnoredRegion> ignored_regions_;
236 int clear_refs_fd_ = -1;
237 int pagemap_fd_ = -1;
238 bool privatized_ = false;
239 std::vector<s_stack_region_t> stack_areas_;
240 std::vector<IgnoredHeapRegion> ignored_heap_;
242 public: // object info
243 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
244 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
245 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
246 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
248 public: // Copies of MCed SMX data structures
249 /** Copy of `simix_global->process_list`
253 std::vector<SimixProcessInformation> smx_process_infos;
255 /** Copy of `simix_global->process_to_destroy`
259 std::vector<SimixProcessInformation> smx_old_process_infos;
262 /** State of the cache (which variables are up to date) */
263 int cache_flags_ = Process::cache_none;
266 /** Address of the heap structure in the MCed process. */
269 /** Copy of the heap structure of the process
271 * This is refreshed with the `MC_process_refresh` call.
272 * This is not used if the process is the current one:
273 * use `get_heap_info()` in order to use it.
275 std::unique_ptr<s_xbt_mheap_t> heap;
277 /** Copy of the allocation info structure
279 * This is refreshed with the `MC_process_refresh` call.
280 * This is not used if the process is the current one:
281 * use `get_malloc_info()` in order to use it.
283 std::vector<malloc_info> heap_info;
285 public: // Libunwind-data
287 /** Full-featured MC-aware libunwind address space for the process
289 * This address space is using a simgrid::mc::UnwindContext*
290 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
291 * and unw_context_t).
293 unw_addr_space_t unw_addr_space;
295 /** Underlying libunwind addres-space
297 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
298 * operations of the native MC address space is currently delegated
299 * to this address space (either the local or a ptrace unwinder).
301 unw_addr_space_t unw_underlying_addr_space;
303 /** The corresponding context
305 void* unw_underlying_context;
308 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
310 XBT_PRIVATE int open_vm(pid_t pid, int flags);