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. */
16 #include "internal_config.h"
17 #include "mc_private.h"
18 #include "xbt/module.h"
19 #include <xbt/mmalloc.h>
20 #include "../smpi/private.h"
23 #include "xbt/mmalloc/mmprivate.h"
25 #include "../simix/smx_private.h"
27 #define UNW_LOCAL_ONLY
28 #include <libunwind.h>
31 #include "mc_private.h"
36 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
37 "Logging specific to mc_checkpoint");
39 char *libsimgrid_path;
41 /************************************ Free functions **************************************/
42 /*****************************************************************************************/
44 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
47 xbt_dynar_free(&(s->local_variables));
48 xbt_dynar_free(&(s->stack_frames));
53 static void MC_snapshot_stack_free_voidp(void *s)
55 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
58 static void local_variable_free(local_variable_t v)
64 static void local_variable_free_voidp(void *v)
66 local_variable_free((local_variable_t) * (void **) v);
69 void MC_region_destroy(mc_mem_region_t reg)
74 //munmap(reg->data, reg->size);
76 if (reg->page_numbers) {
77 mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
82 void MC_free_snapshot(mc_snapshot_t snapshot)
85 for (i = 0; i < NB_REGIONS; i++) {
86 MC_region_destroy(snapshot->regions[i]);
89 xbt_free(snapshot->stack_sizes);
90 xbt_dynar_free(&(snapshot->stacks));
91 xbt_dynar_free(&(snapshot->to_ignore));
92 xbt_dynar_free(&snapshot->ignored_data);
94 if (snapshot->privatization_regions) {
95 size_t n = xbt_dynar_length(snapshot->enabled_processes);
96 for (i = 0; i != n; ++i) {
97 MC_region_destroy(snapshot->privatization_regions[i]);
99 xbt_free(snapshot->privatization_regions);
105 /******************************* Snapshot regions ********************************/
106 /*********************************************************************************/
108 static mc_mem_region_t mc_region_new_dense(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
110 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
111 new_reg->start_addr = start_addr;
112 new_reg->permanent_addr = permanent_addr;
113 new_reg->data = NULL;
114 new_reg->size = size;
115 new_reg->page_numbers = NULL;
116 new_reg->data = xbt_malloc(size);
117 memcpy(new_reg->data, permanent_addr, size);
118 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
119 type, new_reg->data, permanent_addr, size);
124 /** @brief Take a snapshot of a given region
127 * @param start_addr Address of the region in the simulated process
128 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
129 * @param size Size of the data*
130 * @param ref_reg Reference corresponding region
132 static mc_mem_region_t MC_region_new(int type, void *start_addr, void* permanent_addr, size_t size, mc_mem_region_t ref_reg)
134 if (_sg_mc_sparse_checkpoint) {
135 return mc_region_new_sparse(type, start_addr, permanent_addr, size, ref_reg);
137 return mc_region_new_dense(type, start_addr, permanent_addr, size, ref_reg);
141 /** @brief Restore a region from a snapshot
143 * If we are using per page snapshots, it is possible to use the reference
144 * region in order to do an incremental restoration of the region: the
145 * softclean pages which are shared between the two snapshots do not need
148 * @param reg Target region
149 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
151 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
153 /*FIXME: check if start_addr is still mapped, if it is not, then map it
154 before copying the data */
155 if (!reg->page_numbers) {
156 memcpy(reg->permanent_addr, reg->data, reg->size);
158 mc_region_restore_sparse(reg, ref_reg);
163 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
164 void *start_addr, void* permanent_addr, size_t size)
167 mc_mem_region_t ref_reg =
168 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
169 mc_mem_region_t new_reg = MC_region_new(type, start_addr, permanent_addr, size, ref_reg);
170 snapshot->regions[type] = new_reg;
174 static void MC_get_memory_regions(mc_snapshot_t snapshot)
177 void *start_heap = std_heap->base;
178 void *end_heap = std_heap->breakval;
179 MC_snapshot_add_region(snapshot, 0, start_heap, start_heap,
180 (char *) end_heap - (char *) start_heap);
181 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
182 snapshot->privatization_regions = NULL;
184 MC_snapshot_add_region(snapshot, 1,
185 mc_libsimgrid_info->start_rw, mc_libsimgrid_info->start_rw,
186 mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
190 if (smpi_privatize_global_variables && smpi_process_count()) {
191 // Snapshot the global variable of the application separately for each
192 // simulated process:
193 snapshot->privatization_regions =
194 xbt_new(mc_mem_region_t, smpi_process_count());
195 for (i = 0; i < smpi_process_count(); i++) {
196 mc_mem_region_t ref_reg =
197 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
198 snapshot->privatization_regions[i] =
199 MC_region_new(-1, mc_binary_info->start_rw, smpi_privatisation_regions[i].address, size_data_exe, ref_reg);
201 snapshot->privatization_index = smpi_loaded_page;
202 snapshot->regions[2] = NULL;
206 MC_snapshot_add_region(snapshot, 2,
207 mc_binary_info->start_rw, mc_binary_info->start_rw,
208 mc_binary_info->end_rw - mc_binary_info->start_rw);
209 snapshot->privatization_regions = NULL;
210 snapshot->privatization_index = -1;
214 /** @brief Finds the range of the different memory segments and binary paths */
215 void MC_init_memory_map_info()
220 memory_map_t maps = MC_get_memory_map();
222 maestro_stack_start = NULL;
223 maestro_stack_end = NULL;
224 libsimgrid_path = NULL;
226 while (i < maps->mapsize) {
227 reg = maps->regions[i];
228 if (maps->regions[i].pathname == NULL) {
230 } else if ((reg.prot & PROT_WRITE)
231 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
232 maestro_stack_start = reg.start_addr;
233 maestro_stack_end = reg.end_addr;
234 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
235 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
237 if (libsimgrid_path == NULL)
238 libsimgrid_path = strdup(maps->regions[i].pathname);
243 xbt_assert(maestro_stack_start, "maestro_stack_start");
244 xbt_assert(maestro_stack_end, "maestro_stack_end");
245 xbt_assert(libsimgrid_path, "libsimgrid_path&");
247 MC_free_memory_map(maps);
251 /** \brief Fills the position of the segments (executable, read-only, read/write).
253 * TODO, use dl_iterate_phdr to be more robust
255 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
260 const char *name = basename(result->file_name);
261 while (i < maps->mapsize) {
262 reg = maps->regions[i];
263 if (maps->regions[i].pathname == NULL
264 || strcmp(basename(maps->regions[i].pathname), name)) {
266 } else if ((reg.prot & PROT_WRITE)) {
267 xbt_assert(!result->start_rw,
268 "Multiple read-write segments for %s, not supported",
269 maps->regions[i].pathname);
270 result->start_rw = reg.start_addr;
271 result->end_rw = reg.end_addr;
272 // .bss is usually after the .data:
273 s_map_region_t *next = &(maps->regions[i + 1]);
274 if (next->pathname == NULL && (next->prot & PROT_WRITE)
275 && next->start_addr == reg.end_addr) {
276 result->end_rw = maps->regions[i + 1].end_addr;
278 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
279 xbt_assert(!result->start_exec,
280 "Multiple executable segments for %s, not supported",
281 maps->regions[i].pathname);
282 result->start_exec = reg.start_addr;
283 result->end_exec = reg.end_addr;
284 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
285 xbt_assert(!result->start_ro,
286 "Multiple read only segments for %s, not supported",
287 maps->regions[i].pathname);
288 result->start_ro = reg.start_addr;
289 result->end_ro = reg.end_addr;
294 xbt_assert(result->file_name);
295 xbt_assert(result->start_rw);
296 xbt_assert(result->start_exec);
299 /************************************* Take Snapshot ************************************/
300 /****************************************************************************************/
302 /** \brief Checks whether the variable is in scope for a given IP.
304 * A variable may be defined only from a given value of IP.
306 * \param var Variable description
307 * \param frame Scope description
308 * \param ip Instruction pointer
309 * \return true if the variable is valid
311 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
314 // The variable is not yet valid:
315 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
321 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
322 dw_frame_t scope, int process_index, xbt_dynar_t result)
324 void *ip = (void *) stack_frame->ip;
325 if (ip < scope->low_pc || ip >= scope->high_pc)
329 dw_variable_t current_variable;
330 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
332 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
336 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
341 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
342 new_var->subprogram = stack_frame->frame;
343 new_var->ip = stack_frame->ip;
344 new_var->name = xbt_strdup(current_variable->name);
345 new_var->type = current_variable->type;
346 new_var->region = region_type;
348 if (current_variable->address != NULL) {
349 new_var->address = current_variable->address;
350 } else if (current_variable->locations.size != 0) {
351 s_mc_location_t location;
352 mc_dwarf_resolve_locations(&location, ¤t_variable->locations,
353 current_variable->object_info,
354 &(stack_frame->unw_cursor),
355 (void *) stack_frame->frame_base,
356 NULL, process_index);
358 switch(mc_get_location_type(&location)) {
359 case MC_LOCATION_TYPE_ADDRESS:
360 new_var->address = location.memory_location;
362 case MC_LOCATION_TYPE_REGISTER:
364 xbt_die("Cannot handle non-address variable");
368 xbt_die("No address");
371 xbt_dynar_push(result, &new_var);
374 // Recursive processing of nested scopes:
375 dw_frame_t nested_scope = NULL;
376 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
377 mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result);
381 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames, int process_index)
384 unsigned cursor1 = 0;
385 mc_stack_frame_t stack_frame;
386 xbt_dynar_t variables =
387 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
389 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
390 mc_fill_local_variables_values(stack_frame, stack_frame->frame, process_index, variables);
396 static void MC_stack_frame_free_voipd(void *s)
398 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
400 xbt_free(stack_frame->frame_name);
401 xbt_free(stack_frame);
405 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
408 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
412 // TODO, check condition check (unw_init_local==0 means end of frame)
413 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
415 xbt_die("Could not initialize stack unwinding");
420 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
421 xbt_dynar_push(result, &stack_frame);
423 stack_frame->unw_cursor = c;
427 unw_get_reg(&c, UNW_REG_IP, &ip);
428 unw_get_reg(&c, UNW_REG_SP, &sp);
430 stack_frame->ip = ip;
431 stack_frame->sp = sp;
433 // TODO, use real addresses in frame_t instead of fixing it here
435 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
436 stack_frame->frame = frame;
439 stack_frame->frame_name = xbt_strdup(frame->name);
440 stack_frame->frame_base =
441 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
443 stack_frame->frame_base = 0;
444 stack_frame->frame_name = NULL;
447 /* Stop before context switch with maestro */
448 if (frame != NULL && frame->name != NULL
449 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
452 int ret = ret = unw_step(&c);
454 xbt_die("Unexpected end of stack.");
455 } else if (ret < 0) {
456 xbt_die("Error while unwinding stack.");
460 if (xbt_dynar_length(result) == 0) {
461 XBT_INFO("unw_init_local failed");
468 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
472 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
473 MC_snapshot_stack_free_voidp);
475 unsigned int cursor = 0;
476 stack_region_t current_stack;
478 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
479 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
480 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
481 st->local_variables = MC_get_local_variables_values(st->stack_frames, current_stack->process_index);
482 st->process_index = current_stack->process_index;
484 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
486 xbt_dynar_push(res, &st);
487 (*snapshot)->stack_sizes =
488 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
489 (*snapshot)->stack_sizes[cursor] =
490 (char*) current_stack->address + current_stack->size - (char*) sp;
497 static xbt_dynar_t MC_take_snapshot_ignore()
500 if (mc_heap_comparison_ignore == NULL)
504 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
505 heap_ignore_region_free_voidp);
507 unsigned int cursor = 0;
508 mc_heap_ignore_region_t current_region;
510 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
511 mc_heap_ignore_region_t new_region = NULL;
512 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
513 new_region->address = current_region->address;
514 new_region->size = current_region->size;
515 new_region->block = current_region->block;
516 new_region->fragment = current_region->fragment;
517 xbt_dynar_push(cpy, &new_region);
524 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
525 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
526 free(ignored_data->data);
529 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
531 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
534 unsigned int cursor = 0;
535 mc_checkpoint_ignore_region_t region;
536 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
537 s_mc_snapshot_ignored_data_t ignored_data;
538 ignored_data.start = region->addr;
539 ignored_data.size = region->size;
540 ignored_data.data = malloc(region->size);
541 memcpy(ignored_data.data, region->addr, region->size);
542 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
546 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
547 memset(region->addr, 0, region->size);
552 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
554 unsigned int cursor = 0;
555 s_mc_snapshot_ignored_data_t ignored_data;
556 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
557 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
561 /** @brief Can we remove this snapshot?
563 * Some snapshots cannot be removed (yet) because we need them
568 int mc_important_snapshot(mc_snapshot_t snapshot)
570 // We need this snapshot in order to know which
571 // pages needs to be stored in the next snapshot.
572 // This field is only non-NULL when using soft-dirty
574 if (snapshot == mc_model_checker->parent_snapshot)
580 static void MC_get_current_fd(mc_snapshot_t snapshot){
582 snapshot->total_fd = 0;
584 const size_t fd_dir_path_size = 20;
585 char fd_dir_path[fd_dir_path_size];
586 if (snprintf(fd_dir_path, fd_dir_path_size,
587 "/proc/%lli/fd", (long long int) getpid()) > fd_dir_path_size)
588 xbt_die("Unexpected buffer is too small for fd_dir_path");
590 DIR* fd_dir = opendir (fd_dir_path);
592 xbt_die("Cannot open directory '/proc/self/fd'\n");
595 struct dirent* fd_number;
596 while ((fd_number = readdir(fd_dir))) {
598 int fd_value = atoi(fd_number->d_name);
603 const size_t source_size = 25;
605 if (snprintf(source, source_size, "/proc/self/fd/%s", fd_number->d_name) > source_size)
606 xbt_die("Unexpected buffer is too small for fd %s", fd_number->d_name);
608 const size_t link_size = 200;
610 size_t res = readlink(source, link, link_size);
612 xbt_die("Could not read link for %s", source);
615 xbt_die("Buffer to small for link of %s", source);
619 if(smpi_is_privatisation_file(link))
622 // This is (probably) the DIR* we are reading:
623 // TODO, read all the file entries at once and close the DIR.*
624 if(strcmp(fd_dir_path, link) == 0)
627 // We don't handle them.
628 // It does not mean we should silently ignore them however.
629 if (strncmp(link, "pipe:", 5) == 0 || strncmp(link, "socket:", 7) == 0)
632 // This is probably a shared memory used by lttng-ust:
633 if(strncmp("/dev/shm/ust-shm-tmp-", link, 21)==0)
636 // Add an entry for this FD in the snapshot:
637 fd_infos_t fd = xbt_new0(s_fd_infos_t, 1);
638 fd->filename = strdup(link);
639 fd->number = fd_value;
640 fd->flags = fcntl(fd_value, F_GETFL) | fcntl(fd_value, F_GETFD) ;
641 fd->current_position = lseek(fd_value, 0, SEEK_CUR);
642 snapshot->current_fd = xbt_realloc(snapshot->current_fd, (total_fd + 1) * sizeof(fd_infos_t));
643 snapshot->current_fd[total_fd] = fd;
647 snapshot->total_fd = total_fd;
651 mc_snapshot_t MC_take_snapshot(int num_state)
654 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
655 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
656 smx_process_t process;
657 xbt_swag_foreach(process, simix_global->process_list) {
658 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
661 MC_snapshot_handle_ignore(snapshot);
663 MC_get_current_fd(snapshot);
665 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
666 MC_get_memory_regions(snapshot);
667 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
668 mc_softdirty_reset();
671 snapshot->to_ignore = MC_take_snapshot_ignore();
673 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
675 MC_take_snapshot_stacks(&snapshot);
676 if (_sg_mc_hash && snapshot->stacks != NULL) {
677 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
685 MC_snapshot_ignore_restore(snapshot);
686 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
687 mc_model_checker->parent_snapshot = snapshot;
692 void MC_restore_snapshot(mc_snapshot_t snapshot)
694 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
698 for (i = 0; i < NB_REGIONS; i++) {
699 // For privatized, variables we decided it was not necessary to take the snapshot:
700 if (snapshot->regions[i])
701 MC_region_restore(snapshot->regions[i],
702 parent_snapshot ? parent_snapshot->regions[i] : NULL);
706 if (snapshot->privatization_regions) {
707 // Restore the global variables of the application separately for each
708 // simulated process:
709 for (i = 0; i < smpi_process_count(); i++) {
710 if (snapshot->privatization_regions[i]) {
711 MC_region_restore(snapshot->privatization_regions[i],
712 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
716 if(snapshot->privatization_index >= 0) {
717 // We just rewrote the global variables.
718 // The privatisation segment SMPI thinks
719 // is mapped might be inconsistent with the segment which
720 // is really mapped in memory (kernel state).
721 // We ask politely SMPI to map the segment anyway,
722 // even if it thinks it is the current one:
723 smpi_really_switch_data_segment(snapshot->privatization_index);
727 for(i=0; i < snapshot->total_fd; i++){
729 new_fd = open(snapshot->current_fd[i]->filename, snapshot->current_fd[i]->flags);
731 xbt_die("Could not reopen the file %s fo restoring the file descriptor",
732 snapshot->current_fd[i]->filename);
734 if(new_fd != -1 && new_fd != snapshot->current_fd[i]->number){
735 dup2(new_fd, snapshot->current_fd[i]->number);
736 //fprintf(stderr, "%p\n", fdopen(snapshot->current_fd[i]->number, "rw"));
739 lseek(snapshot->current_fd[i]->number, snapshot->current_fd[i]->current_position, SEEK_SET);
742 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
743 mc_softdirty_reset();
746 MC_snapshot_ignore_restore(snapshot);
747 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
748 mc_model_checker->parent_snapshot = snapshot;
753 mc_snapshot_t simcall_HANDLER_mc_snapshot(smx_simcall_t simcall)
755 return MC_take_snapshot(1);
758 void *MC_snapshot(void)
760 return simcall_mc_snapshot();