1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <sys/types.h>
13 #include "simgrid_config.h"
14 #include <sys/types.h>
16 #include <xbt/mmalloc.h>
19 #include "xbt/mmalloc/mmprivate.h"
22 #include <simgrid/simix.h>
23 #include "simix/popping_private.h"
24 #include "simix/smx_private.h"
26 #include "mc_forward.h"
27 #include "mc_mmalloc.h" // std_heap
28 #include "mc_memory_map.h"
29 #include "mc_address_space.h"
30 #include "mc_protocol.h"
34 int MC_process_vm_open(pid_t pid, int flags);
36 typedef int mc_process_flags_t;
37 #define MC_PROCESS_NO_FLAG 0
38 #define MC_PROCESS_SELF_FLAG 1
40 // Those flags are used to track down which cached information
41 // is still up to date and which information needs to be updated.
42 typedef int mc_process_cache_flags_t;
43 #define MC_PROCESS_CACHE_FLAG_NONE 0
44 #define MC_PROCESS_CACHE_FLAG_HEAP 1
45 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
46 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
48 typedef struct s_mc_smx_process_info s_mc_smx_process_info_t, *mc_smx_process_info_t;
50 /** Representation of a process
53 s_mc_address_space_t address_space;
54 mc_process_flags_t process_flags;
59 memory_map_t memory_map;
60 void *maestro_stack_start, *maestro_stack_end;
61 mc_object_info_t libsimgrid_info;
62 mc_object_info_t binary_info;
63 mc_object_info_t* object_infos;
64 size_t object_infos_size;
67 /** Copy of `simix_global->process_list`
71 xbt_dynar_t smx_process_infos;
73 /** Copy of `simix_global->process_to_destroy`
77 xbt_dynar_t smx_old_process_infos;
79 /** State of the cache (which variables are up to date) */
80 mc_process_cache_flags_t cache_flags;
82 /** Address of the heap structure in the MCed process. */
85 /** Copy of the heap structure of the process
87 * This is refreshed with the `MC_process_refresh` call.
88 * This is not used if the process is the current one:
89 * use `MC_process_get_heap_info` in order to use it.
93 /** Copy of the allocation info structure
95 * This is refreshed with the `MC_process_refresh` call.
96 * This is not used if the process is the current one:
97 * use `MC_process_get_malloc_info` in order to use it.
99 malloc_info* heap_info;
101 // ***** Libunwind-data
103 /** Full-featured MC-aware libunwind address space for the process
105 * This address space is using a mc_unw_context_t
106 * (with mc_process_t/mc_address_space_t and unw_context_t).
108 unw_addr_space_t unw_addr_space;
110 /** Underlying libunwind addres-space
112 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
113 * operations of the native MC address space is currently delegated
114 * to this address space (either the local or a ptrace unwinder).
116 unw_addr_space_t unw_underlying_addr_space;
118 /** The corresponding context
120 void* unw_underlying_context;
122 xbt_dynar_t checkpoint_ignore;
125 bool MC_is_process(mc_address_space_t p);
127 void MC_process_init(mc_process_t process, pid_t pid, int sockfd);
128 void MC_process_clear(mc_process_t process);
130 /** Refresh the information about the process
132 * Do not use direclty, this is used by the getters when appropriate
133 * in order to have fresh data.
135 void MC_process_refresh_heap(mc_process_t process);
137 /** Refresh the information about the process
139 * Do not use direclty, this is used by the getters when appropriate
140 * in order to have fresh data.
142 void MC_process_refresh_malloc_info(mc_process_t process);
145 bool MC_process_is_self(mc_process_t process)
147 return process->process_flags & MC_PROCESS_SELF_FLAG;
150 /* Process memory access: */
152 /** Read data from a process memory
154 * @param process the process
155 * @param local local memory address (destination)
156 * @param remote target process memory address (source)
157 * @param len data size
159 const void* MC_process_read(mc_process_t process,
160 adress_space_read_flags_t flags,
161 void* local, const void* remote, size_t len,
164 // Simplified versions/wrappers (whould be moved in mc_address_space):
165 const void* MC_process_read_simple(mc_process_t process,
166 void* local, const void* remote, size_t len);
167 const void* MC_process_read_dynar_element(mc_process_t process,
168 void* local, const void* remote_dynar, size_t i, size_t len);
169 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar);
171 /** Write data to a process memory
173 * @param process the process
174 * @param local local memory address (source)
175 * @param remote target process memory address (target)
176 * @param len data size
178 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len);
180 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len);
182 /* Functions, variables of the process: */
184 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
185 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
186 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
188 dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
190 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size);
191 char* MC_process_read_string(mc_process_t, void* address);
193 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
195 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
196 MC_process_refresh_heap(process);
197 return process->heap;
200 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
202 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
203 MC_process_refresh_malloc_info(process);
204 return process->heap_info;
207 /** Find (one occurence of) the named variable definition
209 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
211 void MC_invalidate_cache(void);