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 mc_mem_region_t ref_reg =
170 mc_model_checker->parent_snapshot ? mc_model_checker->parent_snapshot->privatization_regions[i] : NULL;
171 snapshot->privatization_regions[i] =
172 MC_region_new(-1, mappings[i], size_data_exe, ref_reg);
174 snapshot->privatization_index = loaded_page;
178 /** @brief Finds the range of the different memory segments and binary paths */
179 void MC_init_memory_map_info()
184 memory_map_t maps = MC_get_memory_map();
186 maestro_stack_start = NULL;
187 maestro_stack_end = NULL;
188 libsimgrid_path = NULL;
190 while (i < maps->mapsize) {
191 reg = maps->regions[i];
192 if (maps->regions[i].pathname == NULL) {
194 } else if ((reg.prot & PROT_WRITE)
195 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
196 maestro_stack_start = reg.start_addr;
197 maestro_stack_end = reg.end_addr;
198 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
199 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
201 if (libsimgrid_path == NULL)
202 libsimgrid_path = strdup(maps->regions[i].pathname);
207 xbt_assert(maestro_stack_start, "maestro_stack_start");
208 xbt_assert(maestro_stack_end, "maestro_stack_end");
209 xbt_assert(libsimgrid_path, "libsimgrid_path&");
211 MC_free_memory_map(maps);
215 /** \brief Fill/lookup the "subtype" field.
217 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
220 if (type->dw_type_id == NULL)
222 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
223 if (type->subtype == NULL)
225 if (type->subtype->byte_size != 0)
227 if (type->subtype->name == NULL)
229 // Try to find a more complete description of the type:
230 // We need to fix in order to support C++.
233 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
234 if (subtype != NULL) {
235 type->subtype = subtype;
240 void MC_post_process_types(mc_object_info_t info)
242 xbt_dict_cursor_t cursor = NULL;
246 // Lookup "subtype" field:
247 xbt_dict_foreach(info->types, cursor, origin, type) {
248 MC_resolve_subtype(info, type);
252 if (type->members != NULL)
253 xbt_dynar_foreach(type->members, i, member) {
254 MC_resolve_subtype(info, member);
259 /** \brief Fills the position of the segments (executable, read-only, read/write).
261 * TODO, use dl_iterate_phdr to be more robust
263 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
268 const char *name = basename(result->file_name);
269 while (i < maps->mapsize) {
270 reg = maps->regions[i];
271 if (maps->regions[i].pathname == NULL
272 || strcmp(basename(maps->regions[i].pathname), name)) {
274 } else if ((reg.prot & PROT_WRITE)) {
275 xbt_assert(!result->start_rw,
276 "Multiple read-write segments for %s, not supported",
277 maps->regions[i].pathname);
278 result->start_rw = reg.start_addr;
279 result->end_rw = reg.end_addr;
280 // .bss is usually after the .data:
281 s_map_region_t *next = &(maps->regions[i + 1]);
282 if (next->pathname == NULL && (next->prot & PROT_WRITE)
283 && next->start_addr == reg.end_addr) {
284 result->end_rw = maps->regions[i + 1].end_addr;
286 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
287 xbt_assert(!result->start_exec,
288 "Multiple executable segments for %s, not supported",
289 maps->regions[i].pathname);
290 result->start_exec = reg.start_addr;
291 result->end_exec = reg.end_addr;
292 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
293 xbt_assert(!result->start_ro,
294 "Multiple read only segments for %s, not supported",
295 maps->regions[i].pathname);
296 result->start_ro = reg.start_addr;
297 result->end_ro = reg.end_addr;
302 xbt_assert(result->file_name);
303 xbt_assert(result->start_rw);
304 xbt_assert(result->start_exec);
307 /************************************* Take Snapshot ************************************/
308 /****************************************************************************************/
310 /** \brief Checks whether the variable is in scope for a given IP.
312 * A variable may be defined only from a given value of IP.
314 * \param var Variable description
315 * \param frame Scope description
316 * \param ip Instruction pointer
317 * \return true if the variable is valid
319 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
322 // The variable is not yet valid:
323 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
329 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
330 dw_frame_t scope, xbt_dynar_t result)
332 void *ip = (void *) stack_frame->ip;
333 if (ip < scope->low_pc || ip >= scope->high_pc)
337 dw_variable_t current_variable;
338 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
340 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
344 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
349 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
350 new_var->subprogram = stack_frame->frame;
351 new_var->ip = stack_frame->ip;
352 new_var->name = xbt_strdup(current_variable->name);
353 new_var->type = current_variable->type;
354 new_var->region = region_type;
356 if (current_variable->address != NULL) {
357 new_var->address = current_variable->address;
358 } else if (current_variable->locations.size != 0) {
360 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
361 current_variable->object_info,
362 &(stack_frame->unw_cursor),
363 (void *) stack_frame->frame_base,
366 xbt_die("No address");
369 xbt_dynar_push(result, &new_var);
372 // Recursive processing of nested scopes:
373 dw_frame_t nested_scope = NULL;
374 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
375 mc_fill_local_variables_values(stack_frame, nested_scope, result);
379 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
382 unsigned cursor1 = 0;
383 mc_stack_frame_t stack_frame;
384 xbt_dynar_t variables =
385 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
387 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
388 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
394 static void MC_stack_frame_free_voipd(void *s)
396 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
398 xbt_free(stack_frame->frame_name);
399 xbt_free(stack_frame);
403 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
406 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
410 // TODO, check condition check (unw_init_local==0 means end of frame)
411 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
413 xbt_die("Could not initialize stack unwinding");
418 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
419 xbt_dynar_push(result, &stack_frame);
421 stack_frame->unw_cursor = c;
425 unw_get_reg(&c, UNW_REG_IP, &ip);
426 unw_get_reg(&c, UNW_REG_SP, &sp);
428 stack_frame->ip = ip;
429 stack_frame->sp = sp;
431 // TODO, use real addresses in frame_t instead of fixing it here
433 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
434 stack_frame->frame = frame;
437 stack_frame->frame_name = xbt_strdup(frame->name);
438 stack_frame->frame_base =
439 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
441 stack_frame->frame_base = 0;
442 stack_frame->frame_name = NULL;
445 /* Stop before context switch with maestro */
446 if (frame != NULL && frame->name != NULL
447 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
450 int ret = ret = unw_step(&c);
452 xbt_die("Unexpected end of stack.");
453 } else if (ret < 0) {
454 xbt_die("Error while unwinding stack.");
458 if (xbt_dynar_length(result) == 0) {
459 XBT_INFO("unw_init_local failed");
466 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot)
470 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
471 MC_snapshot_stack_free_voidp);
473 unsigned int cursor = 0;
474 stack_region_t current_stack;
476 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
477 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
478 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
479 st->local_variables = MC_get_local_variables_values(st->stack_frames);
481 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
483 xbt_dynar_push(res, &st);
484 (*snapshot)->stack_sizes =
485 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
486 (*snapshot)->stack_sizes[cursor] =
487 (char*) current_stack->address + current_stack->size - (char*) sp;
494 static xbt_dynar_t MC_take_snapshot_ignore()
497 if (mc_heap_comparison_ignore == NULL)
501 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
502 heap_ignore_region_free_voidp);
504 unsigned int cursor = 0;
505 mc_heap_ignore_region_t current_region;
507 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
508 mc_heap_ignore_region_t new_region = NULL;
509 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
510 new_region->address = current_region->address;
511 new_region->size = current_region->size;
512 new_region->block = current_region->block;
513 new_region->fragment = current_region->fragment;
514 xbt_dynar_push(cpy, &new_region);
521 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
522 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
523 free(ignored_data->data);
526 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
528 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
531 unsigned int cursor = 0;
532 mc_checkpoint_ignore_region_t region;
533 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
534 s_mc_snapshot_ignored_data_t ignored_data;
535 ignored_data.start = region->addr;
536 ignored_data.size = region->size;
537 ignored_data.data = malloc(region->size);
538 memcpy(ignored_data.data, region->addr, region->size);
539 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
543 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
544 memset(region->addr, 0, region->size);
549 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
551 unsigned int cursor = 0;
552 s_mc_snapshot_ignored_data_t ignored_data;
553 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
554 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
558 /** @brief Can we remove this snapshot?
560 * Some snapshots cannot be removed (yet) because we need them
565 int mc_important_snapshot(mc_snapshot_t snapshot)
567 // We need this snapshot in order to know which
568 // pages needs to be stored in the next snapshot:
569 if (_sg_mc_sparse_checkpoint && snapshot == mc_model_checker->parent_snapshot)
575 mc_snapshot_t MC_take_snapshot(int num_state)
578 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
579 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
580 smx_process_t process;
581 xbt_swag_foreach(process, simix_global->process_list) {
582 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
585 MC_snapshot_handle_ignore(snapshot);
587 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
588 MC_get_memory_regions(snapshot);
589 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
590 mc_softdirty_reset();
593 snapshot->to_ignore = MC_take_snapshot_ignore();
595 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
597 MC_take_snapshot_stacks(&snapshot);
598 if (_sg_mc_hash && snapshot->stacks != NULL) {
599 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
607 MC_snapshot_ignore_restore(snapshot);
608 mc_model_checker->parent_snapshot = snapshot;
612 void MC_restore_snapshot(mc_snapshot_t snapshot)
614 mc_snapshot_t parent_snapshot = mc_model_checker->parent_snapshot;
617 for (i = 0; i < NB_REGIONS; i++) {
618 // For privatized, variables we decided it was not necessary to take the snapshot:
619 if (snapshot->regions[i])
620 MC_region_restore(snapshot->regions[i],
621 parent_snapshot ? parent_snapshot->regions[i] : NULL);
624 if (snapshot->privatization_regions) {
625 for (i = 0; i < SIMIX_process_count(); i++) {
626 if (snapshot->privatization_regions[i]) {
627 MC_region_restore(snapshot->privatization_regions[i],
628 parent_snapshot ? parent_snapshot->privatization_regions[i] : NULL);
631 switch_data_segment(snapshot->privatization_index);
634 if (_sg_mc_sparse_checkpoint && _sg_mc_soft_dirty) {
635 mc_softdirty_reset();
638 MC_snapshot_ignore_restore(snapshot);
639 mc_model_checker->parent_snapshot = snapshot;
642 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
644 return MC_take_snapshot(1);
647 void *MC_snapshot(void)
649 return simcall_mc_snapshot();