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 "xbt/mmalloc/mmprivate.h"
27 #include <simgrid/simix.h>
28 #include "simix/popping_private.h"
29 #include "simix/smx_private.h"
31 #include "mc_forward.hpp"
33 #include "mc_mmalloc.h" // std_heap
34 #include "mc_memory_map.h"
35 #include "AddressSpace.hpp"
36 #include "mc_protocol.h"
38 typedef int mc_process_flags_t;
39 #define MC_PROCESS_NO_FLAG 0
40 #define MC_PROCESS_SELF_FLAG 1
42 // Those flags are used to track down which cached information
43 // is still up to date and which information needs to be updated.
44 typedef int mc_process_cache_flags_t;
45 #define MC_PROCESS_CACHE_FLAG_NONE 0
46 #define MC_PROCESS_CACHE_FLAG_HEAP 1
47 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
48 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
50 typedef struct s_mc_smx_process_info s_mc_smx_process_info_t, *mc_smx_process_info_t;
55 struct IgnoredRegion {
60 /** Representation of a process
62 class Process : public AddressSpace {
64 Process(pid_t pid, int sockfd);
70 return this->process_flags & MC_PROCESS_SELF_FLAG;
74 const void* read_bytes(void* buffer, std::size_t size,
75 remote_ptr<void> address, int process_index = ProcessIndexAny,
76 ReadMode mode = Normal) const override;
77 void read_variable(const char* name, void* target, size_t size) const;
79 T read_variable(const char *name) const
81 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
83 read_variable(name, &res, sizeof(T));
86 char* read_string(remote_ptr<void> address) const;
89 void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
90 void clear_bytes(remote_ptr<void> address, size_t len);
93 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
94 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
95 std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
96 simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
97 simgrid::mc::Variable* find_variable(const char* name) const;
100 xbt_mheap_t get_heap()
102 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
103 this->refresh_heap();
106 malloc_info* get_malloc_info()
108 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
109 this->refresh_malloc_info();
110 return this->heap_info;
113 std::vector<IgnoredRegion> const& ignored_regions() const
115 return ignored_regions_;
117 void ignore_region(std::uint64_t address, std::size_t size);
119 pid_t pid() const { return pid_; }
121 bool in_maestro_stack(remote_ptr<void> p) const
123 return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
131 void terminate(int status)
143 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
144 send_message(M const& m)
146 return MC_protocol_send(this->socket_, &m, sizeof(M));
149 int send_message(e_mc_message_type message_id)
151 return MC_protocol_send_simple_message(this->socket_, message_id);
155 typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
156 receive_message(M& m)
158 return MC_receive_message(this->socket_, &m, sizeof(M), 0);
161 void reset_soft_dirty();
162 void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
165 void init_memory_map_info();
167 void refresh_malloc_info();
169 mc_process_flags_t process_flags;
174 std::vector<VmMap> memory_map_;
175 remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
177 std::vector<IgnoredRegion> ignored_regions_;
180 public: // object info
181 // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
182 std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
183 std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
184 std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
186 public: // Copies of MCed SMX data structures
187 /** Copy of `simix_global->process_list`
191 xbt_dynar_t smx_process_infos;
193 /** Copy of `simix_global->process_to_destroy`
197 xbt_dynar_t smx_old_process_infos;
199 /** State of the cache (which variables are up to date) */
200 mc_process_cache_flags_t cache_flags;
202 /** Address of the heap structure in the MCed process. */
205 /** Copy of the heap structure of the process
207 * This is refreshed with the `MC_process_refresh` call.
208 * This is not used if the process is the current one:
209 * use `get_heap_info()` in order to use it.
213 /** Copy of the allocation info structure
215 * This is refreshed with the `MC_process_refresh` call.
216 * This is not used if the process is the current one:
217 * use `get_malloc_info()` in order to use it.
219 malloc_info* heap_info;
221 public: // Libunwind-data
223 /** Full-featured MC-aware libunwind address space for the process
225 * This address space is using a mc_unw_context_t
226 * (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
227 * and unw_context_t).
229 unw_addr_space_t unw_addr_space;
231 /** Underlying libunwind addres-space
233 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
234 * operations of the native MC address space is currently delegated
235 * to this address space (either the local or a ptrace unwinder).
237 unw_addr_space_t unw_underlying_addr_space;
239 /** The corresponding context
241 void* unw_underlying_context;
244 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
246 XBT_PRIVATE int open_vm(pid_t pid, int flags);
253 XBT_PRIVATE void MC_invalidate_cache(void);