Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
53aa8d183232845355f12cc4fcec710bf5cd442c
[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     std::uint64_t address, int process_index = ProcessIndexAny,
58     ReadMode mode = Normal) override;
59 public: // to be private
60   mc_process_flags_t process_flags;
61   pid_t pid;
62   int socket;
63   int status;
64   bool running;
65   memory_map_t memory_map;
66   void *maestro_stack_start, *maestro_stack_end;
67   mc_object_info_t libsimgrid_info;
68   mc_object_info_t binary_info;
69   mc_object_info_t* object_infos;
70   size_t object_infos_size;
71   int memory_file;
72
73   /** Copy of `simix_global->process_list`
74    *
75    *  See mc_smx.c.
76    */
77   xbt_dynar_t smx_process_infos;
78
79   /** Copy of `simix_global->process_to_destroy`
80    *
81    *  See mc_smx.c.
82    */
83   xbt_dynar_t smx_old_process_infos;
84
85   /** State of the cache (which variables are up to date) */
86   mc_process_cache_flags_t cache_flags;
87
88   /** Address of the heap structure in the MCed process. */
89   void* heap_address;
90
91   /** Copy of the heap structure of the process
92    *
93    *  This is refreshed with the `MC_process_refresh` call.
94    *  This is not used if the process is the current one:
95    *  use `MC_process_get_heap_info` in order to use it.
96    */
97    xbt_mheap_t heap;
98
99   /** Copy of the allocation info structure
100    *
101    *  This is refreshed with the `MC_process_refresh` call.
102    *  This is not used if the process is the current one:
103    *  use `MC_process_get_malloc_info` in order to use it.
104    */
105   malloc_info* heap_info;
106
107   // ***** Libunwind-data
108
109   /** Full-featured MC-aware libunwind address space for the process
110    *
111    *  This address space is using a mc_unw_context_t
112    *  (with mc_process_t/mc_address_space_t and unw_context_t).
113    */
114   unw_addr_space_t unw_addr_space;
115
116   /** Underlying libunwind addres-space
117    *
118    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
119    *  operations of the native MC address space is currently delegated
120    *  to this address space (either the local or a ptrace unwinder).
121    */
122   unw_addr_space_t unw_underlying_addr_space;
123
124   /** The corresponding context
125    */
126   void* unw_underlying_context;
127
128   xbt_dynar_t checkpoint_ignore;
129 };
130
131 }
132 }
133
134 SG_BEGIN_DECL()
135
136 int MC_process_vm_open(pid_t pid, int flags);
137
138 /** Refresh the information about the process
139  *
140  *  Do not use direclty, this is used by the getters when appropriate
141  *  in order to have fresh data.
142  */
143 XBT_INTERNAL void MC_process_refresh_heap(mc_process_t process);
144
145 /** Refresh the information about the process
146  *
147  *  Do not use direclty, this is used by the getters when appropriate
148  *  in order to have fresh data.
149  * */
150 XBT_INTERNAL void MC_process_refresh_malloc_info(mc_process_t process);
151
152 static inline
153 bool MC_process_is_self(mc_process_t process)
154 {
155   return process->process_flags & MC_PROCESS_SELF_FLAG;
156 }
157
158 /* Process memory access: */
159
160 static inline
161 const void* MC_process_read(mc_process_t process,
162   simgrid::mc::AddressSpace::ReadMode mode,
163   void* local, const void* remote, size_t len,
164   int process_index)
165 {
166   return process->read_bytes(local, len, (std::uint64_t)remote, process_index, mode);
167 }
168
169 // Simplified versions/wrappers (whould be moved in mc_address_space):
170 XBT_INTERNAL const void* MC_process_read_simple(mc_process_t process,
171   void* local, const void* remote, size_t len);
172 XBT_INTERNAL const void* MC_process_read_dynar_element(mc_process_t process,
173   void* local, const void* remote_dynar, size_t i, size_t len);
174 XBT_INTERNAL unsigned long MC_process_read_dynar_length(mc_process_t process,
175   const void* remote_dynar);
176
177 /** Write data to a process memory
178  *
179  *  @param process the process
180  *  @param local   local memory address (source)
181  *  @param remote  target process memory address (target)
182  *  @param len     data size
183  */
184 XBT_INTERNAL void MC_process_write(mc_process_t process,
185   const void* local, void* remote, size_t len);
186
187 XBT_INTERNAL void MC_process_clear_memory(mc_process_t process,
188   void* remote, size_t len);
189
190 /* Functions, variables of the process: */
191
192 XBT_INTERNAL mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
193 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
194 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
195
196 XBT_INTERNAL dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
197
198 XBT_INTERNAL void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size);
199 XBT_INTERNAL char* MC_process_read_string(mc_process_t, void* address);
200
201 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
202 {
203   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
204     MC_process_refresh_heap(process);
205   return process->heap;
206 }
207
208 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
209 {
210   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
211     MC_process_refresh_malloc_info(process);
212   return process->heap_info;
213 }
214
215 /** Find (one occurence of) the named variable definition
216  */
217 XBT_INTERNAL dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
218
219 XBT_INTERNAL void MC_invalidate_cache(void);
220
221 SG_END_DECL()
222
223 #endif