Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e797a0a1061acd5c718822fd714926d71dde6784
[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<simgrid::simix::Process> address = nullptr;
49   Remote<simgrid::simix::Process> copy;
50
51   /** Hostname (owned by `mc_modelchecker->hostnames`) */
52   const char* hostname = nullptr;
53   std::string name;
54
55   void clear()
56   {
57     name.clear();
58     address = nullptr;
59     hostname = nullptr;
60   }
61 };
62
63 struct IgnoredRegion {
64   std::uint64_t addr;
65   std::size_t size;
66 };
67
68 struct IgnoredHeapRegion {
69   int block;
70   int fragment;
71   void *address;
72   std::size_t size;
73 };
74
75 /** Representation of a process
76  *
77  *  This class is mixing a lot of different responsibilities and is tied
78  *  to SIMIX. It should probably be split into different classes.
79  *
80  *  Responsibilities:
81  *
82  *  - reading from the process memory (`AddressSpace`);
83  *  - accessing the system state of the process (heap, …);
84  *  - storing the SIMIX state of the process;
85  *  - privatization;
86  *  - communication with the model-checked process;
87  *  - stack unwinding;
88  *  - etc.
89  */
90 class Process final : public AddressSpace {
91 private:
92   // Those flags are used to track down which cached information
93   // is still up to date and which information needs to be updated.
94   static constexpr int cache_none = 0;
95   static constexpr int cache_heap = 1;
96   static constexpr int cache_malloc = 2;
97   static constexpr int cache_simix_processes = 4;
98 public:
99   Process(pid_t pid, int sockfd);
100   ~Process();
101   void init();
102
103   Process(Process const&) = delete;
104   Process(Process &&) = delete;
105   Process& operator=(Process const&) = delete;
106   Process& operator=(Process &&) = delete;
107
108   // Read memory:
109   const void* read_bytes(void* buffer, std::size_t size,
110     RemotePtr<void> address, int process_index = ProcessIndexAny,
111     ReadOptions options = ReadOptions::none()) const override;
112   void read_variable(const char* name, void* target, size_t size) const;
113   template<class T>
114   T read_variable(const char *name) const
115   {
116     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
117     T res;
118     read_variable(name, &res, sizeof(T));
119     return res;
120   }
121   std::string read_string(RemotePtr<char> address) const;
122   std::string read_string(RemotePtr<char> address, std::size_t len) const
123   {
124     return AddressSpace::read_string(address, len);
125   }
126
127   // Write memory:
128   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
129   void clear_bytes(RemotePtr<void> address, size_t len);
130
131   // Debug information:
132   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
133   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
134   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
135   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
136   simgrid::mc::Variable* find_variable(const char* name) const;
137
138   // Heap access:
139   xbt_mheap_t get_heap()
140   {
141     if (!(this->cache_flags_ & Process::cache_heap))
142       this->refresh_heap();
143     return this->heap.get();
144   }
145   malloc_info* get_malloc_info()
146   {
147     if (!(this->cache_flags_ & Process::cache_malloc))
148       this->refresh_malloc_info();
149     return this->heap_info.data();
150   }
151   
152   void clear_cache()
153   {
154     this->cache_flags_ = Process::cache_none;
155   }
156
157   Channel const& getChannel() const { return channel_; }
158   Channel& getChannel() { return channel_; }
159
160   std::vector<IgnoredRegion> const& ignored_regions() const
161   {
162     return ignored_regions_;
163   }
164   void ignore_region(std::uint64_t address, std::size_t size);
165
166   pid_t pid() const { return pid_; }
167
168   bool in_maestro_stack(RemotePtr<void> p) const
169   {
170     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
171   }
172
173   bool running() const
174   {
175     return running_;
176   }
177
178   void terminate()
179   {
180     running_ = false;
181   }
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   /** Get a local description of a remote SIMIX process */
221   simgrid::mc::SimixProcessInformation* resolveProcessInfo(
222     simgrid::mc::RemotePtr<simgrid::simix::Process> process)
223   {
224     xbt_assert(mc_model_checker != nullptr);
225     if (!process)
226       return nullptr;
227     this->refresh_simix();
228     for (auto& process_info : this->smx_process_infos)
229       if (process_info.address == process)
230         return &process_info;
231     for (auto& process_info : this->smx_old_process_infos)
232       if (process_info.address == process)
233         return &process_info;
234     return nullptr;
235   }
236
237   /** Get a local copy of the SIMIX process structure */
238   simgrid::simix::Process* resolveProcess(simgrid::mc::RemotePtr<simgrid::simix::Process> process)
239   {
240     simgrid::mc::SimixProcessInformation* process_info =
241       this->resolveProcessInfo(process);
242     if (process_info)
243       return process_info->copy.getBuffer();
244     else
245       return nullptr;
246   }
247
248   void dumpStack();
249
250 private:
251   void init_memory_map_info();
252   void refresh_heap();
253   void refresh_malloc_info();
254   void refresh_simix();
255
256 private:
257   pid_t pid_ = -1;
258   Channel channel_;
259   bool running_ = false;
260   std::vector<simgrid::xbt::VmMap> memory_map_;
261   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
262   int memory_file = -1;
263   std::vector<IgnoredRegion> ignored_regions_;
264   bool privatized_ = false;
265   std::vector<s_stack_region_t> stack_areas_;
266   std::vector<IgnoredHeapRegion> ignored_heap_;
267
268 public: // object info
269   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
270   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
271   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
272   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
273
274 public: // Copies of MCed SMX data structures
275   /** Copy of `simix_global->process_list`
276    *
277    *  See mc_smx.c.
278    */
279   std::vector<SimixProcessInformation> smx_process_infos;
280
281   /** Copy of `simix_global->process_to_destroy`
282    *
283    *  See mc_smx.c.
284    */
285   std::vector<SimixProcessInformation> smx_old_process_infos;
286
287 private:
288   /** State of the cache (which variables are up to date) */
289   int cache_flags_ = Process::cache_none;
290
291 public:
292   /** Address of the heap structure in the MCed process. */
293   void* heap_address;
294
295   /** Copy of the heap structure of the process
296    *
297    *  This is refreshed with the `MC_process_refresh` call.
298    *  This is not used if the process is the current one:
299    *  use `get_heap_info()` in order to use it.
300    */
301    std::unique_ptr<s_xbt_mheap_t> heap;
302
303   /** Copy of the allocation info structure
304    *
305    *  This is refreshed with the `MC_process_refresh` call.
306    *  This is not used if the process is the current one:
307    *  use `get_malloc_info()` in order to use it.
308    */
309   std::vector<malloc_info> heap_info;
310
311 public: // Libunwind-data
312
313   /** Full-featured MC-aware libunwind address space for the process
314    *
315    *  This address space is using a simgrid::mc::UnwindContext*
316    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
317    *  and unw_context_t).
318    */
319   unw_addr_space_t unw_addr_space;
320
321   /** Underlying libunwind address-space
322    *
323    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
324    *  operations of the native MC address space is currently delegated
325    *  to this address space (either the local or a ptrace unwinder).
326    */
327   unw_addr_space_t unw_underlying_addr_space;
328
329   /** The corresponding context
330    */
331   void* unw_underlying_context;
332 };
333
334 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
335  */
336 XBT_PRIVATE int open_vm(pid_t pid, int flags);
337
338 }
339 }
340
341 #endif