Logo AND Algorithmique Numérique Distribuée

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