Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Duplicate restoreState() as a method of SafetyChecker
[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 #include <string>
17
18 #include <sys/types.h>
19
20 #include <simgrid_config.h>
21
22 #include <xbt/base.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/RemotePtr.hpp"
37 #include "src/mc/AddressSpace.hpp"
38 #include "src/mc/mc_protocol.h"
39 #include "src/mc/ObjectInformation.hpp"
40
41
42 namespace simgrid {
43 namespace mc {
44
45 class SimixProcessInformation {
46 public:
47   /** MCed address of the process */
48   RemotePtr<s_smx_process_t> address = nullptr;
49   union {
50     /** (Flat) Copy of the process data structure */
51     struct s_smx_process copy;
52   };
53   /** Hostname (owned by `mc_modelchecker->hostnames`) */
54   const char* hostname = nullptr;
55   std::string name;
56
57   void clear()
58   {
59     name.clear();
60     address = nullptr;
61     hostname = nullptr;
62   }
63 };
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  *  This class is mixing a lot of different responsabilities and is tied
80  *  to SIMIX. It should probably be split into different classes.
81  *
82  *  Responsabilities:
83  *
84  *  - reading from the process memory (`AddressSpace`);
85  *  - accessing the system state of the porcess (heap, …);
86  *  - storing the SIMIX state of the process;
87  *  - privatization;
88  *  - communication with the model-checked process;
89  *  - stack unwinding;
90  *  - etc.
91  */
92 class Process final : public AddressSpace {
93 private:
94   // Those flags are used to track down which cached information
95   // is still up to date and which information needs to be updated.
96   static constexpr int cache_none = 0;
97   static constexpr int cache_heap = 1;
98   static constexpr int cache_malloc = 2;
99   static constexpr int cache_simix_processes = 4;
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   std::string 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_ & Process::cache_heap))
140       this->refresh_heap();
141     return this->heap.get();
142   }
143   malloc_info* get_malloc_info()
144   {
145     if (!(this->cache_flags_ & Process::cache_malloc))
146       this->refresh_malloc_info();
147     return this->heap_info.data();
148   }
149   
150   void clear_cache()
151   {
152     this->cache_flags_ = Process::cache_none;
153   }
154
155   Channel const& getChannel() const { return channel_; }
156   Channel& getChannel() { return channel_; }
157
158   std::vector<IgnoredRegion> const& ignored_regions() const
159   {
160     return ignored_regions_;
161   }
162   void ignore_region(std::uint64_t address, std::size_t size);
163
164   pid_t pid() const { return pid_; }
165
166   bool in_maestro_stack(RemotePtr<void> p) const
167   {
168     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
169   }
170
171   bool running() const
172   {
173     return running_;
174   }
175
176   void terminate()
177   {
178     running_ = false;
179   }
180
181   bool privatized(ObjectInformation const& info) const
182   {
183     return privatized_ && info.executable();
184   }
185   bool privatized() const
186   {
187     return privatized_;
188   }
189   void privatized(bool privatized) { privatized_ = privatized; }
190
191   void ignore_global_variable(const char* name)
192   {
193     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
194         this->object_infos)
195       info->remove_global_variable(name);
196   }
197
198   std::vector<s_stack_region_t>& stack_areas()
199   {
200     return stack_areas_;
201   }
202   std::vector<s_stack_region_t> const& stack_areas() const
203   {
204     return stack_areas_;
205   }
206
207   std::vector<IgnoredHeapRegion> const& ignored_heap() const
208   {
209     return ignored_heap_;
210   }
211   void ignore_heap(IgnoredHeapRegion const& region);
212   void unignore_heap(void *address, size_t size);
213
214   void ignore_local_variable(const char *var_name, const char *frame_name);
215   std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
216   std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
217
218   /** Get a local description of a remote SIMIX process */
219   simgrid::mc::SimixProcessInformation* resolveProcessInfo(
220     simgrid::mc::RemotePtr<s_smx_process_t> process)
221   {
222     xbt_assert(mc_mode == MC_MODE_SERVER);
223     if (!process)
224       return nullptr;
225     this->refresh_simix();
226     for (auto& process_info : this->smx_process_infos)
227       if (process_info.address == process)
228         return &process_info;
229     for (auto& process_info : this->smx_old_process_infos)
230       if (process_info.address == process)
231         return &process_info;
232     return nullptr;
233   }
234
235   /** Get a local copy of the SIMIX process structure */
236   smx_process_t resolveProcess(simgrid::mc::RemotePtr<s_smx_process_t> process)
237   {
238     simgrid::mc::SimixProcessInformation* process_info =
239       this->resolveProcessInfo(process);
240     if (process_info)
241       return &process_info->copy;
242     else
243       return nullptr;
244   }
245
246   void dumpStack();
247
248 private:
249   void init_memory_map_info();
250   void refresh_heap();
251   void refresh_malloc_info();
252   void refresh_simix();
253
254 private:
255   pid_t pid_ = -1;
256   Channel channel_;
257   bool running_ = false;
258   std::vector<simgrid::xbt::VmMap> memory_map_;
259   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
260   int memory_file = -1;
261   std::vector<IgnoredRegion> ignored_regions_;
262   bool privatized_ = false;
263   std::vector<s_stack_region_t> stack_areas_;
264   std::vector<IgnoredHeapRegion> ignored_heap_;
265
266 public: // object info
267   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
268   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
269   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
270   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
271
272 public: // Copies of MCed SMX data structures
273   /** Copy of `simix_global->process_list`
274    *
275    *  See mc_smx.c.
276    */
277   std::vector<SimixProcessInformation> smx_process_infos;
278
279   /** Copy of `simix_global->process_to_destroy`
280    *
281    *  See mc_smx.c.
282    */
283   std::vector<SimixProcessInformation> smx_old_process_infos;
284
285 private:
286   /** State of the cache (which variables are up to date) */
287   int cache_flags_ = Process::cache_none;
288
289 public:
290   /** Address of the heap structure in the MCed process. */
291   void* heap_address;
292
293   /** Copy of the heap structure of the process
294    *
295    *  This is refreshed with the `MC_process_refresh` call.
296    *  This is not used if the process is the current one:
297    *  use `get_heap_info()` in order to use it.
298    */
299    std::unique_ptr<s_xbt_mheap_t> heap;
300
301   /** Copy of the allocation info structure
302    *
303    *  This is refreshed with the `MC_process_refresh` call.
304    *  This is not used if the process is the current one:
305    *  use `get_malloc_info()` in order to use it.
306    */
307   std::vector<malloc_info> heap_info;
308
309 public: // Libunwind-data
310
311   /** Full-featured MC-aware libunwind address space for the process
312    *
313    *  This address space is using a simgrid::mc::UnwindContext*
314    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
315    *  and unw_context_t).
316    */
317   unw_addr_space_t unw_addr_space;
318
319   /** Underlying libunwind addres-space
320    *
321    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
322    *  operations of the native MC address space is currently delegated
323    *  to this address space (either the local or a ptrace unwinder).
324    */
325   unw_addr_space_t unw_underlying_addr_space;
326
327   /** The corresponding context
328    */
329   void* unw_underlying_context;
330 };
331
332 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
333  */
334 XBT_PRIVATE int open_vm(pid_t pid, int flags);
335
336 }
337 }
338
339 #endif