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));
76 if (snapshot->privatization_regions) {
77 size_t n = snapshot->nb_processes;
78 for (i = 0; i != n; ++i) {
79 MC_region_destroy(snapshot->privatization_regions[i]);
81 xbt_free(snapshot->privatization_regions);
88 /******************************* Snapshot regions ********************************/
89 /*********************************************************************************/
91 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
93 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
94 new_reg->start_addr = start_addr;
96 //new_reg->data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
97 //if(new_reg->data==MAP_FAILED)
98 //xbt_die("Could not mmap new memory for snapshot.");
99 new_reg->data = xbt_malloc(size);
100 memcpy(new_reg->data, start_addr, size);
101 //madvise(new_reg->data, size, MADV_MERGEABLE);
103 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu",
104 type, new_reg->data, start_addr, size);
109 static void MC_region_restore(mc_mem_region_t reg)
111 /*FIXME: check if start_addr is still mapped, if it is not, then map it
112 before copying the data */
114 memcpy(reg->start_addr, reg->data, reg->size);
118 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type,
119 void *start_addr, size_t size)
121 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
122 snapshot->regions[type] = new_reg;
126 static void MC_get_memory_regions(mc_snapshot_t snapshot)
130 void *start_heap = ((xbt_mheap_t) std_heap)->base;
131 void *end_heap = ((xbt_mheap_t) std_heap)->breakval;
132 MC_snapshot_add_region(snapshot, 0, start_heap,
133 (char *) end_heap - (char *) start_heap);
134 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
136 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw,
137 mc_libsimgrid_info->end_rw -
138 mc_libsimgrid_info->start_rw);
139 if (!smpi_privatize_global_variables) {
140 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw,
141 mc_binary_info->end_rw - mc_binary_info->start_rw);
142 snapshot->privatization_regions = NULL;
143 snapshot->privatization_index = -1;
145 snapshot->privatization_regions =
146 xbt_new(mc_mem_region_t, SIMIX_process_count());
147 for (i = 0; i < SIMIX_process_count(); i++) {
148 snapshot->privatization_regions[i] =
149 MC_region_new(-1, mappings[i], size_data_exe);
151 snapshot->privatization_index = loaded_page;
155 /** @brief Finds the range of the different memory segments and binary paths */
156 void MC_init_memory_map_info()
161 memory_map_t maps = MC_get_memory_map();
163 maestro_stack_start = NULL;
164 maestro_stack_end = NULL;
165 libsimgrid_path = NULL;
167 while (i < maps->mapsize) {
168 reg = maps->regions[i];
169 if (maps->regions[i].pathname == NULL) {
171 } else if ((reg.prot & PROT_WRITE)
172 && !memcmp(maps->regions[i].pathname, "[stack]", 7)) {
173 maestro_stack_start = reg.start_addr;
174 maestro_stack_end = reg.end_addr;
175 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)
176 && !memcmp(basename(maps->regions[i].pathname), "libsimgrid",
178 if (libsimgrid_path == NULL)
179 libsimgrid_path = strdup(maps->regions[i].pathname);
184 xbt_assert(maestro_stack_start, "maestro_stack_start");
185 xbt_assert(maestro_stack_end, "maestro_stack_end");
186 xbt_assert(libsimgrid_path, "libsimgrid_path&");
188 MC_free_memory_map(maps);
192 /** \brief Fill/lookup the "subtype" field.
194 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type)
197 if (type->dw_type_id == NULL)
199 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
200 if (type->subtype == NULL)
202 if (type->subtype->byte_size != 0)
204 if (type->subtype->name == NULL)
206 // Try to find a more complete description of the type:
207 // We need to fix in order to support C++.
210 xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
211 if (subtype != NULL) {
212 type->subtype = subtype;
217 void MC_post_process_types(mc_object_info_t info)
219 xbt_dict_cursor_t cursor = NULL;
223 // Lookup "subtype" field:
224 xbt_dict_foreach(info->types, cursor, origin, type) {
225 MC_resolve_subtype(info, type);
229 if (type->members != NULL)
230 xbt_dynar_foreach(type->members, i, member) {
231 MC_resolve_subtype(info, member);
236 /** \brief Fills the position of the segments (executable, read-only, read/write).
238 * TODO, use dl_iterate_phdr to be more robust
240 void MC_find_object_address(memory_map_t maps, mc_object_info_t result)
245 const char *name = basename(result->file_name);
246 while (i < maps->mapsize) {
247 reg = maps->regions[i];
248 if (maps->regions[i].pathname == NULL
249 || strcmp(basename(maps->regions[i].pathname), name)) {
251 } else if ((reg.prot & PROT_WRITE)) {
252 xbt_assert(!result->start_rw,
253 "Multiple read-write segments for %s, not supported",
254 maps->regions[i].pathname);
255 result->start_rw = reg.start_addr;
256 result->end_rw = reg.end_addr;
257 // .bss is usually after the .data:
258 s_map_region_t *next = &(maps->regions[i + 1]);
259 if (next->pathname == NULL && (next->prot & PROT_WRITE)
260 && next->start_addr == reg.end_addr) {
261 result->end_rw = maps->regions[i + 1].end_addr;
263 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)) {
264 xbt_assert(!result->start_exec,
265 "Multiple executable segments for %s, not supported",
266 maps->regions[i].pathname);
267 result->start_exec = reg.start_addr;
268 result->end_exec = reg.end_addr;
269 } else if ((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
270 xbt_assert(!result->start_ro,
271 "Multiple read only segments for %s, not supported",
272 maps->regions[i].pathname);
273 result->start_ro = reg.start_addr;
274 result->end_ro = reg.end_addr;
279 xbt_assert(result->file_name);
280 xbt_assert(result->start_rw);
281 xbt_assert(result->start_exec);
284 /************************************* Take Snapshot ************************************/
285 /****************************************************************************************/
287 /** \brief Checks whether the variable is in scope for a given IP.
289 * A variable may be defined only from a given value of IP.
291 * \param var Variable description
292 * \param frame Scope description
293 * \param ip Instruction pointer
294 * \return true if the variable is valid
296 static bool mc_valid_variable(dw_variable_t var, dw_frame_t scope,
299 // The variable is not yet valid:
300 if ((const void *) ((const char *) scope->low_pc + var->start_scope) > ip)
306 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame,
307 dw_frame_t scope, xbt_dynar_t result)
309 void *ip = (void *) stack_frame->ip;
310 if (ip < scope->low_pc || ip >= scope->high_pc)
314 dw_variable_t current_variable;
315 xbt_dynar_foreach(scope->variables, cursor, current_variable) {
317 if (!mc_valid_variable(current_variable, scope, (void *) stack_frame->ip))
321 if ((long) stack_frame->ip > (long) mc_libsimgrid_info->start_exec)
326 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
327 new_var->subprogram = stack_frame->frame;
328 new_var->ip = stack_frame->ip;
329 new_var->name = xbt_strdup(current_variable->name);
330 new_var->type = current_variable->type;
331 new_var->region = region_type;
333 if (current_variable->address != NULL) {
334 new_var->address = current_variable->address;
335 } else if (current_variable->locations.size != 0) {
337 (void *) mc_dwarf_resolve_locations(¤t_variable->locations,
338 current_variable->object_info,
339 &(stack_frame->unw_cursor),
340 (void *) stack_frame->frame_base,
343 xbt_die("No address");
346 xbt_dynar_push(result, &new_var);
349 // Recursive processing of nested scopes:
350 dw_frame_t nested_scope = NULL;
351 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
352 mc_fill_local_variables_values(stack_frame, nested_scope, result);
356 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames)
359 unsigned cursor1 = 0;
360 mc_stack_frame_t stack_frame;
361 xbt_dynar_t variables =
362 xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
364 xbt_dynar_foreach(stack_frames, cursor1, stack_frame) {
365 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
371 static void MC_stack_frame_free_voipd(void *s)
373 mc_stack_frame_t stack_frame = *(mc_stack_frame_t *) s;
375 xbt_free(stack_frame->frame_name);
376 xbt_free(stack_frame);
380 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context)
383 xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
387 // TODO, check condition check (unw_init_local==0 means end of frame)
388 if (unw_init_local(&c, (unw_context_t *) stack_context) != 0) {
390 xbt_die("Could not initialize stack unwinding");
395 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
396 xbt_dynar_push(result, &stack_frame);
398 stack_frame->unw_cursor = c;
402 unw_get_reg(&c, UNW_REG_IP, &ip);
403 unw_get_reg(&c, UNW_REG_SP, &sp);
405 stack_frame->ip = ip;
406 stack_frame->sp = sp;
408 // TODO, use real addresses in frame_t instead of fixing it here
410 dw_frame_t frame = MC_find_function_by_ip((void *) ip);
411 stack_frame->frame = frame;
414 stack_frame->frame_name = xbt_strdup(frame->name);
415 stack_frame->frame_base =
416 (unw_word_t) mc_find_frame_base(frame, frame->object_info, &c);
418 stack_frame->frame_base = 0;
419 stack_frame->frame_name = NULL;
422 /* Stop before context switch with maestro */
423 if (frame != NULL && frame->name != NULL
424 && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
427 int ret = ret = unw_step(&c);
429 xbt_die("Unexpected end of stack.");
430 } else if (ret < 0) {
431 xbt_die("Error while unwinding stack.");
435 if (xbt_dynar_length(result) == 0) {
436 XBT_INFO("unw_init_local failed");
443 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t * snapshot, void *heap)
447 xbt_dynar_new(sizeof(s_mc_snapshot_stack_t),
448 MC_snapshot_stack_free_voidp);
450 unsigned int cursor = 0;
451 stack_region_t current_stack;
453 xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
454 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
455 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
456 st->local_variables = MC_get_local_variables_values(st->stack_frames);
458 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
460 ((char *) heap + (size_t) (((char *) ((long) sp) - (char *) std_heap)));
462 st->real_address = current_stack->address;
463 xbt_dynar_push(res, &st);
464 (*snapshot)->stack_sizes =
465 xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
466 (*snapshot)->stack_sizes[cursor] =
467 current_stack->size - ((char *) st->stack_pointer -
468 (char *) ((char *) heap +
469 ((char *) current_stack->address -
470 (char *) std_heap)));
477 static xbt_dynar_t MC_take_snapshot_ignore()
480 if (mc_heap_comparison_ignore == NULL)
484 xbt_dynar_new(sizeof(mc_heap_ignore_region_t),
485 heap_ignore_region_free_voidp);
487 unsigned int cursor = 0;
488 mc_heap_ignore_region_t current_region;
490 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region) {
491 mc_heap_ignore_region_t new_region = NULL;
492 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
493 new_region->address = current_region->address;
494 new_region->size = current_region->size;
495 new_region->block = current_region->block;
496 new_region->fragment = current_region->fragment;
497 xbt_dynar_push(cpy, &new_region);
504 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot)
507 unsigned int cursor = 0;
508 mc_checkpoint_ignore_region_t region;
511 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region) {
512 if (region->addr > snapshot->regions[0]->start_addr
513 && (char *) (region->addr) <
514 (char *) snapshot->regions[0]->start_addr + STD_HEAP_SIZE) {
516 (char *) region->addr - (char *) snapshot->regions[0]->start_addr;
517 memset((char *) snapshot->regions[0]->data + offset, 0, region->size);
518 } else if (region->addr > snapshot->regions[2]->start_addr
519 && (char *) (region->addr) <
520 (char *) snapshot->regions[2]->start_addr +
521 snapshot->regions[2]->size) {
523 (char *) region->addr - (char *) snapshot->regions[2]->start_addr;
524 memset((char *) snapshot->regions[2]->data + offset, 0, region->size);
525 } else if (region->addr > snapshot->regions[1]->start_addr
526 && (char *) (region->addr) <
527 (char *) snapshot->regions[1]->start_addr +
528 snapshot->regions[1]->size) {
530 (char *) region->addr - (char *) snapshot->regions[1]->start_addr;
531 memset((char *) snapshot->regions[1]->data + offset, 0, region->size);
537 mc_snapshot_t MC_take_snapshot(int num_state)
540 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
541 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
543 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
544 MC_get_memory_regions(snapshot);
546 snapshot->to_ignore = MC_take_snapshot_ignore();
548 if (_sg_mc_visited > 0 || strcmp(_sg_mc_property_file, "")) {
550 MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
551 if (_sg_mc_hash && snapshot->stacks != NULL) {
552 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
561 MC_dump_checkpoint_ignore(snapshot);
563 // mprotect the region after zero-ing ignored parts:
565 for(i=0; i!=NB_REGIONS; ++i) {
566 mc_mem_region_t region = snapshot->regions[i];
567 mprotect(region->data, region->size, PROT_READ);
574 void MC_restore_snapshot(mc_snapshot_t snapshot)
577 for (i = 0; i < NB_REGIONS; i++) {
578 // For privatized, variables we decided it was not necessary to take the snapshot:
579 if (snapshot->regions[i])
580 MC_region_restore(snapshot->regions[i]);
583 if (snapshot->privatization_regions) {
584 for (i = 0; i < SIMIX_process_count(); i++) {
585 if (snapshot->privatization_regions[i]) {
586 MC_region_restore(snapshot->privatization_regions[i]);
589 switch_data_segment(snapshot->privatization_index);
593 void *mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot)
596 // If not in a process state/clone:
598 return (uintptr_t *) addr;
600 // If it is in a snapshot:
601 for (size_t i = 0; i != NB_REGIONS; ++i) {
602 mc_mem_region_t region = snapshot->regions[i];
603 uintptr_t start = (uintptr_t) region->start_addr;
604 uintptr_t end = start + region->size;
606 // The address is in this region:
607 if (addr >= start && addr < end) {
608 uintptr_t offset = addr - start;
609 return (void *) ((uintptr_t) region->data + offset);
614 // It is not in a snapshot:
615 return (void *) addr;
618 uintptr_t mc_untranslate_address(void *addr, mc_snapshot_t snapshot)
621 return (uintptr_t) addr;
624 for (size_t i = 0; i != NB_REGIONS; ++i) {
625 mc_mem_region_t region = snapshot->regions[i];
626 if (addr >= region->data
627 && addr <= (void *) (((char *) region->data) + region->size)) {
628 size_t offset = (size_t) ((char *) addr - (char *) region->data);
629 return ((uintptr_t) region->start_addr) + offset;
633 return (uintptr_t) addr;
636 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall)
638 return MC_take_snapshot(1);
641 void *MC_snapshot(void)
643 return simcall_mc_snapshot();