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){
36 xbt_dynar_free(&(s->local_variables));
37 xbt_dynar_free(&(s->stack_frames));
42 static void MC_snapshot_stack_free_voidp(void *s){
43 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
46 static void local_variable_free(local_variable_t v){
51 static void local_variable_free_voidp(void *v){
52 local_variable_free((local_variable_t) * (void **) v);
55 static void MC_region_destroy(mc_mem_region_t reg)
57 munmap(reg->data, reg->size);
61 void MC_free_snapshot(mc_snapshot_t snapshot){
63 for(i=0; i < NB_REGIONS; i++)
64 MC_region_destroy(snapshot->regions[i]);
66 xbt_free(snapshot->stack_sizes);
67 xbt_dynar_free(&(snapshot->stacks));
68 xbt_dynar_free(&(snapshot->to_ignore));
70 if(snapshot->privatization_regions){
71 size_t n = snapshot->nb_processes;
73 MC_region_destroy(snapshot->privatization_regions[i]);
75 xbt_free(snapshot->privatization_regions);
82 /******************************* Snapshot regions ********************************/
83 /*********************************************************************************/
85 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
87 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
88 new_reg->start_addr = start_addr;
90 new_reg->data = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
91 memcpy(new_reg->data, start_addr, size);
92 madvise(new_reg->data, size, MADV_MERGEABLE);
94 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
99 static void MC_region_restore(mc_mem_region_t reg)
101 /*FIXME: check if start_addr is still mapped, if it is not, then map it
102 before copying the data */
104 memcpy(reg->start_addr, reg->data, reg->size);
108 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
110 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
111 snapshot->regions[type] = new_reg;
115 static void MC_get_memory_regions(mc_snapshot_t snapshot){
118 void* start_heap = ((xbt_mheap_t)std_heap)->base;
119 void* end_heap = ((xbt_mheap_t)std_heap)->breakval;
120 MC_snapshot_add_region(snapshot, 0, start_heap, (char*) end_heap - (char*) start_heap);
121 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
123 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw, mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
124 if(!smpi_privatize_global_variables) {
125 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw, mc_binary_info->end_rw - mc_binary_info->start_rw);
126 snapshot->privatization_regions = NULL;
127 snapshot->privatization_index = -1;
129 snapshot->privatization_regions = xbt_new(mc_mem_region_t, SIMIX_process_count());
130 for (i=0; i< SIMIX_process_count(); i++){
131 snapshot->privatization_regions[i] = MC_region_new(-1, mappings[i], size_data_exe);
133 snapshot->privatization_index = loaded_page;
137 /** @brief Finds the range of the different memory segments and binary paths */
138 void MC_init_memory_map_info(){
142 memory_map_t maps = MC_get_memory_map();
144 maestro_stack_start = NULL;
145 maestro_stack_end = NULL;
146 libsimgrid_path = NULL;
148 while (i < maps->mapsize) {
149 reg = maps->regions[i];
150 if (maps->regions[i].pathname == NULL) {
153 else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
154 maestro_stack_start = reg.start_addr;
155 maestro_stack_end = reg.end_addr;
156 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
157 if(libsimgrid_path == NULL)
158 libsimgrid_path = strdup(maps->regions[i].pathname);
163 xbt_assert(maestro_stack_start, "maestro_stack_start");
164 xbt_assert(maestro_stack_end, "maestro_stack_end");
165 xbt_assert(libsimgrid_path, "libsimgrid_path&");
167 MC_free_memory_map(maps);
171 /** \brief Fill/lookup the "subtype" field.
173 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
175 if(type->dw_type_id==NULL)
177 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
178 if(type->subtype==NULL)
180 if(type->subtype->byte_size != 0)
182 if(type->subtype->name==NULL)
184 // Try to find a more complete description of the type:
185 // We need to fix in order to support C++.
187 dw_type_t subtype = xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
189 type->subtype = subtype;
194 void MC_post_process_types(mc_object_info_t info) {
195 xbt_dict_cursor_t cursor = NULL;
199 // Lookup "subtype" field:
200 xbt_dict_foreach(info->types, cursor, origin, type){
201 MC_resolve_subtype(info, type);
205 if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
206 MC_resolve_subtype(info, member);
211 /** \brief Fills the position of the segments (executable, read-only, read/write).
213 * TODO, use dl_iterate_phdr to be more robust
215 void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
219 const char* name = basename(result->file_name);
220 while (i < maps->mapsize) {
221 reg = maps->regions[i];
222 if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname), name)) {
225 else if ((reg.prot & PROT_WRITE)){
226 xbt_assert(!result->start_rw,
227 "Multiple read-write segments for %s, not supported",
228 maps->regions[i].pathname);
229 result->start_rw = reg.start_addr;
230 result->end_rw = reg.end_addr;
231 // .bss is usually after the .data:
232 s_map_region_t* next = &(maps->regions[i+1]);
233 if(next->pathname == NULL && (next->prot & PROT_WRITE) && next->start_addr == reg.end_addr) {
234 result->end_rw = maps->regions[i+1].end_addr;
236 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
237 xbt_assert(!result->start_exec,
238 "Multiple executable segments for %s, not supported",
239 maps->regions[i].pathname);
240 result->start_exec = reg.start_addr;
241 result->end_exec = reg.end_addr;
243 else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
244 xbt_assert(!result->start_ro,
245 "Multiple read only segments for %s, not supported",
246 maps->regions[i].pathname);
247 result->start_ro = reg.start_addr;
248 result->end_ro = reg.end_addr;
253 xbt_assert(result->file_name);
254 xbt_assert(result->start_rw);
255 xbt_assert(result->start_exec);
258 /************************************* Take Snapshot ************************************/
259 /****************************************************************************************/
261 /** \brief Checks whether the variable is in scope for a given IP.
263 * A variable may be defined only from a given value of IP.
265 * \param var Variable description
266 * \param frame Scope description
267 * \param ip Instruction pointer
268 * \return true if the variable is valid
270 static bool mc_valid_variable(dw_variable_t var, dw_frame_t frame, const void* ip) {
271 // The variable is not yet valid:
272 if((const void*)((const char*) frame->low_pc + var->start_scope) > ip)
278 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame, dw_frame_t scope, xbt_dynar_t result) {
279 void* ip = (void*) stack_frame->ip;
280 if(ip < scope->low_pc || ip>= scope->high_pc)
284 dw_variable_t current_variable;
285 xbt_dynar_foreach(scope->variables, cursor, current_variable){
287 if(!mc_valid_variable(current_variable, stack_frame->frame, (void*) stack_frame->ip))
291 if((long)stack_frame->ip > (long)mc_libsimgrid_info->start_exec)
296 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
297 new_var->subprogram = stack_frame->frame;
298 new_var->ip = stack_frame->ip;
299 new_var->name = xbt_strdup(current_variable->name);
300 new_var->type = current_variable->type;
301 new_var->region= region_type;
303 /* if(current_variable->address!=NULL) {
304 new_var->address = current_variable->address;
306 if(current_variable->locations.size != 0){
307 new_var->address = (void*) mc_dwarf_resolve_locations(¤t_variable->locations,
308 current_variable->object_info,
309 &(stack_frame->unw_cursor), (void*)stack_frame->frame_base, NULL);
312 xbt_dynar_push(result, &new_var);
315 // Recursive processing of nested scopes:
316 dw_frame_t nested_scope = NULL;
317 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
318 mc_fill_local_variables_values(stack_frame, nested_scope, result);
322 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames){
324 unsigned cursor1 = 0;
325 mc_stack_frame_t stack_frame;
326 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
328 xbt_dynar_foreach(stack_frames,cursor1,stack_frame) {
329 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
335 static void MC_stack_frame_free_voipd(void *s){
336 mc_stack_frame_t stack_frame = *(mc_stack_frame_t*)s;
338 xbt_free(stack_frame->frame_name);
339 xbt_free(stack_frame);
343 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context) {
344 xbt_dynar_t result = xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
348 // TODO, check condition check (unw_init_local==0 means end of frame)
349 if(unw_init_local(&c, (unw_context_t *)stack_context)!=0) {
351 xbt_die("Could not initialize stack unwinding");
355 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
356 xbt_dynar_push(result, &stack_frame);
358 stack_frame->unw_cursor = c;
362 unw_get_reg(&c, UNW_REG_IP, &ip);
363 unw_get_reg(&c, UNW_REG_SP, &sp);
365 stack_frame->ip = ip;
366 stack_frame->sp = sp;
368 // TODO, use real addresses in frame_t instead of fixing it here
370 dw_frame_t frame = MC_find_function_by_ip((void*) ip);
371 stack_frame->frame = frame;
374 stack_frame->frame_name = xbt_strdup(frame->name);
375 stack_frame->frame_base = (unw_word_t)mc_find_frame_base(frame, frame->object_info, &c);
377 stack_frame->frame_base = 0;
378 stack_frame->frame_name = NULL;
381 /* Stop before context switch with maestro */
382 if(frame!=NULL && frame->name!=NULL && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
385 int ret = ret = unw_step(&c);
387 xbt_die("Unexpected end of stack.");
389 xbt_die("Error while unwinding stack.");
393 if(xbt_dynar_length(result) == 0){
394 XBT_INFO("unw_init_local failed");
401 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
403 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
405 unsigned int cursor = 0;
406 stack_region_t current_stack;
408 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
409 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
410 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
411 st->local_variables = MC_get_local_variables_values(st->stack_frames);
413 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
414 st->stack_pointer = ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
416 st->real_address = current_stack->address;
417 xbt_dynar_push(res, &st);
418 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
419 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
426 static xbt_dynar_t MC_take_snapshot_ignore(){
428 if(mc_heap_comparison_ignore == NULL)
431 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
433 unsigned int cursor = 0;
434 mc_heap_ignore_region_t current_region;
436 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
437 mc_heap_ignore_region_t new_region = NULL;
438 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
439 new_region->address = current_region->address;
440 new_region->size = current_region->size;
441 new_region->block = current_region->block;
442 new_region->fragment = current_region->fragment;
443 xbt_dynar_push(cpy, &new_region);
450 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
452 unsigned int cursor = 0;
453 mc_checkpoint_ignore_region_t region;
456 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
457 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
458 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
459 memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
460 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
461 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
462 memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
463 }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
464 offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
465 memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
472 mc_snapshot_t MC_take_snapshot(int num_state){
474 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
475 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
477 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
478 MC_get_memory_regions(snapshot);
480 snapshot->to_ignore = MC_take_snapshot_ignore();
482 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
483 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
484 if(_sg_mc_hash && snapshot->stacks!=NULL) {
485 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
495 MC_dump_checkpoint_ignore(snapshot);
501 void MC_restore_snapshot(mc_snapshot_t snapshot){
503 for(i=0; i < NB_REGIONS; i++){
504 // For privatized, variables we decided it was not necessary to take the snapshot:
505 if(snapshot->regions[i])
506 MC_region_restore(snapshot->regions[i]);
509 if(snapshot->privatization_regions) {
510 for (i=0; i< SIMIX_process_count(); i++){
511 if(snapshot->privatization_regions[i]) {
512 MC_region_restore(snapshot->privatization_regions[i]);
515 switch_data_segment(snapshot->privatization_index);
519 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot) {
521 // If not in a process state/clone:
523 return (uintptr_t*) addr;
526 // If it is in a snapshot:
527 for(size_t i=0; i!=NB_REGIONS; ++i) {
528 mc_mem_region_t region = snapshot->regions[i];
529 uintptr_t start = (uintptr_t) region->start_addr;
530 uintptr_t end = start + region->size;
532 // The address is in this region:
533 if(addr >= start && addr < end) {
534 uintptr_t offset = addr - start;
535 return (void*) ((uintptr_t)region->data + offset);
540 // It is not in a snapshot:
544 uintptr_t mc_untranslate_address(void* addr, mc_snapshot_t snapshot) {
546 return (uintptr_t) addr;
549 for(size_t i=0; i!=NB_REGIONS; ++i) {
550 mc_mem_region_t region = snapshot->regions[i];
551 if(addr>=region->data && addr<=(void*)(((char*)region->data)+region->size)) {
552 size_t offset = (size_t) ((char*) addr - (char*) region->data);
553 return ((uintptr_t) region->start_addr) + offset;
557 return (uintptr_t) addr;
560 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
561 return MC_take_snapshot(1);
564 void *MC_snapshot(void){
565 return simcall_mc_snapshot();