Logo AND Algorithmique Numérique Distribuée

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