Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Use Session::execute() in LivenessChecker as well
[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   void reset_soft_dirty();
182   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
183
184   bool privatized(ObjectInformation const& info) const
185   {
186     return privatized_ && info.executable();
187   }
188   bool privatized() const
189   {
190     return privatized_;
191   }
192   void privatized(bool privatized) { privatized_ = privatized; }
193
194   void ignore_global_variable(const char* name)
195   {
196     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
197         this->object_infos)
198       info->remove_global_variable(name);
199   }
200
201   std::vector<s_stack_region_t>& stack_areas()
202   {
203     return stack_areas_;
204   }
205   std::vector<s_stack_region_t> const& stack_areas() const
206   {
207     return stack_areas_;
208   }
209
210   std::vector<IgnoredHeapRegion> const& ignored_heap() const
211   {
212     return ignored_heap_;
213   }
214   void ignore_heap(IgnoredHeapRegion const& region);
215   void unignore_heap(void *address, size_t size);
216
217   void ignore_local_variable(const char *var_name, const char *frame_name);
218   std::vector<simgrid::mc::SimixProcessInformation>& simix_processes();
219   std::vector<simgrid::mc::SimixProcessInformation>& old_simix_processes();
220
221   /** Get a local description of a remote SIMIX process */
222   simgrid::mc::SimixProcessInformation* resolveProcessInfo(
223     simgrid::mc::RemotePtr<s_smx_process_t> process)
224   {
225     xbt_assert(mc_mode == MC_MODE_SERVER);
226     if (!process)
227       return nullptr;
228     this->refresh_simix();
229     for (auto& process_info : this->smx_process_infos)
230       if (process_info.address == process)
231         return &process_info;
232     for (auto& process_info : this->smx_old_process_infos)
233       if (process_info.address == process)
234         return &process_info;
235     return nullptr;
236   }
237
238   /** Get a local copy of the SIMIX process structure */
239   smx_process_t resolveProcess(simgrid::mc::RemotePtr<s_smx_process_t> process)
240   {
241     simgrid::mc::SimixProcessInformation* process_info =
242       this->resolveProcessInfo(process);
243     if (process_info)
244       return &process_info->copy;
245     else
246       return nullptr;
247   }
248
249   void dumpStack();
250
251 private:
252   void init_memory_map_info();
253   void refresh_heap();
254   void refresh_malloc_info();
255   void refresh_simix();
256
257 private:
258   pid_t pid_ = -1;
259   Channel channel_;
260   bool running_ = false;
261   std::vector<simgrid::xbt::VmMap> memory_map_;
262   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
263   int memory_file = -1;
264   std::vector<IgnoredRegion> ignored_regions_;
265   int clear_refs_fd_ = -1;
266   int pagemap_fd_ = -1;
267   bool privatized_ = false;
268   std::vector<s_stack_region_t> stack_areas_;
269   std::vector<IgnoredHeapRegion> ignored_heap_;
270
271 public: // object info
272   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
273   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
274   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
275   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
276
277 public: // Copies of MCed SMX data structures
278   /** Copy of `simix_global->process_list`
279    *
280    *  See mc_smx.c.
281    */
282   std::vector<SimixProcessInformation> smx_process_infos;
283
284   /** Copy of `simix_global->process_to_destroy`
285    *
286    *  See mc_smx.c.
287    */
288   std::vector<SimixProcessInformation> smx_old_process_infos;
289
290 private:
291   /** State of the cache (which variables are up to date) */
292   int cache_flags_ = Process::cache_none;
293
294 public:
295   /** Address of the heap structure in the MCed process. */
296   void* heap_address;
297
298   /** Copy of the heap structure of the process
299    *
300    *  This is refreshed with the `MC_process_refresh` call.
301    *  This is not used if the process is the current one:
302    *  use `get_heap_info()` in order to use it.
303    */
304    std::unique_ptr<s_xbt_mheap_t> heap;
305
306   /** Copy of the allocation info structure
307    *
308    *  This is refreshed with the `MC_process_refresh` call.
309    *  This is not used if the process is the current one:
310    *  use `get_malloc_info()` in order to use it.
311    */
312   std::vector<malloc_info> heap_info;
313
314 public: // Libunwind-data
315
316   /** Full-featured MC-aware libunwind address space for the process
317    *
318    *  This address space is using a simgrid::mc::UnwindContext*
319    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
320    *  and unw_context_t).
321    */
322   unw_addr_space_t unw_addr_space;
323
324   /** Underlying libunwind addres-space
325    *
326    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
327    *  operations of the native MC address space is currently delegated
328    *  to this address space (either the local or a ptrace unwinder).
329    */
330   unw_addr_space_t unw_underlying_addr_space;
331
332   /** The corresponding context
333    */
334   void* unw_underlying_context;
335 };
336
337 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
338  */
339 XBT_PRIVATE int open_vm(pid_t pid, int flags);
340
341 }
342 }
343
344 #endif