Logo AND Algorithmique Numérique Distribuée

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