Logo AND Algorithmique Numérique Distribuée

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