Logo AND Algorithmique Numérique Distribuée

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