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 static void MC_region_destroy(mc_mem_region_t reg)
65 //munmap(reg->data, reg->size);
70 void MC_free_snapshot(mc_snapshot_t snapshot)
73 for (i = 0; i < NB_REGIONS; i++)
74 MC_region_destroy(snapshot->regions[i]);
76 xbt_free(snapshot->stack_sizes);
77 xbt_dynar_free(&(snapshot->stacks));
78 xbt_dynar_free(&(snapshot->to_ignore));
79 xbt_dynar_free(&snapshot->ignored_data);
81 if (snapshot->privatization_regions) {
82 size_t n = xbt_dynar_length(snapshot->enabled_processes);
83 for (i = 0; i != n; ++i) {
84 MC_region_destroy(snapshot->privatization_regions[i]);
86 xbt_free(snapshot->privatization_regions);
92 /******************************* Snapshot regions ********************************/
93 /*********************************************************************************/
95 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg)
97 if (_sg_mc_sparse_checkpoint) {
98 return mc_region_new_sparse(type, start_addr, size, ref_reg);
101 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
102 new_reg->start_addr = start_addr;
103 new_reg->data = NULL;
104 new_reg->size = size;
105 new_reg->page_numbers = NULL;
106 new_reg->data = xbt_malloc(size);
107 memcpy(new_reg->data, start_addr, size);
108 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
109 type, new_reg->data, start_addr, size);
114 /** @brief Restore a region from a snapshot
116 * If we are using per page snapshots, it is possible to use the reference
117 * region in order to do an incremental restoration of the region: the
118 * softclean pages which are shared between the two snapshots do not need
121 * @param reg Target region
122 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
124 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
126 /*FIXME: check if start_addr is still mapped, if it is not, then map it
127 before copying the data */
128 if (!reg->page_numbers) {
129 memcpy(reg->start_addr, reg->data, reg->size);
131 mc_region_restore_sparse(reg, ref_reg);
136 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
137 void *start_addr, size_t size)
140 mc_mem_region_t ref_reg =
141 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
142 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size, ref_reg);
143 snapshot->regions[type] = new_reg;
147 static void MC_get_memory_regions(mc_snapshot_t snapshot)
151 void *start_heap = ((xbt_mheap_t) std_heap)->base;
152 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
153 MC_snapshot_add_region(snapshot, 0, start_heap,
154 (char *) end_heap - (char *) start_heap);
155 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
157 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
158 mc_libsimgrid_info->end_rw -
159 mc_libsimgrid_info->start_rw);
160 if (!smpi_privatize_global_variables) {
161 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
162 mc_binary_info->end_rw - mc_binary_info->start_rw);
163 snapshot->privatization_regions = NULL;
164 snapshot->privatization_index = -1;
166 snapshot->privatization_regions =
167 xbt_new(mc_mem_region_t, SIMIX_process_count());
168 for (i = 0; i < SIMIX_process_count(); i++) {
169 // TODO, add support for sparse snapshot
170 snapshot->privatization_regions[i] =
171 MC_region_new(-1, mappings[i], size_data_exe, NULL);
173 snapshot->privatization_index = loaded_page;
177 /** @brief Finds the range of the different memory segments and binary paths */
178 void MC_init_memory_map_info()
183 memory_map_t maps = MC_get_memory_map();
185 maestro_stack_start = NULL;
186 maestro_stack_end = NULL;
187 libsimgrid_path = NULL;
189 while (i < maps->mapsize) {
190 reg = maps->regions[i];
191 if (maps->regions[i].pathname == NULL) {
193 } else if ((reg.prot & PROT_WRITE)
194 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
195 maestro_stack_start = reg.start_addr;
196 maestro_stack_end = reg.end_addr;
197 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
198 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
200 if (libsimgrid_path == NULL)
201 libsimgrid_path = strdup(maps->regions[i].pathname);
206 xbt_assert(maestro_stack_start, "maestro_stack_start");
207 xbt_assert(maestro_stack_end, "maestro_stack_end");
208 xbt_assert(libsimgrid_path, "libsimgrid_path&");
210 MC_free_memory_map(maps);
214 /** \brief Fill/lookup the "subtype" field.
216 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
219 if (type->dw_type_id == NULL)
221 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
222 if (type->subtype == NULL)
224 if (type->subtype->byte_size != 0)
226 if (type->subtype->name == NULL)
228 // Try to find a more complete description of the type:
229 // We need to fix in order to support C++.
232 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
233 if (subtype != NULL) {
234 type->subtype = subtype;
239 void MC_post_process_types(mc_object_info_t info)
241 xbt_dict_cursor_t cursor = NULL;
245 // Lookup "subtype" field:
246 xbt_dict_foreach(info->types, cursor, origin, type) {
247 MC_resolve_subtype(info, type);
251 if (type->members != NULL)
252 xbt_dynar_foreach(type->members, i, member) {
253 MC_resolve_subtype(info, member);
258 /** \brief Fills the position of the segments (executable, read-only, read/write).
260 * TODO, use dl_iterate_phdr to be more robust
262 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
267 const char *name = basename(result->file_name);
268 while (i < maps->mapsize) {
269 reg = maps->regions[i];
270 if (maps->regions[i].pathname == NULL
271 || strcmp(basename(maps->regions[i].pathname), name)) {
273 } else if ((reg.prot & PROT_WRITE)) {
274 xbt_assert(!result->start_rw,
275 "Multiple read-write segments for %s, not supported",
276 maps->regions[i].pathname);
277 result->start_rw = reg.start_addr;
278 result->end_rw = reg.end_addr;
279 // .bss is usually after the .data:
280 s_map_region_t *next = &(maps->regions[i + 1]);
281 if (next->pathname == NULL && (next->prot & PROT_WRITE)
282 && next->start_addr == reg.end_addr) {
283 result->end_rw = maps->regions[i + 1].end_addr;
285 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
286 xbt_assert(!result->start_exec,
287 "Multiple executable segments for %s, not supported",
288 maps->regions[i].pathname);
289 result->start_exec = reg.start_addr;
290 result->end_exec = reg.end_addr;
291 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
292 xbt_assert(!result->start_ro,
293 "Multiple read only segments for %s, not supported",
294 maps->regions[i].pathname);
295 result->start_ro = reg.start_addr;
296 result->end_ro = reg.end_addr;
301 xbt_assert(result->file_name);
302 xbt_assert(result->start_rw);
303 xbt_assert(result->start_exec);
306 /************************************* Take Snapshot ************************************/
307 /****************************************************************************************/
309 /** \brief Checks whether the variable is in scope for a given IP.
311 * A variable may be defined only from a given value of IP.
313 * \param var Variable description
314 * \param frame Scope description
315 * \param ip Instruction pointer
316 * \return true if the variable is valid
318 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
321 // The variable is not yet valid:
322 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
328 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
329 dw_frame_t scope, xbt_dynar_t result)
331 void *ip = (void *) stack_frame->ip;
332 if (ip < scope->low_pc || ip >= scope->high_pc)
336 dw_variable_t current_variable;
337 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
339 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
343 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
348 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
349 new_var->subprogram = stack_frame->frame;
350 new_var->ip = stack_frame->ip;
351 new_var->name = xbt_strdup(current_variable->name);
352 new_var->type = current_variable->type;
353 new_var->region = region_type;
355 if (current_variable->address != NULL) {
356 new_var->address = current_variable->address;
357 } else if (current_variable->locations.size != 0) {
359 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
360 current_variable->object_info,
361 &(stack_frame->unw_cursor),
362 (void *) stack_frame->frame_base,
365 xbt_die("No address");
368 xbt_dynar_push(result, &new_var);
371 // Recursive processing of nested scopes:
372 dw_frame_t nested_scope = NULL;
373 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
374 mc_fill_local_variables_values(stack_frame, nested_scope, result);
378 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
381 unsigned cursor1 = 0;
382 mc_stack_frame_t stack_frame;
383 xbt_dynar_t variables =
384 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
386 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
387 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
393 static void MC_stack_frame_free_voipd(void *s)
395 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
397 xbt_free(stack_frame->frame_name);
398 xbt_free(stack_frame);
402 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
405 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
409 // TODO, check condition check (unw_init_local==0 means end of frame)
410 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
412 xbt_die("Could not initialize stack unwinding");
417 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
418 xbt_dynar_push(result, &stack_frame);
420 stack_frame->unw_cursor = c;
424 unw_get_reg(&c, UNW_REG_IP, &ip);
425 unw_get_reg(&c, UNW_REG_SP, &sp);
427 stack_frame->ip = ip;
428 stack_frame->sp = sp;
430 // TODO, use real addresses in frame_t instead of fixing it here
432 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
433 stack_frame->frame = frame;
436 stack_frame->frame_name = xbt_strdup(frame->name);
437 stack_frame->frame_base =
438 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
440 stack_frame->frame_base = 0;
441 stack_frame->frame_name = NULL;
444 /* Stop before context switch with maestro */
445 if (frame != NULL && frame->name != NULL
446 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
449 int ret = ret = unw_step(&c);
451 xbt_die("Unexpected end of stack.");
452 } else if (ret < 0) {
453 xbt_die("Error while unwinding stack.");
457 if (xbt_dynar_length(result) == 0) {
458 XBT_INFO("unw_init_local failed");
465 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot, void *heap)
469 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
470 MC_snapshot_stack_free_voidp);
472 unsigned int cursor = 0;
473 stack_region_t current_stack;
475 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
476 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
477 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
478 st->local_variables = MC_get_local_variables_values(st->stack_frames);
480 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
482 xbt_dynar_push(res, &st);
483 (*snapshot)->stack_sizes =
484 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
485 (*snapshot)->stack_sizes[cursor] =
486 (char*) current_stack->address + current_stack->size - (char*) sp;
493 static xbt_dynar_t MC_take_snapshot_ignore()
496 if (mc_heap_comparison_ignore == NULL)
500 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
501 heap_ignore_region_free_voidp);
503 unsigned int cursor = 0;
504 mc_heap_ignore_region_t current_region;
506 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
507 mc_heap_ignore_region_t new_region = NULL;
508 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
509 new_region->address = current_region->address;
510 new_region->size = current_region->size;
511 new_region->block = current_region->block;
512 new_region->fragment = current_region->fragment;
513 xbt_dynar_push(cpy, &new_region);
520 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
521 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
522 free(ignored_data->data);
525 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
527 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
530 unsigned int cursor = 0;
531 mc_checkpoint_ignore_region_t region;
532 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
533 s_mc_snapshot_ignored_data_t ignored_data;
534 ignored_data.start = region->addr;
535 ignored_data.size = region->size;
536 ignored_data.data = malloc(region->size);
537 memcpy(ignored_data.data, region->addr, region->size);
538 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
542 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
543 memset(region->addr, 0, region->size);
548 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
550 unsigned int cursor = 0;
551 s_mc_snapshot_ignored_data_t ignored_data;
552 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
553 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
557 /** @brief Can we remove this snapshot?
559 * Some snapshots cannot be removed (yet) because we need them
564 int mc_important_snapshot(mc_snapshot_t snapshot)
566 // We need this snapshot in order to know which
567 // pages needs to be stored in the next snapshot:
568 if (_sg_mc_sparse_checkpoint && snapshot == mc_model_checker->parent_snapshot)
574 mc_snapshot_t MC_take_snapshot(int num_state)
577 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
578 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
579 smx_process_t process;
580 xbt_swag_foreach(process, simix_global->process_list) {
581 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
584 MC_snapshot_handle_ignore(snapshot);
586 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
587 MC_get_memory_regions(snapshot);
588 if (_sg_mc_sparse_checkpoint) {
589 mc_softdirty_reset();
592 snapshot->to_ignore = MC_take_snapshot_ignore();
594 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
596 MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
597 if (_sg_mc_hash && snapshot->stacks != NULL) {
598 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
606 MC_snapshot_ignore_restore(snapshot);
607 mc_model_checker->parent_snapshot = snapshot;
611 void MC_restore_snapshot(mc_snapshot_t snapshot)
613 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
616 for (i = 0; i < NB_REGIONS; i++) {
617 // For privatized, variables we decided it was not necessary to take the snapshot:
618 if (snapshot->regions[i])
619 MC_region_restore(snapshot->regions[i],
620 parent_snapshot ? parent_snapshot->regions[i] : NULL);
623 if (snapshot->privatization_regions) {
624 for (i = 0; i < SIMIX_process_count(); i++) {
625 if (snapshot->privatization_regions[i]) {
626 MC_region_restore(snapshot->privatization_regions[i],
627 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
630 switch_data_segment(snapshot->privatization_index);
633 MC_snapshot_ignore_restore(snapshot);
634 if (_sg_mc_sparse_checkpoint) {
635 mc_softdirty_reset();
637 mc_model_checker->parent_snapshot = snapshot;
640 void *mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
643 // If not in a process state/clone:
645 return (uintptr_t *) addr;
647 // If it is in a snapshot:
648 for (size_t i = 0; i != NB_REGIONS; ++i) {
649 mc_mem_region_t region = snapshot->regions[i];
650 uintptr_t start = (uintptr_t) region->start_addr;
651 uintptr_t end = start + region->size;
653 // The address is in this region:
654 if (addr >= start && addr < end) {
655 uintptr_t offset = addr - start;
656 return (void *) ((uintptr_t) region->data + offset);
661 // It is not in a snapshot:
662 return (void *) addr;
665 uintptr_t mc_untranslate_address(void *addr, mc_snapshot_t snapshot)
668 return (uintptr_t) addr;
671 for (size_t i = 0; i != NB_REGIONS; ++i) {
672 mc_mem_region_t region = snapshot->regions[i];
673 if (addr >= region->data
674 && addr <= (void *) (((char *) region->data) + region->size)) {
675 size_t offset = (size_t) ((char *) addr - (char *) region->data);
676 return ((uintptr_t) region->start_addr) + offset;
680 return (uintptr_t) addr;
683 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
685 return MC_take_snapshot(1);
688 void *MC_snapshot(void)
690 return simcall_mc_snapshot();