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. */
12 #include "mc_private.h"
13 #include "xbt/module.h"
14 #include <xbt/mmalloc.h>
15 #include "../smpi/private.h"
18 #include "xbt/mmalloc/mmprivate.h"
20 #include "../simix/smx_private.h"
22 #include <libunwind.h>
25 #include "mc_private.h"
30 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
31 "Logging specific to mc_checkpoint");
33 char *libsimgrid_path;
35 /************************************ Free functions **************************************/
36 /*****************************************************************************************/
38 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
41 xbt_dynar_free(&(s->local_variables));
42 xbt_dynar_free(&(s->stack_frames));
47 static void MC_snapshot_stack_free_voidp(void *s)
49 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
52 static void local_variable_free(local_variable_t v)
58 static void local_variable_free_voidp(void *v)
60 local_variable_free((local_variable_t) * (void **) v);
63 void MC_region_destroy(mc_mem_region_t reg)
65 //munmap(reg->data, reg->size);
67 if (reg->page_numbers) {
68 mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
73 void MC_free_snapshot(mc_snapshot_t snapshot)
76 for (i = 0; i < NB_REGIONS; i++)
77 MC_region_destroy(snapshot->regions[i]);
79 xbt_free(snapshot->stack_sizes);
80 xbt_dynar_free(&(snapshot->stacks));
81 xbt_dynar_free(&(snapshot->to_ignore));
82 xbt_dynar_free(&snapshot->ignored_data);
84 if (snapshot->privatization_regions) {
85 size_t n = xbt_dynar_length(snapshot->enabled_processes);
86 for (i = 0; i != n; ++i) {
87 MC_region_destroy(snapshot->privatization_regions[i]);
89 xbt_free(snapshot->privatization_regions);
95 /******************************* Snapshot regions ********************************/
96 /*********************************************************************************/
98 static mc_mem_region_t mc_region_new_dense(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg)
100 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
101 new_reg->start_addr = start_addr;
102 new_reg->data = NULL;
103 new_reg->size = size;
104 new_reg->page_numbers = NULL;
105 new_reg->data = xbt_malloc(size);
106 memcpy(new_reg->data, start_addr, size);
107 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
108 type, new_reg->data, start_addr, size);
113 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg)
115 if (_sg_mc_sparse_checkpoint) {
116 return mc_region_new_sparse(type, start_addr, size, ref_reg);
118 return mc_region_new_dense(type, start_addr, size, ref_reg);
122 /** @brief Restore a region from a snapshot
124 * If we are using per page snapshots, it is possible to use the reference
125 * region in order to do an incremental restoration of the region: the
126 * softclean pages which are shared between the two snapshots do not need
129 * @param reg Target region
130 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
132 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
134 /*FIXME: check if start_addr is still mapped, if it is not, then map it
135 before copying the data */
136 if (!reg->page_numbers) {
137 memcpy(reg->start_addr, reg->data, reg->size);
139 mc_region_restore_sparse(reg, ref_reg);
144 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
145 void *start_addr, size_t size)
148 mc_mem_region_t ref_reg =
149 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
150 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size, ref_reg);
151 snapshot->regions[type] = new_reg;
155 static void MC_get_memory_regions(mc_snapshot_t snapshot)
159 void *start_heap = ((xbt_mheap_t) std_heap)->base;
160 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
161 MC_snapshot_add_region(snapshot, 0, start_heap,
162 (char *) end_heap - (char *) start_heap);
163 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
165 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
166 mc_libsimgrid_info->end_rw -
167 mc_libsimgrid_info->start_rw);
168 if (!smpi_privatize_global_variables) {
169 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
170 mc_binary_info->end_rw - mc_binary_info->start_rw);
171 snapshot->privatization_regions = NULL;
172 snapshot->privatization_index = -1;
174 snapshot->privatization_regions =
175 xbt_new(mc_mem_region_t, SIMIX_process_count());
176 for (i = 0; i < SIMIX_process_count(); i++) {
177 mc_mem_region_t ref_reg =
178 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
179 snapshot->privatization_regions[i] =
180 MC_region_new(-1, mappings[i], size_data_exe, ref_reg);
182 snapshot->privatization_index = loaded_page;
186 /** @brief Finds the range of the different memory segments and binary paths */
187 void MC_init_memory_map_info()
192 memory_map_t maps = MC_get_memory_map();
194 maestro_stack_start = NULL;
195 maestro_stack_end = NULL;
196 libsimgrid_path = NULL;
198 while (i < maps->mapsize) {
199 reg = maps->regions[i];
200 if (maps->regions[i].pathname == NULL) {
202 } else if ((reg.prot & PROT_WRITE)
203 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
204 maestro_stack_start = reg.start_addr;
205 maestro_stack_end = reg.end_addr;
206 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
207 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
209 if (libsimgrid_path == NULL)
210 libsimgrid_path = strdup(maps->regions[i].pathname);
215 xbt_assert(maestro_stack_start, "maestro_stack_start");
216 xbt_assert(maestro_stack_end, "maestro_stack_end");
217 xbt_assert(libsimgrid_path, "libsimgrid_path&");
219 MC_free_memory_map(maps);
223 /** \brief Fill/lookup the "subtype" field.
225 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
228 if (type->dw_type_id == NULL)
230 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
231 if (type->subtype == NULL)
233 if (type->subtype->byte_size != 0)
235 if (type->subtype->name == NULL)
237 // Try to find a more complete description of the type:
238 // We need to fix in order to support C++.
241 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
242 if (subtype != NULL) {
243 type->subtype = subtype;
248 void MC_post_process_types(mc_object_info_t info)
250 xbt_dict_cursor_t cursor = NULL;
254 // Lookup "subtype" field:
255 xbt_dict_foreach(info->types, cursor, origin, type) {
256 MC_resolve_subtype(info, type);
260 if (type->members != NULL)
261 xbt_dynar_foreach(type->members, i, member) {
262 MC_resolve_subtype(info, member);
267 /** \brief Fills the position of the segments (executable, read-only, read/write).
269 * TODO, use dl_iterate_phdr to be more robust
271 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
276 const char *name = basename(result->file_name);
277 while (i < maps->mapsize) {
278 reg = maps->regions[i];
279 if (maps->regions[i].pathname == NULL
280 || strcmp(basename(maps->regions[i].pathname), name)) {
282 } else if ((reg.prot & PROT_WRITE)) {
283 xbt_assert(!result->start_rw,
284 "Multiple read-write segments for %s, not supported",
285 maps->regions[i].pathname);
286 result->start_rw = reg.start_addr;
287 result->end_rw = reg.end_addr;
288 // .bss is usually after the .data:
289 s_map_region_t *next = &(maps->regions[i + 1]);
290 if (next->pathname == NULL && (next->prot & PROT_WRITE)
291 && next->start_addr == reg.end_addr) {
292 result->end_rw = maps->regions[i + 1].end_addr;
294 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
295 xbt_assert(!result->start_exec,
296 "Multiple executable segments for %s, not supported",
297 maps->regions[i].pathname);
298 result->start_exec = reg.start_addr;
299 result->end_exec = reg.end_addr;
300 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
301 xbt_assert(!result->start_ro,
302 "Multiple read only segments for %s, not supported",
303 maps->regions[i].pathname);
304 result->start_ro = reg.start_addr;
305 result->end_ro = reg.end_addr;
310 xbt_assert(result->file_name);
311 xbt_assert(result->start_rw);
312 xbt_assert(result->start_exec);
315 /************************************* Take Snapshot ************************************/
316 /****************************************************************************************/
318 /** \brief Checks whether the variable is in scope for a given IP.
320 * A variable may be defined only from a given value of IP.
322 * \param var Variable description
323 * \param frame Scope description
324 * \param ip Instruction pointer
325 * \return true if the variable is valid
327 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
330 // The variable is not yet valid:
331 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
337 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
338 dw_frame_t scope, xbt_dynar_t result)
340 void *ip = (void *) stack_frame->ip;
341 if (ip < scope->low_pc || ip >= scope->high_pc)
345 dw_variable_t current_variable;
346 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
348 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
352 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
357 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
358 new_var->subprogram = stack_frame->frame;
359 new_var->ip = stack_frame->ip;
360 new_var->name = xbt_strdup(current_variable->name);
361 new_var->type = current_variable->type;
362 new_var->region = region_type;
364 if (current_variable->address != NULL) {
365 new_var->address = current_variable->address;
366 } else if (current_variable->locations.size != 0) {
368 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
369 current_variable->object_info,
370 &(stack_frame->unw_cursor),
371 (void *) stack_frame->frame_base,
374 xbt_die("No address");
377 xbt_dynar_push(result, &new_var);
380 // Recursive processing of nested scopes:
381 dw_frame_t nested_scope = NULL;
382 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
383 mc_fill_local_variables_values(stack_frame, nested_scope, result);
387 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
390 unsigned cursor1 = 0;
391 mc_stack_frame_t stack_frame;
392 xbt_dynar_t variables =
393 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
395 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
396 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
402 static void MC_stack_frame_free_voipd(void *s)
404 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
406 xbt_free(stack_frame->frame_name);
407 xbt_free(stack_frame);
411 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
414 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
418 // TODO, check condition check (unw_init_local==0 means end of frame)
419 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
421 xbt_die("Could not initialize stack unwinding");
426 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
427 xbt_dynar_push(result, &stack_frame);
429 stack_frame->unw_cursor = c;
433 unw_get_reg(&c, UNW_REG_IP, &ip);
434 unw_get_reg(&c, UNW_REG_SP, &sp);
436 stack_frame->ip = ip;
437 stack_frame->sp = sp;
439 // TODO, use real addresses in frame_t instead of fixing it here
441 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
442 stack_frame->frame = frame;
445 stack_frame->frame_name = xbt_strdup(frame->name);
446 stack_frame->frame_base =
447 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
449 stack_frame->frame_base = 0;
450 stack_frame->frame_name = NULL;
453 /* Stop before context switch with maestro */
454 if (frame != NULL && frame->name != NULL
455 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
458 int ret = ret = unw_step(&c);
460 xbt_die("Unexpected end of stack.");
461 } else if (ret < 0) {
462 xbt_die("Error while unwinding stack.");
466 if (xbt_dynar_length(result) == 0) {
467 XBT_INFO("unw_init_local failed");
474 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
478 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
479 MC_snapshot_stack_free_voidp);
481 unsigned int cursor = 0;
482 stack_region_t current_stack;
484 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
485 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
486 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
487 st->local_variables = MC_get_local_variables_values(st->stack_frames);
489 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
491 xbt_dynar_push(res, &st);
492 (*snapshot)->stack_sizes =
493 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
494 (*snapshot)->stack_sizes[cursor] =
495 (char*) current_stack->address + current_stack->size - (char*) sp;
502 static xbt_dynar_t MC_take_snapshot_ignore()
505 if (mc_heap_comparison_ignore == NULL)
509 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
510 heap_ignore_region_free_voidp);
512 unsigned int cursor = 0;
513 mc_heap_ignore_region_t current_region;
515 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
516 mc_heap_ignore_region_t new_region = NULL;
517 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
518 new_region->address = current_region->address;
519 new_region->size = current_region->size;
520 new_region->block = current_region->block;
521 new_region->fragment = current_region->fragment;
522 xbt_dynar_push(cpy, &new_region);
529 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
530 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
531 free(ignored_data->data);
534 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
536 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
539 unsigned int cursor = 0;
540 mc_checkpoint_ignore_region_t region;
541 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
542 s_mc_snapshot_ignored_data_t ignored_data;
543 ignored_data.start = region->addr;
544 ignored_data.size = region->size;
545 ignored_data.data = malloc(region->size);
546 memcpy(ignored_data.data, region->addr, region->size);
547 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
551 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
552 memset(region->addr, 0, region->size);
557 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
559 unsigned int cursor = 0;
560 s_mc_snapshot_ignored_data_t ignored_data;
561 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
562 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
566 /** @brief Can we remove this snapshot?
568 * Some snapshots cannot be removed (yet) because we need them
573 int mc_important_snapshot(mc_snapshot_t snapshot)
575 // We need this snapshot in order to know which
576 // pages needs to be stored in the next snapshot:
577 if (snapshot == mc_model_checker->parent_snapshot)
583 mc_snapshot_t MC_take_snapshot(int num_state)
586 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
587 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
588 smx_process_t process;
589 xbt_swag_foreach(process, simix_global->process_list) {
590 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
593 MC_snapshot_handle_ignore(snapshot);
595 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
596 MC_get_memory_regions(snapshot);
597 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
598 mc_softdirty_reset();
601 snapshot->to_ignore = MC_take_snapshot_ignore();
603 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
605 MC_take_snapshot_stacks(&snapshot);
606 if (_sg_mc_hash && snapshot->stacks != NULL) {
607 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
615 MC_snapshot_ignore_restore(snapshot);
616 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
617 mc_model_checker->parent_snapshot = snapshot;
622 void MC_restore_snapshot(mc_snapshot_t snapshot)
624 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
627 for (i = 0; i < NB_REGIONS; i++) {
628 // For privatized, variables we decided it was not necessary to take the snapshot:
629 if (snapshot->regions[i])
630 MC_region_restore(snapshot->regions[i],
631 parent_snapshot ? parent_snapshot->regions[i] : NULL);
634 if (snapshot->privatization_regions) {
635 for (i = 0; i < SIMIX_process_count(); i++) {
636 if (snapshot->privatization_regions[i]) {
637 MC_region_restore(snapshot->privatization_regions[i],
638 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
641 switch_data_segment(snapshot->privatization_index);
644 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
645 mc_softdirty_reset();
648 MC_snapshot_ignore_restore(snapshot);
649 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
650 mc_model_checker->parent_snapshot = snapshot;
654 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
656 return MC_take_snapshot(1);
659 void *MC_snapshot(void)
661 return simcall_mc_snapshot();