11 #include <sys/mman.h> // PROT_*
17 #include <libunwind.h>
18 #include <libunwind-ptrace.h>
20 #include <xbt/mmalloc.h>
22 #include "mc_process.h"
23 #include "mc_object_info.h"
24 #include "mc_address_space.h"
26 #include "mc_snapshot.h"
27 #include "mc_ignore.h"
29 #include "mc_server.h"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
34 "MC process information");
36 static void MC_process_init_memory_map_info(mc_process_t process);
37 static void MC_process_open_memory_file(mc_process_t process);
39 // ***** Destructor callbacks
41 // ***** mc_address_space methods for mc_process
43 static mc_process_t MC_process_get_process(mc_process_t p) {
47 static const s_mc_address_space_class_t mc_process_class = {
48 .read = (mc_address_space_class_read_callback_t) &MC_process_read,
49 .get_process = (mc_address_space_class_get_process_callback_t) MC_process_get_process
52 bool MC_is_process(mc_address_space_t p)
54 return p->address_space_class == &mc_process_class;
59 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
61 process->address_space.address_space_class = &mc_process_class;
62 process->process_flags = MC_PROCESS_NO_FLAG;
63 process->socket = sockfd;
66 process->process_flags |= MC_PROCESS_SELF_FLAG;
67 process->running = true;
69 process->memory_map = MC_get_memory_map(pid);
70 process->memory_file = -1;
71 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
73 process->heap_info = NULL;
74 MC_process_init_memory_map_info(process);
75 MC_process_open_memory_file(process);
77 // Read std_heap (is a struct mdesc*):
78 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
80 xbt_die("No heap information in the target process");
81 if(!std_heap_var->address)
82 xbt_die("No constant address for this variable");
83 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
84 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
85 MC_PROCESS_INDEX_DISABLED);
87 process->smx_process_infos = MC_smx_process_info_list_new();
88 process->smx_old_process_infos = MC_smx_process_info_list_new();
90 process->checkpoint_ignore = MC_checkpoint_ignore_new();
92 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
93 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
94 process->unw_underlying_addr_space = unw_local_addr_space;
95 process->unw_underlying_context = NULL;
97 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
98 process->unw_underlying_context = _UPT_create(pid);
102 void MC_process_clear(mc_process_t process)
104 process->address_space.address_space_class = NULL;
105 process->process_flags = MC_PROCESS_NO_FLAG;
108 MC_free_memory_map(process->memory_map);
109 process->memory_map = NULL;
111 process->maestro_stack_start = NULL;
112 process->maestro_stack_end = NULL;
114 xbt_dynar_free(&process->checkpoint_ignore);
116 xbt_dynar_free(&process->smx_process_infos);
117 xbt_dynar_free(&process->smx_old_process_infos);
120 for (i=0; i!=process->object_infos_size; ++i) {
121 MC_free_object_info(&process->object_infos[i]);
123 free(process->object_infos);
124 process->object_infos = NULL;
125 process->object_infos_size = 0;
126 if (process->memory_file >= 0) {
127 close(process->memory_file);
130 if (process->unw_underlying_addr_space != unw_local_addr_space) {
131 unw_destroy_addr_space(process->unw_underlying_addr_space);
132 _UPT_destroy(process->unw_underlying_context);
134 process->unw_underlying_context = NULL;
135 process->unw_underlying_addr_space = NULL;
137 unw_destroy_addr_space(process->unw_addr_space);
138 process->unw_addr_space = NULL;
140 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
143 process->heap = NULL;
145 free(process->heap_info);
146 process->heap_info = NULL;
149 void MC_process_refresh_heap(mc_process_t process)
151 assert(!MC_process_is_self(process));
152 // Read/dereference/refresh the std_heap pointer:
153 if (!process->heap) {
154 xbt_mheap_t oldheap = mmalloc_set_current_heap(mc_heap);
155 process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
156 mmalloc_set_current_heap(oldheap);
158 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
159 process->heap, process->heap_address, sizeof(struct mdesc),
160 MC_PROCESS_INDEX_DISABLED
164 void MC_process_refresh_malloc_info(mc_process_t process)
166 assert(!MC_process_is_self(process));
167 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
168 MC_process_refresh_heap(process);
169 // Refresh process->heapinfo:
170 size_t malloc_info_bytesize =
171 (process->heap->heaplimit + 1) * sizeof(malloc_info);
173 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
174 process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
175 mmalloc_set_current_heap(heap);
177 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
179 process->heap->heapinfo, malloc_info_bytesize,
180 MC_PROCESS_INDEX_DISABLED);
183 #define SO_RE "\\.so[\\.0-9]*$"
184 #define VERSION_RE "-[\\.0-9]*$"
186 const char* FILTERED_LIBS[] = {
208 static bool MC_is_simgrid_lib(const char* libname)
210 return !strcmp(libname, "libsimgrid");
213 static bool MC_is_filtered_lib(const char* libname)
215 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
218 if (strcmp(libname, FILTERED_LIBS[i])==0)
223 struct s_mc_memory_map_re {
228 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
229 const char* map_basename = basename((char*) pathname);
232 if(regexec(&res->so_re, map_basename, 1, &match, 0))
235 char* libname = strndup(map_basename, match.rm_so);
237 // Strip the version suffix:
238 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
239 char* temp = libname;
240 libname = strndup(temp, match.rm_so);
247 /** @brief Finds the range of the different memory segments and binary paths */
248 static void MC_process_init_memory_map_info(mc_process_t process)
250 XBT_DEBUG("Get debug information ...");
251 process->maestro_stack_start = NULL;
252 process->maestro_stack_end = NULL;
253 process->object_infos = NULL;
254 process->object_infos_size = 0;
255 process->binary_info = NULL;
256 process->libsimgrid_info = NULL;
258 struct s_mc_memory_map_re res;
260 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
261 xbt_die(".so regexp did not compile");
263 memory_map_t maps = process->memory_map;
265 const char* current_name = NULL;
267 for (ssize_t i=0; i < maps->mapsize; i++) {
268 map_region_t reg = &(maps->regions[i]);
269 const char* pathname = maps->regions[i].pathname;
272 if (maps->regions[i].pathname == NULL) {
277 // [stack], [vvar], [vsyscall], [vdso] ...
278 if (pathname[0] == '[') {
279 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
280 process->maestro_stack_start = reg->start_addr;
281 process->maestro_stack_end = reg->end_addr;
287 if (current_name && strcmp(current_name, pathname)==0)
290 current_name = pathname;
291 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
294 const bool is_executable = !i;
295 char* libname = NULL;
296 if (!is_executable) {
297 libname = MC_get_lib_name(pathname, &res);
300 if (MC_is_filtered_lib(libname)) {
306 mc_object_info_t info =
307 MC_find_object_info(process->memory_map, pathname, is_executable);
308 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
309 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
310 process->object_infos[process->object_infos_size] = info;
311 process->object_infos_size++;
313 process->binary_info = info;
314 else if (libname && MC_is_simgrid_lib(libname))
315 process->libsimgrid_info = info;
320 regfree(&res.version_re);
322 // Resolve time (including accress differents objects):
323 for (size_t i=0; i!=process->object_infos_size; ++i)
324 MC_post_process_object_info(process, process->object_infos[i]);
326 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
327 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
329 XBT_DEBUG("Get debug information done !");
332 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
335 for (i = 0; i != process->object_infos_size; ++i) {
336 if (addr >= (void *) process->object_infos[i]->start
337 && addr <= (void *) process->object_infos[i]->end) {
338 return process->object_infos[i];
344 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
347 for (i = 0; i != process->object_infos_size; ++i) {
348 if (addr >= (void *) process->object_infos[i]->start_exec
349 && addr <= (void *) process->object_infos[i]->end_exec) {
350 return process->object_infos[i];
356 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
359 for (i = 0; i != process->object_infos_size; ++i) {
360 if (addr >= (void *) process->object_infos[i]->start_rw
361 && addr <= (void *) process->object_infos[i]->end_rw) {
362 return process->object_infos[i];
368 // Functions, variables…
370 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
372 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
376 return MC_file_object_info_find_function(info, ip);
379 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
381 const size_t n = process->object_infos_size;
384 // First lookup the variable in the executable shared object.
385 // A global variable used directly by the executable code from a library
386 // is reinstanciated in the executable memory .data/.bss.
387 // We need to look up the variable in the execvutable first.
388 if (process->binary_info) {
389 mc_object_info_t info = process->binary_info;
390 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
395 for (i=0; i!=n; ++i) {
396 mc_object_info_t info =process->object_infos[i];
397 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
405 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
407 dw_variable_t var = MC_process_find_variable_by_name(process, name);
409 xbt_die("No simple location for this variable");
410 if (!var->type->full_type)
411 xbt_die("Partial type for %s, cannot check size", name);
412 if ((size_t) var->type->full_type->byte_size != size)
413 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
414 name, size, (size_t) var->type->full_type->byte_size);
415 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE, target, var->address, size,
416 MC_PROCESS_INDEX_ANY);
419 char* MC_process_read_string(mc_process_t process, void* address)
423 if (MC_process_is_self(process))
424 return strdup((char*) address);
427 char* res = (char*) malloc(len);
431 ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
436 xbt_die("Could not read from from remote process");
439 xbt_die("Could not read string from remote process");
441 void* p = memchr(res + off, '\0', c);
448 res = (char*) realloc(res, len);
453 // ***** Memory access
455 int MC_process_vm_open(pid_t pid, int flags)
457 const size_t buffer_size = 30;
458 char buffer[buffer_size];
459 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
460 if (res < 0 || (size_t) res >= buffer_size) {
461 errno = ENAMETOOLONG;
464 return open(buffer, flags);
467 static void MC_process_open_memory_file(mc_process_t process)
469 if (MC_process_is_self(process) || process->memory_file >= 0)
472 int fd = MC_process_vm_open(process->pid, O_RDWR);
474 xbt_die("Could not open file for process virtual address space");
475 process->memory_file = fd;
478 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
480 char* buffer = (char*) buf;
481 ssize_t real_count = count;
483 ssize_t res = pread(fd, buffer, count, offset);
490 } else if (errno != EINTR) {
497 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
499 const char* buffer = (const char*) buf;
500 ssize_t real_count = count;
502 ssize_t res = pwrite(fd, buffer, count, offset);
509 } else if (errno != EINTR) {
516 const void* MC_process_read(mc_process_t process, adress_space_read_flags_t flags,
517 void* local, const void* remote, size_t len,
520 if (process_index != MC_PROCESS_INDEX_DISABLED) {
521 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
522 // Segment overlap is not handled.
523 if (MC_object_info_is_privatized(info)) {
524 if (process_index < 0)
525 xbt_die("Missing process index");
526 // Address translation in the privaization segment:
527 size_t offset = (const char*) remote - info->start_rw;
528 remote = (const char*) remote - offset;
532 if (MC_process_is_self(process)) {
533 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
536 memcpy(local, remote, len);
540 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
541 xbt_die("Read from process %lli failed", (long long) process->pid);
546 const void* MC_process_read_simple(mc_process_t process,
547 void* local, const void* remote, size_t len)
549 adress_space_read_flags_t flags = MC_ADDRESS_SPACE_READ_FLAGS_NONE;
550 int index = MC_PROCESS_INDEX_ANY;
551 MC_process_read(process, flags, local, remote, len, index);
555 const void* MC_process_read_dynar_element(mc_process_t process,
556 void* local, const void* remote_dynar, size_t i, size_t len)
559 MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
561 xbt_die("Out of bound index %zi/%zi", i, d.used);
562 if (len != d.elmsize)
563 xbt_die("Bad size in MC_process_read_dynar_element");
564 MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len);
568 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
570 if (MC_process_is_self(process)) {
571 memcpy(remote, local, len);
573 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
574 xbt_die("Write to process %lli failed", (long long) process->pid);
578 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
583 MC_process_read_simple(process, &res,
584 &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
588 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
589 static const void* zero_buffer;
590 static const int zero_buffer_size = 10 * 4096;
592 static void MC_zero_buffer_init(void)
594 int fd = open("/dev/zero", O_RDONLY);
596 xbt_die("Could not open /dev/zero");
597 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
598 if (zero_buffer == MAP_FAILED)
599 xbt_die("Could not map the zero buffer");
603 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
605 if (MC_process_is_self(process)) {
606 memset(remote, 0, len);
608 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
610 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
611 MC_process_write(process, zero_buffer, remote, s);
612 remote = (char*) remote + s;