Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
587958046138d478156b1d8ef7f35f9bc94abca4
[simgrid.git] / src / mc / mc_process.h
1 /* Copyright (c) 2008-2014. 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 MC_PROCESS_H
8 #define MC_PROCESS_H
9
10 #include <stdbool.h>
11 #include <sys/types.h>
12
13 #include <vector>
14
15 #include "simgrid_config.h"
16 #include <sys/types.h>
17
18 #include <xbt/mmalloc.h>
19
20 #ifdef HAVE_MC
21 #include "xbt/mmalloc/mmprivate.h"
22 #endif
23
24 #include <simgrid/simix.h>
25 #include "simix/popping_private.h"
26 #include "simix/smx_private.h"
27
28 #include "mc_forward.h"
29 #include "mc_base.h"
30 #include "mc_mmalloc.h" // std_heap
31 #include "mc_memory_map.h"
32 #include "AddressSpace.hpp"
33 #include "mc_protocol.h"
34
35 typedef int mc_process_flags_t;
36 #define MC_PROCESS_NO_FLAG 0
37 #define MC_PROCESS_SELF_FLAG 1
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 typedef struct s_mc_smx_process_info s_mc_smx_process_info_t, *mc_smx_process_info_t;
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 : public AddressSpace {
60 public:
61   Process(pid_t pid, int sockfd);
62   ~Process();
63
64   bool is_self() const
65   {
66     return this->process_flags & MC_PROCESS_SELF_FLAG;
67   }
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 MC_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   mc_object_info_t find_object_info(remote_ptr<void> addr) const;
90   mc_object_info_t find_object_info_exec(remote_ptr<void> addr) const;
91   mc_object_info_t find_object_info_rw(remote_ptr<void> addr) const;
92   dw_frame_t find_function(remote_ptr<void> ip) const;
93   dw_variable_t 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;
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;
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(int status)
128   {
129     status_ = status;
130     running_ = false;
131   }
132
133   int status() const
134   {
135     return status_;
136   }
137
138 private:
139   void init_memory_map_info();
140   void refresh_heap();
141   void refresh_malloc_info();
142 private:
143   mc_process_flags_t process_flags;
144   pid_t pid_;
145 public: // to be private
146   int socket;
147 private:
148   int status_;
149   bool running_;
150   memory_map_t memory_map;
151   remote_ptr<void> maestro_stack_start_, maestro_stack_end_;
152   int memory_file;
153   std::vector<IgnoredRegion> ignored_regions_;
154
155 public: // object info
156   // TODO, make private (first, objectify mc_object_info_t)
157   mc_object_info_t libsimgrid_info;
158   mc_object_info_t binary_info;
159   mc_object_info_t* object_infos;
160   size_t object_infos_size;
161
162 public: // Copies of MCed SMX data structures
163   /** Copy of `simix_global->process_list`
164    *
165    *  See mc_smx.c.
166    */
167   xbt_dynar_t smx_process_infos;
168
169   /** Copy of `simix_global->process_to_destroy`
170    *
171    *  See mc_smx.c.
172    */
173   xbt_dynar_t smx_old_process_infos;
174
175   /** State of the cache (which variables are up to date) */
176   mc_process_cache_flags_t cache_flags;
177
178   /** Address of the heap structure in the MCed process. */
179   void* heap_address;
180
181   /** Copy of the heap structure of the process
182    *
183    *  This is refreshed with the `MC_process_refresh` call.
184    *  This is not used if the process is the current one:
185    *  use `get_heap_info()` in order to use it.
186    */
187    xbt_mheap_t heap;
188
189   /** Copy of the allocation info structure
190    *
191    *  This is refreshed with the `MC_process_refresh` call.
192    *  This is not used if the process is the current one:
193    *  use `get_malloc_info()` in order to use it.
194    */
195   malloc_info* heap_info;
196
197 public: // Libunwind-data
198
199   /** Full-featured MC-aware libunwind address space for the process
200    *
201    *  This address space is using a mc_unw_context_t
202    *  (with mc_process_t/mc_address_space_t and unw_context_t).
203    */
204   unw_addr_space_t unw_addr_space;
205
206   /** Underlying libunwind addres-space
207    *
208    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
209    *  operations of the native MC address space is currently delegated
210    *  to this address space (either the local or a ptrace unwinder).
211    */
212   unw_addr_space_t unw_underlying_addr_space;
213
214   /** The corresponding context
215    */
216   void* unw_underlying_context;
217 };
218
219 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
220  */
221 int open_vm(pid_t pid, int flags);
222
223 }
224 }
225
226 SG_BEGIN_DECL()
227
228 XBT_INTERNAL void MC_invalidate_cache(void);
229
230 SG_END_DECL()
231
232 #endif