Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1e4384d15387b1738b83a29a9ebc6b449b83308f
[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
15 #include <sys/types.h>
16
17 #include <xbt/mmalloc.h>
18 #include "xbt/mmalloc/mmprivate.h"
19
20 #include "simix/popping_private.h"
21
22 #include "mc_forward.h"
23 #include "mc_mmalloc.h" // std_heap
24 #include "mc_memory_map.h"
25 #include "mc_address_space.h"
26
27 SG_BEGIN_DECL()
28
29 int MC_process_vm_open(pid_t pid, int flags);
30
31 typedef enum {
32   MC_PROCESS_NO_FLAG = 0,
33   MC_PROCESS_SELF_FLAG = 1,
34 } e_mc_process_flags_t;
35
36 // Those flags are used to track down which cached information
37 // is still up to date and which information needs to be updated.
38 typedef enum {
39   MC_PROCESS_CACHE_FLAG_HEAP = 1,
40   MC_PROCESS_CACHE_FLAG_MALLOC_INFO = 2,
41 } e_mc_process_cache_flags_t ;
42
43 /** Representation of a process
44  */
45 struct s_mc_process {
46   s_mc_address_space_t address_space;
47   e_mc_process_flags_t process_flags;
48   pid_t pid;
49   int socket;
50   int status;
51   bool running;
52   memory_map_t memory_map;
53   void *maestro_stack_start, *maestro_stack_end;
54   mc_object_info_t libsimgrid_info;
55   mc_object_info_t binary_info;
56   mc_object_info_t* object_infos;
57   size_t object_infos_size;
58   int memory_file;
59
60   // Cache: don't use those fields directly but with the getters
61   // which ensure that proper synchronisation has been done.
62
63   e_mc_process_cache_flags_t cache_flags;
64
65   /** Address of the heap structure in the target process.
66    */
67   void* heap_address;
68
69   /** Copy of the heap structure of the process
70    *
71    *  This is refreshed with the `MC_process_refresh` call.
72    *  This is not used if the process is the current one:
73    *  use `MC_process_get_heap_info` in order to use it.
74    */
75    xbt_mheap_t heap;
76
77   /** Copy of the allocation info structure
78    *
79    *  This is refreshed with the `MC_process_refresh` call.
80    *  This is not used if the process is the current one:
81    *  use `MC_process_get_malloc_info` in order to use it.
82    */
83   malloc_info* heap_info;
84
85   // ***** Libunwind-data
86
87   /** Full-featured MC-aware libunwind address space for the process
88    *
89    *  This address space is using a mc_unw_context_t
90    *  (with mc_process_t/mc_address_space_t and unw_context_t).
91    */
92   unw_addr_space_t unw_addr_space;
93
94   /** Underlying libunwind addres-space
95    *
96    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
97    *  operations of the native MC address space is currently delegated
98    *  to this address space (either the local or a ptrace unwinder).
99    */
100   unw_addr_space_t unw_underlying_addr_space;
101
102   /** The corresponding context
103    */
104   void* unw_underlying_context;
105
106   xbt_dynar_t checkpoint_ignore;
107 };
108
109 bool MC_is_process(mc_address_space_t p);
110
111 void MC_process_init(mc_process_t process, pid_t pid, int sockfd);
112 void MC_process_clear(mc_process_t process);
113
114 /** Refresh the information about the process
115  *
116  *  Do not use direclty, this is used by the getters when appropriate
117  *  in order to have fresh data.
118  */
119 void MC_process_refresh_heap(mc_process_t process);
120
121 /** Refresh the information about the process
122  *
123  *  Do not use direclty, this is used by the getters when appropriate
124  *  in order to have fresh data.
125  * */
126 void MC_process_refresh_malloc_info(mc_process_t process);
127
128 static inline
129 bool MC_process_is_self(mc_process_t process)
130 {
131   return process->process_flags & MC_PROCESS_SELF_FLAG;
132 }
133
134 /* Process memory access: */
135
136 /** Read data from a process memory
137  *
138  *  @param process the process
139  *  @param local   local memory address (destination)
140  *  @param remote  target process memory address (source)
141  *  @param len     data size
142  */
143 const void* MC_process_read(mc_process_t process,
144   e_adress_space_read_flags_t flags,
145   void* local, const void* remote, size_t len,
146   int process_index);
147
148 /** Write data to a process memory
149  *
150  *  @param process the process
151  *  @param local   local memory address (source)
152  *  @param remote  target process memory address (target)
153  *  @param len     data size
154  */
155 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len);
156
157 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len);
158
159 /* Functions, variables of the process: */
160
161 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
162 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
163 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
164
165 dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
166
167 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
168 {
169   if (MC_process_is_self(process))
170     return std_heap;
171   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
172     MC_process_refresh_heap(process);
173   return process->heap;
174 }
175
176 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
177 {
178   if (MC_process_is_self(process))
179     return std_heap->heapinfo;
180   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
181     MC_process_refresh_malloc_info(process);
182   return process->heap_info;
183 }
184
185 /** Find (one occurence of) the named variable definition
186  */
187 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
188
189 SG_END_DECL()
190
191 #endif