Logo AND Algorithmique Numérique Distribuée

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