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