Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cut some includes in MC
[simgrid.git] / src / mc / remote / RemoteClient.hpp
1 /* Copyright (c) 2008-2018. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_PROCESS_H
7 #define SIMGRID_MC_PROCESS_H
8
9 #include "src/xbt/mmalloc/mmprivate.h"
10 #include "src/mc/remote/Channel.hpp"
11 #include "src/simix/smx_private.hpp"
12 #include "src/mc/ObjectInformation.hpp"
13
14 #include <vector>
15
16 namespace simgrid {
17 namespace mc {
18
19 class ActorInformation {
20 public:
21   /** MCed address of the process */
22   RemotePtr<simgrid::kernel::actor::ActorImpl> address{nullptr};
23   Remote<simgrid::kernel::actor::ActorImpl> copy;
24
25   /** Hostname (owned by `mc_modelchecker->hostnames`) */
26   const char* hostname = nullptr;
27   std::string name;
28
29   void clear()
30   {
31     name.clear();
32     address  = nullptr;
33     hostname = nullptr;
34   }
35 };
36
37 struct IgnoredRegion {
38   std::uint64_t addr;
39   std::size_t size;
40 };
41
42 struct IgnoredHeapRegion {
43   int block;
44   int fragment;
45   void* address;
46   std::size_t size;
47 };
48
49 /** The Model-Checked process, seen from the MCer perspective
50  *
51  *  This class is mixing a lot of different responsibilities and is tied
52  *  to SIMIX. It should probably be split into different classes.
53  *
54  *  Responsibilities:
55  *
56  *  - reading from the process memory (`AddressSpace`);
57  *  - accessing the system state of the process (heap, …);
58  *  - storing the SIMIX state of the process;
59  *  - privatization;
60  *  - communication with the model-checked process;
61  *  - stack unwinding;
62  *  - etc.
63  */
64 class RemoteClient final : public AddressSpace {
65 private:
66   // Those flags are used to track down which cached information
67   // is still up to date and which information needs to be updated.
68   static constexpr int cache_none            = 0;
69   static constexpr int cache_heap            = 1;
70   static constexpr int cache_malloc          = 2;
71   static constexpr int cache_simix_processes = 4;
72
73 public:
74   RemoteClient(pid_t pid, int sockfd);
75   ~RemoteClient();
76   void init();
77
78   RemoteClient(RemoteClient const&) = delete;
79   RemoteClient(RemoteClient&&)      = delete;
80   RemoteClient& operator=(RemoteClient const&) = delete;
81   RemoteClient& operator=(RemoteClient&&) = delete;
82
83   // Read memory:
84   const void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address, int process_index = ProcessIndexAny,
85                          ReadOptions options = ReadOptions::none()) const override;
86
87   void read_variable(const char* name, void* target, size_t size) const;
88   template <class T> void read_variable(const char* name, T* target) const
89   {
90     read_variable(name, target, sizeof(*target));
91   }
92   template <class T> Remote<T> read_variable(const char* name) const
93   {
94     Remote<T> res;
95     read_variable(name, res.getBuffer(), sizeof(T));
96     return res;
97   }
98
99   std::string read_string(RemotePtr<char> address) const;
100   using AddressSpace::read_string;
101
102   // Write memory:
103   void write_bytes(const void* buffer, size_t len, RemotePtr<void> address);
104   void clear_bytes(RemotePtr<void> address, size_t len);
105
106   // Debug information:
107   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(RemotePtr<void> addr) const;
108   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(RemotePtr<void> addr) const;
109   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(RemotePtr<void> addr) const;
110   simgrid::mc::Frame* find_function(RemotePtr<void> ip) const;
111   simgrid::mc::Variable* find_variable(const char* name) const;
112
113   // Heap access:
114   xbt_mheap_t get_heap()
115   {
116     if (not(this->cache_flags_ & RemoteClient::cache_heap))
117       this->refresh_heap();
118     return this->heap.get();
119   }
120   const malloc_info* get_malloc_info()
121   {
122     if (not(this->cache_flags_ & RemoteClient::cache_malloc))
123       this->refresh_malloc_info();
124     return this->heap_info.data();
125   }
126
127   void clear_cache() { this->cache_flags_ = RemoteClient::cache_none; }
128
129   Channel const& getChannel() const { return channel_; }
130   Channel& getChannel() { return channel_; }
131
132   std::vector<IgnoredRegion> const& ignored_regions() const { return ignored_regions_; }
133   void ignore_region(std::uint64_t address, std::size_t size);
134
135   pid_t pid() const { return pid_; }
136
137   bool in_maestro_stack(RemotePtr<void> p) const
138   {
139     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
140   }
141
142   bool running() const { return running_; }
143
144   void terminate() { running_ = false; }
145
146   bool privatized(ObjectInformation const& info) const { return privatized_ && info.executable(); }
147   bool privatized() const { return privatized_; }
148   void privatized(bool privatized) { privatized_ = privatized; }
149
150   void ignore_global_variable(const char* name)
151   {
152     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info : this->object_infos)
153       info->remove_global_variable(name);
154   }
155
156   std::vector<s_stack_region_t>& stack_areas() { return stack_areas_; }
157   std::vector<s_stack_region_t> const& stack_areas() const { return stack_areas_; }
158
159   std::vector<IgnoredHeapRegion> const& ignored_heap() const { return ignored_heap_; }
160   void ignore_heap(IgnoredHeapRegion const& region);
161   void unignore_heap(void* address, size_t size);
162
163   void ignore_local_variable(const char* var_name, const char* frame_name);
164   std::vector<simgrid::mc::ActorInformation>& actors();
165   std::vector<simgrid::mc::ActorInformation>& dead_actors();
166
167   /** Get a local description of a remote SIMIX actor */
168   simgrid::mc::ActorInformation* resolveActorInfo(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> actor)
169   {
170     xbt_assert(mc_model_checker != nullptr);
171     if (not actor)
172       return nullptr;
173     this->refresh_simix();
174     for (auto& actor_info : this->smx_actors_infos)
175       if (actor_info.address == actor)
176         return &actor_info;
177     for (auto& actor_info : this->smx_dead_actors_infos)
178       if (actor_info.address == actor)
179         return &actor_info;
180     return nullptr;
181   }
182
183   /** Get a local copy of the SIMIX actor structure */
184   simgrid::kernel::actor::ActorImpl* resolveActor(simgrid::mc::RemotePtr<simgrid::kernel::actor::ActorImpl> process)
185   {
186     simgrid::mc::ActorInformation* actor_info = this->resolveActorInfo(process);
187     if (actor_info)
188       return actor_info->copy.getBuffer();
189     else
190       return nullptr;
191   }
192
193   void dumpStack();
194
195 private:
196   void init_memory_map_info();
197   void refresh_heap();
198   void refresh_malloc_info();
199   void refresh_simix();
200
201   pid_t pid_ = -1;
202   Channel channel_;
203   bool running_ = false;
204   std::vector<simgrid::xbt::VmMap> memory_map_;
205   RemotePtr<void> maestro_stack_start_;
206   RemotePtr<void> maestro_stack_end_;
207   int memory_file = -1;
208   std::vector<IgnoredRegion> ignored_regions_;
209   bool privatized_ = false;
210   std::vector<s_stack_region_t> stack_areas_;
211   std::vector<IgnoredHeapRegion> ignored_heap_;
212
213 public:
214   // object info
215   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
216   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
217   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
218   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
219
220   // Copies of MCed SMX data structures
221   /** Copy of `simix_global->process_list`
222    *
223    *  See mc_smx.c.
224    */
225   std::vector<ActorInformation> smx_actors_infos;
226
227   /** Copy of `simix_global->process_to_destroy`
228    *
229    *  See mc_smx.c.
230    */
231   std::vector<ActorInformation> smx_dead_actors_infos;
232
233 private:
234   /** State of the cache (which variables are up to date) */
235   int cache_flags_ = RemoteClient::cache_none;
236
237 public:
238   /** Address of the heap structure in the MCed process. */
239   void* heap_address;
240
241   /** Copy of the heap structure of the process
242    *
243    *  This is refreshed with the `MC_process_refresh` call.
244    *  This is not used if the process is the current one:
245    *  use `get_heap_info()` in order to use it.
246    */
247   std::unique_ptr<s_xbt_mheap_t> heap;
248
249   /** Copy of the allocation info structure
250    *
251    *  This is refreshed with the `MC_process_refresh` call.
252    *  This is not used if the process is the current one:
253    *  use `get_malloc_info()` in order to use it.
254    */
255   std::vector<malloc_info> heap_info;
256
257   // Libunwind-data
258   /** Full-featured MC-aware libunwind address space for the process
259    *
260    *  This address space is using a simgrid::mc::UnwindContext*
261    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
262    *  and unw_context_t).
263    */
264   unw_addr_space_t unw_addr_space;
265
266   /** Underlying libunwind address-space
267    *
268    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
269    *  operations of the native MC address space is currently delegated
270    *  to this address space (either the local or a ptrace unwinder).
271    */
272   unw_addr_space_t unw_underlying_addr_space;
273
274   /** The corresponding context
275    */
276   void* unw_underlying_context;
277
278   /* Check whether the given actor is enabled */
279   bool actor_is_enabled(aid_t pid);
280 };
281
282 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
283  */
284 XBT_PRIVATE int open_vm(pid_t pid, int flags);
285 }
286 }
287
288 #endif