Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6ecad0e6669fad60dc5e500ce374735ecbc7b865
[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 <type_traits>
11
12 #include <sys/types.h>
13
14 #include <vector>
15 #include <memory>
16
17 #include "simgrid_config.h"
18 #include <sys/types.h>
19
20 #include <xbt/base.h>
21 #include <xbt/mmalloc.h>
22
23 #ifdef HAVE_MC
24 #include "src/xbt/mmalloc/mmprivate.h"
25 #endif
26
27 #include <simgrid/simix.h>
28 #include "src/simix/popping_private.h"
29 #include "src/simix/smx_private.h"
30
31 #include "src/xbt/memory_map.hpp"
32
33 #include "src/mc/mc_forward.hpp"
34 #include "src/mc/mc_base.h"
35 #include "src/mc/mc_mmalloc.h" // std_heap
36 #include "src/mc/AddressSpace.hpp"
37 #include "src/mc/mc_protocol.h"
38 #include "src/mc/ObjectInformation.hpp"
39
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
47
48 namespace simgrid {
49 namespace mc {
50
51 struct IgnoredRegion {
52   std::uint64_t addr;
53   size_t size;
54 };
55
56 struct IgnoredHeapRegion {
57   int block;
58   int fragment;
59   void *address;
60   size_t size;
61 };
62
63 /** Representation of a process
64  */
65 class Process final : public AddressSpace {
66 public:
67   Process(pid_t pid, int sockfd);
68   ~Process();
69   void init();
70
71   Process(Process const&) = delete;
72   Process(Process &&) = delete;
73   Process& operator=(Process const&) = delete;
74   Process& operator=(Process &&) = delete;
75
76   // Read memory:
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;
81   template<class T>
82   T read_variable(const char *name) const
83   {
84     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
85     T res;
86     read_variable(name, &res, sizeof(T));
87     return res;
88   }
89   char* read_string(remote_ptr<void> address) const;
90
91   // Write memory:
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);
94
95   // Debug information:
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;
101
102   // Heap access:
103   xbt_mheap_t get_heap()
104   {
105     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
106       this->refresh_heap();
107     return this->heap.get();
108   }
109   malloc_info* get_malloc_info()
110   {
111     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
112       this->refresh_malloc_info();
113     return this->heap_info.data();
114   }
115
116   std::vector<IgnoredRegion> const& ignored_regions() const
117   {
118     return ignored_regions_;
119   }
120   void ignore_region(std::uint64_t address, std::size_t size);
121
122   pid_t pid() const { return pid_; }
123
124   bool in_maestro_stack(remote_ptr<void> p) const
125   {
126     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
127   }
128
129   bool running() const
130   {
131     return running_;
132   }
133
134   void terminate()
135   {
136     running_ = false;
137   }
138
139   template<class M>
140   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
141   send_message(M const& m)
142   {
143     return MC_protocol_send(this->socket_, &m, sizeof(M));
144   }
145
146   int send_message(e_mc_message_type message_id)
147   {
148     return MC_protocol_send_simple_message(this->socket_, message_id);
149   }
150
151   template<class M>
152   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
153   receive_message(M& m)
154   {
155     return MC_receive_message(this->socket_, &m, sizeof(M), 0);
156   }
157
158   void reset_soft_dirty();
159   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
160
161   bool privatized(ObjectInformation const& info) const
162   {
163     return privatized_ && info.executable();
164   }
165   bool privatized() const
166   {
167     return privatized_;
168   }
169   void privatized(bool privatized) { privatized_ = privatized; }
170
171   void ignore_global_variable(const char* name)
172   {
173     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
174         this->object_infos)
175       info->remove_global_variable(name);
176   }
177
178   std::vector<s_stack_region_t>& stack_areas()
179   {
180     return stack_areas_;
181   }
182   std::vector<s_stack_region_t> const& stack_areas() const
183   {
184     return stack_areas_;
185   }
186
187   std::vector<IgnoredHeapRegion> const& ignored_heap() const
188   {
189     return ignored_heap_;
190   }
191   void ignore_heap(IgnoredHeapRegion const& region);
192   void unignore_heap(void *address, size_t size);
193
194   void ignore_local_variable(const char *var_name, const char *frame_name);
195   int socket() { return socket_; }
196
197 private:
198   void init_memory_map_info();
199   void refresh_heap();
200   void refresh_malloc_info();
201
202 private:
203   pid_t pid_ = -1;
204   int socket_ = -1;
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_;
215
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;
221
222 public: // Copies of MCed SMX data structures
223   /** Copy of `simix_global->process_list`
224    *
225    *  See mc_smx.c.
226    */
227   xbt_dynar_t smx_process_infos = nullptr;
228
229   /** Copy of `simix_global->process_to_destroy`
230    *
231    *  See mc_smx.c.
232    */
233   xbt_dynar_t smx_old_process_infos = nullptr;
234
235   /** State of the cache (which variables are up to date) */
236   mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
237
238   /** Address of the heap structure in the MCed process. */
239   void* heap_address;
240
241   /** Copy of the heap structure of the process
242    *
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.
246    */
247    std::unique_ptr<s_xbt_mheap_t> heap;
248
249   /** Copy of the allocation info structure
250    *
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.
254    */
255   std::vector<malloc_info> heap_info;
256
257 public: // Libunwind-data
258
259   /** Full-featured MC-aware libunwind address space for the process
260    *
261    *  This address space is using a mc_unw_context_t
262    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
263    *  and unw_context_t).
264    */
265   unw_addr_space_t unw_addr_space;
266
267   /** Underlying libunwind addres-space
268    *
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).
272    */
273   unw_addr_space_t unw_underlying_addr_space;
274
275   /** The corresponding context
276    */
277   void* unw_underlying_context;
278 };
279
280 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
281  */
282 XBT_PRIVATE int open_vm(pid_t pid, int flags);
283
284 }
285 }
286
287 #endif