Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove/cleanup/fix #include
[simgrid.git] / src / mc / Process.hpp
1 /* Copyright (c) 2008-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_PROCESS_H
8 #define SIMGRID_MC_PROCESS_H
9
10 #include <cstdint>
11 #include <cstddef>
12
13 #include <type_traits>
14 #include <vector>
15 #include <memory>
16
17 #include <sys/types.h>
18
19 #include <simgrid_config.h>
20
21 #include <xbt/base.h>
22 #include <xbt/dynar.h>
23 #include <xbt/mmalloc.h>
24
25 #ifdef HAVE_MC
26 #include "src/xbt/mmalloc/mmprivate.h"
27 #endif
28
29 #include <simgrid/simix.h>
30 #include "src/simix/popping_private.h"
31 #include "src/simix/smx_private.h"
32
33 #include "src/xbt/memory_map.hpp"
34
35 #include "src/mc/mc_forward.hpp"
36 #include "src/mc/mc_base.h"
37 #include "src/mc/AddressSpace.hpp"
38 #include "src/mc/mc_protocol.h"
39 #include "src/mc/ObjectInformation.hpp"
40
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
48
49 namespace simgrid {
50 namespace mc {
51
52 struct IgnoredRegion {
53   std::uint64_t addr;
54   std::size_t size;
55 };
56
57 struct IgnoredHeapRegion {
58   int block;
59   int fragment;
60   void *address;
61   std::size_t size;
62 };
63
64 /** Representation of a process
65  */
66 class Process final : public AddressSpace {
67 public:
68   Process(pid_t pid, int sockfd);
69   ~Process();
70   void init();
71
72   Process(Process const&) = delete;
73   Process(Process &&) = delete;
74   Process& operator=(Process const&) = delete;
75   Process& operator=(Process &&) = delete;
76
77   // Read memory:
78   const void* read_bytes(void* buffer, std::size_t size,
79     remote_ptr<void> address, int process_index = ProcessIndexAny,
80     ReadOptions options = ReadOptions::none()) const override;
81   void read_variable(const char* name, void* target, size_t size) const;
82   template<class T>
83   T read_variable(const char *name) const
84   {
85     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
86     T res;
87     read_variable(name, &res, sizeof(T));
88     return res;
89   }
90   char* read_string(remote_ptr<void> address) const;
91
92   // Write memory:
93   void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
94   void clear_bytes(remote_ptr<void> address, size_t len);
95
96   // Debug information:
97   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
98   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
99   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
100   simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
101   simgrid::mc::Variable* find_variable(const char* name) const;
102
103   // Heap access:
104   xbt_mheap_t get_heap()
105   {
106     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
107       this->refresh_heap();
108     return this->heap.get();
109   }
110   malloc_info* get_malloc_info()
111   {
112     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
113       this->refresh_malloc_info();
114     return this->heap_info.data();
115   }
116
117   std::vector<IgnoredRegion> const& ignored_regions() const
118   {
119     return ignored_regions_;
120   }
121   void ignore_region(std::uint64_t address, std::size_t size);
122
123   pid_t pid() const { return pid_; }
124
125   bool in_maestro_stack(remote_ptr<void> p) const
126   {
127     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
128   }
129
130   bool running() const
131   {
132     return running_;
133   }
134
135   void terminate()
136   {
137     running_ = false;
138   }
139
140   template<class M>
141   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
142   send_message(M const& m)
143   {
144     return MC_protocol_send(this->socket_, &m, sizeof(M));
145   }
146
147   int send_message(e_mc_message_type message_id)
148   {
149     return MC_protocol_send_simple_message(this->socket_, message_id);
150   }
151
152   template<class M>
153   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
154   receive_message(M& m)
155   {
156     return MC_receive_message(this->socket_, &m, sizeof(M), 0);
157   }
158
159   void reset_soft_dirty();
160   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
161
162   bool privatized(ObjectInformation const& info) const
163   {
164     return privatized_ && info.executable();
165   }
166   bool privatized() const
167   {
168     return privatized_;
169   }
170   void privatized(bool privatized) { privatized_ = privatized; }
171
172   void ignore_global_variable(const char* name)
173   {
174     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
175         this->object_infos)
176       info->remove_global_variable(name);
177   }
178
179   std::vector<s_stack_region_t>& stack_areas()
180   {
181     return stack_areas_;
182   }
183   std::vector<s_stack_region_t> const& stack_areas() const
184   {
185     return stack_areas_;
186   }
187
188   std::vector<IgnoredHeapRegion> const& ignored_heap() const
189   {
190     return ignored_heap_;
191   }
192   void ignore_heap(IgnoredHeapRegion const& region);
193   void unignore_heap(void *address, size_t size);
194
195   void ignore_local_variable(const char *var_name, const char *frame_name);
196   int socket() { return socket_; }
197
198 private:
199   void init_memory_map_info();
200   void refresh_heap();
201   void refresh_malloc_info();
202
203 private:
204   pid_t pid_ = -1;
205   int socket_ = -1;
206   bool running_ = false;
207   std::vector<simgrid::xbt::VmMap> memory_map_;
208   remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
209   int memory_file = -1;
210   std::vector<IgnoredRegion> ignored_regions_;
211   int clear_refs_fd_ = -1;
212   int pagemap_fd_ = -1;
213   bool privatized_ = false;
214   std::vector<s_stack_region_t> stack_areas_;
215   std::vector<IgnoredHeapRegion> ignored_heap_;
216
217 public: // object info
218   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
219   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
220   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
221   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
222
223 public: // Copies of MCed SMX data structures
224   /** Copy of `simix_global->process_list`
225    *
226    *  See mc_smx.c.
227    */
228   xbt_dynar_t smx_process_infos = nullptr;
229
230   /** Copy of `simix_global->process_to_destroy`
231    *
232    *  See mc_smx.c.
233    */
234   xbt_dynar_t smx_old_process_infos = nullptr;
235
236   /** State of the cache (which variables are up to date) */
237   mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
238
239   /** Address of the heap structure in the MCed process. */
240   void* heap_address;
241
242   /** Copy of the heap structure of the process
243    *
244    *  This is refreshed with the `MC_process_refresh` call.
245    *  This is not used if the process is the current one:
246    *  use `get_heap_info()` in order to use it.
247    */
248    std::unique_ptr<s_xbt_mheap_t> heap;
249
250   /** Copy of the allocation info structure
251    *
252    *  This is refreshed with the `MC_process_refresh` call.
253    *  This is not used if the process is the current one:
254    *  use `get_malloc_info()` in order to use it.
255    */
256   std::vector<malloc_info> heap_info;
257
258 public: // Libunwind-data
259
260   /** Full-featured MC-aware libunwind address space for the process
261    *
262    *  This address space is using a mc_unw_context_t
263    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
264    *  and unw_context_t).
265    */
266   unw_addr_space_t unw_addr_space;
267
268   /** Underlying libunwind addres-space
269    *
270    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
271    *  operations of the native MC address space is currently delegated
272    *  to this address space (either the local or a ptrace unwinder).
273    */
274   unw_addr_space_t unw_underlying_addr_space;
275
276   /** The corresponding context
277    */
278   void* unw_underlying_context;
279 };
280
281 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
282  */
283 XBT_PRIVATE int open_vm(pid_t pid, int flags);
284
285 }
286 }
287
288 #endif