11 #include <sys/mman.h> // PROT_*
15 #include "mc_process.h"
16 #include "mc_object_info.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
19 "MC process information");
21 static void MC_process_init_memory_map_info(mc_process_t process);
22 static void MC_process_open_memory_file(mc_process_t process);
25 void MC_process_init(mc_process_t process, pid_t pid)
27 process->process_flags = MC_PROCESS_NO_FLAG;
30 process->process_flags |= MC_PROCESS_SELF_FLAG;
31 process->memory_map = MC_get_memory_map(pid);
32 process->memory_file = -1;
33 process->cache_flags = 0;
35 process->heap_info = NULL;
36 MC_process_init_memory_map_info(process);
37 MC_process_open_memory_file(process);
39 // Read std_heap (is a struct mdesc*):
40 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
42 xbt_die("No heap information in the target process");
43 if(!std_heap_var->address)
44 xbt_die("No constant address for this variable");
45 MC_process_read(process, &process->heap_address,
46 std_heap_var->address, sizeof(struct mdesc*));
49 void MC_process_clear(mc_process_t process)
51 process->process_flags = MC_PROCESS_NO_FLAG;
54 MC_free_memory_map(process->memory_map);
55 process->memory_map = NULL;
57 process->maestro_stack_start = NULL;
58 process->maestro_stack_end = NULL;
61 for (i=0; i!=process->object_infos_size; ++i) {
62 MC_free_object_info(&process->object_infos[i]);
64 free(process->object_infos);
65 process->object_infos = NULL;
66 process->object_infos_size = 0;
67 if (process->memory_file >= 0) {
68 close(process->memory_file);
71 process->cache_flags = 0;
76 free(process->heap_info);
77 process->heap_info = NULL;
80 void MC_process_refresh_heap(mc_process_t process)
82 assert(!MC_process_is_self(process));
83 // Read/dereference/refresh the std_heap pointer:
85 xbt_mheap_t oldheap = mmalloc_get_current_heap();
87 process->heap = malloc(sizeof(struct mdesc));
88 mmalloc_set_current_heap(oldheap);
90 MC_process_read(process, process->heap,
91 process->heap_address, sizeof(struct mdesc));
94 void MC_process_refresh_malloc_info(mc_process_t process)
96 assert(!MC_process_is_self(process));
97 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
98 MC_process_refresh_heap(process);
99 // Refresh process->heapinfo:
100 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
102 xbt_mheap_t oldheap = mmalloc_get_current_heap();
104 process->heap_info = (malloc_info*) realloc(process->heap_info,
105 malloc_info_bytesize);
106 mmalloc_set_current_heap(oldheap);
108 MC_process_read(process, process->heap_info,
109 process->heap->heapinfo, malloc_info_bytesize);
112 #define SO_RE "\\.so[\\.0-9]*$"
113 #define VERSION_RE "-[\\.0-9]*$"
115 const char* FILTERED_LIBS[] = {
136 static bool MC_is_simgrid_lib(const char* libname)
138 return !strcmp(libname, "libsimgrid");
141 static bool MC_is_filtered_lib(const char* libname)
143 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
146 if (strcmp(libname, FILTERED_LIBS[i])==0)
151 struct s_mc_memory_map_re {
156 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
157 const char* map_basename = basename((char*) pathname);
160 if(regexec(&res->so_re, map_basename, 1, &match, 0))
163 char* libname = strndup(map_basename, match.rm_so);
165 // Strip the version suffix:
166 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
167 char* temp = libname;
168 libname = strndup(temp, match.rm_so);
175 /** @brief Finds the range of the different memory segments and binary paths */
176 static void MC_process_init_memory_map_info(mc_process_t process)
178 XBT_INFO("Get debug information ...");
179 process->maestro_stack_start = NULL;
180 process->maestro_stack_end = NULL;
181 process->object_infos = NULL;
182 process->object_infos_size = 0;
183 process->binary_info = NULL;
184 process->libsimgrid_info = NULL;
186 struct s_mc_memory_map_re res;
188 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
189 xbt_die(".so regexp did not compile");
191 memory_map_t maps = process->memory_map;
193 const char* current_name = NULL;
196 for (i=0; i < maps->mapsize; i++) {
197 map_region_t reg = &(maps->regions[i]);
198 const char* pathname = maps->regions[i].pathname;
201 if (maps->regions[i].pathname == NULL) {
206 // [stack], [vvar], [vsyscall], [vdso] ...
207 if (pathname[0] == '[') {
208 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
209 process->maestro_stack_start = reg->start_addr;
210 process->maestro_stack_end = reg->end_addr;
216 if (current_name && strcmp(current_name, pathname)==0)
219 current_name = pathname;
220 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
223 const bool is_executable = !i;
224 char* libname = NULL;
225 if (!is_executable) {
226 libname = MC_get_lib_name(pathname, &res);
229 if (MC_is_filtered_lib(libname)) {
235 mc_object_info_t info =
236 MC_find_object_info(process->memory_map, pathname, is_executable);
237 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
238 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
239 process->object_infos[process->object_infos_size] = info;
240 process->object_infos_size++;
242 process->binary_info = info;
243 else if (libname && MC_is_simgrid_lib(libname))
244 process->libsimgrid_info = info;
249 regfree(&res.version_re);
251 // Resolve time (including accress differents objects):
252 for (i=0; i!=process->object_infos_size; ++i)
253 MC_post_process_object_info(process, process->object_infos[i]);
255 xbt_assert(process->maestro_stack_start, "maestro_stack_start");
256 xbt_assert(process->maestro_stack_end, "maestro_stack_end");
258 XBT_INFO("Get debug information done !");
261 mc_object_info_t MC_process_find_object_info(mc_process_t process, void *ip)
264 for (i = 0; i != process->object_infos_size; ++i) {
265 if (ip >= (void *) process->object_infos[i]->start_exec
266 && ip <= (void *) process->object_infos[i]->end_exec) {
267 return process->object_infos[i];
273 // Functions, variables…
275 dw_frame_t MC_process_find_function(mc_process_t process, void *ip)
277 mc_object_info_t info = MC_process_find_object_info(process, ip);
281 return MC_file_object_info_find_function(info, ip);
284 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
286 const size_t n = process->object_infos_size;
288 for (i=0; i!=n; ++i) {
289 mc_object_info_t info =process->object_infos[i];
290 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
297 // ***** Memory access
299 static void MC_process_open_memory_file(mc_process_t process)
301 if (MC_process_is_self(process) || process->memory_file >= 0)
304 const size_t buffer_size = 30;
305 char buffer[buffer_size];
306 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
307 if (res < 0 || res>= buffer_size) {
308 XBT_ERROR("Could not open memory file descriptor for process %lli",
309 (long long) process->pid);
313 int fd = open(buffer, O_RDWR);
315 xbt_die("Could not initialise memory access for remote process");
316 process->memory_file = fd;
319 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
321 char* buffer = (char*) buf;
322 ssize_t real_count = count;
324 ssize_t res = pread(fd, buffer, count, offset);
331 } else if (errno != EINTR) {
338 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
340 const char* buffer = (const char*) buf;
341 ssize_t real_count = count;
343 ssize_t res = pwrite(fd, buffer, count, offset);
350 } else if (errno != EINTR) {
357 void MC_process_read(mc_process_t process, void* local, const void* remote, size_t len)
359 if (MC_process_is_self(process)) {
360 memcpy(local, remote, len);
362 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
363 xbt_die("Read from process %lli failed", (long long) process->pid);
367 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
369 if (MC_process_is_self(process)) {
370 memcpy(remote, local, len);
372 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
373 xbt_die("Write to process %lli failed", (long long) process->pid);