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 <boost/range/iterator_range.hpp>
19 #include <sys/types.h>
21 #include <simgrid_config.h>
24 #include <xbt/dynar.h>
25 #include <xbt/dynar.hpp>
26 #include <xbt/mmalloc.h>
29 #include "src/xbt/mmalloc/mmprivate.h"
32 #include <simgrid/simix.h>
33 #include "src/simix/popping_private.h"
34 #include "src/simix/smx_private.h"
36 #include "src/xbt/memory_map.hpp"
38 #include "src/mc/mc_forward.hpp"
39 #include "src/mc/mc_base.h"
40 #include "src/mc/AddressSpace.hpp"
41 #include "src/mc/mc_protocol.h"
42 #include "src/mc/ObjectInformation.hpp"
44 // Those flags are used to track down which cached information
45 // is still up to date and which information needs to be updated.
46 typedef int mc_process_cache_flags_t;
47 #define MC_PROCESS_CACHE_FLAG_NONE 0
48 #define MC_PROCESS_CACHE_FLAG_HEAP 1
49 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
50 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
55 class SimixProcessInformation {
57 /** MCed address of the process */
58 void* address = nullptr;
60 /** (Flat) Copy of the process data structure */
61 struct s_smx_process copy;
63 /** Hostname (owned by `mc_modelchecker->hostnames`) */
64 const char* hostname = nullptr;
75 struct IgnoredRegion {
80 struct IgnoredHeapRegion {
87 /** Representation of a process
89 class Process final : public AddressSpace {
91 Process(pid_t pid, int sockfd);
95 Process(Process const&) = delete;
96 Process(Process &&) = delete;
97 Process& operator=(Process const&) = delete;
98 Process& operator=(Process &&) = delete;
101 const void* read_bytes(void* buffer, std::size_t size,
102 RemotePtr<void> address, int process_index = ProcessIndexAny,
103 ReadOptions options = ReadOptions::none()) const override;
104 void read_variable(const char* name, void* target, size_t size) const;
106 T read_variable(const char *name) const
108 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
110 read_variable(name, &res, sizeof(T));
113 char* read_string(RemotePtr<void> address) const;
116 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
117 void clear_bytes(RemotePtr<void> address, size_t len);
119 // Debug information:
120 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
121 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
122 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
123 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
124 simgrid::mc::Variable* find_variable(const char* name) const;
127 xbt_mheap_t get_heap()
129 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
130 this->refresh_heap();
131 return this->heap.get();
133 malloc_info* get_malloc_info()
135 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
136 this->refresh_malloc_info();
137 return this->heap_info.data();
140 std::vector<IgnoredRegion> const& ignored_regions() const
142 return ignored_regions_;
144 void ignore_region(std::uint64_t address, std::size_t size);
146 pid_t pid() const { return pid_; }
148 bool in_maestro_stack(RemotePtr<void> p) const
150 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
164 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
165 send_message(M const& m)
167 return MC_protocol_send(this->socket_, &m, sizeof(M));
170 int send_message(e_mc_message_type message_id)
172 return MC_protocol_send_simple_message(this->socket_, message_id);
176 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
177 receive_message(M& m)
179 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
182 void reset_soft_dirty();
183 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
185 bool privatized(ObjectInformation const& info) const
187 return privatized_ && info.executable();
189 bool privatized() const
193 void privatized(bool privatized) { privatized_ = privatized; }
195 void ignore_global_variable(const char* name)
197 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
199 info->remove_global_variable(name);
202 std::vector<s_stack_region_t>& stack_areas()
206 std::vector<s_stack_region_t> const& stack_areas() const
211 std::vector<IgnoredHeapRegion> const& ignored_heap() const
213 return ignored_heap_;
215 void ignore_heap(IgnoredHeapRegion const& region);
216 void unignore_heap(void *address, size_t size);
218 void ignore_local_variable(const char *var_name, const char *frame_name);
219 int socket() { return socket_; }
220 simgrid::xbt::DynarRange<s_mc_smx_process_info> simix_processes();
223 void init_memory_map_info();
225 void refresh_malloc_info();
230 bool running_ = false;
231 std::vector<simgrid::xbt::VmMap> memory_map_;
232 RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
233 int memory_file = -1;
234 std::vector<IgnoredRegion> ignored_regions_;
235 int clear_refs_fd_ = -1;
236 int pagemap_fd_ = -1;
237 bool privatized_ = false;
238 std::vector<s_stack_region_t> stack_areas_;
239 std::vector<IgnoredHeapRegion> ignored_heap_;
241 public: // object info
242 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
243 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
244 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
245 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
247 public: // Copies of MCed SMX data structures
248 /** Copy of `simix_global->process_list`
252 std::vector<SimixProcessInformation> smx_process_infos;
254 /** Copy of `simix_global->process_to_destroy`
258 std::vector<SimixProcessInformation> smx_old_process_infos;
260 /** State of the cache (which variables are up to date) */
261 mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_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 mc_unw_context_t
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;
306 #define MC_PROCESS_FOREACH(xs, cursor, p) \
308 for (auto __it = (cursor = 0, p = &*xs.begin(), xs.begin()); \
309 __it != xs.end(); ++__it, ++cursor, p = &*__it)
311 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
313 XBT_PRIVATE int open_vm(pid_t pid, int flags);