Logo AND Algorithmique Numérique Distribuée

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