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
52 struct s_mc_smx_process_info {
53 /** MCed address of the process */
55 /** (Flat) Copy of the process data structure */
56 struct s_smx_process copy;
57 /** Hostname (owned by `mc_modelchecker->hostnames`) */
65 struct IgnoredRegion {
70 struct IgnoredHeapRegion {
77 /** Representation of a process
79 class Process final : public AddressSpace {
81 Process(pid_t pid, int sockfd);
85 Process(Process const&) = delete;
86 Process(Process &&) = delete;
87 Process& operator=(Process const&) = delete;
88 Process& operator=(Process &&) = delete;
91 const void* read_bytes(void* buffer, std::size_t size,
92 RemotePtr<void> address, int process_index = ProcessIndexAny,
93 ReadOptions options = ReadOptions::none()) const override;
94 void read_variable(const char* name, void* target, size_t size) const;
96 T read_variable(const char *name) const
98 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
100 read_variable(name, &res, sizeof(T));
103 char* read_string(RemotePtr<void> address) const;
106 void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
107 void clear_bytes(RemotePtr<void> address, size_t len);
109 // Debug information:
110 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
111 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
112 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
113 simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
114 simgrid::mc::Variable* find_variable(const char* name) const;
117 xbt_mheap_t get_heap()
119 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
120 this->refresh_heap();
121 return this->heap.get();
123 malloc_info* get_malloc_info()
125 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
126 this->refresh_malloc_info();
127 return this->heap_info.data();
130 std::vector<IgnoredRegion> const& ignored_regions() const
132 return ignored_regions_;
134 void ignore_region(std::uint64_t address, std::size_t size);
136 pid_t pid() const { return pid_; }
138 bool in_maestro_stack(RemotePtr<void> p) const
140 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
154 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
155 send_message(M const& m)
157 return MC_protocol_send(this->socket_, &m, sizeof(M));
160 int send_message(e_mc_message_type message_id)
162 return MC_protocol_send_simple_message(this->socket_, message_id);
166 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
167 receive_message(M& m)
169 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
172 void reset_soft_dirty();
173 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
175 bool privatized(ObjectInformation const& info) const
177 return privatized_ && info.executable();
179 bool privatized() const
183 void privatized(bool privatized) { privatized_ = privatized; }
185 void ignore_global_variable(const char* name)
187 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
189 info->remove_global_variable(name);
192 std::vector<s_stack_region_t>& stack_areas()
196 std::vector<s_stack_region_t> const& stack_areas() const
201 std::vector<IgnoredHeapRegion> const& ignored_heap() const
203 return ignored_heap_;
205 void ignore_heap(IgnoredHeapRegion const& region);
206 void unignore_heap(void *address, size_t size);
208 void ignore_local_variable(const char *var_name, const char *frame_name);
209 int socket() { return socket_; }
210 simgrid::xbt::DynarRange<s_mc_smx_process_info> simix_processes();
213 void init_memory_map_info();
215 void refresh_malloc_info();
220 bool running_ = false;
221 std::vector<simgrid::xbt::VmMap> memory_map_;
222 RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
223 int memory_file = -1;
224 std::vector<IgnoredRegion> ignored_regions_;
225 int clear_refs_fd_ = -1;
226 int pagemap_fd_ = -1;
227 bool privatized_ = false;
228 std::vector<s_stack_region_t> stack_areas_;
229 std::vector<IgnoredHeapRegion> ignored_heap_;
231 public: // object info
232 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
233 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
234 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
235 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
237 public: // Copies of MCed SMX data structures
238 /** Copy of `simix_global->process_list`
242 xbt_dynar_t smx_process_infos = nullptr;
244 /** Copy of `simix_global->process_to_destroy`
248 xbt_dynar_t smx_old_process_infos = nullptr;
250 /** State of the cache (which variables are up to date) */
251 mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
253 /** Address of the heap structure in the MCed process. */
256 /** Copy of the heap structure of the process
258 * This is refreshed with the `MC_process_refresh` call.
259 * This is not used if the process is the current one:
260 * use `get_heap_info()` in order to use it.
262 std::unique_ptr<s_xbt_mheap_t> heap;
264 /** Copy of the allocation info structure
266 * This is refreshed with the `MC_process_refresh` call.
267 * This is not used if the process is the current one:
268 * use `get_malloc_info()` in order to use it.
270 std::vector<malloc_info> heap_info;
272 public: // Libunwind-data
274 /** Full-featured MC-aware libunwind address space for the process
276 * This address space is using a mc_unw_context_t
277 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
278 * and unw_context_t).
280 unw_addr_space_t unw_addr_space;
282 /** Underlying libunwind addres-space
284 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
285 * operations of the native MC address space is currently delegated
286 * to this address space (either the local or a ptrace unwinder).
288 unw_addr_space_t unw_underlying_addr_space;
290 /** The corresponding context
292 void* unw_underlying_context;
295 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
297 XBT_PRIVATE int open_vm(pid_t pid, int flags);