Logo AND Algorithmique Numérique Distribuée

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