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);
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(int type, void *start_addr, size_t size, mc_mem_region_t ref_reg)
100 if (_sg_mc_sparse_checkpoint) {
101 return mc_region_new_sparse(type, start_addr, size, ref_reg);
104 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
105 new_reg->start_addr = start_addr;
106 new_reg->data = NULL;
107 new_reg->size = size;
108 new_reg->page_numbers = NULL;
109 new_reg->data = xbt_malloc(size);
110 memcpy(new_reg->data, start_addr, size);
111 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
112 type, new_reg->data, start_addr, size);
117 /** @brief Restore a region from a snapshot
119 * If we are using per page snapshots, it is possible to use the reference
120 * region in order to do an incremental restoration of the region: the
121 * softclean pages which are shared between the two snapshots do not need
124 * @param reg Target region
125 * @param reg_reg Current region (if not NULL), used for lazy per page restoration
127 static void MC_region_restore(mc_mem_region_t reg, mc_mem_region_t ref_reg)
129 /*FIXME: check if start_addr is still mapped, if it is not, then map it
130 before copying the data */
131 if (!reg->page_numbers) {
132 memcpy(reg->start_addr, reg->data, reg->size);
134 mc_region_restore_sparse(reg, ref_reg);
139 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
140 void *start_addr, size_t size)
143 mc_mem_region_t ref_reg =
144 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->regions[type] : NULL;
145 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size, ref_reg);
146 snapshot->regions[type] = new_reg;
150 static void MC_get_memory_regions(mc_snapshot_t snapshot)
154 void *start_heap = ((xbt_mheap_t) std_heap)->base;
155 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
156 MC_snapshot_add_region(snapshot, 0, start_heap,
157 (char *) end_heap - (char *) start_heap);
158 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
160 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
161 mc_libsimgrid_info->end_rw -
162 mc_libsimgrid_info->start_rw);
163 if (!smpi_privatize_global_variables) {
164 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
165 mc_binary_info->end_rw - mc_binary_info->start_rw);
166 snapshot->privatization_regions = NULL;
167 snapshot->privatization_index = -1;
169 snapshot->privatization_regions =
170 xbt_new(mc_mem_region_t, SIMIX_process_count());
171 for (i = 0; i < SIMIX_process_count(); i++) {
172 mc_mem_region_t ref_reg =
173 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
174 snapshot->privatization_regions[i] =
175 MC_region_new(-1, mappings[i], size_data_exe, ref_reg);
177 snapshot->privatization_index = loaded_page;
181 /** @brief Finds the range of the different memory segments and binary paths */
182 void MC_init_memory_map_info()
187 memory_map_t maps = MC_get_memory_map();
189 maestro_stack_start = NULL;
190 maestro_stack_end = NULL;
191 libsimgrid_path = NULL;
193 while (i < maps->mapsize) {
194 reg = maps->regions[i];
195 if (maps->regions[i].pathname == NULL) {
197 } else if ((reg.prot & PROT_WRITE)
198 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
199 maestro_stack_start = reg.start_addr;
200 maestro_stack_end = reg.end_addr;
201 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
202 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
204 if (libsimgrid_path == NULL)
205 libsimgrid_path = strdup(maps->regions[i].pathname);
210 xbt_assert(maestro_stack_start, "maestro_stack_start");
211 xbt_assert(maestro_stack_end, "maestro_stack_end");
212 xbt_assert(libsimgrid_path, "libsimgrid_path&");
214 MC_free_memory_map(maps);
218 /** \brief Fill/lookup the "subtype" field.
220 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
223 if (type->dw_type_id == NULL)
225 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
226 if (type->subtype == NULL)
228 if (type->subtype->byte_size != 0)
230 if (type->subtype->name == NULL)
232 // Try to find a more complete description of the type:
233 // We need to fix in order to support C++.
236 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
237 if (subtype != NULL) {
238 type->subtype = subtype;
243 void MC_post_process_types(mc_object_info_t info)
245 xbt_dict_cursor_t cursor = NULL;
249 // Lookup "subtype" field:
250 xbt_dict_foreach(info->types, cursor, origin, type) {
251 MC_resolve_subtype(info, type);
255 if (type->members != NULL)
256 xbt_dynar_foreach(type->members, i, member) {
257 MC_resolve_subtype(info, member);
262 /** \brief Fills the position of the segments (executable, read-only, read/write).
264 * TODO, use dl_iterate_phdr to be more robust
266 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
271 const char *name = basename(result->file_name);
272 while (i < maps->mapsize) {
273 reg = maps->regions[i];
274 if (maps->regions[i].pathname == NULL
275 || strcmp(basename(maps->regions[i].pathname), name)) {
277 } else if ((reg.prot & PROT_WRITE)) {
278 xbt_assert(!result->start_rw,
279 "Multiple read-write segments for %s, not supported",
280 maps->regions[i].pathname);
281 result->start_rw = reg.start_addr;
282 result->end_rw = reg.end_addr;
283 // .bss is usually after the .data:
284 s_map_region_t *next = &(maps->regions[i + 1]);
285 if (next->pathname == NULL && (next->prot & PROT_WRITE)
286 && next->start_addr == reg.end_addr) {
287 result->end_rw = maps->regions[i + 1].end_addr;
289 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
290 xbt_assert(!result->start_exec,
291 "Multiple executable segments for %s, not supported",
292 maps->regions[i].pathname);
293 result->start_exec = reg.start_addr;
294 result->end_exec = reg.end_addr;
295 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
296 xbt_assert(!result->start_ro,
297 "Multiple read only segments for %s, not supported",
298 maps->regions[i].pathname);
299 result->start_ro = reg.start_addr;
300 result->end_ro = reg.end_addr;
305 xbt_assert(result->file_name);
306 xbt_assert(result->start_rw);
307 xbt_assert(result->start_exec);
310 /************************************* Take Snapshot ************************************/
311 /****************************************************************************************/
313 /** \brief Checks whether the variable is in scope for a given IP.
315 * A variable may be defined only from a given value of IP.
317 * \param var Variable description
318 * \param frame Scope description
319 * \param ip Instruction pointer
320 * \return true if the variable is valid
322 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
325 // The variable is not yet valid:
326 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
332 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
333 dw_frame_t scope, xbt_dynar_t result)
335 void *ip = (void *) stack_frame->ip;
336 if (ip < scope->low_pc || ip >= scope->high_pc)
340 dw_variable_t current_variable;
341 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
343 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
347 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
352 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
353 new_var->subprogram = stack_frame->frame;
354 new_var->ip = stack_frame->ip;
355 new_var->name = xbt_strdup(current_variable->name);
356 new_var->type = current_variable->type;
357 new_var->region = region_type;
359 if (current_variable->address != NULL) {
360 new_var->address = current_variable->address;
361 } else if (current_variable->locations.size != 0) {
363 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
364 current_variable->object_info,
365 &(stack_frame->unw_cursor),
366 (void *) stack_frame->frame_base,
369 xbt_die("No address");
372 xbt_dynar_push(result, &new_var);
375 // Recursive processing of nested scopes:
376 dw_frame_t nested_scope = NULL;
377 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
378 mc_fill_local_variables_values(stack_frame, nested_scope, result);
382 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
385 unsigned cursor1 = 0;
386 mc_stack_frame_t stack_frame;
387 xbt_dynar_t variables =
388 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
390 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
391 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
397 static void MC_stack_frame_free_voipd(void *s)
399 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
401 xbt_free(stack_frame->frame_name);
402 xbt_free(stack_frame);
406 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
409 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
413 // TODO, check condition check (unw_init_local==0 means end of frame)
414 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
416 xbt_die("Could not initialize stack unwinding");
421 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
422 xbt_dynar_push(result, &stack_frame);
424 stack_frame->unw_cursor = c;
428 unw_get_reg(&c, UNW_REG_IP, &ip);
429 unw_get_reg(&c, UNW_REG_SP, &sp);
431 stack_frame->ip = ip;
432 stack_frame->sp = sp;
434 // TODO, use real addresses in frame_t instead of fixing it here
436 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
437 stack_frame->frame = frame;
440 stack_frame->frame_name = xbt_strdup(frame->name);
441 stack_frame->frame_base =
442 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
444 stack_frame->frame_base = 0;
445 stack_frame->frame_name = NULL;
448 /* Stop before context switch with maestro */
449 if (frame != NULL && frame->name != NULL
450 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
453 int ret = ret = unw_step(&c);
455 xbt_die("Unexpected end of stack.");
456 } else if (ret < 0) {
457 xbt_die("Error while unwinding stack.");
461 if (xbt_dynar_length(result) == 0) {
462 XBT_INFO("unw_init_local failed");
469 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
473 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
474 MC_snapshot_stack_free_voidp);
476 unsigned int cursor = 0;
477 stack_region_t current_stack;
479 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
480 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
481 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
482 st->local_variables = MC_get_local_variables_values(st->stack_frames);
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 if (_sg_mc_sparse_checkpoint && snapshot == mc_model_checker->parent_snapshot)
578 mc_snapshot_t MC_take_snapshot(int num_state)
581 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
582 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
583 smx_process_t process;
584 xbt_swag_foreach(process, simix_global->process_list) {
585 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
588 MC_snapshot_handle_ignore(snapshot);
590 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
591 MC_get_memory_regions(snapshot);
592 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
593 mc_softdirty_reset();
596 snapshot->to_ignore = MC_take_snapshot_ignore();
598 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
600 MC_take_snapshot_stacks(&snapshot);
601 if (_sg_mc_hash && snapshot->stacks != NULL) {
602 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
610 MC_snapshot_ignore_restore(snapshot);
611 mc_model_checker->parent_snapshot = snapshot;
615 void MC_restore_snapshot(mc_snapshot_t snapshot)
617 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
620 for (i = 0; i < NB_REGIONS; i++) {
621 // For privatized, variables we decided it was not necessary to take the snapshot:
622 if (snapshot->regions[i])
623 MC_region_restore(snapshot->regions[i],
624 parent_snapshot ? parent_snapshot->regions[i] : NULL);
627 if (snapshot->privatization_regions) {
628 for (i = 0; i < SIMIX_process_count(); i++) {
629 if (snapshot->privatization_regions[i]) {
630 MC_region_restore(snapshot->privatization_regions[i],
631 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
634 switch_data_segment(snapshot->privatization_index);
637 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
638 mc_softdirty_reset();
641 MC_snapshot_ignore_restore(snapshot);
642 mc_model_checker->parent_snapshot = snapshot;
645 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
647 return MC_take_snapshot(1);
650 void *MC_snapshot(void)
652 return simcall_mc_snapshot();