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"
15 #include <sys/types.h>
17 #include <xbt/mmalloc.h>
18 #include "xbt/mmalloc/mmprivate.h"
20 #include "simix/popping_private.h"
22 #include "mc_forward.h"
23 #include "mc_mmalloc.h" // std_heap
24 #include "mc_memory_map.h"
25 #include "mc_address_space.h"
29 int MC_process_vm_open(pid_t pid, int flags);
32 MC_PROCESS_NO_FLAG = 0,
33 MC_PROCESS_SELF_FLAG = 1,
34 } e_mc_process_flags_t;
36 // Those flags are used to track down which cached information
37 // is still up to date and which information needs to be updated.
39 MC_PROCESS_CACHE_FLAG_HEAP = 1,
40 MC_PROCESS_CACHE_FLAG_MALLOC_INFO = 2,
41 } e_mc_process_cache_flags_t ;
43 /** Representation of a process
46 s_mc_address_space_t address_space;
47 e_mc_process_flags_t process_flags;
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;
57 // Cache: don't use those fields directly but with the getters
58 // which ensure that proper synchronisation has been done.
60 e_mc_process_cache_flags_t cache_flags;
62 /** Address of the heap structure in the target process.
66 /** Copy of the heap structure of the process
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.
74 /** Copy of the allocation info structure
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.
80 malloc_info* heap_info;
82 // ***** Libunwind-data
84 /** Full-featured MC-aware libunwind address space for the process
86 * This address space is using a mc_unw_context_t
87 * (with mc_process_t/mc_address_space_t and unw_context_t).
89 unw_addr_space_t unw_addr_space;
91 /** Underlying libunwind addres-space
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).
97 unw_addr_space_t unw_underlying_addr_space;
99 /** The corresponding context
101 void* unw_underlying_context;
104 bool MC_is_process(mc_address_space_t p);
106 void MC_process_init(mc_process_t process, pid_t pid);
107 void MC_process_clear(mc_process_t process);
109 /** Refresh the information about the process
111 * Do not use direclty, this is used by the getters when appropriate
112 * in order to have fresh data.
114 void MC_process_refresh_heap(mc_process_t process);
116 /** Refresh the information about the process
118 * Do not use direclty, this is used by the getters when appropriate
119 * in order to have fresh data.
121 void MC_process_refresh_malloc_info(mc_process_t process);
124 bool MC_process_is_self(mc_process_t process)
126 return process->process_flags & MC_PROCESS_SELF_FLAG;
129 /* Process memory access: */
131 /** Read data from a process memory
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
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,
143 /** Write data to a process memory
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
150 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len);
152 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len);
154 /* Functions, variables of the process: */
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);
160 dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
162 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
164 if (MC_process_is_self(process))
166 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
167 MC_process_refresh_heap(process);
168 return process->heap;
171 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
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;
180 /** Find (one occurence of) the named variable definition
182 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);