1 /* Copyright (c) 2014-2015. 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. */
13 #include <sys/types.h>
17 #include <sys/mman.h> // PROT_*
23 #include <libunwind.h>
24 #include <libunwind-ptrace.h>
26 #include <xbt/mmalloc.h>
28 #include "mc_process.h"
29 #include "mc_object_info.h"
30 #include "mc_address_space.h"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
35 #include "mc_server.h"
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
40 "MC process information");
42 static void MC_process_init_memory_map_info(mc_process_t process);
43 static void MC_process_open_memory_file(mc_process_t process);
45 // ***** Destructor callbacks
47 // ***** mc_address_space methods for mc_process
49 static mc_process_t MC_process_get_process(mc_process_t p) {
53 static const s_mc_address_space_class_t mc_process_class = {
54 .read = (mc_address_space_class_read_callback_t) &MC_process_read,
55 .get_process = (mc_address_space_class_get_process_callback_t) MC_process_get_process
58 bool MC_is_process(mc_address_space_t p)
60 return p->address_space_class == &mc_process_class;
65 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
67 process->address_space.address_space_class = &mc_process_class;
68 process->process_flags = MC_PROCESS_NO_FLAG;
69 process->socket = sockfd;
72 process->process_flags |= MC_PROCESS_SELF_FLAG;
73 process->running = true;
75 process->memory_map = MC_get_memory_map(pid);
76 process->memory_file = -1;
77 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
79 process->heap_info = NULL;
80 MC_process_init_memory_map_info(process);
81 MC_process_open_memory_file(process);
83 // Read std_heap (is a struct mdesc*):
84 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
86 xbt_die("No heap information in the target process");
87 if(!std_heap_var->address)
88 xbt_die("No constant address for this variable");
89 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
90 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
91 MC_PROCESS_INDEX_DISABLED);
93 process->smx_process_infos = MC_smx_process_info_list_new();
94 process->smx_old_process_infos = MC_smx_process_info_list_new();
96 process->checkpoint_ignore = MC_checkpoint_ignore_new();
98 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
99 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
100 process->unw_underlying_addr_space = unw_local_addr_space;
101 process->unw_underlying_context = NULL;
103 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
104 process->unw_underlying_context = _UPT_create(pid);
108 void MC_process_clear(mc_process_t process)
110 process->address_space.address_space_class = NULL;
111 process->process_flags = MC_PROCESS_NO_FLAG;
114 MC_free_memory_map(process->memory_map);
115 process->memory_map = NULL;
117 process->maestro_stack_start = NULL;
118 process->maestro_stack_end = NULL;
120 xbt_dynar_free(&process->checkpoint_ignore);
122 xbt_dynar_free(&process->smx_process_infos);
123 xbt_dynar_free(&process->smx_old_process_infos);
126 for (i=0; i!=process->object_infos_size; ++i) {
127 MC_free_object_info(&process->object_infos[i]);
129 free(process->object_infos);
130 process->object_infos = NULL;
131 process->object_infos_size = 0;
132 if (process->memory_file >= 0) {
133 close(process->memory_file);
136 if (process->unw_underlying_addr_space != unw_local_addr_space) {
137 unw_destroy_addr_space(process->unw_underlying_addr_space);
138 _UPT_destroy(process->unw_underlying_context);
140 process->unw_underlying_context = NULL;
141 process->unw_underlying_addr_space = NULL;
143 unw_destroy_addr_space(process->unw_addr_space);
144 process->unw_addr_space = NULL;
146 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
149 process->heap = NULL;
151 free(process->heap_info);
152 process->heap_info = NULL;
155 void MC_process_refresh_heap(mc_process_t process)
157 xbt_assert(mc_mode == MC_MODE_SERVER);
158 xbt_assert(!MC_process_is_self(process));
159 // Read/dereference/refresh the std_heap pointer:
160 if (!process->heap) {
161 process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
163 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
164 process->heap, process->heap_address, sizeof(struct mdesc),
165 MC_PROCESS_INDEX_DISABLED
167 process->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
170 void MC_process_refresh_malloc_info(mc_process_t process)
172 xbt_assert(mc_mode == MC_MODE_SERVER);
173 xbt_assert(!MC_process_is_self(process));
174 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
175 MC_process_refresh_heap(process);
176 // Refresh process->heapinfo:
177 size_t malloc_info_bytesize =
178 (process->heap->heaplimit + 1) * sizeof(malloc_info);
179 process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
180 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
182 process->heap->heapinfo, malloc_info_bytesize,
183 MC_PROCESS_INDEX_DISABLED);
184 process->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
187 #define SO_RE "\\.so[\\.0-9]*$"
188 #define VERSION_RE "-[\\.0-9]*$"
190 const char* FILTERED_LIBS[] = {
212 static bool MC_is_simgrid_lib(const char* libname)
214 return !strcmp(libname, "libsimgrid");
217 static bool MC_is_filtered_lib(const char* libname)
219 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
222 if (strcmp(libname, FILTERED_LIBS[i])==0)
227 struct s_mc_memory_map_re {
232 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
233 const char* map_basename = basename((char*) pathname);
236 if(regexec(&res->so_re, map_basename, 1, &match, 0))
239 char* libname = strndup(map_basename, match.rm_so);
241 // Strip the version suffix:
242 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
243 char* temp = libname;
244 libname = strndup(temp, match.rm_so);
251 /** @brief Finds the range of the different memory segments and binary paths */
252 static void MC_process_init_memory_map_info(mc_process_t process)
254 XBT_DEBUG("Get debug information ...");
255 process->maestro_stack_start = NULL;
256 process->maestro_stack_end = NULL;
257 process->object_infos = NULL;
258 process->object_infos_size = 0;
259 process->binary_info = NULL;
260 process->libsimgrid_info = NULL;
262 struct s_mc_memory_map_re res;
264 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
265 xbt_die(".so regexp did not compile");
267 memory_map_t maps = process->memory_map;
269 const char* current_name = NULL;
271 for (ssize_t i=0; i < maps->mapsize; i++) {
272 map_region_t reg = &(maps->regions[i]);
273 const char* pathname = maps->regions[i].pathname;
276 if (maps->regions[i].pathname == NULL) {
281 // [stack], [vvar], [vsyscall], [vdso] ...
282 if (pathname[0] == '[') {
283 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
284 process->maestro_stack_start = reg->start_addr;
285 process->maestro_stack_end = reg->end_addr;
291 if (current_name && strcmp(current_name, pathname)==0)
294 current_name = pathname;
295 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
298 const bool is_executable = !i;
299 char* libname = NULL;
300 if (!is_executable) {
301 libname = MC_get_lib_name(pathname, &res);
304 if (MC_is_filtered_lib(libname)) {
310 mc_object_info_t info =
311 MC_find_object_info(process->memory_map, pathname, is_executable);
312 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
313 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
314 process->object_infos[process->object_infos_size] = info;
315 process->object_infos_size++;
317 process->binary_info = info;
318 else if (libname && MC_is_simgrid_lib(libname))
319 process->libsimgrid_info = info;
324 regfree(&res.version_re);
326 // Resolve time (including accress differents objects):
327 for (size_t i=0; i!=process->object_infos_size; ++i)
328 MC_post_process_object_info(process, process->object_infos[i]);
330 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
331 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
333 XBT_DEBUG("Get debug information done !");
336 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
339 for (i = 0; i != process->object_infos_size; ++i) {
340 if (addr >= (void *) process->object_infos[i]->start
341 && addr <= (void *) process->object_infos[i]->end) {
342 return process->object_infos[i];
348 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
351 for (i = 0; i != process->object_infos_size; ++i) {
352 if (addr >= (void *) process->object_infos[i]->start_exec
353 && addr <= (void *) process->object_infos[i]->end_exec) {
354 return process->object_infos[i];
360 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
363 for (i = 0; i != process->object_infos_size; ++i) {
364 if (addr >= (void *) process->object_infos[i]->start_rw
365 && addr <= (void *) process->object_infos[i]->end_rw) {
366 return process->object_infos[i];
372 // Functions, variables…
374 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
376 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
380 return MC_file_object_info_find_function(info, ip);
383 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
385 const size_t n = process->object_infos_size;
388 // First lookup the variable in the executable shared object.
389 // A global variable used directly by the executable code from a library
390 // is reinstanciated in the executable memory .data/.bss.
391 // We need to look up the variable in the execvutable first.
392 if (process->binary_info) {
393 mc_object_info_t info = process->binary_info;
394 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
399 for (i=0; i!=n; ++i) {
400 mc_object_info_t info =process->object_infos[i];
401 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
409 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
411 dw_variable_t var = MC_process_find_variable_by_name(process, name);
413 xbt_die("No simple location for this variable");
414 if (!var->type->full_type)
415 xbt_die("Partial type for %s, cannot check size", name);
416 if ((size_t) var->type->full_type->byte_size != size)
417 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
418 name, size, (size_t) var->type->full_type->byte_size);
419 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE, target, var->address, size,
420 MC_PROCESS_INDEX_ANY);
423 char* MC_process_read_string(mc_process_t process, void* address)
427 if (MC_process_is_self(process))
428 return strdup((char*) address);
431 char* res = (char*) malloc(len);
435 ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
440 xbt_die("Could not read from from remote process");
443 xbt_die("Could not read string from remote process");
445 void* p = memchr(res + off, '\0', c);
452 res = (char*) realloc(res, len);
457 // ***** Memory access
459 int MC_process_vm_open(pid_t pid, int flags)
461 const size_t buffer_size = 30;
462 char buffer[buffer_size];
463 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
464 if (res < 0 || (size_t) res >= buffer_size) {
465 errno = ENAMETOOLONG;
468 return open(buffer, flags);
471 static void MC_process_open_memory_file(mc_process_t process)
473 if (MC_process_is_self(process) || process->memory_file >= 0)
476 int fd = MC_process_vm_open(process->pid, O_RDWR);
478 xbt_die("Could not open file for process virtual address space");
479 process->memory_file = fd;
482 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
484 char* buffer = (char*) buf;
485 ssize_t real_count = count;
487 ssize_t res = pread(fd, buffer, count, offset);
494 } else if (errno != EINTR) {
501 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
503 const char* buffer = (const char*) buf;
504 ssize_t real_count = count;
506 ssize_t res = pwrite(fd, buffer, count, offset);
513 } else if (errno != EINTR) {
520 const void* MC_process_read(mc_process_t process, adress_space_read_flags_t flags,
521 void* local, const void* remote, size_t len,
524 if (process_index != MC_PROCESS_INDEX_DISABLED) {
525 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
526 // Segment overlap is not handled.
527 if (MC_object_info_is_privatized(info)) {
528 if (process_index < 0)
529 xbt_die("Missing process index");
530 // Address translation in the privaization segment:
531 size_t offset = (const char*) remote - info->start_rw;
532 remote = (const char*) remote - offset;
536 if (MC_process_is_self(process)) {
537 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
540 memcpy(local, remote, len);
544 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
545 xbt_die("Read from process %lli failed", (long long) process->pid);
550 const void* MC_process_read_simple(mc_process_t process,
551 void* local, const void* remote, size_t len)
553 adress_space_read_flags_t flags = MC_ADDRESS_SPACE_READ_FLAGS_NONE;
554 int index = MC_PROCESS_INDEX_ANY;
555 MC_process_read(process, flags, local, remote, len, index);
559 const void* MC_process_read_dynar_element(mc_process_t process,
560 void* local, const void* remote_dynar, size_t i, size_t len)
563 MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
565 xbt_die("Out of bound index %zi/%zi", i, d.used);
566 if (len != d.elmsize)
567 xbt_die("Bad size in MC_process_read_dynar_element");
568 MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len);
572 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
574 if (MC_process_is_self(process)) {
575 memcpy(remote, local, len);
577 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
578 xbt_die("Write to process %lli failed", (long long) process->pid);
582 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
587 MC_process_read_simple(process, &res,
588 &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
592 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
593 static const void* zero_buffer;
594 static const int zero_buffer_size = 10 * 4096;
596 static void MC_zero_buffer_init(void)
598 int fd = open("/dev/zero", O_RDONLY);
600 xbt_die("Could not open /dev/zero");
601 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
602 if (zero_buffer == MAP_FAILED)
603 xbt_die("Could not map the zero buffer");
607 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
609 if (MC_process_is_self(process)) {
610 memset(remote, 0, len);
612 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
614 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
615 MC_process_write(process, zero_buffer, remote, s);
616 remote = (char*) remote + s;