Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : fix ignore mechanism
[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 /************************************  Free functions **************************************/
28 /*****************************************************************************************/
29
30 static void MC_snapshot_stack_free(mc_snapshot_stack_t s){
31   if(s){
32     xbt_free(s->local_variables->data);
33     xbt_free(s->local_variables);
34     xbt_free(s);
35   }
36 }
37
38 static void MC_snapshot_stack_free_voidp(void *s){
39   MC_snapshot_stack_free((mc_snapshot_stack_t) * (void **) s);
40 }
41
42 static void local_variable_free(local_variable_t v){
43   xbt_free(v->frame);
44   xbt_free(v->name);
45   xbt_free(v->type);
46   xbt_free(v);
47 }
48
49 static void local_variable_free_voidp(void *v){
50   local_variable_free((local_variable_t) * (void **) v);
51 }
52
53
54 /*******************************  Snapshot regions ********************************/
55 /*********************************************************************************/
56
57 static mc_mem_region_t MC_region_new(int type, void *start_addr, size_t size)
58 {
59   mc_mem_region_t new_reg = xbt_new0(s_mc_mem_region_t, 1);
60   new_reg->start_addr = start_addr;
61   new_reg->size = size;
62   new_reg->data = xbt_malloc0(size);
63   memcpy(new_reg->data, start_addr, size);
64
65   XBT_DEBUG("New region : type : %d, data : %p (real addr %p), size : %zu", type, new_reg->data, start_addr, size);
66   
67   return new_reg;
68 }
69
70 static void MC_region_restore(mc_mem_region_t reg)
71 {
72   /*FIXME: check if start_addr is still mapped, if it is not, then map it
73     before copying the data */
74  
75   memcpy(reg->start_addr, reg->data, reg->size);
76  
77   return;
78 }
79
80 static void MC_region_destroy(mc_mem_region_t reg)
81 {
82   xbt_free(reg->data);
83   xbt_free(reg);
84 }
85
86 static void MC_snapshot_add_region(mc_snapshot_t snapshot, int type, void *start_addr, size_t size)
87 {
88   mc_mem_region_t new_reg = MC_region_new(type, start_addr, size);
89   snapshot->regions[type] = new_reg;
90   return;
91
92
93 static void MC_get_memory_regions(mc_snapshot_t snapshot){
94
95   FILE *fp;
96   char *line = NULL;
97   ssize_t read;
98   size_t n = 0;
99   
100   char *lfields[6] = {0}, *tok;
101   void *start_addr, *start_addr1, *end_addr;
102   size_t size;
103   int i;
104
105   fp = fopen("/proc/self/maps", "r");
106   
107   xbt_assert(fp, 
108              "Cannot open /proc/self/maps to investigate the memory map of the process. Please report this bug.");
109
110   setbuf(fp, NULL);
111
112   while((read = xbt_getline(&line, &n, fp)) != -1){
113
114     /* Wipeout the new line character */
115     line[read - 1] = '\0';
116
117     /* Tokenize the line using spaces as delimiters and store each token */
118     lfields[0] = strtok(line, " ");
119
120     for (i = 1; i < 6 && lfields[i - 1] != NULL; i++) {
121       lfields[i] = strtok(NULL, " ");
122     }
123
124     /* First get the permissions flags, need write permission */
125     if(lfields[1][1] == 'w'){
126
127       /* Get the start address of the map */
128       tok = strtok(lfields[0], "-");
129       start_addr = (void *)strtoul(tok, NULL, 16);
130     
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, " ");
148               }
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;
154               }
155             }
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, " ");
167               }
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;
179                 }
180               }
181             }
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);
187           }
188         }
189       }
190     }
191     
192   }
193
194   free(line);
195   fclose(fp);
196
197 }
198
199 void MC_init_memory_map_info(){
200  
201   unsigned int i = 0;
202   s_map_region_t reg;
203   memory_map_t maps = MC_get_memory_map();
204
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;
211           i++;
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;
217           i++;
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;
221             i++;
222           }
223         }else if(!memcmp(maps->regions[i].pathname, "[stack]", 7)){
224           maestro_stack_start = reg.start_addr;
225           maestro_stack_end = reg.end_addr;
226           i++;
227         }
228       }
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;
236         }
237       }
238     }
239     i++;
240   }
241    
242   MC_free_memory_map(maps);
243
244 }
245
246 void MC_get_libsimgrid_plt_section(){
247
248   FILE *fp;
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 */
252
253   char *lfields[7];
254   int i, plt_found = 0;
255   unsigned long int size, offset;
256
257   char *command = bprintf("objdump --section-headers %s", libsimgrid_path);
258
259   fp = popen(command, "r");
260
261   if(fp == NULL){
262     perror("popen failed");
263     xbt_abort();
264   }
265
266   while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
267
268     if(n == 0)
269       continue;
270
271     /* Wipeout the new line character */
272     line[read - 1] = '\0';
273
274     lfields[0] = strtok(line, " ");
275
276     if(lfields[0] == NULL)
277       continue;
278
279     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], libsimgrid_path, strlen(libsimgrid_path)) == 0)
280       continue;
281
282     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
283       lfields[i] = strtok(NULL, " ");
284     }
285
286     if(i>=6){
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;
292         plt_found++;
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;
298         plt_found++;
299        }
300
301     }
302     
303   }
304
305   xbt_free(command);
306   xbt_free(line);
307   pclose(fp);
308
309 }
310
311 void MC_get_binary_plt_section(){
312
313   FILE *fp;
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 */
317
318   char *lfields[7];
319   int i, plt_found = 0;
320   unsigned long int size;
321
322   char *command = bprintf( "objdump --section-headers %s", xbt_binary_name);
323
324   fp = popen(command, "r");
325
326   if(fp == NULL){
327     perror("popen failed");
328     xbt_abort();
329   }
330
331   while ((read = xbt_getline(&line, &n, fp)) != -1 && plt_found != 2) {
332
333     if(n == 0)
334       continue;
335
336     /* Wipeout the new line character */
337     line[read - 1] = '\0';
338
339     lfields[0] = strtok(line, " ");
340
341     if(lfields[0] == NULL)
342       continue;
343
344     if(strcmp(lfields[0], "Sections:") == 0 || strcmp(lfields[0], "Idx") == 0 || strncmp(lfields[0], basename(xbt_binary_name), strlen(xbt_binary_name)) == 0)
345       continue;
346
347     for (i = 1; i < 7 && lfields[i - 1] != NULL; i++) {
348       lfields[i] = strtok(NULL, " ");
349     }
350
351     if(i>=6){
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;
356         plt_found++;
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;
361         plt_found++;
362        }
363     }
364     
365     
366   }
367
368   xbt_free(command);
369   xbt_free(line);
370   pclose(fp);
371
372 }
373
374 /************************************* Take Snapshot ************************************/
375 /****************************************************************************************/
376
377 static void MC_get_hash_global(char *snapshot_hash, void *data1, void *data2){
378   
379   /* unsigned int cursor = 0; */
380   /* size_t offset;  */
381   /* global_variable_t current_var;  */
382   /* void *addr_pointed = NULL; */
383   /* void *res = NULL; */
384
385   /* xbt_strbuff_t clear = xbt_strbuff_new(); */
386   
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 ))) */
392   /*       continue; */
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 )) */
400   /*       continue; */
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); */
404   /*   } */
405   /*   if(res != NULL){ */
406   /*     xbt_strbuff_append(clear, (const char*)res); */
407   /*     xbt_free(res); */
408   /*     res = NULL; */
409   /*   } */
410   /* } */
411
412   /* xbt_sha(clear->data, snapshot_hash); */
413
414   /* xbt_strbuff_free(clear); */
415
416 }
417
418 static void MC_get_hash_local(char *snapshot_hash, xbt_dynar_t stacks){
419
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; */
424   /* void *addr; */
425
426   /* xbt_strbuff_t clear = xbt_strbuff_new(); */
427
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); */
431   /*   cursor2 = 0; */
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 *)); */
439   /*         cursor2++; */
440   /*         xbt_dynar_free(&s_tokens); */
441   /*         continue; */
442   /*       } */
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){ */
445   /*         cursor2++; */
446   /*         xbt_dynar_free(&s_tokens); */
447   /*         continue; */
448   /*       } */
449   /*       if(is_stack_ignore_variable(frame_name, xbt_dynar_get_as(s_tokens, 0, char *))){ */
450   /*         cursor2++; */
451   /*         xbt_dynar_free(&s_tokens); */
452   /*         continue; */
453   /*       } */
454   /*       xbt_strbuff_append(clear, (const char *)xbt_dynar_get_as(tokens, cursor2, char *)); */
455   /*     } */
456   /*     xbt_dynar_free(&s_tokens); */
457   /*     cursor2++; */
458   /*   } */
459   /*   xbt_dynar_free(&tokens); */
460   /*   cursor1++; */
461   /* } */
462
463   /* xbt_free(frame_name); */
464
465   /* xbt_sha(clear->data, snapshot_hash); */
466
467   /* xbt_strbuff_free(clear); */
468
469 }
470
471 static xbt_dynar_t MC_get_local_variables_values(void *stack_context){
472   
473   unw_cursor_t c;
474   int ret;
475
476   char frame_name[256];
477   
478   ret = unw_init_local(&c, (unw_context_t *)stack_context);
479   if(ret < 0){
480     XBT_INFO("unw_init_local failed");
481     xbt_abort();
482   }
483
484   unw_word_t ip, sp, off;
485   dw_frame_t frame;
486
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;
491   unw_word_t res;
492   int frame_found = 0, region_type;
493   void *frame_pointer_address = NULL;
494   long true_ip, value;
495
496   xbt_dynar_t variables = xbt_dynar_new(sizeof(local_variable_t), local_variable_free_voidp);
497
498   while(ret >= 0){
499
500     unw_get_reg(&c, UNW_REG_IP, &ip);
501     unw_get_reg(&c, UNW_REG_SP, &sp);
502
503     unw_get_proc_name(&c, frame_name, sizeof (frame_name), &off);
504
505     if((long)ip > (long)start_text_libsimgrid)
506       frame = xbt_dict_get_or_null(mc_local_variables_libsimgrid, frame_name);
507     else
508       frame = xbt_dict_get_or_null(mc_local_variables_binary, frame_name);
509
510     if(frame == NULL){
511       ret = unw_step(&c);
512       continue;
513     }
514     
515     true_ip = (long)frame->low_pc + (long)off;
516     frame_pointer_address = NULL;
517
518     /* Get frame pointer */
519     switch(frame->frame_base->type){
520     case e_dw_loclist:
521       cursor = 0;
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)){
525           frame_found = 1;
526           switch(entry->location->type){
527           case e_dw_compose:
528             if(xbt_dynar_length(entry->location->location.compose) > 1){
529               frame_pointer_address = NULL; /* TODO : location list with optimizations enabled */
530             }else{
531               location_entry = xbt_dynar_get_as(entry->location->location.compose, 0, dw_location_t);
532               switch(location_entry->type){
533               case e_dw_register:
534                 unw_get_reg(&c, location_entry->location.reg, &res);
535                 frame_pointer_address = (void*)(long)res;
536                 break;
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);
540                 break;
541               default:
542                 frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled) */
543                 break;
544               }
545             }
546             break;
547           default:
548             frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled) */
549             break;
550           }
551         }
552         cursor++;
553       }
554       break;
555     default :
556       frame_pointer_address = NULL; /* FIXME : implement other cases (with optimizations enabled)*/
557       break;
558     }
559
560     frame_found = 0;
561     cursor = 0;
562
563     xbt_dynar_foreach(frame->variables, cursor, current_variable){
564       
565       if((long)ip > (long)start_text_libsimgrid)
566         region_type = 1;
567       else
568         region_type = 2;
569
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;
576       
577       if(current_variable->address.location != NULL){
578         switch(current_variable->address.location->type){
579         case e_dw_compose:
580           if(xbt_dynar_length(current_variable->address.location->location.compose) > 1){
581             /* TODO : location list with optimizations enabled */
582           }else{
583             location_entry = xbt_dynar_get_as(current_variable->address.location->location.compose, 0, dw_location_t);
584             
585             switch(location_entry->type){
586             case e_dw_register:
587               unw_get_reg(&c, location_entry->location.reg, &res);
588               value = (long)res;
589               break;
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;
593               break;
594             case e_dw_fbregister_op:
595               if(frame_pointer_address != NULL)
596                 value = (long)((char *)frame_pointer_address + location_entry->location.fbreg_op);
597               else
598                 value = 0;
599               break;
600             default:
601               value = 0; /* FIXME : implement other cases (with optimizations enabled)*/
602               break;
603             }
604
605             if(value)
606               new_var->address = (void *)value;
607             else
608               new_var->address = NULL;
609           }
610           break;
611         default :
612           break;
613         }
614       }
615
616       xbt_dynar_push(variables, &new_var);
617
618     }
619
620     ret = unw_step(&c);
621      
622   }
623
624   return variables;
625
626 }
627
628
629 static void *MC_get_stack_pointer(void *stack_context, void *heap){
630
631   unw_cursor_t c;
632   int ret;
633   unw_word_t sp;
634
635   ret = unw_init_local(&c, (unw_context_t *)stack_context);
636   if(ret < 0){
637     XBT_INFO("unw_init_local failed");
638     xbt_abort();
639   }
640
641   unw_get_reg(&c, UNW_REG_SP, &sp);
642
643   return ((char *)heap + (size_t)(((char *)((long)sp) - (char*)std_heap)));
644
645 }
646
647 static xbt_dynar_t MC_take_snapshot_stacks(mc_snapshot_t *snapshot, void *heap){
648
649   xbt_dynar_t res = xbt_dynar_new(sizeof(s_mc_snapshot_stack_t), MC_snapshot_stack_free_voidp);
650
651   unsigned int cursor = 0;
652   stack_region_t current_stack;
653   
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)));
662   }
663
664   return res;
665
666 }
667
668 static xbt_dynar_t MC_take_snapshot_ignore(){
669   
670   if(mc_heap_comparison_ignore == NULL)
671     return NULL;
672
673   xbt_dynar_t cpy = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
674
675   unsigned int cursor = 0;
676   mc_heap_ignore_region_t current_region;
677
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);
686   }
687
688   return cpy;
689
690 }
691
692 static void MC_dump_checkpoint_ignore(mc_snapshot_t snapshot){
693   
694   unsigned int cursor = 0;
695   mc_checkpoint_ignore_region_t region;
696   size_t offset;
697   
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]->data + 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]->data + offset, 0, region->size);
705     }else if(region->addr > snapshot->regions[1]->start_addr && (char *)(region->addr) < (char*)snapshot->regions[1]->start_addr + snapshot->regions[1]->size){
706       offset = (char *)region->addr - (char *)snapshot->regions[1]->start_addr;
707       memset((char *)snapshot->regions[1]->data + offset, 0, region->size);
708     }
709   }
710
711 }
712
713
714 mc_snapshot_t MC_take_snapshot(){
715
716   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
717   
718   MC_SET_RAW_MEM;
719
720   mc_snapshot_t snapshot = xbt_new0(s_mc_snapshot_t, 1);
721   snapshot->nb_processes = xbt_swag_size(simix_global->process_list);
722
723   /* Save the std heap and the writable mapped pages of libsimgrid and binary */
724   MC_get_memory_regions(snapshot);
725
726   snapshot->to_ignore = MC_take_snapshot_ignore();
727
728   if(_sg_mc_visited > 0 || strcmp(_sg_mc_property_file,"")){
729     snapshot->stacks = MC_take_snapshot_stacks(&snapshot, snapshot->regions[0]->data);
730     //MC_get_hash_global(snapshot->hash_global, snapshot->regions[1]->data, snapshot->regions[2]->data);
731     //MC_get_hash_local(snapshot->hash_local, snapshot->stacks);
732   }
733
734   MC_dump_checkpoint_ignore(snapshot);
735
736   MC_UNSET_RAW_MEM;
737
738   if(raw_mem)
739     MC_SET_RAW_MEM;
740
741   return snapshot;
742
743 }
744
745 void MC_restore_snapshot(mc_snapshot_t snapshot){
746   unsigned int i;
747   for(i=0; i < NB_REGIONS; i++){
748     MC_region_restore(snapshot->regions[i]);
749   }
750
751 }
752
753 void MC_free_snapshot(mc_snapshot_t snapshot){
754   unsigned int i;
755   for(i=0; i < NB_REGIONS; i++)
756     MC_region_destroy(snapshot->regions[i]);
757
758   xbt_free(snapshot->stack_sizes);
759   xbt_dynar_free(&(snapshot->stacks));
760   xbt_dynar_free(&(snapshot->to_ignore));
761   xbt_free(snapshot);
762 }
763
764 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall){
765   return MC_take_snapshot();
766 }
767
768 void *MC_snapshot(void){
769   return simcall_mc_snapshot();
770 }