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"
17 #include "xbt/mmalloc/mmprivate.h"
19 #include "../simix/smx_private.h"
21 #include <libunwind.h>
24 #include "mc_private.h"
26 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
27 "Logging specific to mc_checkpoint");
29 char *libsimgrid_path;
31 /************************************ Free functions **************************************/
32 /*****************************************************************************************/
34 static void MC_snapshot_stack_free(mc_snapshot_stack_t s)
37 xbt_dynar_free(&(s->local_variables));
38 xbt_dynar_free(&(s->stack_frames));
43 static void MC_snapshot_stack_free_voidp(void *s)
45 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
48 static void local_variable_free(local_variable_t v)
54 static void local_variable_free_voidp(void *v)
56 local_variable_free((local_variable_t) * (void **) v);
59 static void MC_region_destroy(mc_mem_region_t reg)
61 //munmap(reg->data, reg->size);
66 void MC_free_snapshot(mc_snapshot_t snapshot)
69 for (i = 0; i < NB_REGIONS; i++)
70 MC_region_destroy(snapshot->regions[i]);
72 xbt_free(snapshot->stack_sizes);
73 xbt_dynar_free(&(snapshot->stacks));
74 xbt_dynar_free(&(snapshot->to_ignore));
75 xbt_dynar_free(&snapshot->ignored_data);
77 if (snapshot->privatization_regions) {
78 size_t n = xbt_dynar_length(snapshot->enabled_processes);
79 for (i = 0; i != n; ++i) {
80 MC_region_destroy(snapshot->privatization_regions[i]);
82 xbt_free(snapshot->privatization_regions);
89 /******************************* Snapshot regions ********************************/
90 /*********************************************************************************/
92 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
94 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
95 new_reg->start_addr = start_addr;
97 //new_reg->data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
98 //if(new_reg->data==MAP_FAILED)
99 //xbt_die("Could not mmap new memory for snapshot.");
100 new_reg->data = xbt_malloc(size);
101 memcpy(new_reg->data, start_addr, size);
102 //madvise(new_reg->data, size, MADV_MERGEABLE);
104 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
105 type, new_reg->data, start_addr, size);
110 static void MC_region_restore(mc_mem_region_t reg)
112 /*FIXME: check if start_addr is still mapped, if it is not, then map it
113 before copying the data */
115 memcpy(reg->start_addr, reg->data, reg->size);
119 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
120 void *start_addr, size_t size)
122 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
123 snapshot->regions[type] = new_reg;
127 static void MC_get_memory_regions(mc_snapshot_t snapshot)
131 void *start_heap = ((xbt_mheap_t) std_heap)->base;
132 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
133 MC_snapshot_add_region(snapshot, 0, start_heap,
134 (char *) end_heap - (char *) start_heap);
135 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
137 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
138 mc_libsimgrid_info->end_rw -
139 mc_libsimgrid_info->start_rw);
140 if (!smpi_privatize_global_variables) {
141 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
142 mc_binary_info->end_rw - mc_binary_info->start_rw);
143 snapshot->privatization_regions = NULL;
144 snapshot->privatization_index = -1;
146 snapshot->privatization_regions =
147 xbt_new(mc_mem_region_t, SIMIX_process_count());
148 for (i = 0; i < SIMIX_process_count(); i++) {
149 snapshot->privatization_regions[i] =
150 MC_region_new(-1, mappings[i], size_data_exe);
152 snapshot->privatization_index = loaded_page;
156 /** @brief Finds the range of the different memory segments and binary paths */
157 void MC_init_memory_map_info()
162 memory_map_t maps = MC_get_memory_map();
164 maestro_stack_start = NULL;
165 maestro_stack_end = NULL;
166 libsimgrid_path = NULL;
168 while (i < maps->mapsize) {
169 reg = maps->regions[i];
170 if (maps->regions[i].pathname == NULL) {
172 } else if ((reg.prot & PROT_WRITE)
173 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
174 maestro_stack_start = reg.start_addr;
175 maestro_stack_end = reg.end_addr;
176 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
177 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
179 if (libsimgrid_path == NULL)
180 libsimgrid_path = strdup(maps->regions[i].pathname);
185 xbt_assert(maestro_stack_start, "maestro_stack_start");
186 xbt_assert(maestro_stack_end, "maestro_stack_end");
187 xbt_assert(libsimgrid_path, "libsimgrid_path&");
189 MC_free_memory_map(maps);
193 /** \brief Fill/lookup the "subtype" field.
195 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
198 if (type->dw_type_id == NULL)
200 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
201 if (type->subtype == NULL)
203 if (type->subtype->byte_size != 0)
205 if (type->subtype->name == NULL)
207 // Try to find a more complete description of the type:
208 // We need to fix in order to support C++.
211 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
212 if (subtype != NULL) {
213 type->subtype = subtype;
218 void MC_post_process_types(mc_object_info_t info)
220 xbt_dict_cursor_t cursor = NULL;
224 // Lookup "subtype" field:
225 xbt_dict_foreach(info->types, cursor, origin, type) {
226 MC_resolve_subtype(info, type);
230 if (type->members != NULL)
231 xbt_dynar_foreach(type->members, i, member) {
232 MC_resolve_subtype(info, member);
237 /** \brief Fills the position of the segments (executable, read-only, read/write).
239 * TODO, use dl_iterate_phdr to be more robust
241 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
246 const char *name = basename(result->file_name);
247 while (i < maps->mapsize) {
248 reg = maps->regions[i];
249 if (maps->regions[i].pathname == NULL
250 || strcmp(basename(maps->regions[i].pathname), name)) {
252 } else if ((reg.prot & PROT_WRITE)) {
253 xbt_assert(!result->start_rw,
254 "Multiple read-write segments for %s, not supported",
255 maps->regions[i].pathname);
256 result->start_rw = reg.start_addr;
257 result->end_rw = reg.end_addr;
258 // .bss is usually after the .data:
259 s_map_region_t *next = &(maps->regions[i + 1]);
260 if (next->pathname == NULL && (next->prot & PROT_WRITE)
261 && next->start_addr == reg.end_addr) {
262 result->end_rw = maps->regions[i + 1].end_addr;
264 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
265 xbt_assert(!result->start_exec,
266 "Multiple executable segments for %s, not supported",
267 maps->regions[i].pathname);
268 result->start_exec = reg.start_addr;
269 result->end_exec = reg.end_addr;
270 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
271 xbt_assert(!result->start_ro,
272 "Multiple read only segments for %s, not supported",
273 maps->regions[i].pathname);
274 result->start_ro = reg.start_addr;
275 result->end_ro = reg.end_addr;
280 xbt_assert(result->file_name);
281 xbt_assert(result->start_rw);
282 xbt_assert(result->start_exec);
285 /************************************* Take Snapshot ************************************/
286 /****************************************************************************************/
288 /** \brief Checks whether the variable is in scope for a given IP.
290 * A variable may be defined only from a given value of IP.
292 * \param var Variable description
293 * \param frame Scope description
294 * \param ip Instruction pointer
295 * \return true if the variable is valid
297 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
300 // The variable is not yet valid:
301 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
307 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
308 dw_frame_t scope, xbt_dynar_t result)
310 void *ip = (void *) stack_frame->ip;
311 if (ip < scope->low_pc || ip >= scope->high_pc)
315 dw_variable_t current_variable;
316 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
318 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
322 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
327 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
328 new_var->subprogram = stack_frame->frame;
329 new_var->ip = stack_frame->ip;
330 new_var->name = xbt_strdup(current_variable->name);
331 new_var->type = current_variable->type;
332 new_var->region = region_type;
334 if (current_variable->address != NULL) {
335 new_var->address = current_variable->address;
336 } else if (current_variable->locations.size != 0) {
338 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
339 current_variable->object_info,
340 &(stack_frame->unw_cursor),
341 (void *) stack_frame->frame_base,
344 xbt_die("No address");
347 xbt_dynar_push(result, &new_var);
350 // Recursive processing of nested scopes:
351 dw_frame_t nested_scope = NULL;
352 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
353 mc_fill_local_variables_values(stack_frame, nested_scope, result);
357 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
360 unsigned cursor1 = 0;
361 mc_stack_frame_t stack_frame;
362 xbt_dynar_t variables =
363 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
365 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
366 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
372 static void MC_stack_frame_free_voipd(void *s)
374 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
376 xbt_free(stack_frame->frame_name);
377 xbt_free(stack_frame);
381 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
384 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
388 // TODO, check condition check (unw_init_local==0 means end of frame)
389 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
391 xbt_die("Could not initialize stack unwinding");
396 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
397 xbt_dynar_push(result, &stack_frame);
399 stack_frame->unw_cursor = c;
403 unw_get_reg(&c, UNW_REG_IP, &ip);
404 unw_get_reg(&c, UNW_REG_SP, &sp);
406 stack_frame->ip = ip;
407 stack_frame->sp = sp;
409 // TODO, use real addresses in frame_t instead of fixing it here
411 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
412 stack_frame->frame = frame;
415 stack_frame->frame_name = xbt_strdup(frame->name);
416 stack_frame->frame_base =
417 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
419 stack_frame->frame_base = 0;
420 stack_frame->frame_name = NULL;
423 /* Stop before context switch with maestro */
424 if (frame != NULL && frame->name != NULL
425 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
428 int ret = ret = unw_step(&c);
430 xbt_die("Unexpected end of stack.");
431 } else if (ret < 0) {
432 xbt_die("Error while unwinding stack.");
436 if (xbt_dynar_length(result) == 0) {
437 XBT_INFO("unw_init_local failed");
444 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot, void *heap)
448 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
449 MC_snapshot_stack_free_voidp);
451 unsigned int cursor = 0;
452 stack_region_t current_stack;
454 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
455 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
456 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
457 st->local_variables = MC_get_local_variables_values(st->stack_frames);
459 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
461 ((char *) heap + (size_t) (((char *) ((long) sp) - (char *) std_heap)));
463 st->real_address = current_stack->address;
464 xbt_dynar_push(res, &st);
465 (*snapshot)->stack_sizes =
466 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
467 (*snapshot)->stack_sizes[cursor] =
468 current_stack->size - ((char *) st->stack_pointer -
469 (char *) ((char *) heap +
470 ((char *) current_stack->address -
471 (char *) std_heap)));
478 static xbt_dynar_t MC_take_snapshot_ignore()
481 if (mc_heap_comparison_ignore == NULL)
485 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
486 heap_ignore_region_free_voidp);
488 unsigned int cursor = 0;
489 mc_heap_ignore_region_t current_region;
491 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
492 mc_heap_ignore_region_t new_region = NULL;
493 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
494 new_region->address = current_region->address;
495 new_region->size = current_region->size;
496 new_region->block = current_region->block;
497 new_region->fragment = current_region->fragment;
498 xbt_dynar_push(cpy, &new_region);
505 static void mc_free_snapshot_ignored_data_pvoid(void* data) {
506 mc_snapshot_ignored_data_t ignored_data = (mc_snapshot_ignored_data_t) data;
507 free(ignored_data->data);
510 static void MC_snapshot_handle_ignore(mc_snapshot_t snapshot)
512 snapshot->ignored_data = xbt_dynar_new(sizeof(s_mc_snapshot_ignored_data_t), mc_free_snapshot_ignored_data_pvoid);
515 unsigned int cursor = 0;
516 mc_checkpoint_ignore_region_t region;
517 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
518 s_mc_snapshot_ignored_data_t ignored_data;
519 ignored_data.start = region->addr;
520 ignored_data.size = region->size;
521 ignored_data.data = malloc(region->size);
522 memcpy(ignored_data.data, region->addr, region->size);
523 xbt_dynar_push(snapshot->ignored_data, &ignored_data);
527 xbt_dynar_foreach (mc_checkpoint_ignore, cursor, region) {
528 memset(region->addr, 0, region->size);
533 static void MC_snapshot_ignore_restore(mc_snapshot_t snapshot)
535 unsigned int cursor = 0;
536 s_mc_snapshot_ignored_data_t ignored_data;
537 xbt_dynar_foreach (snapshot->ignored_data, cursor, ignored_data) {
538 memcpy(ignored_data.start, ignored_data.data, ignored_data.size);
542 mc_snapshot_t MC_take_snapshot(int num_state)
545 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
546 snapshot->enabled_processes = xbt_dynar_new(sizeof(int), NULL);
547 smx_process_t process;
548 xbt_swag_foreach(process, simix_global->process_list) {
549 xbt_dynar_push_as(snapshot->enabled_processes, int, (int)process->pid);
552 MC_snapshot_handle_ignore(snapshot);
554 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
555 MC_get_memory_regions(snapshot);
557 snapshot->to_ignore = MC_take_snapshot_ignore();
559 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
561 MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
562 if (_sg_mc_hash && snapshot->stacks != NULL) {
563 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
571 // mprotect the region after zero-ing ignored parts:
573 for(i=0; i!=NB_REGIONS; ++i) {
574 mc_mem_region_t region = snapshot->regions[i];
575 mprotect(region->data, region->size, PROT_READ);
578 MC_snapshot_ignore_restore(snapshot);
584 void MC_restore_snapshot(mc_snapshot_t snapshot)
587 for (i = 0; i < NB_REGIONS; i++) {
588 // For privatized, variables we decided it was not necessary to take the snapshot:
589 if (snapshot->regions[i])
590 MC_region_restore(snapshot->regions[i]);
593 if (snapshot->privatization_regions) {
594 for (i = 0; i < SIMIX_process_count(); i++) {
595 if (snapshot->privatization_regions[i]) {
596 MC_region_restore(snapshot->privatization_regions[i]);
599 switch_data_segment(snapshot->privatization_index);
602 MC_snapshot_ignore_restore(snapshot);
605 void *mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
608 // If not in a process state/clone:
610 return (uintptr_t *) addr;
612 // If it is in a snapshot:
613 for (size_t i = 0; i != NB_REGIONS; ++i) {
614 mc_mem_region_t region = snapshot->regions[i];
615 uintptr_t start = (uintptr_t) region->start_addr;
616 uintptr_t end = start + region->size;
618 // The address is in this region:
619 if (addr >= start && addr < end) {
620 uintptr_t offset = addr - start;
621 return (void *) ((uintptr_t) region->data + offset);
626 // It is not in a snapshot:
627 return (void *) addr;
630 uintptr_t mc_untranslate_address(void *addr, mc_snapshot_t snapshot)
633 return (uintptr_t) addr;
636 for (size_t i = 0; i != NB_REGIONS; ++i) {
637 mc_mem_region_t region = snapshot->regions[i];
638 if (addr >= region->data
639 && addr <= (void *) (((char *) region->data) + region->size)) {
640 size_t offset = (size_t) ((char *) addr - (char *) region->data);
641 return ((uintptr_t) region->start_addr) + offset;
645 return (uintptr_t) addr;
648 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
650 return MC_take_snapshot(1);
653 void *MC_snapshot(void)
655 return simcall_mc_snapshot();