1 /* Copyright (c) 2008-2013. 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){
52 static void local_variable_free_voidp(void *v){
53 local_variable_free((local_variable_t) * (void **) v);
56 static void MC_region_destroy(mc_mem_region_t reg)
62 void MC_free_snapshot(mc_snapshot_t snapshot){
64 for(i=0; i < NB_REGIONS; i++)
65 MC_region_destroy(snapshot->regions[i]);
67 xbt_free(snapshot->stack_sizes);
68 xbt_dynar_free(&(snapshot->stacks));
69 xbt_dynar_free(&(snapshot->to_ignore));
74 /******************************* Snapshot regions ********************************/
75 /*********************************************************************************/
77 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
79 mc_mem_region_t new_reg = xbt_new(s_mc_mem_region_t, 1);
80 new_reg->start_addr = start_addr;
82 new_reg->data = xbt_malloc(size);
83 memcpy(new_reg->data, start_addr, size);
85 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
90 static void MC_region_restore(mc_mem_region_t reg)
92 /*FIXME: check if start_addr is still mapped, if it is not, then map it
93 before copying the data */
95 memcpy(reg->start_addr, reg->data, reg->size);
99 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
101 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
102 snapshot->regions[type] = new_reg;
106 static void MC_get_memory_regions(mc_snapshot_t snapshot){
108 void* start_heap = ((xbt_mheap_t)std_heap)->base;
109 void* end_heap = ((xbt_mheap_t)std_heap)->breakval;
110 MC_snapshot_add_region(snapshot, 0, start_heap, (char*) end_heap - (char*) start_heap);
111 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
113 MC_snapshot_add_region(snapshot, 1, mc_libsimgrid_info->start_rw, mc_libsimgrid_info->end_rw - mc_libsimgrid_info->start_rw);
114 MC_snapshot_add_region(snapshot, 2, mc_binary_info->start_rw, mc_binary_info->end_rw - mc_binary_info->start_rw);
117 /** @brief Finds the range of the different memory segments and binary paths */
118 void MC_init_memory_map_info(){
122 memory_map_t maps = MC_get_memory_map();
124 maestro_stack_start = NULL;
125 maestro_stack_end = NULL;
126 libsimgrid_path = NULL;
128 while (i < maps->mapsize) {
129 reg = maps->regions[i];
130 if (maps->regions[i].pathname == NULL) {
133 else if ((reg.prot & PROT_WRITE) && !memcmp(maps->regions[i].pathname, "[stack]", 7)){
134 maestro_stack_start = reg.start_addr;
135 maestro_stack_end = reg.end_addr;
136 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC) && !memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
137 if(libsimgrid_path == NULL)
138 libsimgrid_path = strdup(maps->regions[i].pathname);
143 xbt_assert(maestro_stack_start, "maestro_stack_start");
144 xbt_assert(maestro_stack_end, "maestro_stack_end");
145 xbt_assert(libsimgrid_path, "libsimgrid_path&");
147 MC_free_memory_map(maps);
151 /** \brief Fill/llokup the "subtype" field.
153 static void MC_resolve_subtype(mc_object_info_t info, dw_type_t type) {
155 if(type->dw_type_id==NULL)
157 type->subtype = xbt_dict_get_or_null(info->types, type->dw_type_id);
158 if(type->subtype==NULL)
160 if(type->subtype->byte_size != 0)
162 if(type->subtype->name==NULL)
164 // Try to find a more complete description of the type:
165 // We need to fix in order to support C++.
167 dw_type_t subtype = xbt_dict_get_or_null(info->types_by_name, type->subtype->name);
169 type->subtype = subtype;
172 // TODO, support "switch type" (looking up the type in another lib) when possible
175 void MC_post_process_types(mc_object_info_t info) {
176 xbt_dict_cursor_t cursor = NULL;
180 // Lookup "subtype" field:
181 xbt_dict_foreach(info->types, cursor, origin, type){
182 MC_resolve_subtype(info, type);
186 if(type->members!=NULL) xbt_dynar_foreach(type->members, i, member) {
187 MC_resolve_subtype(info, member);
192 /** \brief Fills the position of the .bss and .data sections. */
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 // TODO, use dl_iterate_phdr to be more robust
211 s_map_region_t* next = &(maps->regions[i+1]);
212 if(next->pathname == NULL && (next->prot & PROT_WRITE) && next->start_addr == reg.end_addr) {
213 result->end_rw = maps->regions[i+1].end_addr;
215 } else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
216 xbt_assert(!result->start_exec,
217 "Multiple executable segments for %s, not supported",
218 maps->regions[i].pathname);
219 result->start_exec = reg.start_addr;
220 result->end_exec = reg.end_addr;
222 else if((reg.prot & PROT_READ) && !(reg.prot & PROT_EXEC)) {
223 xbt_assert(!result->start_ro,
224 "Multiple read only segments for %s, not supported",
225 maps->regions[i].pathname);
226 result->start_ro = reg.start_addr;
227 result->end_ro = reg.end_addr;
232 xbt_assert(result->file_name);
233 xbt_assert(result->start_rw);
234 xbt_assert(result->start_exec);
237 /************************************* Take Snapshot ************************************/
238 /****************************************************************************************/
240 static xbt_dynar_t MC_get_local_variables_values(xbt_dynar_t stack_frames){
242 unsigned cursor1 = 0;
243 mc_stack_frame_t stack_frame;
244 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
246 xbt_dynar_foreach(stack_frames,cursor1,stack_frame) {
248 unsigned cursor2 = 0;
249 dw_variable_t current_variable;
250 xbt_dynar_foreach(stack_frame->frame->variables, cursor2, current_variable){
253 if((long)stack_frame->ip > (long)mc_libsimgrid_info->start_exec)
258 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
259 new_var->frame = xbt_strdup(stack_frame->frame_name);
260 new_var->ip = stack_frame->ip;
261 new_var->name = xbt_strdup(current_variable->name);
262 new_var->type = strdup(current_variable->type_origin);
263 new_var->region= region_type;
265 /* if(current_variable->address!=NULL) {
266 new_var->address = current_variable->address;
268 if(current_variable->location != NULL){
269 new_var->address = (void*) MC_dwarf_resolve_location(
270 &(stack_frame->unw_cursor), current_variable->location, (void*)stack_frame->frame_base);
273 xbt_dynar_push(variables, &new_var);
282 static void MC_stack_frame_free_voipd(void *s){
283 mc_stack_frame_t stack_frame = *(mc_stack_frame_t*)s;
285 xbt_free(stack_frame->frame_name);
286 xbt_free(stack_frame);
290 static xbt_dynar_t MC_unwind_stack_frames(void *stack_context) {
291 xbt_dynar_t result = xbt_dynar_new(sizeof(mc_stack_frame_t), MC_stack_frame_free_voipd);
295 dw_frame_t test = MC_find_function_by_ip(&MC_unwind_stack_frames);
299 for(ret = unw_init_local(&c, (unw_context_t *)stack_context); ret >= 0; ret = unw_step(&c)){
300 mc_stack_frame_t stack_frame = xbt_new(s_mc_stack_frame_t, 1);
301 xbt_dynar_push(result, &stack_frame);
303 stack_frame->unw_cursor = c;
307 unw_get_reg(&c, UNW_REG_IP, &ip);
308 unw_get_reg(&c, UNW_REG_SP, &sp);
310 stack_frame->ip = ip;
311 stack_frame->sp = sp;
313 // TODO, use real addresses in frame_t instead of fixing it here
315 dw_frame_t frame = MC_find_function_by_ip((void*) ip);
316 stack_frame->frame = frame;
319 stack_frame->frame_name = xbt_strdup(frame->name);
321 mc_object_info_t info = MC_ip_find_object_info((void*)ip);
323 // This is the instruction pointer as present in the DWARF of the object:
324 // Relocated addresses are offset for shared objets and constant for executables objects.
325 // See DWARF4 spec 7.5
326 unw_word_t normalized_ip;
327 if(info->flags & MC_OBJECT_INFO_EXECUTABLE) {
330 void* base = MC_object_base_address(info);
331 normalized_ip = (unw_word_t) ip - (unw_word_t) base;
334 stack_frame->frame_base = (unw_word_t)mc_find_frame_base(normalized_ip, frame, &c);
336 stack_frame->frame_base = 0;
339 /* Stop before context switch with maestro */
340 if(frame!=NULL && frame->name!=NULL && !strcmp(frame->name, "smx_ctx_sysv_wrapper"))
344 if(xbt_dynar_length(result) == 0){
345 XBT_INFO("unw_init_local failed");
352 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
354 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
356 unsigned int cursor = 0;
357 stack_region_t current_stack;
359 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
360 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
361 st->stack_frames = MC_unwind_stack_frames(current_stack->context);
362 st->local_variables = MC_get_local_variables_values(st->stack_frames);
364 unw_word_t sp = xbt_dynar_get_as(st->stack_frames, 0, mc_stack_frame_t)->sp;
365 st->stack_pointer = ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
367 st->real_address = current_stack->address;
368 xbt_dynar_push(res, &st);
369 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
370 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
377 static xbt_dynar_t MC_take_snapshot_ignore(){
379 if(mc_heap_comparison_ignore == NULL)
382 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
384 unsigned int cursor = 0;
385 mc_heap_ignore_region_t current_region;
387 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
388 mc_heap_ignore_region_t new_region = NULL;
389 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
390 new_region->address = current_region->address;
391 new_region->size = current_region->size;
392 new_region->block = current_region->block;
393 new_region->fragment = current_region->fragment;
394 xbt_dynar_push(cpy, &new_region);
401 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
403 unsigned int cursor = 0;
404 mc_checkpoint_ignore_region_t region;
407 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
408 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
409 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
410 memset((char *)snapshot->regions[0]->data + offset, 0, region->size);
411 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
412 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
413 memset((char *)snapshot->regions[2]->data + offset, 0, region->size);
414 }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
415 offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
416 memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
423 mc_snapshot_t MC_take_snapshot(int num_state){
425 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
426 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
428 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
429 MC_get_memory_regions(snapshot);
431 snapshot->to_ignore = MC_take_snapshot_ignore();
433 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
434 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
435 if(_sg_mc_hash && snapshot->stacks!=NULL) {
436 snapshot->hash = mc_hash_processes_state(num_state, snapshot->stacks);
446 MC_dump_checkpoint_ignore(snapshot);
452 void MC_restore_snapshot(mc_snapshot_t snapshot){
454 for(i=0; i < NB_REGIONS; i++){
455 MC_region_restore(snapshot->regions[i]);
460 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
461 return MC_take_snapshot(1);
464 void *MC_snapshot(void){
465 return simcall_mc_snapshot();