Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Fix privatisation support
[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 "xbt/mmalloc/mmprivate.h"
25 #endif
26
27 #include <simgrid/simix.h>
28 #include "simix/popping_private.h"
29 #include "simix/smx_private.h"
30
31 #include "mc_forward.hpp"
32 #include "mc_base.h"
33 #include "mc_mmalloc.h" // std_heap
34 #include "mc_memory_map.h"
35 #include "AddressSpace.hpp"
36 #include "mc_protocol.h"
37
38 #include "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 /** Representation of a process
57  */
58 class Process final : public AddressSpace {
59 public:
60   Process(pid_t pid, int sockfd);
61   ~Process();
62
63   Process(Process const&) = delete;
64   Process(Process &&) = delete;
65   Process& operator=(Process const&) = delete;
66   Process& operator=(Process &&) = delete;
67
68   // Read memory:
69   const void* read_bytes(void* buffer, std::size_t size,
70     remote_ptr<void> address, int process_index = ProcessIndexAny,
71     ReadMode mode = Normal) const override;
72   void read_variable(const char* name, void* target, size_t size) const;
73   template<class T>
74   T read_variable(const char *name) const
75   {
76     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
77     T res;
78     read_variable(name, &res, sizeof(T));
79     return res;
80   }
81   char* read_string(remote_ptr<void> address) const;
82
83   // Write memory:
84   void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
85   void clear_bytes(remote_ptr<void> address, size_t len);
86
87   // Debug information:
88   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
89   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
90   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
91   simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
92   simgrid::mc::Variable* find_variable(const char* name) const;
93
94   // Heap access:
95   xbt_mheap_t get_heap()
96   {
97     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
98       this->refresh_heap();
99     return this->heap;
100   }
101   malloc_info* get_malloc_info()
102   {
103     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
104       this->refresh_malloc_info();
105     return this->heap_info;
106   }
107
108   std::vector<IgnoredRegion> const& ignored_regions() const
109   {
110     return ignored_regions_;
111   }
112   void ignore_region(std::uint64_t address, std::size_t size);
113
114   pid_t pid() const { return pid_; }
115
116   bool in_maestro_stack(remote_ptr<void> p) const
117   {
118     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
119   }
120
121   bool running() const
122   {
123     return running_;
124   }
125
126   void terminate(int status)
127   {
128     status_ = status;
129     running_ = false;
130   }
131
132   int status() const
133   {
134     return status_;
135   }
136
137   template<class M>
138   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
139   send_message(M const& m)
140   {
141     return MC_protocol_send(this->socket_, &m, sizeof(M));
142   }
143
144   int send_message(e_mc_message_type message_id)
145   {
146     return MC_protocol_send_simple_message(this->socket_, message_id);
147   }
148
149   template<class M>
150   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
151   receive_message(M& m)
152   {
153     return MC_receive_message(this->socket_, &m, sizeof(M), 0);
154   }
155
156   void reset_soft_dirty();
157   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
158
159   bool privatized(ObjectInformation const& info) const
160   {
161     return privatized_ && info.executable();
162   }
163   bool privatized() const
164   {
165     return privatized_;
166   }
167   void privatized(bool privatized) { privatized_ = privatized; }
168
169 private:
170   void init_memory_map_info();
171   void refresh_heap();
172   void refresh_malloc_info();
173 private:
174   pid_t pid_;
175   int socket_;
176   int status_;
177   bool running_;
178   std::vector<VmMap> memory_map_;
179   remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
180   int memory_file;
181   std::vector<IgnoredRegion> ignored_regions_;
182   int clear_refs_fd_;
183   int pagemap_fd_;
184   bool privatized_;
185 public: // object info
186   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
187   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
188   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
189   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
190
191 public: // Copies of MCed SMX data structures
192   /** Copy of `simix_global->process_list`
193    *
194    *  See mc_smx.c.
195    */
196   xbt_dynar_t smx_process_infos;
197
198   /** Copy of `simix_global->process_to_destroy`
199    *
200    *  See mc_smx.c.
201    */
202   xbt_dynar_t smx_old_process_infos;
203
204   /** State of the cache (which variables are up to date) */
205   mc_process_cache_flags_t cache_flags;
206
207   /** Address of the heap structure in the MCed process. */
208   void* heap_address;
209
210   /** Copy of the heap structure of the process
211    *
212    *  This is refreshed with the `MC_process_refresh` call.
213    *  This is not used if the process is the current one:
214    *  use `get_heap_info()` in order to use it.
215    */
216    xbt_mheap_t heap;
217
218   /** Copy of the allocation info structure
219    *
220    *  This is refreshed with the `MC_process_refresh` call.
221    *  This is not used if the process is the current one:
222    *  use `get_malloc_info()` in order to use it.
223    */
224   malloc_info* heap_info;
225
226 public: // Libunwind-data
227
228   /** Full-featured MC-aware libunwind address space for the process
229    *
230    *  This address space is using a mc_unw_context_t
231    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
232    *  and unw_context_t).
233    */
234   unw_addr_space_t unw_addr_space;
235
236   /** Underlying libunwind addres-space
237    *
238    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
239    *  operations of the native MC address space is currently delegated
240    *  to this address space (either the local or a ptrace unwinder).
241    */
242   unw_addr_space_t unw_underlying_addr_space;
243
244   /** The corresponding context
245    */
246   void* unw_underlying_context;
247 };
248
249 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
250  */
251 XBT_PRIVATE int open_vm(pid_t pid, int flags);
252
253 }
254 }
255
256 SG_BEGIN_DECL()
257
258 XBT_PRIVATE void MC_invalidate_cache(void);
259
260 SG_END_DECL()
261
262 #endif