Logo AND Algorithmique Numérique Distribuée

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