Logo AND Algorithmique Numérique Distribuée

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