Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'hypervisor' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid into hypervisor
[simgrid.git] / src / mc / mc_checkpoint.c
1 /* Copyright (c) 2008-2013 Da SimGrid Team. All rights reserved.            */
2
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. */
5
6 #include <libgen.h>
7 #include "mc_private.h"
8 #include "xbt/module.h"
9
10 #include "../simix/smx_private.h"
11
12 #include <libunwind.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_checkpoint, mc,
15                                 "Logging specific to mc_checkpoint");
16
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;
26
27 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size);
28 static void MC_region_restore(mc_mem_region_t reg);
29 static void MC_region_destroy(mc_mem_region_t reg);
30
31 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size);
32
33 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val);
34 static xbt_dynar_t take_snapshot_stacks(mc_snapshot_t *s, void *heap);
35 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap);
36 static void print_local_variables_values(xbt_dynar_t all_variables);
37 static void *get_stack_pointer(void *stack_context, void *heap);
38
39 static void snapshot_stack_free(mc_snapshot_stack_t s);
40 static xbt_dynar_t take_snapshot_ignore(void);
41
42 static void get_hash_global(char *snapshot_hash, void *data1, void *data2);
43 static void get_hash_local(char *snapshot_hash, xbt_dynar_t stacks);
44
45 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
46 {
47   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
48   new_reg->start_addr = start_addr;
49   new_reg->size = size;
50   new_reg->data = xbt_malloc0(size);
51   memcpy(new_reg->data, start_addr, size);
52
53   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
54   
55   return new_reg;
56 }
57
58 static void MC_region_restore(mc_mem_region_t reg)
59 {
60   /*FIXME: check if start_addr is still mapped, if it is not, then map it
61     before copying the data */
62  
63   memcpy(reg->start_addr, reg->data, reg->size);
64  
65   return;
66 }
67
68 static void MC_region_destroy(mc_mem_region_t reg)
69 {
70   xbt_free(reg->data);
71   xbt_free(reg);
72 }
73
74 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
75 {
76   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
77   snapshot->regions[type] = new_reg;
78   return;
79
80
81 static void get_memory_regions(mc_snapshot_t snapshot){
82
83   FILE *fp;
84   char *line = NULL;
85   ssize_t read;
86   size_t n = 0;
87   
88   char *lfields[6] = {0}, *tok;
89   void *start_addr, *start_addr1, *end_addr;
90   size_t size;
91   int i;
92
93   fp = fopen("/proc/self/maps", "r");
94   
95   xbt_assert(fp, 
96              "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
97
98   setbuf(fp, NULL);
99
100   while((read = xbt_getline(&line, &n, fp)) != -1){
101
102     /* Wipeout the new line character */
103     line[read - 1] = '\0';
104
105     /* Tokenize the line using spaces as delimiters and store each token */
106     lfields[0] = strtok(line, " ");
107
108     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
109       lfields[i] = strtok(NULL, " ");
110     }
111
112     /* First get the permissions flags, need write permission */
113     if(lfields[1][1] == 'w'){
114
115       /* Get the start address of the map */
116       tok = strtok(lfields[0], "-");
117       start_addr = (void *)strtoul(tok, NULL, 16);
118     
119       if(start_addr == std_heap){     /* Std_heap ? */
120         tok = strtok(NULL, "-");
121         end_addr = (void *)strtoul(tok, NULL, 16);
122         MC_snapshot_add_region(snapshot, 0, start_addr, (char*)end_addr - (char*)start_addr);
123         snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
124       }else{ /* map name == libsimgrid || binary_name ? */
125         if(lfields[5] != NULL){
126           if(!memcmp(basename(lfields[5]), "libsimgrid", 10)){
127             tok = strtok(NULL, "-");
128             end_addr = (void *)strtoul(tok, NULL, 16);
129             size = (char*)end_addr - (char*)start_addr;
130             /* BSS and data segments may be separated according to the OS */
131             if((read = xbt_getline(&line, &n, fp)) != -1){
132               line[read - 1] = '\0';
133               lfields[0] = strtok(line, " ");
134               for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
135                 lfields[i] = strtok(NULL, " ");
136               }
137               if(lfields[1][1] == 'w' && lfields[5] == NULL){
138                 tok = strtok(lfields[0], "-");
139                 start_addr1 = (void *)strtoul(tok, NULL, 16);
140                 tok = strtok(NULL, "-");
141                 size += (char *)(void *)strtoul(tok, NULL, 16) - (char*)start_addr1;
142               }
143             }
144             MC_snapshot_add_region(snapshot, 1, start_addr, size);
145           }else if(!memcmp(basename(lfields[5]), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
146             tok = strtok(NULL, "-");
147             end_addr = (void *)strtoul(tok, NULL, 16);
148             size = (char*)end_addr - (char*)start_addr;
149              /* BSS and data segments may be separated according to the OS */
150             if((read = xbt_getline(&line, &n, fp)) != -1){
151               line[read - 1] = '\0';
152               lfields[0] = strtok(line, " ");
153               for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
154                 lfields[i] = strtok(NULL, " ");
155               }
156               tok = strtok(lfields[0], "-");
157               start_addr1 = (void *)strtoul(tok, NULL, 16);
158               if(lfields[1][1] == 'w' && lfields[5] == NULL){
159                 if(start_addr1 == std_heap){     /* Std_heap ? */
160                   tok = strtok(NULL, "-");
161                   end_addr = (void *)strtoul(tok, NULL, 16);
162                   MC_snapshot_add_region(snapshot, 0, start_addr1, (char*)end_addr - (char*)start_addr1);
163                   snapshot->heap_bytes_used = mmalloc_get_bytes_used(std_heap);
164                 }else if(start_addr1 != raw_heap){
165                   tok = strtok(NULL, "-");
166                   size += (char *)(void *)strtoul(tok, NULL, 16) - (char *)start_addr1;
167                 }
168               }
169             }
170             MC_snapshot_add_region(snapshot, 2, start_addr, size);
171           }else if (!memcmp(lfields[5], "[stack]", 7)){
172             maestro_stack_start = start_addr;
173             tok = strtok(NULL, "-");
174             maestro_stack_end = (void *)strtoul(tok, NULL, 16);
175           }
176         }
177       }
178     }
179     
180   }
181
182   free(line);
183   fclose(fp);
184
185 }
186
187 void MC_init_memory_map_info(){
188  
189   unsigned int i = 0;
190   s_map_region_t reg;
191   memory_map_t maps = get_memory_map();
192
193   while (i < maps->mapsize) {
194     reg = maps->regions[i];
195     if ((reg.prot & PROT_WRITE)){
196       if (maps->regions[i].pathname != NULL){
197         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
198           start_data_libsimgrid = reg.start_addr;
199           i++;
200           reg = maps->regions[i];
201           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && i < maps->mapsize)
202             start_bss_libsimgrid = reg.start_addr;
203         }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
204           start_data_binary = reg.start_addr;
205           i++;
206           reg = maps->regions[i];
207           if(reg.pathname == NULL && (reg.prot & PROT_WRITE) && reg.start_addr != std_heap && reg.start_addr != raw_heap && i < maps->mapsize){
208             start_bss_binary = reg.start_addr;
209             i++;
210           }
211         }else if(!memcmp(maps->regions[i].pathname, "[stack]", 7)){
212           maestro_stack_start = reg.start_addr;
213           maestro_stack_end = reg.end_addr;
214           i++;
215         }
216       }
217     }else if ((reg.prot & PROT_READ) && (reg.prot & PROT_EXEC)){
218       if (maps->regions[i].pathname != NULL){
219         if (!memcmp(basename(maps->regions[i].pathname), "libsimgrid", 10)){
220           start_text_libsimgrid = reg.start_addr;
221           libsimgrid_path = strdup(maps->regions[i].pathname);
222         }else if (!memcmp(basename(maps->regions[i].pathname), basename(xbt_binary_name), strlen(basename(xbt_binary_name)))){
223           start_text_binary = reg.start_addr;
224         }
225       }
226     }
227     i++;
228   }
229    
230   free_memory_map(maps);
231
232 }
233
234 mc_snapshot_t MC_take_snapshot()
235 {
236
237   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
238   
239   MC_SET_RAW_MEM;
240
241   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
242   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
243
244   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
245   get_memory_regions(snapshot);
246
247   snapshot->to_ignore = take_snapshot_ignore();
248
249   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
250     snapshot->stacks = take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
251     get_hash_global(snapshot->hash_global, snapshot->regions[1]->data, snapshot->regions[2]->data);
252     get_hash_local(snapshot->hash_local, snapshot->stacks);
253   }
254
255   MC_UNSET_RAW_MEM;
256
257   if(raw_mem)
258     MC_SET_RAW_MEM;
259
260   return snapshot;
261
262 }
263
264 void MC_restore_snapshot(mc_snapshot_t snapshot)
265 {
266   unsigned int i;
267   for(i=0; i < NB_REGIONS; i++){
268     MC_region_restore(snapshot->regions[i]);
269   }
270
271 }
272
273 void MC_free_snapshot(mc_snapshot_t snapshot)
274 {
275   unsigned int i;
276   for(i=0; i < NB_REGIONS; i++)
277     MC_region_destroy(snapshot->regions[i]);
278
279   xbt_free(snapshot->stack_sizes);
280   xbt_dynar_free(&(snapshot->stacks));
281   xbt_dynar_free(&(snapshot->to_ignore));
282   xbt_free(snapshot);
283 }
284
285
286 void get_libsimgrid_plt_section(){
287
288   FILE *fp;
289   char *line = NULL;            /* Temporal storage for each line that is readed */
290   ssize_t read;                 /* Number of bytes readed */
291   size_t n = 0;                 /* Amount of bytes to read by xbt_getline */
292
293   char *lfields[7];
294   int i, plt_found = 0;
295   unsigned long int size, offset;
296
297   char *command = bprintf("objdump --section-headers %s", libsimgrid_path);
298
299   fp = popen(command, "r");
300
301   if(fp == NULL){
302     perror("popen failed");
303     xbt_abort();
304   }
305
306   while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
307
308     if(n == 0)
309       continue;
310
311     /* Wipeout the new line character */
312     line[read - 1] = '\0';
313
314     lfields[0] = strtok(line, " ");
315
316     if(lfields[0] == NULL)
317       continue;
318
319     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
320       continue;
321
322     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
323       lfields[i] = strtok(NULL, " ");
324     }
325
326     if(i>=6){
327       if(strcmp(lfields[1], ".plt") == 0){
328         size = strtoul(lfields[2], NULL, 16);
329         offset = strtoul(lfields[5], NULL, 16);
330         start_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
331         end_plt_libsimgrid = (char *)start_plt_libsimgrid + size;
332         plt_found++;
333       }else if(strcmp(lfields[1], ".got.plt") == 0){
334         size = strtoul(lfields[2], NULL, 16);
335         offset = strtoul(lfields[5], NULL, 16);
336         start_got_plt_libsimgrid = (char *)start_text_libsimgrid + offset;
337         end_got_plt_libsimgrid = (char *)start_got_plt_libsimgrid + size;
338         plt_found++;
339        }
340
341     }
342     
343   }
344
345   xbt_free(command);
346   xbt_free(line);
347   pclose(fp);
348
349 }
350
351 void get_binary_plt_section(){
352
353   FILE *fp;
354   char *line = NULL;            /* Temporal storage for each line that is readed */
355   ssize_t read;                 /* Number of bytes readed */
356   size_t n = 0;                 /* Amount of bytes to read by xbt_getline */
357
358   char *lfields[7];
359   int i, plt_found = 0;
360   unsigned long int size;
361
362   char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
363
364   fp = popen(command, "r");
365
366   if(fp == NULL){
367     perror("popen failed");
368     xbt_abort();
369   }
370
371   while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
372
373     if(n == 0)
374       continue;
375
376     /* Wipeout the new line character */
377     line[read - 1] = '\0';
378
379     lfields[0] = strtok(line, " ");
380
381     if(lfields[0] == NULL)
382       continue;
383
384     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
385       continue;
386
387     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
388       lfields[i] = strtok(NULL, " ");
389     }
390
391     if(i>=6){
392       if(strcmp(lfields[1], ".plt") == 0){
393         size = strtoul(lfields[2], NULL, 16);
394         start_plt_binary = (void *)strtoul(lfields[3], NULL, 16);
395         end_plt_binary = (char *)start_plt_binary + size;
396         plt_found++;
397       }else if(strcmp(lfields[1], ".got.plt") == 0){
398         size = strtoul(lfields[2], NULL, 16);
399         start_got_plt_binary = (char *)strtoul(lfields[3], NULL, 16);
400         end_got_plt_binary = (char *)start_got_plt_binary + size;
401         plt_found++;
402        }
403     }
404     
405     
406   }
407
408   xbt_free(command);
409   xbt_free(line);
410   pclose(fp);
411
412 }
413
414 static void add_value(xbt_dynar_t *list, const char *type, unsigned long int val){
415   variable_value_t value = xbt_new0(s_variable_value_t, 1);
416   value->type = strdup(type);
417   if(strcmp(type, "value") == 0){
418     value->value.res = val;
419   }else{
420     value->value.address = (void *)val;
421   }
422   xbt_dynar_push(*list, &value);
423 }
424
425 static xbt_dynar_t take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
426
427   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), snapshot_stack_free_voidp);
428
429   unsigned int cursor = 0;
430   stack_region_t current_stack;
431   
432   xbt_dynar_foreach(stacks_areas, cursor, current_stack){
433     mc_snapshot_stack_t st = xbt_new(s_mc_snapshot_stack_t, 1);
434     st->local_variables = get_local_variables_values(current_stack->context, heap);
435     st->stack_pointer = get_stack_pointer(current_stack->context, heap);
436     xbt_dynar_push(res, &st);
437     (*snapshot)->stack_sizes = xbt_realloc((*snapshot)->stack_sizes, (cursor + 1) * sizeof(size_t));
438     (*snapshot)->stack_sizes[cursor] = current_stack->size - ((char *)st->stack_pointer - (char *)((char *)heap + ((char *)current_stack->address - (char *)std_heap)));
439   }
440
441   return res;
442
443 }
444
445 static void *get_stack_pointer(void *stack_context, void *heap){
446
447   unw_cursor_t c;
448   int ret;
449   unw_word_t sp;
450
451   ret = unw_init_local(&c, (unw_context_t *)stack_context);
452   if(ret < 0){
453     XBT_INFO("unw_init_local failed");
454     xbt_abort();
455   }
456
457   unw_get_reg(&c, UNW_REG_SP, &sp);
458
459   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
460
461 }
462
463 static xbt_strbuff_t get_local_variables_values(void *stack_context, void *heap){
464   
465   unw_cursor_t c;
466   int ret;
467
468   char frame_name[256];
469   
470   ret = unw_init_local(&c, (unw_context_t *)stack_context);
471   if(ret < 0){
472     XBT_INFO("unw_init_local failed");
473     xbt_abort();
474   }
475
476   unw_word_t ip, sp, off;
477   dw_frame_t frame;
478  
479   xbt_dynar_t compose = xbt_dynar_new(sizeof(variable_value_t), variable_value_free_voidp);
480
481   xbt_strbuff_t variables = xbt_strbuff_new();
482   xbt_dict_cursor_t dict_cursor;
483   char *variable_name;
484   dw_local_variable_t current_variable;
485   unsigned int cursor = 0, cursor2 = 0;
486   dw_location_entry_t entry = NULL;
487   dw_location_t location_entry = NULL;
488   unw_word_t res;
489   int frame_found = 0;
490   void *frame_pointer_address = NULL;
491   long true_ip;
492   char *to_append;
493
494   while(ret >= 0){
495
496     unw_get_reg(&c, UNW_REG_IP, &ip);
497     unw_get_reg(&c, UNW_REG_SP, &sp);
498
499     unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
500
501     frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
502
503     if(frame == NULL){
504       xbt_dynar_free(&compose);
505       xbt_dict_cursor_free(&dict_cursor);
506       return variables;
507     }
508
509     to_append = bprintf("frame_name=%s\n", frame_name);
510     xbt_strbuff_append(variables, to_append);
511     xbt_free(to_append);
512     to_append = bprintf("ip=%lx\n", (unsigned long)ip);
513     xbt_strbuff_append(variables, to_append);
514     xbt_free(to_append);
515     
516     true_ip = (long)frame->low_pc + (long)off;
517
518     /* Get frame pointer */
519     switch(frame->frame_base->type){
520     case e_dw_loclist:
521       while((cursor < xbt_dynar_length(frame->frame_base->location.loclist)) && frame_found == 0){
522         entry = xbt_dynar_get_as(frame->frame_base->location.loclist, cursor, dw_location_entry_t);
523         if((true_ip >= entry->lowpc) && (true_ip < entry->highpc)){
524           frame_found = 1;
525           switch(entry->location->type){
526           case e_dw_compose:
527             xbt_dynar_reset(compose);
528             cursor2 = 0;
529             while(cursor2 < xbt_dynar_length(entry->location->location.compose)){
530               location_entry = xbt_dynar_get_as(entry->location->location.compose, cursor2, dw_location_t);
531               switch(location_entry->type){
532               case e_dw_register:
533                 unw_get_reg(&c, location_entry->location.reg, &res);
534                 add_value(&compose, "address", (long)res);
535                 break;
536               case e_dw_bregister_op:
537                 unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
538                 add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
539                 break;
540               default:
541                 xbt_dynar_reset(compose);
542                 break;
543               }
544               cursor2++;
545             }
546
547             if(!xbt_dynar_is_empty(compose)){
548               frame_pointer_address = xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address ; 
549               xbt_dynar_reset(compose);
550             }
551             break;
552           default :
553             frame_pointer_address = NULL;
554             break;
555           }
556         }
557         cursor++;
558       }
559       break;
560     default :
561       frame_pointer_address = NULL;
562       break;
563     }
564
565     frame_found = 0;
566     cursor = 0;
567
568     xbt_dict_foreach(frame->variables, dict_cursor, variable_name, current_variable){
569       if(current_variable->location != NULL){
570         switch(current_variable->location->type){
571         case e_dw_compose:
572           xbt_dynar_reset(compose);
573           cursor = 0;
574           while(cursor < xbt_dynar_length(current_variable->location->location.compose)){
575             location_entry = xbt_dynar_get_as(current_variable->location->location.compose, cursor, dw_location_t);
576             switch(location_entry->type){
577             case e_dw_register:
578               unw_get_reg(&c, location_entry->location.reg, &res);
579               add_value(&compose, "value", (long)res);
580               break;
581             case e_dw_bregister_op:
582               unw_get_reg(&c, location_entry->location.breg_op.reg, &res);
583               add_value(&compose, "address", (long)res + location_entry->location.breg_op.offset);
584               break;
585             case e_dw_fbregister_op:
586               if(frame_pointer_address != NULL)
587                 add_value(&compose, "address", (long)((char *)frame_pointer_address + location_entry->location.fbreg_op));
588               break;
589             default:
590               xbt_dynar_reset(compose);
591               break;
592             }
593             cursor++;
594           }
595           
596           if(!xbt_dynar_is_empty(compose)){
597             if(strcmp(xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->type, "value") == 0){
598               to_append = bprintf("%s=%lx\n", current_variable->name, xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.res);
599               xbt_strbuff_append(variables, to_append);
600               xbt_free(to_append);
601             }else{
602               if((long)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address < 0 || *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) == NULL){
603                 to_append = bprintf("%s=NULL\n", current_variable->name);
604                 xbt_strbuff_append(variables, to_append);
605                 xbt_free(to_append);
606               }else if(((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) > 0xffffffff) || ((long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1,variable_value_t)->value.address) < (long)start_text_binary)){
607                 to_append = bprintf("%s=%u\n", current_variable->name, (unsigned int)(long)*((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
608                 xbt_strbuff_append(variables, to_append);
609                 xbt_free(to_append);
610               }else{ 
611                 to_append = bprintf("%s=%p\n", current_variable->name, *((void**)xbt_dynar_get_as(compose, xbt_dynar_length(compose) - 1, variable_value_t)->value.address));
612                 xbt_strbuff_append(variables, to_append);
613                 xbt_free(to_append);
614               }
615             }
616             xbt_dynar_reset(compose);
617           }else{
618             to_append = bprintf("%s=undefined\n", current_variable->name);
619             xbt_strbuff_append(variables, to_append);
620             xbt_free(to_append);
621           }
622           break;
623         default :
624           break;
625         }
626       }else{
627         to_append = bprintf("%s=undefined\n", current_variable->name);
628         xbt_strbuff_append(variables, to_append);
629         xbt_free(to_append);
630       }
631     }    
632  
633     ret = unw_step(&c);
634      
635   }
636
637   xbt_dynar_free(&compose);
638   xbt_dict_cursor_free(&dict_cursor);
639
640   return variables;
641
642 }
643
644 static void print_local_variables_values(xbt_dynar_t all_variables){
645
646   unsigned cursor = 0;
647   mc_snapshot_stack_t stack;
648
649   xbt_dynar_foreach(all_variables, cursor, stack){
650     XBT_INFO("%s", stack->local_variables->data);
651   }
652 }
653
654
655 static void snapshot_stack_free(mc_snapshot_stack_t s){
656   if(s){
657     xbt_free(s->local_variables->data);
658     xbt_free(s->local_variables);
659     xbt_free(s);
660   }
661 }
662
663 void snapshot_stack_free_voidp(void *s){
664   snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
665 }
666
667 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
668   return MC_take_snapshot();
669 }
670
671 void *MC_snapshot(void){
672
673   return simcall_mc_snapshot();
674   
675 }
676
677 void variable_value_free(variable_value_t v){
678   if(v){
679     xbt_free(v->type);
680     xbt_free(v);
681   }
682 }
683
684 void variable_value_free_voidp(void* v){
685   variable_value_free((variable_value_t) * (void **)v);
686 }
687
688 static void get_hash_global(char *snapshot_hash, void *data1, void *data2){
689   
690   unsigned int cursor = 0;
691   size_t offset; 
692   global_variable_t current_var; 
693   void *addr_pointed = NULL;
694   void *res = NULL;
695
696   xbt_strbuff_t clear = xbt_strbuff_new();
697   
698   xbt_dynar_foreach(mc_global_variables, cursor, current_var){
699     if(current_var->address < start_data_libsimgrid){ /* binary */
700       offset = (char *)current_var->address - (char *)start_data_binary;
701       addr_pointed = *((void **)((char *)data2 + offset));
702       if(((addr_pointed >= start_plt_binary && addr_pointed <= end_plt_binary)) || ((addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE )))
703         continue;
704       res = xbt_malloc0(current_var->size + 1);
705       memset(res, 0, current_var->size + 1);
706       memcpy(res, (char*)data2 + offset, current_var->size);
707     }else{ /* libsimgrid */
708       offset = (char *)current_var->address - (char *)start_data_libsimgrid;
709       addr_pointed = *((void **)((char *)data1 + offset));
710       if((addr_pointed >= start_plt_libsimgrid && addr_pointed <= end_plt_libsimgrid) || (addr_pointed >= std_heap && (char *)addr_pointed <= (char *)std_heap + STD_HEAP_SIZE ))
711         continue;
712       res = xbt_malloc0(current_var->size + 1);
713       memset(res, 0, current_var->size + 1);
714       memcpy(res, (char*)data1 + offset, current_var->size);
715     }
716     if(res != NULL){
717       xbt_strbuff_append(clear, (const char*)res);
718       xbt_free(res);
719       res = NULL;
720     }
721   }
722
723   xbt_sha(clear->data, snapshot_hash);
724
725   xbt_strbuff_free(clear);
726
727 }
728
729 static void get_hash_local(char *snapshot_hash, xbt_dynar_t stacks){
730
731   xbt_dynar_t tokens = NULL, s_tokens = NULL;
732   unsigned int cursor1 = 0, cursor2 = 0;
733   mc_snapshot_stack_t current_stack;
734   char *frame_name = NULL;
735   void *addr;
736
737   xbt_strbuff_t clear = xbt_strbuff_new();
738
739   while(cursor1 < xbt_dynar_length(stacks)){
740     current_stack = xbt_dynar_get_as(stacks, cursor1, mc_snapshot_stack_t);
741     tokens = xbt_str_split(current_stack->local_variables->data, NULL);
742     cursor2 = 0;
743     while(cursor2 < xbt_dynar_length(tokens)){
744       s_tokens = xbt_str_split(xbt_dynar_get_as(tokens, cursor2, char *), "=");
745       if(xbt_dynar_length(s_tokens) > 1){
746         if(strcmp(xbt_dynar_get_as(s_tokens, 0, char *), "frame_name") == 0){
747           xbt_free(frame_name);
748           frame_name = xbt_strdup(xbt_dynar_get_as(s_tokens, 1, char *));
749           xbt_strbuff_append(clear, (const char*)xbt_dynar_get_as(tokens, cursor2, char *));
750           cursor2++;
751           xbt_dynar_free(&s_tokens);
752           continue;
753         }
754         addr = (void *) strtoul(xbt_dynar_get_as(s_tokens, 1, char *), NULL, 16);
755         if(addr > std_heap && (char *)addr <= (char *)std_heap + STD_HEAP_SIZE){
756           cursor2++;
757           xbt_dynar_free(&s_tokens);
758           continue;
759         }
760         if(is_stack_ignore_variable(frame_name, xbt_dynar_get_as(s_tokens, 0, char *))){
761           cursor2++;
762           xbt_dynar_free(&s_tokens);
763           continue;
764         }
765         xbt_strbuff_append(clear, (const char *)xbt_dynar_get_as(tokens, cursor2, char *));
766       }
767       xbt_dynar_free(&s_tokens);
768       cursor2++;
769     }
770     xbt_dynar_free(&tokens);
771     cursor1++;
772   }
773
774   xbt_free(frame_name);
775
776   xbt_sha(clear->data, snapshot_hash);
777
778   xbt_strbuff_free(clear);
779
780 }
781
782
783 static xbt_dynar_t take_snapshot_ignore(){
784   
785   if(mc_heap_comparison_ignore == NULL)
786     return NULL;
787
788   xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
789
790   unsigned int cursor = 0;
791   mc_heap_ignore_region_t current_region;
792
793   xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
794     mc_heap_ignore_region_t new_region = NULL;
795     new_region = xbt_new0(s_mc_heap_ignore_region_t, 1);
796     new_region->address = current_region->address;
797     new_region->size = current_region->size;
798     new_region->block = current_region->block;
799     new_region->fragment = current_region->fragment;
800     xbt_dynar_push(cpy, &new_region);
801   }
802
803   return cpy;
804
805 }