Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I think I just killed a simcall
[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
27 #include "src/mc/remote/Channel.hpp"
28 #include "src/mc/remote/RemotePtr.hpp"
29
30 #include <simgrid/simix.h>
31 #include "src/simix/popping_private.h"
32 #include "src/simix/smx_private.h"
33
34 #include "src/xbt/memory_map.hpp"
35
36 #include "src/mc/AddressSpace.hpp"
37 #include "src/mc/ObjectInformation.hpp"
38 #include "src/mc/mc_base.h"
39 #include "src/mc/mc_forward.hpp"
40 #include "src/mc/remote/mc_protocol.h"
41
42 namespace simgrid {
43 namespace mc {
44
45 class ActorInformation {
46 public:
47   /** MCed address of the process */
48   RemotePtr<simgrid::simix::ActorImpl> address = nullptr;
49   Remote<simgrid::simix::ActorImpl> 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
113   void read_variable(const char* name, void* target, size_t size) const;
114   template<class T> void read_variable(const char* name, T* target) const
115   {
116     read_variable(name, target, sizeof(*target));
117   }
118   template<class T>
119   Remote<T> read_variable(const char *name) const
120   {
121     Remote<T> res;
122     read_variable(name, res.getBuffer(), sizeof(T));
123     return res;
124   }
125
126   std::string read_string(RemotePtr<char> address) const;
127   std::string read_string(RemotePtr<char> address, std::size_t len) const
128   {
129     return AddressSpace::read_string(address, len);
130   }
131
132   // Write memory:
133   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
134   void clear_bytes(RemotePtr<void> address, size_t len);
135
136   // Debug information:
137   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
138   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
139   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
140   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
141   simgrid::mc::Variable* find_variable(const char* name) const;
142
143   // Heap access:
144   xbt_mheap_t get_heap()
145   {
146     if (!(this->cache_flags_ & Process::cache_heap))
147       this->refresh_heap();
148     return this->heap.get();
149   }
150   const malloc_info* get_malloc_info()
151   {
152     if (!(this->cache_flags_ & Process::cache_malloc))
153       this->refresh_malloc_info();
154     return this->heap_info.data();
155   }
156   
157   void clear_cache()
158   {
159     this->cache_flags_ = Process::cache_none;
160   }
161
162   Channel const& getChannel() const { return channel_; }
163   Channel& getChannel() { return channel_; }
164
165   std::vector<IgnoredRegion> const& ignored_regions() const
166   {
167     return ignored_regions_;
168   }
169   void ignore_region(std::uint64_t address, std::size_t size);
170
171   pid_t pid() const { return pid_; }
172
173   bool in_maestro_stack(RemotePtr<void> p) const
174   {
175     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
176   }
177
178   bool running() const
179   {
180     return running_;
181   }
182
183   void terminate()
184   {
185     running_ = false;
186   }
187
188   bool privatized(ObjectInformation const& info) const
189   {
190     return privatized_ && info.executable();
191   }
192   bool privatized() const
193   {
194     return privatized_;
195   }
196   void privatized(bool privatized) { privatized_ = privatized; }
197
198   void ignore_global_variable(const char* name)
199   {
200     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
201         this->object_infos)
202       info->remove_global_variable(name);
203   }
204
205   std::vector<s_stack_region_t>& stack_areas()
206   {
207     return stack_areas_;
208   }
209   std::vector<s_stack_region_t> const& stack_areas() const
210   {
211     return stack_areas_;
212   }
213
214   std::vector<IgnoredHeapRegion> const& ignored_heap() const
215   {
216     return ignored_heap_;
217   }
218   void ignore_heap(IgnoredHeapRegion const& region);
219   void unignore_heap(void *address, size_t size);
220
221   void ignore_local_variable(const char *var_name, const char *frame_name);
222   std::vector<simgrid::mc::ActorInformation>& actors();
223   std::vector<simgrid::mc::ActorInformation>& dead_actors();
224
225   /** Get a local description of a remote SIMIX actor */
226   simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> actor)
227   {
228     xbt_assert(mc_model_checker != nullptr);
229     if (!actor)
230       return nullptr;
231     this->refresh_simix();
232     for (auto& actor_info : this->smx_actors_infos)
233       if (actor_info.address == actor)
234         return &actor_info;
235     for (auto& actor_info : this->smx_dead_actors_infos)
236       if (actor_info.address == actor)
237         return &actor_info;
238     return nullptr;
239   }
240
241   /** Get a local copy of the SIMIX actor structure */
242   simgrid::simix::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::simix::ActorImpl> process)
243   {
244     simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
245     if (actor_info)
246       return actor_info->copy.getBuffer();
247     else
248       return nullptr;
249   }
250
251   void dumpStack();
252
253 private:
254   void init_memory_map_info();
255   void refresh_heap();
256   void refresh_malloc_info();
257   void refresh_simix();
258
259 private:
260   pid_t pid_ = -1;
261   Channel channel_;
262   bool running_ = false;
263   std::vector<simgrid::xbt::VmMap> memory_map_;
264   RemotePtr<void> maestro_stack_start_, maestro_stack_end_;
265   int memory_file = -1;
266   std::vector<IgnoredRegion> ignored_regions_;
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<ActorInformation> smx_actors_infos;
283
284   /** Copy of `simix_global->process_to_destroy`
285    *
286    *  See mc_smx.c.
287    */
288   std::vector<ActorInformation> smx_dead_actors_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 address-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