Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f5fd69f97665e82ba86f6a5804c3cbb01aca1896
[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   memory_map_t memory_map;
50   void *maestro_stack_start, *maestro_stack_end;
51   mc_object_info_t libsimgrid_info;
52   mc_object_info_t binary_info;
53   mc_object_info_t* object_infos;
54   size_t object_infos_size;
55   int memory_file;
56
57   // Cache: don't use those fields directly but with the getters
58   // which ensure that proper synchronisation has been done.
59
60   e_mc_process_cache_flags_t cache_flags;
61
62   /** Address of the heap structure in the target process.
63    */
64   void* heap_address;
65
66   /** Copy of the heap structure of the process
67    *
68    *  This is refreshed with the `MC_process_refresh` call.
69    *  This is not used if the process is the current one:
70    *  use `MC_process_get_heap_info` in order to use it.
71    */
72    xbt_mheap_t heap;
73
74   /** Copy of the allocation info structure
75    *
76    *  This is refreshed with the `MC_process_refresh` call.
77    *  This is not used if the process is the current one:
78    *  use `MC_process_get_malloc_info` in order to use it.
79    */
80   malloc_info* heap_info;
81
82   // ***** Libunwind-data
83
84   /** Full-featured MC-aware libunwind address space for the process
85    *
86    *  This address space is using a mc_unw_context_t
87    *  (with mc_process_t/mc_address_space_t and unw_context_t).
88    */
89   unw_addr_space_t unw_addr_space;
90
91   /** Underlying libunwind addres-space
92    *
93    *  The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
94    *  operations of the native MC address space is currently delegated
95    *  to this address space (either the local or a ptrace unwinder).
96    */
97   unw_addr_space_t unw_underlying_addr_space;
98
99   /** The corresponding context
100    */
101   void* unw_underlying_context;
102 };
103
104 bool MC_is_process(mc_address_space_t p);
105
106 void MC_process_init(mc_process_t process, pid_t pid);
107 void MC_process_clear(mc_process_t process);
108
109 /** Refresh the information about the process
110  *
111  *  Do not use direclty, this is used by the getters when appropriate
112  *  in order to have fresh data.
113  */
114 void MC_process_refresh_heap(mc_process_t process);
115
116 /** Refresh the information about the process
117  *
118  *  Do not use direclty, this is used by the getters when appropriate
119  *  in order to have fresh data.
120  * */
121 void MC_process_refresh_malloc_info(mc_process_t process);
122
123 static inline
124 bool MC_process_is_self(mc_process_t process)
125 {
126   return process->process_flags & MC_PROCESS_SELF_FLAG;
127 }
128
129 /* Process memory access: */
130
131 /** Read data from a process memory
132  *
133  *  @param process the process
134  *  @param local   local memory address (destination)
135  *  @param remote  target process memory address (source)
136  *  @param len     data size
137  */
138 const void* MC_process_read(mc_process_t process,
139   e_adress_space_read_flags_t flags,
140   void* local, const void* remote, size_t len,
141   int process_index);
142
143 /** Write data to a process memory
144  *
145  *  @param process the process
146  *  @param local   local memory address (source)
147  *  @param remote  target process memory address (target)
148  *  @param len     data size
149  */
150 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len);
151
152 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len);
153
154 /* Functions, variables of the process: */
155
156 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
157 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
158 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
159
160 dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
161
162 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
163 {
164   if (MC_process_is_self(process))
165     return std_heap;
166   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
167     MC_process_refresh_heap(process);
168   return process->heap;
169 }
170
171 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
172 {
173   if (MC_process_is_self(process))
174     return std_heap->heapinfo;
175   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
176     MC_process_refresh_malloc_info(process);
177   return process->heap_info;
178 }
179
180 /** Find (one occurence of) the named variable definition
181  */
182 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
183
184 SG_END_DECL()
185
186 #endif