Logo AND Algorithmique Numérique Distribuée

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