Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a2cef8542ea247ee169e815cc6f759328d458eca
[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 #if 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 class SimixProcessInformation {
53 public:
54   /** MCed address of the process */
55   void* address = nullptr;
56   union {
57     /** (Flat) Copy of the process data structure */
58     struct s_smx_process copy;
59   };
60   /** Hostname (owned by `mc_modelchecker->hostnames`) */
61   const char* hostname = nullptr;
62   std::string name;
63
64   void clear()
65   {
66     name.clear();
67     address = nullptr;
68     hostname = nullptr;
69   }
70 };
71
72 struct IgnoredRegion {
73   std::uint64_t addr;
74   std::size_t size;
75 };
76
77 struct IgnoredHeapRegion {
78   int block;
79   int fragment;
80   void *address;
81   std::size_t size;
82 };
83
84 /** Representation of a process
85  *
86  *  This class is mixing a lot of differents responsabilities and is tied
87  *  to SIMIX. It should probably split into different classes.
88  *
89  *  Responsabilities:
90  *
91  *  - reading from the process memory (`AddressSpace`);
92  *  - accessing the system state of the porcess (heap, …);
93  *  - storing the SIMIX state of the process;
94  *  - privatization;
95  *  - communication with the model-checked process;
96  *  - stack unwinding;
97  *  - etc.
98  */
99 class Process final : public AddressSpace {
100 public:
101   Process(pid_t pid, int sockfd);
102   ~Process();
103   void init();
104
105   Process(Process const&) = delete;
106   Process(Process &&) = delete;
107   Process& operator=(Process const&) = delete;
108   Process& operator=(Process &&) = delete;
109
110   // Read memory:
111   const void* read_bytes(void* buffer, std::size_t size,
112     RemotePtr<void> address, int process_index = ProcessIndexAny,
113     ReadOptions options = ReadOptions::none()) const override;
114   void read_variable(const char* name, void* target, size_t size) const;
115   template<class T>
116   T read_variable(const char *name) const
117   {
118     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
119     T res;
120     read_variable(name, &res, sizeof(T));
121     return res;
122   }
123   char* read_string(RemotePtr<void> address) const;
124
125   // Write memory:
126   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
127   void clear_bytes(RemotePtr<void> address, size_t len);
128
129   // Debug information:
130   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
131   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
132   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
133   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
134   simgrid::mc::Variable* find_variable(const char* name) const;
135
136   // Heap access:
137   xbt_mheap_t get_heap()
138   {
139     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
140       this->refresh_heap();
141     return this->heap.get();
142   }
143   malloc_info* get_malloc_info()
144   {
145     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
146       this->refresh_malloc_info();
147     return this->heap_info.data();
148   }
149
150   std::vector<IgnoredRegion> const& ignored_regions() const
151   {
152     return ignored_regions_;
153   }
154   void ignore_region(std::uint64_t address, std::size_t size);
155
156   pid_t pid() const { return pid_; }
157
158   bool in_maestro_stack(RemotePtr<void> p) const
159   {
160     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
161   }
162
163   bool running() const
164   {
165     return running_;
166   }
167
168   void terminate()
169   {
170     running_ = false;
171   }
172
173   template<class M>
174   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
175   send_message(M const& m)
176   {
177     return MC_protocol_send(this->socket_, &m, sizeof(M));
178   }
179
180   int send_message(e_mc_message_type message_id)
181   {
182     return MC_protocol_send_simple_message(this->socket_, message_id);
183   }
184
185   template<class M>
186   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
187   receive_message(M& m)
188   {
189     return MC_receive_message(this->socket_, &m, sizeof(M), 0);
190   }
191
192   void reset_soft_dirty();
193   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
194
195   bool privatized(ObjectInformation const& info) const
196   {
197     return privatized_ && info.executable();
198   }
199   bool privatized() const
200   {
201     return privatized_;
202   }
203   void privatized(bool privatized) { privatized_ = privatized; }
204
205   void ignore_global_variable(const char* name)
206   {
207     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
208         this->object_infos)
209       info->remove_global_variable(name);
210   }
211
212   std::vector<s_stack_region_t>& stack_areas()
213   {
214     return stack_areas_;
215   }
216   std::vector<s_stack_region_t> const& stack_areas() const
217   {
218     return stack_areas_;
219   }
220
221   std::vector<IgnoredHeapRegion> const& ignored_heap() const
222   {
223     return ignored_heap_;
224   }
225   void ignore_heap(IgnoredHeapRegion const& region);
226   void unignore_heap(void *address, size_t size);
227
228   void ignore_local_variable(const char *var_name, const char *frame_name);
229   int socket() { return socket_; }
230   std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
231   std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
232
233 private:
234   void init_memory_map_info();
235   void refresh_heap();
236   void refresh_malloc_info();
237   void refresh_simix();
238
239 private:
240   pid_t pid_ = -1;
241   int socket_ = -1;
242   bool running_ = false;
243   std::vector<simgrid::xbt::VmMap> memory_map_;
244   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
245   int memory_file = -1;
246   std::vector<IgnoredRegion> ignored_regions_;
247   int clear_refs_fd_ = -1;
248   int pagemap_fd_ = -1;
249   bool privatized_ = false;
250   std::vector<s_stack_region_t> stack_areas_;
251   std::vector<IgnoredHeapRegion> ignored_heap_;
252
253 public: // object info
254   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
255   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
256   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
257   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
258
259 public: // Copies of MCed SMX data structures
260   /** Copy of `simix_global->process_list`
261    *
262    *  See mc_smx.c.
263    */
264   std::vector<SimixProcessInformation> smx_process_infos;
265
266   /** Copy of `simix_global->process_to_destroy`
267    *
268    *  See mc_smx.c.
269    */
270   std::vector<SimixProcessInformation> smx_old_process_infos;
271
272   /** State of the cache (which variables are up to date) */
273   mc_process_cache_flags_t cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
274
275   /** Address of the heap structure in the MCed process. */
276   void* heap_address;
277
278   /** Copy of the heap structure of the process
279    *
280    *  This is refreshed with the `MC_process_refresh` call.
281    *  This is not used if the process is the current one:
282    *  use `get_heap_info()` in order to use it.
283    */
284    std::unique_ptr<s_xbt_mheap_t> heap;
285
286   /** Copy of the allocation info structure
287    *
288    *  This is refreshed with the `MC_process_refresh` call.
289    *  This is not used if the process is the current one:
290    *  use `get_malloc_info()` in order to use it.
291    */
292   std::vector<malloc_info> heap_info;
293
294 public: // Libunwind-data
295
296   /** Full-featured MC-aware libunwind address space for the process
297    *
298    *  This address space is using a mc_unw_context_t
299    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
300    *  and unw_context_t).
301    */
302   unw_addr_space_t unw_addr_space;
303
304   /** Underlying libunwind addres-space
305    *
306    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
307    *  operations of the native MC address space is currently delegated
308    *  to this address space (either the local or a ptrace unwinder).
309    */
310   unw_addr_space_t unw_underlying_addr_space;
311
312   /** The corresponding context
313    */
314   void* unw_underlying_context;
315 };
316
317 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
318  */
319 XBT_PRIVATE int open_vm(pid_t pid, int flags);
320
321 }
322 }
323
324 #endif