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/dynar.h>
23 #include <xbt/mmalloc.h>
26 #include "src/xbt/mmalloc/mmprivate.h"
29 #include <simgrid/simix.h>
30 #include "src/simix/popping_private.h"
31 #include "src/simix/smx_private.h"
33 #include "src/xbt/memory_map.hpp"
35 #include "src/mc/mc_forward.hpp"
36 #include "src/mc/mc_base.h"
37 #include "src/mc/AddressSpace.hpp"
38 #include "src/mc/mc_protocol.h"
39 #include "src/mc/ObjectInformation.hpp"
41 // Those flags are used to track down which cached information
42 // is still up to date and which information needs to be updated.
43 typedef int mc_process_cache_flags_t;
44 #define MC_PROCESS_CACHE_FLAG_NONE 0
45 #define MC_PROCESS_CACHE_FLAG_HEAP 1
46 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
47 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
52 class SimixProcessInformation {
54 /** MCed address of the process */
55 void* address = nullptr;
57 /** (Flat) Copy of the process data structure */
58 struct s_smx_process copy;
60 /** Hostname (owned by `mc_modelchecker->hostnames`) */
61 const char* hostname = nullptr;
72 struct IgnoredRegion {
77 struct IgnoredHeapRegion {
84 /** Representation of a process
86 class Process final : public AddressSpace {
88 Process(pid_t pid, int sockfd);
92 Process(Process const&) = delete;
93 Process(Process &&) = delete;
94 Process& operator=(Process const&) = delete;
95 Process& operator=(Process &&) = delete;
98 const void* read_bytes(void* buffer, std::size_t size,
99 RemotePtr<void> address, int process_index = ProcessIndexAny,
100 ReadOptions options = ReadOptions::none()) const override;
101 void read_variable(const char* name, void* target, size_t size) const;
103 T read_variable(const char *name) const
105 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
107 read_variable(name, &res, sizeof(T));
110 char* read_string(RemotePtr<void> address) const;
113 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
114 void clear_bytes(RemotePtr<void> address, size_t len);
116 // Debug information:
117 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
118 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
119 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
120 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
121 simgrid::mc::Variable* find_variable(const char* name) const;
124 xbt_mheap_t get_heap()
126 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
127 this->refresh_heap();
128 return this->heap.get();
130 malloc_info* get_malloc_info()
132 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
133 this->refresh_malloc_info();
134 return this->heap_info.data();
137 std::vector<IgnoredRegion> const& ignored_regions() const
139 return ignored_regions_;
141 void ignore_region(std::uint64_t address, std::size_t size);
143 pid_t pid() const { return pid_; }
145 bool in_maestro_stack(RemotePtr<void> p) const
147 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
161 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
162 send_message(M const& m)
164 return MC_protocol_send(this->socket_, &m, sizeof(M));
167 int send_message(e_mc_message_type message_id)
169 return MC_protocol_send_simple_message(this->socket_, message_id);
173 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
174 receive_message(M& m)
176 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
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 int socket() { return socket_; }
217 std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
220 void init_memory_map_info();
222 void refresh_malloc_info();
227 bool running_ = false;
228 std::vector<simgrid::xbt::VmMap> memory_map_;
229 RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
230 int memory_file = -1;
231 std::vector<IgnoredRegion> ignored_regions_;
232 int clear_refs_fd_ = -1;
233 int pagemap_fd_ = -1;
234 bool privatized_ = false;
235 std::vector<s_stack_region_t> stack_areas_;
236 std::vector<IgnoredHeapRegion> ignored_heap_;
238 public: // object info
239 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
240 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
241 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
242 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
244 public: // Copies of MCed SMX data structures
245 /** Copy of `simix_global->process_list`
249 std::vector<SimixProcessInformation> smx_process_infos;
251 /** Copy of `simix_global->process_to_destroy`
255 std::vector<SimixProcessInformation> smx_old_process_infos;
257 /** State of the cache (which variables are up to date) */
258 mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
260 /** Address of the heap structure in the MCed process. */
263 /** Copy of the heap structure of the process
265 * This is refreshed with the `MC_process_refresh` call.
266 * This is not used if the process is the current one:
267 * use `get_heap_info()` in order to use it.
269 std::unique_ptr<s_xbt_mheap_t> heap;
271 /** Copy of the allocation info structure
273 * This is refreshed with the `MC_process_refresh` call.
274 * This is not used if the process is the current one:
275 * use `get_malloc_info()` in order to use it.
277 std::vector<malloc_info> heap_info;
279 public: // Libunwind-data
281 /** Full-featured MC-aware libunwind address space for the process
283 * This address space is using a mc_unw_context_t
284 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
285 * and unw_context_t).
287 unw_addr_space_t unw_addr_space;
289 /** Underlying libunwind addres-space
291 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
292 * operations of the native MC address space is currently delegated
293 * to this address space (either the local or a ptrace unwinder).
295 unw_addr_space_t unw_underlying_addr_space;
297 /** The corresponding context
299 void* unw_underlying_context;
302 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
304 XBT_PRIVATE int open_vm(pid_t pid, int flags);