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. */
14 #include "internal_config.h"
15 #include "mc_private.h"
16 #include "xbt/module.h"
17 #include <xbt/mmalloc.h>
18 #include "../smpi/private.h"
21 #include "xbt/mmalloc/mmprivate.h"
23 #include "../simix/smx_private.h"
25 #include <libunwind.h>
28 #include "mc_private.h"
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
34 "Logging specific to mc_checkpoint");
36 char *libsimgrid_path;
38 /************************************ Free functions **************************************/
39 /*****************************************************************************************/
41 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
44 xbt_dynar_free(&(s->local_variables));
45 xbt_dynar_free(&(s->stack_frames));
50 static void MC_snapshot_stack_free_voidp(void *s)
52 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
55 static void local_variable_free(local_variable_t v)
61 static void local_variable_free_voidp(void *v)
63 local_variable_free((local_variable_t) * (void **) v);
66 void MC_region_destroy(mc_mem_region_t reg)
71 //munmap(reg->data, reg->size);
73 if (reg->page_numbers) {
74 mc_free_page_snapshot_region(reg->page_numbers, mc_page_count(reg->size));
79 void MC_free_snapshot(mc_snapshot_t snapshot)
82 for (i = 0; i < NB_REGIONS; i++) {
83 MC_region_destroy(snapshot->regions[i]);
86 xbt_free(snapshot->stack_sizes);
87 xbt_dynar_free(&(snapshot->stacks));
88 xbt_dynar_free(&(snapshot->to_ignore));
89 xbt_dynar_free(&snapshot->ignored_data);
91 if (snapshot->privatization_regions) {
92 size_t n = xbt_dynar_length(snapshot->enabled_processes);
93 for (i = 0; i != n; ++i) {
94 MC_region_destroy(snapshot->privatization_regions[i]);
96 xbt_free(snapshot->privatization_regions);
102 /******************************* Snapshot regions ********************************/
103 /*********************************************************************************/
105 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)
107 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
108 new_reg->start_addr = start_addr;
109 new_reg->permanent_addr = permanent_addr;
110 new_reg->data = NULL;
111 new_reg->size = size;
112 new_reg->page_numbers = NULL;
113 new_reg->data = xbt_malloc(size);
114 memcpy(new_reg->data, permanent_addr, size);
115 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
116 type, new_reg->data, permanent_addr, size);
121 /** @brief Take a snapshot of a given region
124 * @param start_addr Address of the region in the simulated process
125 * @param permanent_addr Permanent address of this data (for privatized variables, this is the virtual address of the privatized mapping)
126 * @param size Size of the data*
127 * @param ref_reg Reference corresponding region
129 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)
131 if (_sg_mc_sparse_checkpoint) {
132 return mc_region_new_sparse(type, start_addr, permanent_addr, size, ref_reg);
134 return mc_region_new_dense(type, start_addr, permanent_addr, size, ref_reg);
138 /** @brief Restore a region from a snapshot
140 * If we are using per page snapshots, it is possible to use the reference
141 * region in order to do an incremental restoration of the region: the
142 * softclean pages which are shared between the two snapshots do not need
145 * @param reg Target region
146 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
148 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
150 /*FIXME: check if start_addr is still mapped, if it is not, then map it
151 before copying the data */
152 if (!reg->page_numbers) {
153 memcpy(reg->permanent_addr, reg->data, reg->size);
155 mc_region_restore_sparse(reg, ref_reg);
160 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
161 void *start_addr, void* permanent_addr, size_t size)
164 mc_mem_region_t ref_reg =
165 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
166 mc_mem_region_t new_reg = MC_region_new(type, start_addr, permanent_addr, size, ref_reg);
167 snapshot->regions[type] = new_reg;
171 static void MC_get_memory_regions(mc_snapshot_t snapshot)
174 void *start_heap = ((xbt_mheap_t) std_heap)->base;
175 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
176 MC_snapshot_add_region(snapshot, 0, start_heap, start_heap,
177 (char *) end_heap - (char *) start_heap);
178 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
179 snapshot->privatization_regions = NULL;
181 MC_snapshot_add_region(snapshot, 1,
182 mc_libsimgrid_info->start_rw, mc_libsimgrid_info->start_rw,
183 mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
187 if (smpi_privatize_global_variables && smpi_process_count()) {
188 // Snapshot the global variable of the application separately for each
189 // simulated process:
190 snapshot->privatization_regions =
191 xbt_new(mc_mem_region_t, smpi_process_count());
192 for (i = 0; i < smpi_process_count(); i++) {
193 mc_mem_region_t ref_reg =
194 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
195 snapshot->privatization_regions[i] =
196 MC_region_new(-1, mc_binary_info->start_rw, smpi_privatisation_regions[i].address, size_data_exe, ref_reg);
198 snapshot->privatization_index = smpi_loaded_page;
199 snapshot->regions[2] = NULL;
203 MC_snapshot_add_region(snapshot, 2,
204 mc_binary_info->start_rw, mc_binary_info->start_rw,
205 mc_binary_info->end_rw - mc_binary_info->start_rw);
206 snapshot->privatization_regions = NULL;
207 snapshot->privatization_index = -1;
211 /** @brief Finds the range of the different memory segments and binary paths */
212 void MC_init_memory_map_info()
217 memory_map_t maps = MC_get_memory_map();
219 maestro_stack_start = NULL;
220 maestro_stack_end = NULL;
221 libsimgrid_path = NULL;
223 while (i < maps->mapsize) {
224 reg = maps->regions[i];
225 if (maps->regions[i].pathname == NULL) {
227 } else if ((reg.prot & PROT_WRITE)
228 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
229 maestro_stack_start = reg.start_addr;
230 maestro_stack_end = reg.end_addr;
231 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
232 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
234 if (libsimgrid_path == NULL)
235 libsimgrid_path = strdup(maps->regions[i].pathname);
240 xbt_assert(maestro_stack_start, "maestro_stack_start");
241 xbt_assert(maestro_stack_end, "maestro_stack_end");
242 xbt_assert(libsimgrid_path, "libsimgrid_path&");
244 MC_free_memory_map(maps);
248 /** \brief Fill/lookup the "subtype" field.
250 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
253 if (type->dw_type_id == NULL)
255 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
256 if (type->subtype == NULL)
258 if (type->subtype->byte_size != 0)
260 if (type->subtype->name == NULL)
262 // Try to find a more complete description of the type:
263 // We need to fix in order to support C++.
266 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
267 if (subtype != NULL) {
268 type->subtype = subtype;
273 void MC_post_process_types(mc_object_info_t info)
275 xbt_dict_cursor_t cursor = NULL;
279 // Lookup "subtype" field:
280 xbt_dict_foreach(info->types, cursor, origin, type) {
281 MC_resolve_subtype(info, type);
285 if (type->members != NULL)
286 xbt_dynar_foreach(type->members, i, member) {
287 MC_resolve_subtype(info, member);
292 /** \brief Fills the position of the segments (executable, read-only, read/write).
294 * TODO, use dl_iterate_phdr to be more robust
296 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
301 const char *name = basename(result->file_name);
302 while (i < maps->mapsize) {
303 reg = maps->regions[i];
304 if (maps->regions[i].pathname == NULL
305 || strcmp(basename(maps->regions[i].pathname), name)) {
307 } else if ((reg.prot & PROT_WRITE)) {
308 xbt_assert(!result->start_rw,
309 "Multiple read-write segments for %s, not supported",
310 maps->regions[i].pathname);
311 result->start_rw = reg.start_addr;
312 result->end_rw = reg.end_addr;
313 // .bss is usually after the .data:
314 s_map_region_t *next = &(maps->regions[i + 1]);
315 if (next->pathname == NULL && (next->prot & PROT_WRITE)
316 && next->start_addr == reg.end_addr) {
317 result->end_rw = maps->regions[i + 1].end_addr;
319 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
320 xbt_assert(!result->start_exec,
321 "Multiple executable segments for %s, not supported",
322 maps->regions[i].pathname);
323 result->start_exec = reg.start_addr;
324 result->end_exec = reg.end_addr;
325 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
326 xbt_assert(!result->start_ro,
327 "Multiple read only segments for %s, not supported",
328 maps->regions[i].pathname);
329 result->start_ro = reg.start_addr;
330 result->end_ro = reg.end_addr;
335 xbt_assert(result->file_name);
336 xbt_assert(result->start_rw);
337 xbt_assert(result->start_exec);
340 /************************************* Take Snapshot ************************************/
341 /****************************************************************************************/
343 /** \brief Checks whether the variable is in scope for a given IP.
345 * A variable may be defined only from a given value of IP.
347 * \param var Variable description
348 * \param frame Scope description
349 * \param ip Instruction pointer
350 * \return true if the variable is valid
352 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
355 // The variable is not yet valid:
356 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
362 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
363 dw_frame_t scope, int process_index, xbt_dynar_t result)
365 void *ip = (void *) stack_frame->ip;
366 if (ip < scope->low_pc || ip >= scope->high_pc)
370 dw_variable_t current_variable;
371 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
373 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
377 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
382 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
383 new_var->subprogram = stack_frame->frame;
384 new_var->ip = stack_frame->ip;
385 new_var->name = xbt_strdup(current_variable->name);
386 new_var->type = current_variable->type;
387 new_var->region = region_type;
389 if (current_variable->address != NULL) {
390 new_var->address = current_variable->address;
391 } else if (current_variable->locations.size != 0) {
393 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
394 current_variable->object_info,
395 &(stack_frame->unw_cursor),
396 (void *) stack_frame->frame_base,
397 NULL, process_index);
399 xbt_die("No address");
402 xbt_dynar_push(result, &new_var);
405 // Recursive processing of nested scopes:
406 dw_frame_t nested_scope = NULL;
407 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
408 mc_fill_local_variables_values(stack_frame, nested_scope, process_index, result);
412 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames, int process_index)
415 unsigned cursor1 = 0;
416 mc_stack_frame_t stack_frame;
417 xbt_dynar_t variables =
418 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
420 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
421 mc_fill_local_variables_values(stack_frame, stack_frame->frame, process_index, variables);
427 static void MC_stack_frame_free_voipd(void *s)
429 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
431 xbt_free(stack_frame->frame_name);
432 xbt_free(stack_frame);
436 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
439 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
443 // TODO, check condition check (unw_init_local==0 means end of frame)
444 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
446 xbt_die("Could not initialize stack unwinding");
451 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
452 xbt_dynar_push(result, &stack_frame);
454 stack_frame->unw_cursor = c;
458 unw_get_reg(&c, UNW_REG_IP, &ip);
459 unw_get_reg(&c, UNW_REG_SP, &sp);
461 stack_frame->ip = ip;
462 stack_frame->sp = sp;
464 // TODO, use real addresses in frame_t instead of fixing it here
466 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
467 stack_frame->frame = frame;
470 stack_frame->frame_name = xbt_strdup(frame->name);
471 stack_frame->frame_base =
472 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
474 stack_frame->frame_base = 0;
475 stack_frame->frame_name = NULL;
478 /* Stop before context switch with maestro */
479 if (frame != NULL && frame->name != NULL
480 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
483 int ret = ret = unw_step(&c);
485 xbt_die("Unexpected end of stack.");
486 } else if (ret < 0) {
487 xbt_die("Error while unwinding stack.");
491 if (xbt_dynar_length(result) == 0) {
492 XBT_INFO("unw_init_local failed");
499 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
503 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
504 MC_snapshot_stack_free_voidp);
506 unsigned int cursor = 0;
507 stack_region_t current_stack;
509 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
510 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
511 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
512 st->local_variables = MC_get_local_variables_values(st->stack_frames, current_stack->process_index);
513 st->process_index = current_stack->process_index;
515 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
517 xbt_dynar_push(res, &st);
518 (*snapshot)->stack_sizes =
519 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
520 (*snapshot)->stack_sizes[cursor] =
521 (char*) current_stack->address + current_stack->size - (char*) sp;
528 static xbt_dynar_t MC_take_snapshot_ignore()
531 if (mc_heap_comparison_ignore == NULL)
535 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
536 heap_ignore_region_free_voidp);
538 unsigned int cursor = 0;
539 mc_heap_ignore_region_t current_region;
541 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
542 mc_heap_ignore_region_t new_region = NULL;
543 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
544 new_region->address = current_region->address;
545 new_region->size = current_region->size;
546 new_region->block = current_region->block;
547 new_region->fragment = current_region->fragment;
548 xbt_dynar_push(cpy, &new_region);
555 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
556 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
557 free(ignored_data->data);
560 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
562 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
565 unsigned int cursor = 0;
566 mc_checkpoint_ignore_region_t region;
567 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
568 s_mc_snapshot_ignored_data_t ignored_data;
569 ignored_data.start = region->addr;
570 ignored_data.size = region->size;
571 ignored_data.data = malloc(region->size);
572 memcpy(ignored_data.data, region->addr, region->size);
573 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
577 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
578 memset(region->addr, 0, region->size);
583 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
585 unsigned int cursor = 0;
586 s_mc_snapshot_ignored_data_t ignored_data;
587 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
588 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
592 /** @brief Can we remove this snapshot?
594 * Some snapshots cannot be removed (yet) because we need them
599 int mc_important_snapshot(mc_snapshot_t snapshot)
601 // We need this snapshot in order to know which
602 // pages needs to be stored in the next snapshot:
603 if (snapshot == mc_model_checker->parent_snapshot)
609 mc_snapshot_t MC_take_snapshot(int num_state)
612 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
613 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
614 smx_process_t process;
615 xbt_swag_foreach(process, simix_global->process_list) {
616 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
619 MC_snapshot_handle_ignore(snapshot);
621 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
622 MC_get_memory_regions(snapshot);
623 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
624 mc_softdirty_reset();
627 snapshot->to_ignore = MC_take_snapshot_ignore();
629 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
631 MC_take_snapshot_stacks(&snapshot);
632 if (_sg_mc_hash && snapshot->stacks != NULL) {
633 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
641 MC_snapshot_ignore_restore(snapshot);
642 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
643 mc_model_checker->parent_snapshot = snapshot;
648 void MC_restore_snapshot(mc_snapshot_t snapshot)
650 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
653 for (i = 0; i < NB_REGIONS; i++) {
654 // For privatized, variables we decided it was not necessary to take the snapshot:
655 if (snapshot->regions[i])
656 MC_region_restore(snapshot->regions[i],
657 parent_snapshot ? parent_snapshot->regions[i] : NULL);
661 if (snapshot->privatization_regions) {
662 // Restore the global variables of the application separately for each
663 // simulated process:
664 for (i = 0; i < smpi_process_count(); i++) {
665 if (snapshot->privatization_regions[i]) {
666 MC_region_restore(snapshot->privatization_regions[i],
667 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
671 if(snapshot->privatization_index >= 0) {
672 smpi_switch_data_segment(snapshot->privatization_index);
676 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
677 mc_softdirty_reset();
680 MC_snapshot_ignore_restore(snapshot);
681 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
682 mc_model_checker->parent_snapshot = snapshot;
686 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
688 return MC_take_snapshot(1);
691 void *MC_snapshot(void)
693 return simcall_mc_snapshot();