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>
25 #include "src/xbt/mmalloc/mmprivate.h"
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"
40 // Those flags are used to track down which cached information
41 // is still up to date and which information needs to be updated.
42 typedef int mc_process_cache_flags_t;
43 #define MC_PROCESS_CACHE_FLAG_NONE 0
44 #define MC_PROCESS_CACHE_FLAG_HEAP 1
45 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
46 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
51 struct IgnoredRegion {
56 struct IgnoredHeapRegion {
63 /** Representation of a process
65 class Process final : public AddressSpace {
67 Process(pid_t pid, int sockfd);
71 Process(Process const&) = delete;
72 Process(Process &&) = delete;
73 Process& operator=(Process const&) = delete;
74 Process& operator=(Process &&) = delete;
77 const void* read_bytes(void* buffer, std::size_t size,
78 remote_ptr<void> address, int process_index = ProcessIndexAny,
79 ReadOptions options = ReadOptions::none()) const override;
80 void read_variable(const char* name, void* target, size_t size) const;
82 T read_variable(const char *name) const
84 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
86 read_variable(name, &res, sizeof(T));
89 char* read_string(remote_ptr<void> address) const;
92 void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
93 void clear_bytes(remote_ptr<void> address, size_t len);
96 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
97 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
98 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
99 simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
100 simgrid::mc::Variable* find_variable(const char* name) const;
103 xbt_mheap_t get_heap()
105 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
106 this->refresh_heap();
107 return this->heap.get();
109 malloc_info* get_malloc_info()
111 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
112 this->refresh_malloc_info();
113 return this->heap_info.data();
116 std::vector<IgnoredRegion> const& ignored_regions() const
118 return ignored_regions_;
120 void ignore_region(std::uint64_t address, std::size_t size);
122 pid_t pid() const { return pid_; }
124 bool in_maestro_stack(remote_ptr<void> p) const
126 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
140 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
141 send_message(M const& m)
143 return MC_protocol_send(this->socket_, &m, sizeof(M));
146 int send_message(e_mc_message_type message_id)
148 return MC_protocol_send_simple_message(this->socket_, message_id);
152 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
153 receive_message(M& m)
155 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
158 void reset_soft_dirty();
159 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
161 bool privatized(ObjectInformation const& info) const
163 return privatized_ && info.executable();
165 bool privatized() const
169 void privatized(bool privatized) { privatized_ = privatized; }
171 void ignore_global_variable(const char* name)
173 for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
175 info->remove_global_variable(name);
178 std::vector<s_stack_region_t>& stack_areas()
182 std::vector<s_stack_region_t> const& stack_areas() const
187 std::vector<IgnoredHeapRegion> const& ignored_heap() const
189 return ignored_heap_;
191 void ignore_heap(IgnoredHeapRegion const& region);
192 void unignore_heap(void *address, size_t size);
194 void ignore_local_variable(const char *var_name, const char *frame_name);
195 int socket() { return socket_; }
198 void init_memory_map_info();
200 void refresh_malloc_info();
205 bool running_ = false;
206 std::vector<simgrid::xbt::VmMap> memory_map_;
207 remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
208 int memory_file = -1;
209 std::vector<IgnoredRegion> ignored_regions_;
210 int clear_refs_fd_ = -1;
211 int pagemap_fd_ = -1;
212 bool privatized_ = false;
213 std::vector<s_stack_region_t> stack_areas_;
214 std::vector<IgnoredHeapRegion> ignored_heap_;
216 public: // object info
217 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
218 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
219 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
220 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
222 public: // Copies of MCed SMX data structures
223 /** Copy of `simix_global->process_list`
227 xbt_dynar_t smx_process_infos = nullptr;
229 /** Copy of `simix_global->process_to_destroy`
233 xbt_dynar_t smx_old_process_infos = nullptr;
235 /** State of the cache (which variables are up to date) */
236 mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
238 /** Address of the heap structure in the MCed process. */
241 /** Copy of the heap structure of the process
243 * This is refreshed with the `MC_process_refresh` call.
244 * This is not used if the process is the current one:
245 * use `get_heap_info()` in order to use it.
247 std::unique_ptr<s_xbt_mheap_t> heap;
249 /** Copy of the allocation info structure
251 * This is refreshed with the `MC_process_refresh` call.
252 * This is not used if the process is the current one:
253 * use `get_malloc_info()` in order to use it.
255 std::vector<malloc_info> heap_info;
257 public: // Libunwind-data
259 /** Full-featured MC-aware libunwind address space for the process
261 * This address space is using a mc_unw_context_t
262 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
263 * and unw_context_t).
265 unw_addr_space_t unw_addr_space;
267 /** Underlying libunwind addres-space
269 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
270 * operations of the native MC address space is currently delegated
271 * to this address space (either the local or a ptrace unwinder).
273 unw_addr_space_t unw_underlying_addr_space;
275 /** The corresponding context
277 void* unw_underlying_context;
280 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
282 XBT_PRIVATE int open_vm(pid_t pid, int flags);