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
10 #include <type_traits>
12 #include <sys/types.h>
17 #include "simgrid_config.h"
18 #include <sys/types.h>
21 #include <xbt/mmalloc.h>
24 #include "src/xbt/mmalloc/mmprivate.h"
27 #include <simgrid/simix.h>
28 #include "src/simix/popping_private.h"
29 #include "src/simix/smx_private.h"
31 #include "../xbt/memory_map.hpp"
33 #include "mc_forward.hpp"
35 #include "mc_mmalloc.h" // std_heap
36 #include "AddressSpace.hpp"
37 #include "mc_protocol.h"
39 #include "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 struct IgnoredRegion {
57 /** Representation of a process
59 class Process final : public AddressSpace {
61 Process(pid_t pid, int sockfd);
64 Process(Process const&) = delete;
65 Process(Process &&) = delete;
66 Process& operator=(Process const&) = delete;
67 Process& operator=(Process &&) = delete;
70 const void* read_bytes(void* buffer, std::size_t size,
71 remote_ptr<void> address, int process_index = ProcessIndexAny,
72 ReadMode mode = Normal) const override;
73 void read_variable(const char* name, void* target, size_t size) const;
75 T read_variable(const char *name) const
77 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
79 read_variable(name, &res, sizeof(T));
82 char* read_string(remote_ptr<void> address) const;
85 void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
86 void clear_bytes(remote_ptr<void> address, size_t len);
89 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
90 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
91 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
92 simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
93 simgrid::mc::Variable* find_variable(const char* name) const;
96 xbt_mheap_t get_heap()
98 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
100 return this->heap.get();
102 malloc_info* get_malloc_info()
104 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
105 this->refresh_malloc_info();
106 return this->heap_info.data();
109 std::vector<IgnoredRegion> const& ignored_regions() const
111 return ignored_regions_;
113 void ignore_region(std::uint64_t address, std::size_t size);
115 pid_t pid() const { return pid_; }
117 bool in_maestro_stack(remote_ptr<void> p) const
119 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
133 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
134 send_message(M const& m)
136 return MC_protocol_send(this->socket_, &m, sizeof(M));
139 int send_message(e_mc_message_type message_id)
141 return MC_protocol_send_simple_message(this->socket_, message_id);
145 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
146 receive_message(M& m)
148 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
151 void reset_soft_dirty();
152 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
154 bool privatized(ObjectInformation const& info) const
156 return privatized_ && info.executable();
158 bool privatized() const
162 void privatized(bool privatized) { privatized_ = privatized; }
165 void init_memory_map_info();
167 void refresh_malloc_info();
172 std::vector<simgrid::xbt::VmMap> memory_map_;
173 remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
175 std::vector<IgnoredRegion> ignored_regions_;
179 public: // object info
180 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
181 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
182 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
183 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
185 public: // Copies of MCed SMX data structures
186 /** Copy of `simix_global->process_list`
190 xbt_dynar_t smx_process_infos;
192 /** Copy of `simix_global->process_to_destroy`
196 xbt_dynar_t smx_old_process_infos;
198 /** State of the cache (which variables are up to date) */
199 mc_process_cache_flags_t cache_flags;
201 /** Address of the heap structure in the MCed process. */
204 /** Copy of the heap structure of the process
206 * This is refreshed with the `MC_process_refresh` call.
207 * This is not used if the process is the current one:
208 * use `get_heap_info()` in order to use it.
210 std::unique_ptr<s_xbt_mheap_t> heap;
212 /** Copy of the allocation info structure
214 * This is refreshed with the `MC_process_refresh` call.
215 * This is not used if the process is the current one:
216 * use `get_malloc_info()` in order to use it.
218 std::vector<malloc_info> heap_info;
220 public: // Libunwind-data
222 /** Full-featured MC-aware libunwind address space for the process
224 * This address space is using a mc_unw_context_t
225 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
226 * and unw_context_t).
228 unw_addr_space_t unw_addr_space;
230 /** Underlying libunwind addres-space
232 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
233 * operations of the native MC address space is currently delegated
234 * to this address space (either the local or a ptrace unwinder).
236 unw_addr_space_t unw_underlying_addr_space;
238 /** The corresponding context
240 void* unw_underlying_context;
243 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
245 XBT_PRIVATE int open_vm(pid_t pid, int flags);
252 XBT_PRIVATE void MC_invalidate_cache(void);