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>
17 #include <sys/types.h>
19 #include <simgrid_config.h>
22 #include <xbt/mmalloc.h>
24 #include "src/xbt/mmalloc/mmprivate.h"
25 #include "src/mc/Channel.hpp"
27 #include <simgrid/simix.h>
28 #include "src/simix/popping_private.h"
29 #include "src/simix/smx_private.h"
31 #include "src/xbt/memory_map.hpp"
33 #include "src/mc/mc_forward.hpp"
34 #include "src/mc/mc_base.h"
35 #include "src/mc/AddressSpace.hpp"
36 #include "src/mc/mc_protocol.h"
37 #include "src/mc/ObjectInformation.hpp"
43 class SimixProcessInformation {
45 /** MCed address of the process */
46 void* address = nullptr;
48 /** (Flat) Copy of the process data structure */
49 struct s_smx_process copy;
51 /** Hostname (owned by `mc_modelchecker->hostnames`) */
52 const char* hostname = nullptr;
63 struct IgnoredRegion {
68 struct IgnoredHeapRegion {
75 /** Representation of a process
77 * This class is mixing a lot of differents responsabilities and is tied
78 * to SIMIX. It should probably split into different classes.
82 * - reading from the process memory (`AddressSpace`);
83 * - accessing the system state of the porcess (heap, …);
84 * - storing the SIMIX state of the process;
86 * - communication with the model-checked process;
90 class Process final : public AddressSpace {
92 // Those flags are used to track down which cached information
93 // is still up to date and which information needs to be updated.
94 static constexpr int cache_none = 0;
95 static constexpr int cache_heap = 1;
96 static constexpr int cache_malloc = 2;
97 static constexpr int cache_simix_processes = 4;
99 Process(pid_t pid, int sockfd);
103 Process(Process const&) = delete;
104 Process(Process &&) = delete;
105 Process& operator=(Process const&) = delete;
106 Process& operator=(Process &&) = delete;
109 const void* read_bytes(void* buffer, std::size_t size,
110 RemotePtr<void> address, int process_index = ProcessIndexAny,
111 ReadOptions options = ReadOptions::none()) const override;
112 void read_variable(const char* name, void* target, size_t size) const;
114 T read_variable(const char *name) const
116 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
118 read_variable(name, &res, sizeof(T));
121 char* read_string(RemotePtr<void> address) const;
124 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
125 void clear_bytes(RemotePtr<void> address, size_t len);
127 // Debug information:
128 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
129 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
130 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
131 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
132 simgrid::mc::Variable* find_variable(const char* name) const;
135 xbt_mheap_t get_heap()
137 if (!(this->cache_flags_ & Process::cache_heap))
138 this->refresh_heap();
139 return this->heap.get();
141 malloc_info* get_malloc_info()
143 if (!(this->cache_flags_ & Process::cache_malloc))
144 this->refresh_malloc_info();
145 return this->heap_info.data();
150 this->cache_flags_ = Process::cache_none;
153 Channel const& getChannel() const { return channel_; }
154 Channel& getChannel() { return channel_; }
156 std::vector<IgnoredRegion> const& ignored_regions() const
158 return ignored_regions_;
160 void ignore_region(std::uint64_t address, std::size_t size);
162 pid_t pid() const { return pid_; }
164 bool in_maestro_stack(RemotePtr<void> p) const
166 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
179 void reset_soft_dirty();
180 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
182 bool privatized(ObjectInformation const& info) const
184 return privatized_ && info.executable();
186 bool privatized() const
190 void privatized(bool privatized) { privatized_ = privatized; }
192 void ignore_global_variable(const char* name)
194 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
196 info->remove_global_variable(name);
199 std::vector<s_stack_region_t>& stack_areas()
203 std::vector<s_stack_region_t> const& stack_areas() const
208 std::vector<IgnoredHeapRegion> const& ignored_heap() const
210 return ignored_heap_;
212 void ignore_heap(IgnoredHeapRegion const& region);
213 void unignore_heap(void *address, size_t size);
215 void ignore_local_variable(const char *var_name, const char *frame_name);
216 std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
217 std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
220 void init_memory_map_info();
222 void refresh_malloc_info();
223 void refresh_simix();
228 bool running_ = false;
229 std::vector<simgrid::xbt::VmMap> memory_map_;
230 RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
231 int memory_file = -1;
232 std::vector<IgnoredRegion> ignored_regions_;
233 int clear_refs_fd_ = -1;
234 int pagemap_fd_ = -1;
235 bool privatized_ = false;
236 std::vector<s_stack_region_t> stack_areas_;
237 std::vector<IgnoredHeapRegion> ignored_heap_;
239 public: // object info
240 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
241 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
242 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
243 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
245 public: // Copies of MCed SMX data structures
246 /** Copy of `simix_global->process_list`
250 std::vector<SimixProcessInformation> smx_process_infos;
252 /** Copy of `simix_global->process_to_destroy`
256 std::vector<SimixProcessInformation> smx_old_process_infos;
259 /** State of the cache (which variables are up to date) */
260 int cache_flags_ = Process::cache_none;
263 /** Address of the heap structure in the MCed process. */
266 /** Copy of the heap structure of the process
268 * This is refreshed with the `MC_process_refresh` call.
269 * This is not used if the process is the current one:
270 * use `get_heap_info()` in order to use it.
272 std::unique_ptr<s_xbt_mheap_t> heap;
274 /** Copy of the allocation info structure
276 * This is refreshed with the `MC_process_refresh` call.
277 * This is not used if the process is the current one:
278 * use `get_malloc_info()` in order to use it.
280 std::vector<malloc_info> heap_info;
282 public: // Libunwind-data
284 /** Full-featured MC-aware libunwind address space for the process
286 * This address space is using a simgrid::mc::UnwindContext*
287 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
288 * and unw_context_t).
290 unw_addr_space_t unw_addr_space;
292 /** Underlying libunwind addres-space
294 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
295 * operations of the native MC address space is currently delegated
296 * to this address space (either the local or a ptrace unwinder).
298 unw_addr_space_t unw_underlying_addr_space;
300 /** The corresponding context
302 void* unw_underlying_context;
305 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
307 XBT_PRIVATE int open_vm(pid_t pid, int flags);