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_memory_map.h"
18 #include "mc_private.h"
19 #include "xbt/module.h"
20 #include <xbt/mmalloc.h>
21 #include "../smpi/private.h"
24 #include "xbt/mmalloc/mmprivate.h"
26 #include "../simix/smx_private.h"
28 #define UNW_LOCAL_ONLY
29 #include <libunwind.h>
32 #include "mc_private.h"
35 #include "mc_snapshot.h"
36 #include "mc_object_info.h"
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
40 "Logging specific to mc_checkpoint");
42 char *libsimgrid_path;
44 /************************************ Free functions **************************************/
45 /*****************************************************************************************/
47 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
50 xbt_dynar_free(&(s->local_variables));
51 xbt_dynar_free(&(s->stack_frames));
56 static void MC_snapshot_stack_free_voidp(void *s)
58 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
61 static void local_variable_free(local_variable_t v)
67 static void local_variable_free_voidp(void *v)
69 local_variable_free((local_variable_t) * (void **) v);
72 void MC_region_destroy(mc_mem_region_t reg)
77 //munmap(reg->data, reg->size);
79 if (reg->page_numbers) {
80 mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
85 void MC_free_snapshot(mc_snapshot_t snapshot)
88 for (i = 0; i < NB_REGIONS; i++) {
89 MC_region_destroy(snapshot->regions[i]);
92 xbt_free(snapshot->stack_sizes);
93 xbt_dynar_free(&(snapshot->stacks));
94 xbt_dynar_free(&(snapshot->to_ignore));
95 xbt_dynar_free(&snapshot->ignored_data);
97 if (snapshot->privatization_regions) {
98 size_t n = xbt_dynar_length(snapshot->enabled_processes);
99 for (i = 0; i != n; ++i) {
100 MC_region_destroy(snapshot->privatization_regions[i]);
102 xbt_free(snapshot->privatization_regions);
108 /******************************* Snapshot regions ********************************/
109 /*********************************************************************************/
111 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)
113 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
114 new_reg->start_addr = start_addr;
115 new_reg->permanent_addr = permanent_addr;
116 new_reg->data = NULL;
117 new_reg->size = size;
118 new_reg->page_numbers = NULL;
119 new_reg->data = xbt_malloc(size);
120 memcpy(new_reg->data, permanent_addr, size);
121 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
122 type, new_reg->data, permanent_addr, size);
127 /** @brief Take a snapshot of a given region
130 * @param start_addr Address of the region in the simulated process
131 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
132 * @param size Size of the data*
133 * @param ref_reg Reference corresponding region
135 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)
137 if (_sg_mc_sparse_checkpoint) {
138 return mc_region_new_sparse(type, start_addr, permanent_addr, size, ref_reg);
140 return mc_region_new_dense(type, start_addr, permanent_addr, size, ref_reg);
144 /** @brief Restore a region from a snapshot
146 * If we are using per page snapshots, it is possible to use the reference
147 * region in order to do an incremental restoration of the region: the
148 * softclean pages which are shared between the two snapshots do not need
151 * @param reg Target region
152 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
154 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
156 /*FIXME: check if start_addr is still mapped, if it is not, then map it
157 before copying the data */
158 if (!reg->page_numbers) {
159 memcpy(reg->permanent_addr, reg->data, reg->size);
161 mc_region_restore_sparse(reg, ref_reg);
166 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
167 void *start_addr, void* permanent_addr, size_t size)
170 mc_mem_region_t ref_reg =
171 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
172 mc_mem_region_t new_reg = MC_region_new(type, start_addr, permanent_addr, size, ref_reg);
173 snapshot->regions[type] = new_reg;
177 static void MC_get_memory_regions(mc_snapshot_t snapshot)
180 void *start_heap = std_heap->base;
181 void *end_heap = std_heap->breakval;
182 MC_snapshot_add_region(snapshot, 0, start_heap, start_heap,
183 (char *) end_heap - (char *) start_heap);
184 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
185 snapshot->privatization_regions = NULL;
187 MC_snapshot_add_region(snapshot, 1,
188 mc_libsimgrid_info->start_rw, mc_libsimgrid_info->start_rw,
189 mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
193 if (smpi_privatize_global_variables && smpi_process_count()) {
194 // Snapshot the global variable of the application separately for each
195 // simulated process:
196 snapshot->privatization_regions =
197 xbt_new(mc_mem_region_t, smpi_process_count());
198 for (i = 0; i < smpi_process_count(); i++) {
199 mc_mem_region_t ref_reg =
200 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
201 snapshot->privatization_regions[i] =
202 MC_region_new(-1, mc_binary_info->start_rw, smpi_privatisation_regions[i].address, size_data_exe, ref_reg);
204 snapshot->privatization_index = smpi_loaded_page;
205 snapshot->regions[2] = NULL;
209 MC_snapshot_add_region(snapshot, 2,
210 mc_binary_info->start_rw, mc_binary_info->start_rw,
211 mc_binary_info->end_rw - mc_binary_info->start_rw);
212 snapshot->privatization_regions = NULL;
213 snapshot->privatization_index = -1;
217 /** @brief Finds the range of the different memory segments and binary paths */
218 void MC_init_memory_map_info()
223 memory_map_t maps = MC_get_memory_map();
225 maestro_stack_start = NULL;
226 maestro_stack_end = NULL;
227 libsimgrid_path = NULL;
229 while (i < maps->mapsize) {
230 reg = maps->regions[i];
231 if (maps->regions[i].pathname == NULL) {
233 } else if ((reg.prot & PROT_WRITE)
234 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
235 maestro_stack_start = reg.start_addr;
236 maestro_stack_end = reg.end_addr;
237 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
238 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
240 if (libsimgrid_path == NULL)
241 libsimgrid_path = strdup(maps->regions[i].pathname);
246 xbt_assert(maestro_stack_start, "maestro_stack_start");
247 xbt_assert(maestro_stack_end, "maestro_stack_end");
248 xbt_assert(libsimgrid_path, "libsimgrid_path&");
250 MC_free_memory_map(maps);
254 /** \brief Fills the position of the segments (executable, read-only, read/write).
256 * TODO, use dl_iterate_phdr to be more robust
258 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
263 const char *name = basename(result->file_name);
264 while (i < maps->mapsize) {
265 reg = maps->regions[i];
266 if (maps->regions[i].pathname == NULL
267 || strcmp(basename(maps->regions[i].pathname), name)) {
269 } else if ((reg.prot & PROT_WRITE)) {
270 xbt_assert(!result->start_rw,
271 "Multiple read-write segments for %s, not supported",
272 maps->regions[i].pathname);
273 result->start_rw = reg.start_addr;
274 result->end_rw = reg.end_addr;
275 // .bss is usually after the .data:
276 s_map_region_t *next = &(maps->regions[i + 1]);
277 if (next->pathname == NULL && (next->prot & PROT_WRITE)
278 && next->start_addr == reg.end_addr) {
279 result->end_rw = maps->regions[i + 1].end_addr;
281 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
282 xbt_assert(!result->start_exec,
283 "Multiple executable segments for %s, not supported",
284 maps->regions[i].pathname);
285 result->start_exec = reg.start_addr;
286 result->end_exec = reg.end_addr;
287 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
288 xbt_assert(!result->start_ro,
289 "Multiple read only segments for %s, not supported",
290 maps->regions[i].pathname);
291 result->start_ro = reg.start_addr;
292 result->end_ro = reg.end_addr;
297 xbt_assert(result->file_name);
298 xbt_assert(result->start_rw);
299 xbt_assert(result->start_exec);
302 /************************************* Take Snapshot ************************************/
303 /****************************************************************************************/
305 /** \brief Checks whether the variable is in scope for a given IP.
307 * A variable may be defined only from a given value of IP.
309 * \param var Variable description
310 * \param frame Scope description
311 * \param ip Instruction pointer
312 * \return true if the variable is valid
314 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
317 // The variable is not yet valid:
318 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
324 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
325 dw_frame_t scope, int process_index, xbt_dynar_t result)
327 void *ip = (void *) stack_frame->ip;
328 if (ip < scope->low_pc || ip >= scope->high_pc)
332 dw_variable_t current_variable;
333 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
335 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
339 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
344 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
345 new_var->subprogram = stack_frame->frame;
346 new_var->ip = stack_frame->ip;
347 new_var->name = xbt_strdup(current_variable->name);
348 new_var->type = current_variable->type;
349 new_var->region = region_type;
351 if (current_variable->address != NULL) {
352 new_var->address = current_variable->address;
353 } else if (current_variable->locations.size != 0) {
354 s_mc_location_t location;
355 mc_dwarf_resolve_locations(&location, ¤t_variable->locations,
356 current_variable->object_info,
357 &(stack_frame->unw_cursor),
358 (void *) stack_frame->frame_base,
359 NULL, process_index);
361 switch(mc_get_location_type(&location)) {
362 case MC_LOCATION_TYPE_ADDRESS:
363 new_var->address = location.memory_location;
365 case MC_LOCATION_TYPE_REGISTER:
367 xbt_die("Cannot handle non-address variable");
371 xbt_die("No address");
374 xbt_dynar_push(result, &new_var);
377 // Recursive processing of nested scopes:
378 dw_frame_t nested_scope = NULL;
379 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
380 mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result);
384 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames, int process_index)
387 unsigned cursor1 = 0;
388 mc_stack_frame_t stack_frame;
389 xbt_dynar_t variables =
390 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
392 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
393 mc_fill_local_variables_values(stack_frame, stack_frame->frame, process_index, variables);
399 static void MC_stack_frame_free_voipd(void *s)
401 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
403 xbt_free(stack_frame->frame_name);
404 xbt_free(stack_frame);
408 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
411 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
415 // TODO, check condition check (unw_init_local==0 means end of frame)
416 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
418 xbt_die("Could not initialize stack unwinding");
423 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
424 xbt_dynar_push(result, &stack_frame);
426 stack_frame->unw_cursor = c;
430 unw_get_reg(&c, UNW_REG_IP, &ip);
431 unw_get_reg(&c, UNW_REG_SP, &sp);
433 stack_frame->ip = ip;
434 stack_frame->sp = sp;
436 // TODO, use real addresses in frame_t instead of fixing it here
438 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
439 stack_frame->frame = frame;
442 stack_frame->frame_name = xbt_strdup(frame->name);
443 stack_frame->frame_base =
444 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
446 stack_frame->frame_base = 0;
447 stack_frame->frame_name = NULL;
450 /* Stop before context switch with maestro */
451 if (frame != NULL && frame->name != NULL
452 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
455 int ret = ret = unw_step(&c);
457 xbt_die("Unexpected end of stack.");
458 } else if (ret < 0) {
459 xbt_die("Error while unwinding stack.");
463 if (xbt_dynar_length(result) == 0) {
464 XBT_INFO("unw_init_local failed");
471 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
475 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
476 MC_snapshot_stack_free_voidp);
478 unsigned int cursor = 0;
479 stack_region_t current_stack;
481 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
482 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
483 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
484 st->local_variables = MC_get_local_variables_values(st->stack_frames, current_stack->process_index);
485 st->process_index = current_stack->process_index;
487 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
489 xbt_dynar_push(res, &st);
490 (*snapshot)->stack_sizes =
491 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
492 (*snapshot)->stack_sizes[cursor] =
493 (char*) current_stack->address + current_stack->size - (char*) sp;
500 static xbt_dynar_t MC_take_snapshot_ignore()
503 if (mc_heap_comparison_ignore == NULL)
507 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
508 heap_ignore_region_free_voidp);
510 unsigned int cursor = 0;
511 mc_heap_ignore_region_t current_region;
513 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
514 mc_heap_ignore_region_t new_region = NULL;
515 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
516 new_region->address = current_region->address;
517 new_region->size = current_region->size;
518 new_region->block = current_region->block;
519 new_region->fragment = current_region->fragment;
520 xbt_dynar_push(cpy, &new_region);
527 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
528 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
529 free(ignored_data->data);
532 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
534 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
537 unsigned int cursor = 0;
538 mc_checkpoint_ignore_region_t region;
539 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
540 s_mc_snapshot_ignored_data_t ignored_data;
541 ignored_data.start = region->addr;
542 ignored_data.size = region->size;
543 ignored_data.data = malloc(region->size);
544 memcpy(ignored_data.data, region->addr, region->size);
545 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
549 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
550 memset(region->addr, 0, region->size);
555 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
557 unsigned int cursor = 0;
558 s_mc_snapshot_ignored_data_t ignored_data;
559 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
560 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
564 /** @brief Can we remove this snapshot?
566 * Some snapshots cannot be removed (yet) because we need them
571 int mc_important_snapshot(mc_snapshot_t snapshot)
573 // We need this snapshot in order to know which
574 // pages needs to be stored in the next snapshot.
575 // This field is only non-NULL when using soft-dirty
577 if (snapshot == mc_model_checker->parent_snapshot)
583 static void MC_get_current_fd(mc_snapshot_t snapshot){
585 snapshot->total_fd = 0;
587 const size_t fd_dir_path_size = 20;
588 char fd_dir_path[fd_dir_path_size];
589 if (snprintf(fd_dir_path, fd_dir_path_size,
590 "/proc/%lli/fd", (long long int) getpid()) > fd_dir_path_size)
591 xbt_die("Unexpected buffer is too small for fd_dir_path");
593 DIR* fd_dir = opendir (fd_dir_path);
595 xbt_die("Cannot open directory '/proc/self/fd'\n");
598 struct dirent* fd_number;
599 while ((fd_number = readdir(fd_dir))) {
601 int fd_value = atoi(fd_number->d_name);
606 const size_t source_size = 25;
608 if (snprintf(source, source_size, "/proc/self/fd/%s", fd_number->d_name) > source_size)
609 xbt_die("Unexpected buffer is too small for fd %s", fd_number->d_name);
611 const size_t link_size = 200;
613 int res = readlink(source, link, link_size);
615 xbt_die("Could not read link for %s", source);
618 xbt_die("Buffer to small for link of %s", source);
622 if(smpi_is_privatisation_file(link))
625 // This is (probably) the DIR* we are reading:
626 // TODO, read all the file entries at once and close the DIR.*
627 if(strcmp(fd_dir_path, link) == 0)
630 // We don't handle them.
631 // It does not mean we should silently ignore them however.
632 if (strncmp(link, "pipe:", 5) == 0 || strncmp(link, "socket:", 7) == 0)
635 // This is probably a shared memory used by lttng-ust:
636 if(strncmp("/dev/shm/ust-shm-tmp-", link, 21)==0)
639 // Add an entry for this FD in the snapshot:
640 fd_infos_t fd = xbt_new0(s_fd_infos_t, 1);
641 fd->filename = strdup(link);
642 fd->number = fd_value;
643 fd->flags = fcntl(fd_value, F_GETFL) | fcntl(fd_value, F_GETFD) ;
644 fd->current_position = lseek(fd_value, 0, SEEK_CUR);
645 snapshot->current_fd = xbt_realloc(snapshot->current_fd, (total_fd + 1) * sizeof(fd_infos_t));
646 snapshot->current_fd[total_fd] = fd;
650 snapshot->total_fd = total_fd;
654 mc_snapshot_t MC_take_snapshot(int num_state)
657 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
658 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
659 smx_process_t process;
660 xbt_swag_foreach(process, simix_global->process_list) {
661 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
664 MC_snapshot_handle_ignore(snapshot);
666 MC_get_current_fd(snapshot);
668 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
669 MC_get_memory_regions(snapshot);
670 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
671 mc_softdirty_reset();
674 snapshot->to_ignore = MC_take_snapshot_ignore();
676 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
678 MC_take_snapshot_stacks(&snapshot);
679 if (_sg_mc_hash && snapshot->stacks != NULL) {
680 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
688 MC_snapshot_ignore_restore(snapshot);
689 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
690 mc_model_checker->parent_snapshot = snapshot;
695 void MC_restore_snapshot(mc_snapshot_t snapshot)
697 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
701 for (i = 0; i < NB_REGIONS; i++) {
702 // For privatized, variables we decided it was not necessary to take the snapshot:
703 if (snapshot->regions[i])
704 MC_region_restore(snapshot->regions[i],
705 parent_snapshot ? parent_snapshot->regions[i] : NULL);
709 if (snapshot->privatization_regions) {
710 // Restore the global variables of the application separately for each
711 // simulated process:
712 for (i = 0; i < smpi_process_count(); i++) {
713 if (snapshot->privatization_regions[i]) {
714 MC_region_restore(snapshot->privatization_regions[i],
715 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
719 if(snapshot->privatization_index >= 0) {
720 // We just rewrote the global variables.
721 // The privatisation segment SMPI thinks
722 // is mapped might be inconsistent with the segment which
723 // is really mapped in memory (kernel state).
724 // We ask politely SMPI to map the segment anyway,
725 // even if it thinks it is the current one:
726 smpi_really_switch_data_segment(snapshot->privatization_index);
730 for(i=0; i < snapshot->total_fd; i++){
732 new_fd = open(snapshot->current_fd[i]->filename, snapshot->current_fd[i]->flags);
734 xbt_die("Could not reopen the file %s fo restoring the file descriptor",
735 snapshot->current_fd[i]->filename);
737 if(new_fd != -1 && new_fd != snapshot->current_fd[i]->number){
738 dup2(new_fd, snapshot->current_fd[i]->number);
739 //fprintf(stderr, "%p\n", fdopen(snapshot->current_fd[i]->number, "rw"));
742 lseek(snapshot->current_fd[i]->number, snapshot->current_fd[i]->current_position, SEEK_SET);
745 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
746 mc_softdirty_reset();
749 MC_snapshot_ignore_restore(snapshot);
750 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
751 mc_model_checker->parent_snapshot = snapshot;
756 mc_snapshot_t simcall_HANDLER_mc_snapshot(smx_simcall_t simcall)
758 return MC_take_snapshot(1);
761 void *MC_snapshot(void)
763 return simcall_mc_snapshot();