Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
code simplification around simgrid::surf::NetCardImpl
[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 <type_traits>
11
12 #include <sys/types.h>
13
14 #include <vector>
15 #include <memory>
16
17 #include "simgrid_config.h"
18 #include <sys/types.h>
19
20 #include <xbt/base.h>
21 #include <xbt/mmalloc.h>
22
23 #ifdef HAVE_MC
24 #include "src/xbt/mmalloc/mmprivate.h"
25 #endif
26
27 #include <simgrid/simix.h>
28 #include "src/simix/popping_private.h"
29 #include "src/simix/smx_private.h"
30
31 #include "src/xbt/memory_map.hpp"
32
33 #include "src/mc/mc_forward.hpp"
34 #include "src/mc/mc_base.h"
35 #include "src/mc/mc_mmalloc.h" // std_heap
36 #include "src/mc/AddressSpace.hpp"
37 #include "src/mc/mc_protocol.h"
38 #include "src/mc/ObjectInformation.hpp"
39
40 // Those flags are used to track down which cached information
41 // is still up to date and which information needs to be updated.
42 typedef int mc_process_cache_flags_t;
43 #define MC_PROCESS_CACHE_FLAG_NONE 0
44 #define MC_PROCESS_CACHE_FLAG_HEAP 1
45 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
46 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
47
48 namespace simgrid {
49 namespace mc {
50
51 struct IgnoredRegion {
52   std::uint64_t addr;
53   size_t size;
54 };
55
56 struct IgnoredHeapRegion {
57   int block;
58   int fragment;
59   void *address;
60   size_t size;
61 };
62
63 /** Representation of a process
64  */
65 class Process final : public AddressSpace {
66 public:
67   Process(pid_t pid, int sockfd);
68   ~Process();
69
70   Process(Process const&) = delete;
71   Process(Process &&) = delete;
72   Process& operator=(Process const&) = delete;
73   Process& operator=(Process &&) = delete;
74
75   // Read memory:
76   const void* read_bytes(void* buffer, std::size_t size,
77     remote_ptr<void> address, int process_index = ProcessIndexAny,
78     ReadMode mode = Normal) const override;
79   void read_variable(const char* name, void* target, size_t size) const;
80   template<class T>
81   T read_variable(const char *name) const
82   {
83     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
84     T res;
85     read_variable(name, &res, sizeof(T));
86     return res;
87   }
88   char* read_string(remote_ptr<void> address) const;
89
90   // Write memory:
91   void write_bytes(const void* buffer, size_t len, remote_ptr<void> address);
92   void clear_bytes(remote_ptr<void> address, size_t len);
93
94   // Debug information:
95   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info(remote_ptr<void> addr) const;
96   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_exec(remote_ptr<void> addr) const;
97   std::shared_ptr<simgrid::mc::ObjectInformation> find_object_info_rw(remote_ptr<void> addr) const;
98   simgrid::mc::Frame* find_function(remote_ptr<void> ip) const;
99   simgrid::mc::Variable* find_variable(const char* name) const;
100
101   // Heap access:
102   xbt_mheap_t get_heap()
103   {
104     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
105       this->refresh_heap();
106     return this->heap.get();
107   }
108   malloc_info* get_malloc_info()
109   {
110     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
111       this->refresh_malloc_info();
112     return this->heap_info.data();
113   }
114
115   std::vector<IgnoredRegion> const& ignored_regions() const
116   {
117     return ignored_regions_;
118   }
119   void ignore_region(std::uint64_t address, std::size_t size);
120
121   pid_t pid() const { return pid_; }
122
123   bool in_maestro_stack(remote_ptr<void> p) const
124   {
125     return p >= this->maestro_stack_start_ && p < this->maestro_stack_end_;
126   }
127
128   bool running() const
129   {
130     return running_;
131   }
132
133   void terminate()
134   {
135     running_ = false;
136   }
137
138   template<class M>
139   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
140   send_message(M const& m)
141   {
142     return MC_protocol_send(this->socket_, &m, sizeof(M));
143   }
144
145   int send_message(e_mc_message_type message_id)
146   {
147     return MC_protocol_send_simple_message(this->socket_, message_id);
148   }
149
150   template<class M>
151   typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
152   receive_message(M& m)
153   {
154     return MC_receive_message(this->socket_, &m, sizeof(M), 0);
155   }
156
157   void reset_soft_dirty();
158   void read_pagemap(uint64_t* pagemap, size_t start_page, size_t page_count);
159
160   bool privatized(ObjectInformation const& info) const
161   {
162     return privatized_ && info.executable();
163   }
164   bool privatized() const
165   {
166     return privatized_;
167   }
168   void privatized(bool privatized) { privatized_ = privatized; }
169
170   void ignore_global_variable(const char* name)
171   {
172     for (std::shared_ptr<simgrid::mc::ObjectInformation> const& info :
173         this->object_infos)
174       info->remove_global_variable(name);
175   }
176
177   std::vector<s_stack_region_t>& stack_areas()
178   {
179     return stack_areas_;
180   }
181   std::vector<s_stack_region_t> const& stack_areas() const
182   {
183     return stack_areas_;
184   }
185
186   std::vector<IgnoredHeapRegion> const& ignored_heap() const
187   {
188     return ignored_heap_;
189   }
190   void ignore_heap(IgnoredHeapRegion const& region);
191   void unignore_heap(void *address, size_t size);
192
193   void ignore_local_variable(const char *var_name, const char *frame_name);
194
195 private:
196   void init_memory_map_info();
197   void refresh_heap();
198   void refresh_malloc_info();
199 private:
200   pid_t pid_;
201   int socket_;
202   bool running_;
203   std::vector<simgrid::xbt::VmMap> memory_map_;
204   remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
205   int memory_file;
206   std::vector<IgnoredRegion> ignored_regions_;
207   int clear_refs_fd_;
208   int pagemap_fd_;
209   bool privatized_;
210   std::vector<s_stack_region_t> stack_areas_;
211   std::vector<IgnoredHeapRegion> ignored_heap_;
212
213 public: // object info
214   // TODO, make private (first, objectify simgrid::mc::ObjectInformation*)
215   std::vector<std::shared_ptr<simgrid::mc::ObjectInformation>> object_infos;
216   std::shared_ptr<simgrid::mc::ObjectInformation> libsimgrid_info;
217   std::shared_ptr<simgrid::mc::ObjectInformation> binary_info;
218
219 public: // Copies of MCed SMX data structures
220   /** Copy of `simix_global->process_list`
221    *
222    *  See mc_smx.c.
223    */
224   xbt_dynar_t smx_process_infos;
225
226   /** Copy of `simix_global->process_to_destroy`
227    *
228    *  See mc_smx.c.
229    */
230   xbt_dynar_t smx_old_process_infos;
231
232   /** State of the cache (which variables are up to date) */
233   mc_process_cache_flags_t cache_flags;
234
235   /** Address of the heap structure in the MCed process. */
236   void* heap_address;
237
238   /** Copy of the heap structure of the process
239    *
240    *  This is refreshed with the `MC_process_refresh` call.
241    *  This is not used if the process is the current one:
242    *  use `get_heap_info()` in order to use it.
243    */
244    std::unique_ptr<s_xbt_mheap_t> heap;
245
246   /** Copy of the allocation info structure
247    *
248    *  This is refreshed with the `MC_process_refresh` call.
249    *  This is not used if the process is the current one:
250    *  use `get_malloc_info()` in order to use it.
251    */
252   std::vector<malloc_info> heap_info;
253
254 public: // Libunwind-data
255
256   /** Full-featured MC-aware libunwind address space for the process
257    *
258    *  This address space is using a mc_unw_context_t
259    *  (with simgrid::mc::Process* / simgrid::mc::AddressSpace*
260    *  and unw_context_t).
261    */
262   unw_addr_space_t unw_addr_space;
263
264   /** Underlying libunwind addres-space
265    *
266    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
267    *  operations of the native MC address space is currently delegated
268    *  to this address space (either the local or a ptrace unwinder).
269    */
270   unw_addr_space_t unw_underlying_addr_space;
271
272   /** The corresponding context
273    */
274   void* unw_underlying_context;
275 };
276
277 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
278  */
279 XBT_PRIVATE int open_vm(pid_t pid, int flags);
280
281 }
282 }
283
284 #endif