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"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
31 "MC process information");
33 static void MC_process_init_memory_map_info(mc_process_t process);
34 static void MC_process_open_memory_file(mc_process_t process);
36 // ***** Destructor callbacks
38 // ***** mc_address_space methods for mc_process
40 static mc_process_t MC_process_get_process(mc_process_t p) {
44 static const s_mc_address_space_class_t mc_process_class = {
45 .read = (void*) &MC_process_read,
46 .get_process = (void*) MC_process_get_process
49 bool MC_is_process(mc_address_space_t p)
51 return p->address_space_class == &mc_process_class;
56 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
58 process->address_space.address_space_class = &mc_process_class;
59 process->process_flags = MC_PROCESS_NO_FLAG;
60 process->socket = sockfd;
63 process->process_flags |= MC_PROCESS_SELF_FLAG;
64 process->running = true;
66 process->memory_map = MC_get_memory_map(pid);
67 process->memory_file = -1;
68 process->cache_flags = 0;
70 process->heap_info = NULL;
71 MC_process_init_memory_map_info(process);
72 MC_process_open_memory_file(process);
74 // Read std_heap (is a struct mdesc*):
75 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
77 xbt_die("No heap information in the target process");
78 if(!std_heap_var->address)
79 xbt_die("No constant address for this variable");
80 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
81 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
82 MC_PROCESS_INDEX_DISABLED);
84 process->smx_process_infos = MC_smx_process_info_list_new();
85 process->smx_old_process_infos = MC_smx_process_info_list_new();
87 process->checkpoint_ignore = MC_checkpoint_ignore_new();
89 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
90 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
91 process->unw_underlying_addr_space = unw_local_addr_space;
92 process->unw_underlying_context = NULL;
94 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
95 process->unw_underlying_context = _UPT_create(pid);
99 void MC_process_clear(mc_process_t process)
101 process->address_space.address_space_class = NULL;
102 process->process_flags = MC_PROCESS_NO_FLAG;
105 MC_free_memory_map(process->memory_map);
106 process->memory_map = NULL;
108 process->maestro_stack_start = NULL;
109 process->maestro_stack_end = NULL;
111 xbt_dynar_free(&process->checkpoint_ignore);
113 xbt_dynar_free(&process->smx_process_infos);
114 xbt_dynar_free(&process->smx_old_process_infos);
117 for (i=0; i!=process->object_infos_size; ++i) {
118 MC_free_object_info(&process->object_infos[i]);
120 free(process->object_infos);
121 process->object_infos = NULL;
122 process->object_infos_size = 0;
123 if (process->memory_file >= 0) {
124 close(process->memory_file);
127 if (process->unw_underlying_addr_space != unw_local_addr_space) {
128 unw_destroy_addr_space(process->unw_underlying_addr_space);
129 _UPT_destroy(process->unw_underlying_context);
131 process->unw_underlying_context = NULL;
132 process->unw_underlying_addr_space = NULL;
134 unw_destroy_addr_space(process->unw_addr_space);
135 process->unw_addr_space = NULL;
137 process->cache_flags = 0;
140 process->heap = NULL;
142 free(process->heap_info);
143 process->heap_info = NULL;
146 void MC_process_refresh_heap(mc_process_t process)
148 assert(!MC_process_is_self(process));
149 // Read/dereference/refresh the std_heap pointer:
150 if (!process->heap) {
151 xbt_mheap_t oldheap = mmalloc_set_current_heap(mc_heap);
152 process->heap = malloc(sizeof(struct mdesc));
153 mmalloc_set_current_heap(oldheap);
155 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
156 process->heap, process->heap_address, sizeof(struct mdesc),
157 MC_PROCESS_INDEX_DISABLED
161 void MC_process_refresh_malloc_info(mc_process_t process)
163 assert(!MC_process_is_self(process));
164 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
165 MC_process_refresh_heap(process);
166 // Refresh process->heapinfo:
167 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
169 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
170 process->heap_info = (malloc_info*) realloc(process->heap_info,
171 malloc_info_bytesize);
172 mmalloc_set_current_heap(heap);
174 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
176 process->heap->heapinfo, malloc_info_bytesize,
177 MC_PROCESS_INDEX_DISABLED);
180 #define SO_RE "\\.so[\\.0-9]*$"
181 #define VERSION_RE "-[\\.0-9]*$"
183 const char* FILTERED_LIBS[] = {
205 static bool MC_is_simgrid_lib(const char* libname)
207 return !strcmp(libname, "libsimgrid");
210 static bool MC_is_filtered_lib(const char* libname)
212 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
215 if (strcmp(libname, FILTERED_LIBS[i])==0)
220 struct s_mc_memory_map_re {
225 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
226 const char* map_basename = basename((char*) pathname);
229 if(regexec(&res->so_re, map_basename, 1, &match, 0))
232 char* libname = strndup(map_basename, match.rm_so);
234 // Strip the version suffix:
235 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
236 char* temp = libname;
237 libname = strndup(temp, match.rm_so);
244 /** @brief Finds the range of the different memory segments and binary paths */
245 static void MC_process_init_memory_map_info(mc_process_t process)
247 XBT_DEBUG("Get debug information ...");
248 process->maestro_stack_start = NULL;
249 process->maestro_stack_end = NULL;
250 process->object_infos = NULL;
251 process->object_infos_size = 0;
252 process->binary_info = NULL;
253 process->libsimgrid_info = NULL;
255 struct s_mc_memory_map_re res;
257 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
258 xbt_die(".so regexp did not compile");
260 memory_map_t maps = process->memory_map;
262 const char* current_name = NULL;
265 for (i=0; i < maps->mapsize; i++) {
266 map_region_t reg = &(maps->regions[i]);
267 const char* pathname = maps->regions[i].pathname;
270 if (maps->regions[i].pathname == NULL) {
275 // [stack], [vvar], [vsyscall], [vdso] ...
276 if (pathname[0] == '[') {
277 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
278 process->maestro_stack_start = reg->start_addr;
279 process->maestro_stack_end = reg->end_addr;
285 if (current_name && strcmp(current_name, pathname)==0)
288 current_name = pathname;
289 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
292 const bool is_executable = !i;
293 char* libname = NULL;
294 if (!is_executable) {
295 libname = MC_get_lib_name(pathname, &res);
298 if (MC_is_filtered_lib(libname)) {
304 mc_object_info_t info =
305 MC_find_object_info(process->memory_map, pathname, is_executable);
306 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
307 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
308 process->object_infos[process->object_infos_size] = info;
309 process->object_infos_size++;
311 process->binary_info = info;
312 else if (libname && MC_is_simgrid_lib(libname))
313 process->libsimgrid_info = info;
318 regfree(&res.version_re);
320 // Resolve time (including accress differents objects):
321 for (i=0; i!=process->object_infos_size; ++i)
322 MC_post_process_object_info(process, process->object_infos[i]);
324 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
325 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
327 XBT_DEBUG("Get debug information done !");
330 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
333 for (i = 0; i != process->object_infos_size; ++i) {
334 if (addr >= (void *) process->object_infos[i]->start
335 && addr <= (void *) process->object_infos[i]->end) {
336 return process->object_infos[i];
342 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
345 for (i = 0; i != process->object_infos_size; ++i) {
346 if (addr >= (void *) process->object_infos[i]->start_exec
347 && addr <= (void *) process->object_infos[i]->end_exec) {
348 return process->object_infos[i];
354 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
357 for (i = 0; i != process->object_infos_size; ++i) {
358 if (addr >= (void *) process->object_infos[i]->start_rw
359 && addr <= (void *) process->object_infos[i]->end_rw) {
360 return process->object_infos[i];
366 // Functions, variables…
368 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
370 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
374 return MC_file_object_info_find_function(info, ip);
377 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
379 const size_t n = process->object_infos_size;
382 // First lookup the variable in the executable shared object.
383 // A global variable used directly by the executable code from a library
384 // is reinstanciated in the executable memory .data/.bss.
385 // We need to look up the variable in the execvutable first.
386 if (process->binary_info) {
387 mc_object_info_t info = process->binary_info;
388 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
393 for (i=0; i!=n; ++i) {
394 mc_object_info_t info =process->object_infos[i];
395 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
403 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
405 dw_variable_t var = MC_process_find_variable_by_name(process, name);
407 xbt_die("No simple location for this variable");
408 if (!var->type->full_type)
409 xbt_die("Partial type for %s, cannot check size", name);
410 if (var->type->full_type->byte_size != size)
411 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
412 name, size, (size_t) var->type->full_type->byte_size);
413 MC_process_read(process, MC_PROCESS_NO_FLAG, target, var->address, size,
414 MC_PROCESS_INDEX_ANY);
417 char* MC_process_read_string(mc_process_t process, void* address)
421 if (MC_process_is_self(process))
422 return strdup((char*) address);
425 char* res = malloc(len);
429 ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
434 xbt_die("Could not read from from remote process");
437 xbt_die("Could not read string from remote process");
439 void* p = memchr(res + off, '\0', c);
446 res = realloc(res, len);
451 // ***** Memory access
453 int MC_process_vm_open(pid_t pid, int flags)
455 const size_t buffer_size = 30;
456 char buffer[buffer_size];
457 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
458 if (res < 0 || res >= buffer_size) {
459 errno = ENAMETOOLONG;
462 return open(buffer, flags);
465 static void MC_process_open_memory_file(mc_process_t process)
467 if (MC_process_is_self(process) || process->memory_file >= 0)
470 int fd = MC_process_vm_open(process->pid, O_RDWR);
472 xbt_die("Could not open file for process virtual address space");
473 process->memory_file = fd;
476 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
478 char* buffer = (char*) buf;
479 ssize_t real_count = count;
481 ssize_t res = pread(fd, buffer, count, offset);
488 } else if (errno != EINTR) {
495 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
497 const char* buffer = (const char*) buf;
498 ssize_t real_count = count;
500 ssize_t res = pwrite(fd, buffer, count, offset);
507 } else if (errno != EINTR) {
514 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
515 void* local, const void* remote, size_t len,
518 if (process_index != MC_PROCESS_INDEX_DISABLED) {
519 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
520 // Segment overlap is not handled.
521 if (MC_object_info_is_privatized(info)) {
522 if (process_index < 0)
523 xbt_die("Missing process index");
524 // Address translation in the privaization segment:
525 size_t offset = (const char*) remote - info->start_rw;
526 remote = (const char*) remote - offset;
530 if (MC_process_is_self(process)) {
531 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
534 memcpy(local, remote, len);
538 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
539 xbt_die("Read from process %lli failed", (long long) process->pid);
544 const void* MC_process_read_simple(mc_process_t process,
545 void* local, const void* remote, size_t len)
547 e_adress_space_read_flags_t flags = MC_PROCESS_NO_FLAG;
548 int index = MC_PROCESS_INDEX_ANY;
549 MC_process_read(process, flags, local, remote, len, index);
553 const void* MC_process_read_dynar_element(mc_process_t process,
554 void* local, const void* remote_dynar, size_t i)
557 MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
558 MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), i);
562 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
564 if (MC_process_is_self(process)) {
565 memcpy(remote, local, len);
567 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
568 xbt_die("Write to process %lli failed", (long long) process->pid);
572 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
577 MC_process_read_simple(process, &res,
578 &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
582 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
583 static const void* zero_buffer;
584 static const int zero_buffer_size = 10 * 4096;
586 static void MC_zero_buffer_init(void)
588 int fd = open("/dev/zero", O_RDONLY);
590 xbt_die("Could not open /dev/zero");
591 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
592 if (zero_buffer == MAP_FAILED)
593 xbt_die("Could not map the zero buffer");
597 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
599 if (MC_process_is_self(process)) {
600 memset(remote, 0, len);
602 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
604 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
605 MC_process_write(process, zero_buffer, remote, s);
606 remote = (char*) remote + s;
612 void MC_simcall_handle(smx_simcall_t req, int value)
614 if (MC_process_is_self(&mc_model_checker->process)) {
615 SIMIX_simcall_handle(req, value);
619 MC_process_smx_refresh(&mc_model_checker->process);
622 mc_smx_process_info_t pi = NULL;
624 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, pi) {
625 smx_process_t p = (smx_process_t) pi->address;
626 if (req == &pi->copy.simcall) {
627 smx_simcall_t real_req = &p->simcall;
628 // TODO, use a remote call
629 SIMIX_simcall_handle(real_req, value);
634 // Check (remove afterwards):
635 xbt_dynar_foreach_ptr(mc_model_checker->process.smx_process_infos, i, pi) {
636 smx_process_t p = (smx_process_t) pi->address;
637 if (req == &p->simcall)
638 xbt_die("The real simcall was passed. We expected the local copy.");
641 xbt_die("Could not find the request");