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>
16 #include "xbt/mmalloc/mmprivate.h"
18 #include "../simix/smx_private.h"
20 #include <libunwind.h>
23 #include "mc_private.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
26 "Logging specific to mc_checkpoint");
28 char *libsimgrid_path;
30 /************************************ Free functions **************************************/
31 /*****************************************************************************************/
33 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
35 xbt_dynar_free(&(s->local_variables));
36 xbt_dynar_free(&(s->stack_frames));
41 static void MC_snapshot_stack_free_voidp(void *s){
42 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
45 static void local_variable_free(local_variable_t v){
50 static void local_variable_free_voidp(void *v){
51 local_variable_free((local_variable_t) * (void **) v);
54 static void MC_region_destroy(mc_mem_region_t reg)
60 void MC_free_snapshot(mc_snapshot_t snapshot){
62 for(i=0; i < NB_REGIONS; i++)
63 MC_region_destroy(snapshot->regions[i]);
65 xbt_free(snapshot->stack_sizes);
66 xbt_dynar_free(&(snapshot->stacks));
67 xbt_dynar_free(&(snapshot->to_ignore));
72 /******************************* Snapshot regions ********************************/
73 /*********************************************************************************/
75 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
77 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
78 new_reg->start_addr = start_addr;
80 new_reg->data = xbt_malloc(size);
81 memcpy(new_reg->data, start_addr, size);
83 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
88 static void MC_region_restore(mc_mem_region_t reg)
90 /*FIXME: check if start_addr is still mapped, if it is not, then map it
91 before copying the data */
93 memcpy(reg->start_addr, reg->data, reg->size);
97 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
99 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
100 snapshot->regions[type] = new_reg;
104 static void MC_get_memory_regions(mc_snapshot_t snapshot){
106 void* start_heap = ((xbt_mheap_t)std_heap)->base;
107 void* end_heap = ((xbt_mheap_t)std_heap)->breakval;
108 MC_snapshot_add_region(snapshot, 0, start_heap, (char*) end_heap - (char*) start_heap);
109 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
111 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw, mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
112 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw, mc_binary_info->end_rw - mc_binary_info->start_rw);
115 /** @brief Finds the range of the different memory segments and binary paths */
116 void MC_init_memory_map_info(){
120 memory_map_t maps = MC_get_memory_map();
122 maestro_stack_start = NULL;
123 maestro_stack_end = NULL;
124 libsimgrid_path = NULL;
126 while (i < maps->mapsize) {
127 reg = maps->regions[i];
128 if (maps->regions[i].pathname == NULL) {
131 else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
132 maestro_stack_start = reg.start_addr;
133 maestro_stack_end = reg.end_addr;
134 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
135 if(libsimgrid_path == NULL)
136 libsimgrid_path = strdup(maps->regions[i].pathname);
141 xbt_assert(maestro_stack_start, "maestro_stack_start");
142 xbt_assert(maestro_stack_end, "maestro_stack_end");
143 xbt_assert(libsimgrid_path, "libsimgrid_path&");
145 MC_free_memory_map(maps);
149 /** \brief Fill/lookup the "subtype" field.
151 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
153 if(type->dw_type_id==NULL)
155 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
156 if(type->subtype==NULL)
158 if(type->subtype->byte_size != 0)
160 if(type->subtype->name==NULL)
162 // Try to find a more complete description of the type:
163 // We need to fix in order to support C++.
165 dw_type_t subtype = xbt_dict_get_or_null(info->full_types_by_name, type->subtype->name);
167 type->subtype = subtype;
172 void MC_post_process_types(mc_object_info_t info) {
173 xbt_dict_cursor_t cursor = NULL;
177 // Lookup "subtype" field:
178 xbt_dict_foreach(info->types, cursor, origin, type){
179 MC_resolve_subtype(info, type);
183 if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
184 MC_resolve_subtype(info, member);
189 /** \brief Fills the position of the segments (executable, read-only, read/write).
191 * TODO, use dl_iterate_phdr to be more robust
193 void MC_find_object_address(memory_map_t maps, mc_object_info_t result) {
197 const char* name = basename(result->file_name);
198 while (i < maps->mapsize) {
199 reg = maps->regions[i];
200 if (maps->regions[i].pathname == NULL || strcmp(basename(maps->regions[i].pathname), name)) {
203 else if ((reg.prot & PROT_WRITE)){
204 xbt_assert(!result->start_rw,
205 "Multiple read-write segments for %s, not supported",
206 maps->regions[i].pathname);
207 result->start_rw = reg.start_addr;
208 result->end_rw = reg.end_addr;
209 // .bss is usually after the .data:
210 s_map_region_t* next = &(maps->regions[i+1]);
211 if(next->pathname == NULL && (next->prot & PROT_WRITE) && next->start_addr == reg.end_addr) {
212 result->end_rw = maps->regions[i+1].end_addr;
214 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
215 xbt_assert(!result->start_exec,
216 "Multiple executable segments for %s, not supported",
217 maps->regions[i].pathname);
218 result->start_exec = reg.start_addr;
219 result->end_exec = reg.end_addr;
221 else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
222 xbt_assert(!result->start_ro,
223 "Multiple read only segments for %s, not supported",
224 maps->regions[i].pathname);
225 result->start_ro = reg.start_addr;
226 result->end_ro = reg.end_addr;
231 xbt_assert(result->file_name);
232 xbt_assert(result->start_rw);
233 xbt_assert(result->start_exec);
236 /************************************* Take Snapshot ************************************/
237 /****************************************************************************************/
239 /** \brief Checks whether the variable is in scope for a given IP.
241 * A variable may be defined only from a given value of IP.
243 * \param var Variable description
244 * \param frame Scope description
245 * \param ip Instruction pointer
246 * \return true if the variable is valid
248 static bool mc_valid_variable(dw_variable_t var, dw_frame_t frame, const void* ip) {
249 // The variable is not yet valid:
250 if((const void*)((const char*) frame->low_pc + var->start_scope) > ip)
256 static void mc_fill_local_variables_values(mc_stack_frame_t stack_frame, dw_frame_t scope, xbt_dynar_t result) {
257 void* ip = (void*) stack_frame->ip;
258 if(ip < scope->low_pc || ip>= scope->high_pc)
262 dw_variable_t current_variable;
263 xbt_dynar_foreach(scope->variables, cursor, current_variable){
265 if(!mc_valid_variable(current_variable, stack_frame->frame, (void*) stack_frame->ip))
269 if((long)stack_frame->ip > (long)mc_libsimgrid_info->start_exec)
274 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
275 new_var->subprogram = stack_frame->frame;
276 new_var->ip = stack_frame->ip;
277 new_var->name = xbt_strdup(current_variable->name);
278 new_var->type = current_variable->type;
279 new_var->region= region_type;
281 /* if(current_variable->address!=NULL) {
282 new_var->address = current_variable->address;
284 if(current_variable->locations.size != 0){
285 new_var->address = (void*) mc_dwarf_resolve_locations(¤t_variable->locations,
286 current_variable->object_info,
287 &(stack_frame->unw_cursor), (void*)stack_frame->frame_base, NULL);
290 xbt_dynar_push(result, &new_var);
293 // Recursive processing of nested scopes:
294 dw_frame_t nested_scope = NULL;
295 xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
296 mc_fill_local_variables_values(stack_frame, nested_scope, result);
300 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames){
302 unsigned cursor1 = 0;
303 mc_stack_frame_t stack_frame;
304 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
306 xbt_dynar_foreach(stack_frames,cursor1,stack_frame) {
307 mc_fill_local_variables_values(stack_frame, stack_frame->frame, variables);
313 static void MC_stack_frame_free_voipd(void *s){
314 mc_stack_frame_t stack_frame = *(mc_stack_frame_t*)s;
316 xbt_free(stack_frame->frame_name);
317 xbt_free(stack_frame);
321 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context) {
322 xbt_dynar_t result = xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
326 // TODO, check condition check (unw_init_local==0 means end of frame)
327 if(unw_init_local(&c, (unw_context_t *)stack_context)!=0) {
329 xbt_die("Could not initialize stack unwinding");
333 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
334 xbt_dynar_push(result, &stack_frame);
336 stack_frame->unw_cursor = c;
340 unw_get_reg(&c, UNW_REG_IP, &ip);
341 unw_get_reg(&c, UNW_REG_SP, &sp);
343 stack_frame->ip = ip;
344 stack_frame->sp = sp;
346 // TODO, use real addresses in frame_t instead of fixing it here
348 dw_frame_t frame = MC_find_function_by_ip((void*) ip);
349 stack_frame->frame = frame;
352 stack_frame->frame_name = xbt_strdup(frame->name);
353 stack_frame->frame_base = (unw_word_t)mc_find_frame_base(frame, frame->object_info, &c);
355 stack_frame->frame_base = 0;
356 stack_frame->frame_name = NULL;
359 /* Stop before context switch with maestro */
360 if(frame!=NULL && frame->name!=NULL && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
363 int ret = ret = unw_step(&c);
365 xbt_die("Unexpected end of stack.");
367 xbt_die("Error while unwinding stack.");
371 if(xbt_dynar_length(result) == 0){
372 XBT_INFO("unw_init_local failed");
379 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
381 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
383 unsigned int cursor = 0;
384 stack_region_t current_stack;
386 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
387 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
388 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
389 st->local_variables = MC_get_local_variables_values(st->stack_frames);
391 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
392 st->stack_pointer = ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
394 st->real_address = current_stack->address;
395 xbt_dynar_push(res, &st);
396 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
397 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
404 static xbt_dynar_t MC_take_snapshot_ignore(){
406 if(mc_heap_comparison_ignore == NULL)
409 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
411 unsigned int cursor = 0;
412 mc_heap_ignore_region_t current_region;
414 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
415 mc_heap_ignore_region_t new_region = NULL;
416 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
417 new_region->address = current_region->address;
418 new_region->size = current_region->size;
419 new_region->block = current_region->block;
420 new_region->fragment = current_region->fragment;
421 xbt_dynar_push(cpy, &new_region);
428 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
430 unsigned int cursor = 0;
431 mc_checkpoint_ignore_region_t region;
434 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
435 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
436 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
437 memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
438 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
439 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
440 memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
441 }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
442 offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
443 memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
450 mc_snapshot_t MC_take_snapshot(int num_state){
452 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
453 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
455 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
456 MC_get_memory_regions(snapshot);
458 snapshot->to_ignore = MC_take_snapshot_ignore();
460 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
461 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
462 if(_sg_mc_hash && snapshot->stacks!=NULL) {
463 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
473 MC_dump_checkpoint_ignore(snapshot);
479 void MC_restore_snapshot(mc_snapshot_t snapshot){
481 for(i=0; i < NB_REGIONS; i++){
482 MC_region_restore(snapshot->regions[i]);
487 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot) {
489 // If not in a process state/clone:
491 return (uintptr_t*) addr;
494 // If it is in a snapshot:
495 for(size_t i=0; i!=NB_REGIONS; ++i) {
496 mc_mem_region_t region = snapshot->regions[i];
497 uintptr_t start = (uintptr_t) region->start_addr;
498 uintptr_t end = start + region->size;
500 // The address is in this region:
501 if(addr >= start && addr < end) {
502 uintptr_t offset = addr - start;
503 return (void*) ((uintptr_t)region->data + offset);
508 // It is not in a snapshot:
512 uintptr_t mc_untranslate_address(void* addr, mc_snapshot_t snapshot) {
514 return (uintptr_t) addr;
517 for(size_t i=0; i!=NB_REGIONS; ++i) {
518 mc_mem_region_t region = snapshot->regions[i];
519 if(addr>=region->data && addr<=(void*)(((char*)region->data)+region->size)) {
520 size_t offset = (size_t) ((char*) addr - (char*) region->data);
521 return ((uintptr_t) region->start_addr) + offset;
525 return (uintptr_t) addr;
528 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
529 return MC_take_snapshot(1);
532 void *MC_snapshot(void){
533 return simcall_mc_snapshot();