Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Add remote_ptr for marking pointers-to/addresses-in the remote process
[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 "simgrid_config.h"
14 #include <sys/types.h>
15
16 #include <xbt/mmalloc.h>
17
18 #ifdef HAVE_MC
19 #include "xbt/mmalloc/mmprivate.h"
20 #endif
21
22 #include <simgrid/simix.h>
23 #include "simix/popping_private.h"
24 #include "simix/smx_private.h"
25
26 #include "mc_forward.h"
27 #include "mc_base.h"
28 #include "mc_mmalloc.h" // std_heap
29 #include "mc_memory_map.h"
30 #include "AddressSpace.hpp"
31 #include "mc_protocol.h"
32
33 typedef int mc_process_flags_t;
34 #define MC_PROCESS_NO_FLAG 0
35 #define MC_PROCESS_SELF_FLAG 1
36
37 // Those flags are used to track down which cached information
38 // is still up to date and which information needs to be updated.
39 typedef int mc_process_cache_flags_t;
40 #define MC_PROCESS_CACHE_FLAG_NONE 0
41 #define MC_PROCESS_CACHE_FLAG_HEAP 1
42 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
43 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
44
45 typedef struct s_mc_smx_process_info s_mc_smx_process_info_t, *mc_smx_process_info_t;
46
47 namespace simgrid {
48 namespace mc {
49
50 /** Representation of a process
51  */
52 class Process : public AddressSpace {
53 public:
54   Process(pid_t pid, int sockfd);
55   ~Process();
56   const void* read_bytes(void* buffer, std::size_t size,
57     remote_ptr<void> address, int process_index = ProcessIndexAny,
58     ReadMode mode = Normal) override;
59   xbt_mheap_t get_heap()
60   {
61     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
62       this->refresh_heap();
63     return this->heap;
64   }
65   malloc_info* get_malloc_info()
66   {
67     if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
68       this->refresh_malloc_info();
69     return this->heap_info;
70   }
71   bool is_self()
72   {
73     return this->process_flags & MC_PROCESS_SELF_FLAG;
74   }
75 private:
76   void refresh_heap();
77   void refresh_malloc_info();
78 public: // to be private
79   mc_process_flags_t process_flags;
80   pid_t pid;
81   int socket;
82   int status;
83   bool running;
84   memory_map_t memory_map;
85   void *maestro_stack_start, *maestro_stack_end;
86   mc_object_info_t libsimgrid_info;
87   mc_object_info_t binary_info;
88   mc_object_info_t* object_infos;
89   size_t object_infos_size;
90   int memory_file;
91
92   /** Copy of `simix_global->process_list`
93    *
94    *  See mc_smx.c.
95    */
96   xbt_dynar_t smx_process_infos;
97
98   /** Copy of `simix_global->process_to_destroy`
99    *
100    *  See mc_smx.c.
101    */
102   xbt_dynar_t smx_old_process_infos;
103
104   /** State of the cache (which variables are up to date) */
105   mc_process_cache_flags_t cache_flags;
106
107   /** Address of the heap structure in the MCed process. */
108   void* heap_address;
109
110   /** Copy of the heap structure of the process
111    *
112    *  This is refreshed with the `MC_process_refresh` call.
113    *  This is not used if the process is the current one:
114    *  use `get_heap_info()` in order to use it.
115    */
116    xbt_mheap_t heap;
117
118   /** Copy of the allocation info structure
119    *
120    *  This is refreshed with the `MC_process_refresh` call.
121    *  This is not used if the process is the current one:
122    *  use `get_malloc_info()` in order to use it.
123    */
124   malloc_info* heap_info;
125
126   // ***** Libunwind-data
127
128   /** Full-featured MC-aware libunwind address space for the process
129    *
130    *  This address space is using a mc_unw_context_t
131    *  (with mc_process_t/mc_address_space_t and unw_context_t).
132    */
133   unw_addr_space_t unw_addr_space;
134
135   /** Underlying libunwind addres-space
136    *
137    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
138    *  operations of the native MC address space is currently delegated
139    *  to this address space (either the local or a ptrace unwinder).
140    */
141   unw_addr_space_t unw_underlying_addr_space;
142
143   /** The corresponding context
144    */
145   void* unw_underlying_context;
146
147   xbt_dynar_t checkpoint_ignore;
148 };
149
150 }
151 }
152
153 SG_BEGIN_DECL()
154
155 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
156  */
157 int MC_process_vm_open(pid_t pid, int flags);
158
159 /* Process memory access: */
160
161 static inline
162 const void* MC_process_read(mc_process_t process,
163   simgrid::mc::AddressSpace::ReadMode mode,
164   void* local, const void* remote, size_t len,
165   int process_index)
166 {
167   return process->read_bytes(local, len, (std::uint64_t)remote, process_index, mode);
168 }
169
170 // Simplified versions/wrappers (whould be moved in mc_address_space):
171 static inline const void* MC_process_read_simple(mc_process_t process,
172   void* local, const void* remote, size_t len)
173 {
174   return process->read_bytes(local, len, (std::uint64_t)remote);
175 }
176
177 XBT_INTERNAL const void* MC_process_read_dynar_element(mc_process_t process,
178   void* local, const void* remote_dynar, size_t i, size_t len);
179 XBT_INTERNAL unsigned long MC_process_read_dynar_length(mc_process_t process,
180   const void* remote_dynar);
181
182 /** Write data to a process memory
183  *
184  *  @param process the process
185  *  @param local   local memory address (source)
186  *  @param remote  target process memory address (target)
187  *  @param len     data size
188  */
189 XBT_INTERNAL void MC_process_write(mc_process_t process,
190   const void* local, void* remote, size_t len);
191
192 XBT_INTERNAL void MC_process_clear_memory(mc_process_t process,
193   void* remote, size_t len);
194
195 /* Functions, variables of the process: */
196
197 XBT_INTERNAL mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
198 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
199 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
200
201 XBT_INTERNAL dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
202
203 XBT_INTERNAL void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size);
204 XBT_INTERNAL char* MC_process_read_string(mc_process_t, void* address);
205
206 /** Find (one occurence of) the named variable definition
207  */
208 XBT_INTERNAL dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
209
210 XBT_INTERNAL void MC_invalidate_cache(void);
211
212 SG_END_DECL()
213
214 #endif