Logo AND Algorithmique Numérique Distribuée

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