1 /* Copyright (c) 2008-2013 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "mc_private.h"
8 #include "xbt/module.h"
10 #include "../simix/smx_private.h"
12 #include <libunwind.h>
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
15 "Logging specific to mc_checkpoint");
17 void *start_text_libsimgrid;
18 void *start_plt_libsimgrid, *end_plt_libsimgrid;
19 void *start_got_plt_libsimgrid, *end_got_plt_libsimgrid;
20 void *start_plt_binary, *end_plt_binary;
21 void *start_got_plt_binary, *end_got_plt_binary;
22 char *libsimgrid_path;
23 void *start_data_libsimgrid, *start_bss_libsimgrid;
24 void *start_data_binary, *start_bss_binary;
25 void *start_text_binary;
27 /************************************ Free functions **************************************/
28 /*****************************************************************************************/
30 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
32 xbt_free(s->local_variables->data);
33 xbt_free(s->local_variables);
38 static void MC_snapshot_stack_free_voidp(void *s){
39 MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
42 static void local_variable_free(local_variable_t v){
49 static void local_variable_free_voidp(void *v){
50 local_variable_free((local_variable_t) * (void **) v);
54 /******************************* Snapshot regions ********************************/
55 /*********************************************************************************/
57 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
59 mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
60 new_reg->start_addr = start_addr;
62 new_reg->data = xbt_malloc0(size);
63 memcpy(new_reg->data, start_addr, size);
65 XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
70 static void MC_region_restore(mc_mem_region_t reg)
72 /*FIXME: check if start_addr is still mapped, if it is not, then map it
73 before copying the data */
75 memcpy(reg->start_addr, reg->data, reg->size);
80 static void MC_region_destroy(mc_mem_region_t reg)
86 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
88 mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
89 snapshot->regions[type] = new_reg;
93 static void MC_get_memory_regions(mc_snapshot_t snapshot){
100 char *lfields[6] = {0}, *tok;
101 void *start_addr, *start_addr1, *end_addr;
105 fp = fopen("/proc/self/maps", "r");
108 "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
112 while((read = xbt_getline(&line, &n, fp)) != -1){
114 /* Wipeout the new line character */
115 line[read - 1] = '\0';
117 /* Tokenize the line using spaces as delimiters and store each token */
118 lfields[0] = strtok(line, " ");
120 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
121 lfields[i] = strtok(NULL, " ");
124 /* First get the permissions flags, need write permission */
125 if(lfields[1][1] == 'w'){
127 /* Get the start address of the map */
128 tok = strtok(lfields[0], "-");
129 start_addr = (void *)strtoul(tok, NULL, 16);
131 if(start_addr == std_heap){ /* Std_heap ? */
132 tok = strtok(NULL, "-");
133 end_addr = (void *)strtoul(tok, NULL, 16);
134 MC_snapshot_add_region(snapshot, 0, start_addr, (char*)end_addr - (char*)start_addr);
135 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
136 }else{ /* map name == libsimgrid || binary_name ? */
137 if(lfields[5] != NULL){
138 if(!memcmp(basename(lfields[5]), "libsimgrid", 10)){
139 tok = strtok(NULL, "-");
140 end_addr = (void *)strtoul(tok, NULL, 16);
141 size = (char*)end_addr - (char*)start_addr;
142 /* BSS and data segments may be separated according to the OS */
143 if((read = xbt_getline(&line, &n, fp)) != -1){
144 line[read - 1] = '\0';
145 lfields[0] = strtok(line, " ");
146 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
147 lfields[i] = strtok(NULL, " ");
149 if(lfields[1][1] == 'w' && lfields[5] == NULL){
150 tok = strtok(lfields[0], "-");
151 start_addr1 = (void *)strtoul(tok, NULL, 16);
152 tok = strtok(NULL, "-");
153 size += (char *)(void *)strtoul(tok, NULL, 16) - (char*)start_addr1;
156 MC_snapshot_add_region(snapshot, 1, start_addr, size);
157 }else if(!memcmp(basename(lfields[5]), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
158 tok = strtok(NULL, "-");
159 end_addr = (void *)strtoul(tok, NULL, 16);
160 size = (char*)end_addr - (char*)start_addr;
161 /* BSS and data segments may be separated according to the OS */
162 if((read = xbt_getline(&line, &n, fp)) != -1){
163 line[read - 1] = '\0';
164 lfields[0] = strtok(line, " ");
165 for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
166 lfields[i] = strtok(NULL, " ");
168 tok = strtok(lfields[0], "-");
169 start_addr1 = (void *)strtoul(tok, NULL, 16);
170 if(lfields[1][1] == 'w' && lfields[5] == NULL){
171 if(start_addr1 == std_heap){ /* Std_heap ? */
172 tok = strtok(NULL, "-");
173 end_addr = (void *)strtoul(tok, NULL, 16);
174 MC_snapshot_add_region(snapshot, 0, start_addr1, (char*)end_addr - (char*)start_addr1);
175 snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
176 }else if(start_addr1 != raw_heap){
177 tok = strtok(NULL, "-");
178 size += (char *)(void *)strtoul(tok, NULL, 16) - (char *)start_addr1;
182 MC_snapshot_add_region(snapshot, 2, start_addr, size);
183 }else if (!memcmp(lfields[5], "[stack]", 7)){
184 maestro_stack_start = start_addr;
185 tok = strtok(NULL, "-");
186 maestro_stack_end = (void *)strtoul(tok, NULL, 16);
199 void MC_init_memory_map_info(){
203 memory_map_t maps = MC_get_memory_map();
205 while (i < maps->mapsize) {
206 reg = maps->regions[i];
207 if ((reg.prot & PROT_WRITE)){
208 if (maps->regions[i].pathname != NULL){
209 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
210 start_data_libsimgrid = reg.start_addr;
212 reg = maps->regions[i];
213 if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize)
214 start_bss_libsimgrid = reg.start_addr;
215 }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
216 start_data_binary = reg.start_addr;
218 reg = maps->regions[i];
219 if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
220 start_bss_binary = reg.start_addr;
223 }else if(!memcmp(maps->regions[i].pathname, "[stack]", 7)){
224 maestro_stack_start = reg.start_addr;
225 maestro_stack_end = reg.end_addr;
229 }else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
230 if (maps->regions[i].pathname != NULL){
231 if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
232 start_text_libsimgrid = reg.start_addr;
233 libsimgrid_path = strdup(maps->regions[i].pathname);
234 }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
235 start_text_binary = reg.start_addr;
242 MC_free_memory_map(maps);
246 void MC_get_libsimgrid_plt_section(){
249 char *line = NULL; /* Temporal storage for each line that is readed */
250 ssize_t read; /* Number of bytes readed */
251 size_t n = 0; /* Amount of bytes to read by xbt_getline */
254 int i, plt_found = 0;
255 unsigned long int size, offset;
257 char *command = bprintf("objdump --section-headers %s", libsimgrid_path);
259 fp = popen(command, "r");
262 perror("popen failed");
266 while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
271 /* Wipeout the new line character */
272 line[read - 1] = '\0';
274 lfields[0] = strtok(line, " ");
276 if(lfields[0] == NULL)
279 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
282 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
283 lfields[i] = strtok(NULL, " ");
287 if(strcmp(lfields[1], ".plt") == 0){
288 size = strtoul(lfields[2], NULL, 16);
289 offset = strtoul(lfields[5], NULL, 16);
290 start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
291 end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
293 }else if(strcmp(lfields[1], ".got.plt") == 0){
294 size = strtoul(lfields[2], NULL, 16);
295 offset = strtoul(lfields[5], NULL, 16);
296 start_got_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
297 end_got_plt_libsimgrid = (char *)start_got_plt_libsimgrid + size;
311 void MC_get_binary_plt_section(){
314 char *line = NULL; /* Temporal storage for each line that is readed */
315 ssize_t read; /* Number of bytes readed */
316 size_t n = 0; /* Amount of bytes to read by xbt_getline */
319 int i, plt_found = 0;
320 unsigned long int size;
322 char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
324 fp = popen(command, "r");
327 perror("popen failed");
331 while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
336 /* Wipeout the new line character */
337 line[read - 1] = '\0';
339 lfields[0] = strtok(line, " ");
341 if(lfields[0] == NULL)
344 if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
347 for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
348 lfields[i] = strtok(NULL, " ");
352 if(strcmp(lfields[1], ".plt") == 0){
353 size = strtoul(lfields[2], NULL, 16);
354 start_plt_binary = (void *)strtoul(lfields[3], NULL, 16);
355 end_plt_binary = (char *)start_plt_binary + size;
357 }else if(strcmp(lfields[1], ".got.plt") == 0){
358 size = strtoul(lfields[2], NULL, 16);
359 start_got_plt_binary = (char *)strtoul(lfields[3], NULL, 16);
360 end_got_plt_binary = (char *)start_got_plt_binary + size;
374 /************************************* Take Snapshot ************************************/
375 /****************************************************************************************/
377 static void MC_get_hash_global(char *snapshot_hash, void *data1, void *data2){
379 /* unsigned int cursor = 0; */
381 /* global_variable_t current_var; */
382 /* void *addr_pointed = NULL; */
383 /* void *res = NULL; */
385 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
387 /* xbt_dynar_foreach(mc_global_variables, cursor, current_var){ */
388 /* if(current_var->address < start_data_libsimgrid){ /\* binary *\/ */
389 /* offset = (char *)current_var->address - (char *)start_data_binary; */
390 /* addr_pointed = *((void **)((char *)data2 + offset)); */
391 /* if(((addr_pointed >= start_plt_binary && addr_pointed <= end_plt_binary)) || ((addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE ))) */
393 /* res = xbt_malloc0(current_var->size + 1); */
394 /* memset(res, 0, current_var->size + 1); */
395 /* memcpy(res, (char*)data2 + offset, current_var->size); */
396 /* }else{ /\* libsimgrid *\/ */
397 /* offset = (char *)current_var->address - (char *)start_data_libsimgrid; */
398 /* addr_pointed = *((void **)((char *)data1 + offset)); */
399 /* if((addr_pointed >= start_plt_libsimgrid && addr_pointed <= end_plt_libsimgrid) || (addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE )) */
401 /* res = xbt_malloc0(current_var->size + 1); */
402 /* memset(res, 0, current_var->size + 1); */
403 /* memcpy(res, (char*)data1 + offset, current_var->size); */
405 /* if(res != NULL){ */
406 /* xbt_strbuff_append(clear, (const char*)res); */
412 /* xbt_sha(clear->data, snapshot_hash); */
414 /* xbt_strbuff_free(clear); */
418 static void MC_get_hash_local(char *snapshot_hash, xbt_dynar_t stacks){
420 /* xbt_dynar_t tokens = NULL, s_tokens = NULL; */
421 /* unsigned int cursor1 = 0, cursor2 = 0; */
422 /* mc_snapshot_stack_t current_stack; */
423 /* char *frame_name = NULL; */
426 /* xbt_strbuff_t clear = xbt_strbuff_new(); */
428 /* while(cursor1 < xbt_dynar_length(stacks)){ */
429 /* current_stack = xbt_dynar_get_as(stacks, cursor1, mc_snapshot_stack_t); */
430 /* tokens = xbt_str_split(current_stack->local_variables->data, NULL); */
432 /* while(cursor2 < xbt_dynar_length(tokens)){ */
433 /* s_tokens = xbt_str_split(xbt_dynar_get_as(tokens, cursor2, char *), "="); */
434 /* if(xbt_dynar_length(s_tokens) > 1){ */
435 /* if(strcmp(xbt_dynar_get_as(s_tokens, 0, char *), "frame_name") == 0){ */
436 /* xbt_free(frame_name); */
437 /* frame_name = xbt_strdup(xbt_dynar_get_as(s_tokens, 1, char *)); */
438 /* xbt_strbuff_append(clear, (const char*)xbt_dynar_get_as(tokens, cursor2, char *)); */
440 /* xbt_dynar_free(&s_tokens); */
443 /* addr = (void *) strtoul(xbt_dynar_get_as(s_tokens, 1, char *), NULL, 16); */
444 /* if(addr > std_heap && (char *)addr <= (char *)std_heap + STD_HEAP_SIZE){ */
446 /* xbt_dynar_free(&s_tokens); */
449 /* if(is_stack_ignore_variable(frame_name, xbt_dynar_get_as(s_tokens, 0, char *))){ */
451 /* xbt_dynar_free(&s_tokens); */
454 /* xbt_strbuff_append(clear, (const char *)xbt_dynar_get_as(tokens, cursor2, char *)); */
456 /* xbt_dynar_free(&s_tokens); */
459 /* xbt_dynar_free(&tokens); */
463 /* xbt_free(frame_name); */
465 /* xbt_sha(clear->data, snapshot_hash); */
467 /* xbt_strbuff_free(clear); */
471 static xbt_dynar_t MC_get_local_variables_values(void *stack_context){
476 char frame_name[256];
478 ret = unw_init_local(&c, (unw_context_t *)stack_context);
480 XBT_INFO("unw_init_local failed");
484 unw_word_t ip, sp, off;
487 unsigned int cursor = 0;
488 dw_variable_t current_variable;
489 dw_location_entry_t entry = NULL;
490 dw_location_t location_entry = NULL;
492 int frame_found = 0, region_type;
493 void *frame_pointer_address = NULL;
496 xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
500 unw_get_reg(&c, UNW_REG_IP, &ip);
501 unw_get_reg(&c, UNW_REG_SP, &sp);
503 unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
505 if((long)ip > (long)start_text_libsimgrid)
506 frame = xbt_dict_get_or_null(mc_local_variables_libsimgrid, frame_name);
508 frame = xbt_dict_get_or_null(mc_local_variables_binary, frame_name);
515 true_ip = (long)frame->low_pc + (long)off;
516 frame_pointer_address = NULL;
518 /* Get frame pointer */
519 switch(frame->frame_base->type){
522 while(cursor < xbt_dynar_length(frame->frame_base->location.loclist) && !frame_found){
523 entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
524 if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
526 switch(entry->location->type){
528 if(xbt_dynar_length(entry->location->location.compose) > 1){
529 frame_pointer_address = NULL; /* TODO : location list with optimizations enabled */
531 location_entry = xbt_dynar_get_as(entry->location->location.compose, 0, dw_location_t);
532 switch(location_entry->type){
534 unw_get_reg(&c, location_entry->location.reg, &res);
535 frame_pointer_address = (void*)(long)res;
537 case e_dw_bregister_op:
538 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
539 frame_pointer_address = (void*)((long)res + location_entry->location.breg_op.offset);
542 frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled) */
548 frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled) */
556 frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled)*/
563 xbt_dynar_foreach(frame->variables, cursor, current_variable){
565 if((long)ip > (long)start_text_libsimgrid)
570 local_variable_t new_var = xbt_new0(s_local_variable_t, 1);
571 new_var->frame = xbt_strdup(frame_name);
572 new_var->ip = (unsigned long)ip;
573 new_var->name = xbt_strdup(current_variable->name);
574 new_var->type = strdup(current_variable->type_origin);
575 new_var->region= region_type;
577 if(current_variable->address.location != NULL){
578 switch(current_variable->address.location->type){
580 if(xbt_dynar_length(current_variable->address.location->location.compose) > 1){
581 /* TODO : location list with optimizations enabled */
583 location_entry = xbt_dynar_get_as(current_variable->address.location->location.compose, 0, dw_location_t);
585 switch(location_entry->type){
587 unw_get_reg(&c, location_entry->location.reg, &res);
590 case e_dw_bregister_op:
591 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
592 value = (long)res + location_entry->location.breg_op.offset;
594 case e_dw_fbregister_op:
595 if(frame_pointer_address != NULL)
596 value = (long)((char *)frame_pointer_address + location_entry->location.fbreg_op);
601 value = 0; /* FIXME : implement other cases (with optimizations enabled)*/
606 new_var->address = (void *)value;
608 new_var->address = NULL;
616 xbt_dynar_push(variables, &new_var);
629 static void *MC_get_stack_pointer(void *stack_context, void *heap){
635 ret = unw_init_local(&c, (unw_context_t *)stack_context);
637 XBT_INFO("unw_init_local failed");
641 unw_get_reg(&c, UNW_REG_SP, &sp);
643 return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
647 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
649 xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
651 unsigned int cursor = 0;
652 stack_region_t current_stack;
654 xbt_dynar_foreach(stacks_areas, cursor, current_stack){
655 mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
656 st->local_variables = MC_get_local_variables_values(current_stack->context);
657 st->stack_pointer = MC_get_stack_pointer(current_stack->context, heap);
658 st->real_address = current_stack->address;
659 xbt_dynar_push(res, &st);
660 (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
661 (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
668 static xbt_dynar_t MC_take_snapshot_ignore(){
670 if(mc_heap_comparison_ignore == NULL)
673 xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
675 unsigned int cursor = 0;
676 mc_heap_ignore_region_t current_region;
678 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
679 mc_heap_ignore_region_t new_region = NULL;
680 new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
681 new_region->address = current_region->address;
682 new_region->size = current_region->size;
683 new_region->block = current_region->block;
684 new_region->fragment = current_region->fragment;
685 xbt_dynar_push(cpy, &new_region);
692 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
694 unsigned int cursor = 0;
695 mc_checkpoint_ignore_region_t region;
698 xbt_dynar_foreach(mc_checkpoint_ignore, cursor, region){
699 if(region->addr > snapshot->regions[0]->start_addr && (char *)(region->addr) < (char *)snapshot->regions[0]->start_addr + STD_HEAP_SIZE){
700 offset = (char *)region->addr - (char *)snapshot->regions[0]->start_addr;
701 memset((char *)snapshot->regions[0]->start_addr + offset, 0, region->size);
702 }else if(region->addr > snapshot->regions[2]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[2]->start_addr + snapshot->regions[2]->size){
703 offset = (char *)region->addr - (char *)snapshot->regions[2]->start_addr;
704 memset((char *)snapshot->regions[2]->start_addr + offset, 0, region->size);
711 mc_snapshot_t MC_take_snapshot(){
713 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
717 mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
718 snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
720 /* Save the std heap and the writable mapped pages of libsimgrid and binary */
721 MC_get_memory_regions(snapshot);
723 snapshot->to_ignore = MC_take_snapshot_ignore();
725 if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
726 snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
727 //MC_get_hash_global(snapshot->hash_global, snapshot->regions[1]->data, snapshot->regions[2]->data);
728 //MC_get_hash_local(snapshot->hash_local, snapshot->stacks);
731 MC_dump_checkpoint_ignore(snapshot);
742 void MC_restore_snapshot(mc_snapshot_t snapshot){
744 for(i=0; i < NB_REGIONS; i++){
745 MC_region_restore(snapshot->regions[i]);
750 void MC_free_snapshot(mc_snapshot_t snapshot){
752 for(i=0; i < NB_REGIONS; i++)
753 MC_region_destroy(snapshot->regions[i]);
755 xbt_free(snapshot->stack_sizes);
756 xbt_dynar_free(&(snapshot->stacks));
757 xbt_dynar_free(&(snapshot->to_ignore));
761 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
762 return MC_take_snapshot();
765 void *MC_snapshot(void){
766 return simcall_mc_snapshot();