Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'smpi-topo'
[simgrid.git] / src / mc / mc_global.c
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <sys/time.h>
11 #include <sys/mman.h>
12 #include <libgen.h>
13
14 #include "simgrid/sg_config.h"
15 #include "../surf/surf_private.h"
16 #include "../simix/smx_private.h"
17 #include "../xbt/mmalloc/mmprivate.h"
18 #include "xbt/fifo.h"
19 #include "mc_private.h"
20 #include "xbt/automaton.h"
21 #include "xbt/dict.h"
22
23 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
25                                 "Logging specific to MC (global)");
26
27 /* Configuration support */
28 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
29
30 int _sg_do_model_check = 0;
31 int _sg_mc_checkpoint=0;
32 char* _sg_mc_property_file=NULL;
33 int _sg_mc_timeout=0;
34 int _sg_mc_hash=0;
35 int _sg_mc_max_depth=1000;
36 int _sg_mc_visited=0;
37 char *_sg_mc_dot_output_file = NULL;
38 int _sg_mc_comms_determinism=0;
39 int _sg_mc_send_determinism=0;
40
41 int user_max_depth_reached = 0;
42
43 void _mc_cfg_cb_reduce(const char *name, int pos) {
44   if (_sg_cfg_init_status && !_sg_do_model_check) {
45     xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
46   }
47   char *val= xbt_cfg_get_string(_sg_cfg_set, name);
48   if (!strcasecmp(val,"none")) {
49     mc_reduce_kind = e_mc_reduce_none;
50   } else if (!strcasecmp(val,"dpor")) {
51     mc_reduce_kind = e_mc_reduce_dpor;
52   } else {
53     xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
54   }
55 }
56
57 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
58   if (_sg_cfg_init_status && !_sg_do_model_check) {
59     xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
60   }
61   _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
62 }
63 void _mc_cfg_cb_property(const char *name, int pos) {
64   if (_sg_cfg_init_status && !_sg_do_model_check) {
65     xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
66   }
67   _sg_mc_property_file= xbt_cfg_get_string(_sg_cfg_set, name);
68 }
69
70 void _mc_cfg_cb_timeout(const char *name, int pos) {
71   if (_sg_cfg_init_status && !_sg_do_model_check) {
72     xbt_die("You are specifying a value to enable/disable timeout for wait requests after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
73   }
74   _sg_mc_timeout= xbt_cfg_get_boolean(_sg_cfg_set, name);
75 }
76
77 void _mc_cfg_cb_hash(const char *name, int pos) {
78   if (_sg_cfg_init_status && !_sg_do_model_check) {
79     xbt_die("You are specifying a value to enable/disable the use of global hash to speedup state comparaison, but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
80   }
81   _sg_mc_hash= xbt_cfg_get_boolean(_sg_cfg_set, name);
82 }
83
84 void _mc_cfg_cb_max_depth(const char *name, int pos) {
85   if (_sg_cfg_init_status && !_sg_do_model_check) {
86     xbt_die("You are specifying a max depth value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
87   }
88   _sg_mc_max_depth= xbt_cfg_get_int(_sg_cfg_set, name);
89 }
90
91 void _mc_cfg_cb_visited(const char *name, int pos) {
92   if (_sg_cfg_init_status && !_sg_do_model_check) {
93     xbt_die("You are specifying a number of stored visited states after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
94   }
95   _sg_mc_visited= xbt_cfg_get_int(_sg_cfg_set, name);
96 }
97
98 void _mc_cfg_cb_dot_output(const char *name, int pos) {
99   if (_sg_cfg_init_status && !_sg_do_model_check) {
100     xbt_die("You are specifying a file name for a dot output of graph state after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
101   }
102   _sg_mc_dot_output_file= xbt_cfg_get_string(_sg_cfg_set, name);
103 }
104
105 void _mc_cfg_cb_comms_determinism(const char *name, int pos) {
106   if (_sg_cfg_init_status && !_sg_do_model_check) {
107     xbt_die("You are specifying a value to enable/disable the detection of determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
108   }
109   _sg_mc_comms_determinism= xbt_cfg_get_boolean(_sg_cfg_set, name);
110 }
111
112 void _mc_cfg_cb_send_determinism(const char *name, int pos) {
113   if (_sg_cfg_init_status && !_sg_do_model_check) {
114     xbt_die("You are specifying a value to enable/disable the detection of send-determinism in the communications schemes after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
115   }
116   _sg_mc_send_determinism= xbt_cfg_get_boolean(_sg_cfg_set, name);
117 }
118
119 /* MC global data structures */
120 mc_state_t mc_current_state = NULL;
121 char mc_replay_mode = FALSE;
122 double *mc_time = NULL;
123 __thread mc_comparison_times_t mc_comp_times = NULL;
124 __thread double mc_snapshot_comparison_time;
125 mc_stats_t mc_stats = NULL;
126
127 /* Safety */
128 xbt_fifo_t mc_stack_safety = NULL;
129 mc_global_t initial_state_safety = NULL;
130
131 /* Liveness */
132 xbt_fifo_t mc_stack_liveness = NULL;
133 mc_global_t initial_state_liveness = NULL;
134 int compare;
135
136 xbt_automaton_t _mc_property_automaton = NULL;
137
138 /* Variables */
139 mc_object_info_t mc_libsimgrid_info = NULL;
140 mc_object_info_t mc_binary_info = NULL;
141
142 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
143 size_t mc_object_infos_size = 2;
144
145 /* Ignore mechanism */
146 extern xbt_dynar_t mc_heap_comparison_ignore;
147 extern xbt_dynar_t stacks_areas;
148
149 /* Dot output */
150 FILE *dot_output = NULL;
151 const char* colors[13];
152
153
154 /*******************************  DWARF Information *******************************/
155 /**********************************************************************************/
156
157 /************************** Free functions *************************/
158
159 void mc_frame_free(dw_frame_t frame){
160   xbt_free(frame->name);
161   mc_dwarf_location_list_clear(&(frame->frame_base));
162   xbt_dynar_free(&(frame->variables));
163   xbt_dynar_free(&(frame->scopes));
164   xbt_free(frame);
165 }
166
167 void dw_type_free(dw_type_t t){
168   xbt_free(t->name);
169   xbt_free(t->dw_type_id);
170   xbt_dynar_free(&(t->members));
171   mc_dwarf_expression_clear(&t->location);
172   xbt_free(t);
173 }
174
175 void dw_variable_free(dw_variable_t v){
176   if(v){
177     xbt_free(v->name);
178     xbt_free(v->type_origin);
179
180     if(v->locations.locations)
181       mc_dwarf_location_list_clear(&v->locations);
182     xbt_free(v);
183   }
184 }
185
186 void dw_variable_free_voidp(void *t){
187   dw_variable_free((dw_variable_t) * (void **) t);
188 }
189
190 // ***** object_info
191
192
193
194 mc_object_info_t MC_new_object_info(void) {
195   mc_object_info_t res = xbt_new0(s_mc_object_info_t, 1);
196   res->subprograms = xbt_dict_new_homogeneous((void (*)(void*))mc_frame_free);
197   res->global_variables = xbt_dynar_new(sizeof(dw_variable_t), dw_variable_free_voidp);
198   res->types = xbt_dict_new_homogeneous((void (*)(void*))dw_type_free);
199   res->full_types_by_name = xbt_dict_new_homogeneous(NULL);
200   return res;
201 }
202
203 void MC_free_object_info(mc_object_info_t* info) {
204   xbt_free(&(*info)->file_name);
205   xbt_dict_free(&(*info)->subprograms);
206   xbt_dynar_free(&(*info)->global_variables);
207   xbt_dict_free(&(*info)->types);
208   xbt_dict_free(&(*info)->full_types_by_name);
209   xbt_free(info);
210   xbt_dynar_free(&(*info)->functions_index);
211   *info = NULL;
212 }
213
214 // ***** Helpers
215
216 void* MC_object_base_address(mc_object_info_t info) {
217   if(info->flags & MC_OBJECT_INFO_EXECUTABLE)
218     return 0;
219   void* result = info->start_exec;
220   if(info->start_rw!=NULL && result > (void*) info->start_rw) result = info->start_rw;
221   if(info->start_ro!=NULL && result > (void*) info->start_ro) result = info->start_ro;
222   return result;
223 }
224
225 // ***** Functions index
226
227 static int MC_compare_frame_index_items(mc_function_index_item_t a, mc_function_index_item_t b) {
228   if(a->low_pc < b->low_pc)
229     return -1;
230   else if(a->low_pc == b->low_pc)
231     return 0;
232   else
233     return 1;
234 }
235
236 static void MC_make_functions_index(mc_object_info_t info) {
237   xbt_dynar_t index = xbt_dynar_new(sizeof(s_mc_function_index_item_t), NULL);
238
239   // Populate the array:
240   dw_frame_t frame = NULL;
241   xbt_dict_cursor_t cursor;
242   char* key;
243   xbt_dict_foreach(info->subprograms, cursor, key, frame) {
244     if(frame->low_pc==NULL)
245       continue;
246     s_mc_function_index_item_t entry;
247     entry.low_pc = frame->low_pc;
248     entry.high_pc = frame->high_pc;
249     entry.function = frame;
250     xbt_dynar_push(index, &entry);
251   }
252
253   mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(index, 0);
254
255   // Sort the array by low_pc:
256   qsort(base,
257     xbt_dynar_length(index),
258     sizeof(s_mc_function_index_item_t),
259     (int (*)(const void *, const void *))MC_compare_frame_index_items);
260
261   info->functions_index = index;
262 }
263
264 mc_object_info_t MC_ip_find_object_info(void* ip) {
265   size_t i;
266   for(i=0; i!=mc_object_infos_size; ++i) {
267     if(ip >= (void*)mc_object_infos[i]->start_exec && ip <= (void*)mc_object_infos[i]->end_exec) {
268       return mc_object_infos[i];
269     }
270   }
271   return NULL;
272 }
273
274 static dw_frame_t MC_find_function_by_ip_and_object(void* ip, mc_object_info_t info) {
275   xbt_dynar_t dynar = info->functions_index;
276   mc_function_index_item_t base = (mc_function_index_item_t) xbt_dynar_get_ptr(dynar, 0);
277   int i = 0;
278   int j = xbt_dynar_length(dynar) - 1;
279   while(j>=i) {
280     int k = i + ((j-i)/2);
281     if(ip < base[k].low_pc) {
282       j = k-1;
283     } else if(ip >= base[k].high_pc) {
284       i = k+1;
285     } else {
286       return base[k].function;
287     }
288   }
289   return NULL;
290 }
291
292 dw_frame_t MC_find_function_by_ip(void* ip) {
293   mc_object_info_t info = MC_ip_find_object_info(ip);
294   if(info==NULL)
295     return NULL;
296   else
297     return MC_find_function_by_ip_and_object(ip, info);
298 }
299
300 static void MC_post_process_variables(mc_object_info_t info) {
301   unsigned cursor = 0;
302   dw_variable_t variable = NULL;
303   xbt_dynar_foreach(info->global_variables, cursor, variable) {
304     if(variable->type_origin) {
305       variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
306     }
307   }
308 }
309
310 static void mc_post_process_scope(mc_object_info_t info, dw_frame_t scope) {
311
312   if(scope->tag == DW_TAG_inlined_subroutine) {
313
314     // Attach correct namespaced name in inlined subroutine:
315     char* key = bprintf("%" PRIx64, (uint64_t) scope->abstract_origin_id);
316     dw_frame_t abstract_origin = xbt_dict_get_or_null(info->subprograms, key);
317     xbt_assert(abstract_origin, "Could not lookup abstract origin %s", key);
318     xbt_free(key);
319     scope->name = xbt_strdup(abstract_origin->name);
320
321   }
322
323   // Direct:
324   unsigned cursor = 0;
325   dw_variable_t variable = NULL;
326   xbt_dynar_foreach(scope->variables, cursor, variable) {
327     if(variable->type_origin) {
328       variable->type = xbt_dict_get_or_null(info->types, variable->type_origin);
329     }
330   }
331
332   // Recursive post-processing of nested-scopes:
333   dw_frame_t nested_scope = NULL;
334   xbt_dynar_foreach(scope->scopes, cursor, nested_scope)
335     mc_post_process_scope(info, nested_scope);
336
337 }
338
339 static void MC_post_process_functions(mc_object_info_t info) {
340   xbt_dict_cursor_t cursor;
341   char* key;
342   dw_frame_t subprogram = NULL;
343   xbt_dict_foreach(info->subprograms, cursor, key, subprogram) {
344     mc_post_process_scope(info, subprogram);
345   }
346 }
347
348 /** \brief Finds informations about a given shared object/executable */
349 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable) {
350   mc_object_info_t result = MC_new_object_info();
351   if(executable)
352     result->flags |= MC_OBJECT_INFO_EXECUTABLE;
353   result->file_name = xbt_strdup(name);
354   MC_find_object_address(maps, result);
355   MC_dwarf_get_variables(result);
356   MC_post_process_types(result);
357   MC_post_process_variables(result);
358   MC_post_process_functions(result);
359   MC_make_functions_index(result);
360   return result;
361 }
362
363 /*************************************************************************/
364
365 static int MC_dwarf_get_variable_index(xbt_dynar_t variables, char* var, void *address){
366
367   if(xbt_dynar_is_empty(variables))
368     return 0;
369
370   unsigned int cursor = 0;
371   int start = 0;
372   int end = xbt_dynar_length(variables) - 1;
373   dw_variable_t var_test = NULL;
374
375   while(start <= end){
376     cursor = (start + end) / 2;
377     var_test = (dw_variable_t)xbt_dynar_get_as(variables, cursor, dw_variable_t);
378     if(strcmp(var_test->name, var) < 0){
379       start = cursor + 1;
380     }else if(strcmp(var_test->name, var) > 0){
381       end = cursor - 1;
382     }else{
383       if(address){ /* global variable */
384         if(var_test->address == address)
385           return -1;
386         if(var_test->address > address)
387           end = cursor - 1;
388         else
389           start = cursor + 1;
390       }else{ /* local variable */
391         return -1;
392       }
393     }
394   }
395
396   if(strcmp(var_test->name, var) == 0){
397     if(address && var_test->address < address)
398       return cursor+1;
399     else
400       return cursor;
401   }else if(strcmp(var_test->name, var) < 0)
402     return cursor+1;
403   else
404     return cursor;
405
406 }
407
408 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable) {
409   int index = MC_dwarf_get_variable_index(info->global_variables, variable->name, variable->address);
410   if (index != -1)
411     xbt_dynar_insert_at(info->global_variables, index, &variable);
412   // TODO, else ?
413 }
414
415 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
416   xbt_assert(frame, "Frame is NULL");
417   int index = MC_dwarf_get_variable_index(frame->variables, variable->name, NULL);
418   if (index != -1)
419     xbt_dynar_insert_at(frame->variables, index, &variable);
420   // TODO, else ?
421 }
422
423 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable) {
424   if(variable->global)
425     MC_dwarf_register_global_variable(info, variable);
426   else if(frame==NULL)
427     xbt_die("No frame for this local variable");
428   else
429     MC_dwarf_register_non_global_variable(info, frame, variable);
430 }
431
432
433 /*******************************  Ignore mechanism *******************************/
434 /*********************************************************************************/
435
436 xbt_dynar_t mc_checkpoint_ignore;
437
438 typedef struct s_mc_stack_ignore_variable{
439   char *var_name;
440   char *frame;
441 }s_mc_stack_ignore_variable_t, *mc_stack_ignore_variable_t;
442
443 /**************************** Free functions ******************************/
444
445 static void stack_ignore_variable_free(mc_stack_ignore_variable_t v){
446   xbt_free(v->var_name);
447   xbt_free(v->frame);
448   xbt_free(v);
449 }
450
451 static void stack_ignore_variable_free_voidp(void *v){
452   stack_ignore_variable_free((mc_stack_ignore_variable_t) * (void **) v);
453 }
454
455 void heap_ignore_region_free(mc_heap_ignore_region_t r){
456   xbt_free(r);
457 }
458
459 void heap_ignore_region_free_voidp(void *r){
460   heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
461 }
462
463 static void checkpoint_ignore_region_free(mc_checkpoint_ignore_region_t r){
464   xbt_free(r);
465 }
466
467 static void checkpoint_ignore_region_free_voidp(void *r){
468   checkpoint_ignore_region_free((mc_checkpoint_ignore_region_t) * (void **) r);
469 }
470
471 /***********************************************************************/
472
473 void MC_ignore_heap(void *address, size_t size){
474
475   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
476
477   MC_SET_RAW_MEM;
478
479   mc_heap_ignore_region_t region = NULL;
480   region = xbt_new0(s_mc_heap_ignore_region_t, 1);
481   region->address = address;
482   region->size = size;
483   
484   region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
485   
486   if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
487     region->fragment = -1;
488     ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_block.ignore++;
489   }else{
490     region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
491     ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
492   }
493   
494   if(mc_heap_comparison_ignore == NULL){
495     mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
496     xbt_dynar_push(mc_heap_comparison_ignore, &region);
497     if(!raw_mem_set)
498       MC_UNSET_RAW_MEM;
499     return;
500   }
501
502   unsigned int cursor = 0;
503   mc_heap_ignore_region_t current_region = NULL;
504   int start = 0;
505   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
506   
507   while(start <= end){
508     cursor = (start + end) / 2;
509     current_region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
510     if(current_region->address == address){
511       heap_ignore_region_free(region);
512       if(!raw_mem_set)
513         MC_UNSET_RAW_MEM;
514       return;
515     }else if(current_region->address < address){
516       start = cursor + 1;
517     }else{
518       end = cursor - 1;
519     }   
520   }
521
522   if(current_region->address < address)
523     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, &region);
524   else
525     xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, &region);
526
527   if(!raw_mem_set)
528     MC_UNSET_RAW_MEM;
529 }
530
531 void MC_remove_ignore_heap(void *address, size_t size){
532
533   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
534
535   MC_SET_RAW_MEM;
536
537   unsigned int cursor = 0;
538   int start = 0;
539   int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
540   mc_heap_ignore_region_t region;
541   int ignore_found = 0;
542
543   while(start <= end){
544     cursor = (start + end) / 2;
545     region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
546     if(region->address == address){
547       ignore_found = 1;
548       break;
549     }else if(region->address < address){
550       start = cursor + 1;
551     }else{
552       if((char * )region->address <= ((char *)address + size)){
553         ignore_found = 1;
554         break;
555       }else{
556         end = cursor - 1;   
557       }
558     }
559   }
560   
561   if(ignore_found == 1){
562     xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
563     MC_remove_ignore_heap(address, size);
564   }
565
566   if(!raw_mem_set)
567     MC_UNSET_RAW_MEM;
568
569 }
570
571 void MC_ignore_global_variable(const char *name){
572
573   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
574
575   MC_SET_RAW_MEM;
576
577   xbt_assert(mc_libsimgrid_info, "MC subsystem not initialized");
578
579     unsigned int cursor = 0;
580     dw_variable_t current_var;
581     int start = 0;
582     int end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
583
584     while(start <= end){
585       cursor = (start + end) /2;
586       current_var = (dw_variable_t)xbt_dynar_get_as(mc_libsimgrid_info->global_variables, cursor, dw_variable_t);
587       if(strcmp(current_var->name, name) == 0){
588         xbt_dynar_remove_at(mc_libsimgrid_info->global_variables, cursor, NULL);
589         start = 0;
590         end = xbt_dynar_length(mc_libsimgrid_info->global_variables) - 1;
591       }else if(strcmp(current_var->name, name) < 0){
592         start = cursor + 1;
593       }else{
594         end = cursor - 1;
595       } 
596     }
597
598   if(!raw_mem_set)
599     MC_UNSET_RAW_MEM;
600 }
601
602 /** \brief Ignore a local variable in a scope
603  *
604  *  Ignore all instances of variables with a given name in
605  *  any (possibly inlined) subprogram with a given namespaced
606  *  name.
607  *
608  *  \param var_name        Name of the local variable (or parameter to ignore)
609  *  \param subprogram_name Name of the subprogram fo ignore (NULL for any)
610  *  \param subprogram      (possibly inlined) Subprogram of the scope
611  *  \param scope           Current scope
612  */
613 static void mc_ignore_local_variable_in_scope(
614   const char *var_name, const char *subprogram_name,
615   dw_frame_t subprogram, dw_frame_t scope) {
616   // Processing of direct variables:
617
618   // If the current subprogram matche the given name:
619   if(subprogram_name==NULL || strcmp(subprogram_name, subprogram->name)==0) {
620
621     // Try to find the variable and remove it:
622     int start = 0;
623     int end = xbt_dynar_length(scope->variables) - 1;
624
625     // Dichotomic search:
626     while(start <= end){
627       int cursor = (start + end) / 2;
628       dw_variable_t current_var = (dw_variable_t)xbt_dynar_get_as(scope->variables, cursor, dw_variable_t);
629
630       int compare = strcmp(current_var->name, var_name);
631       if(compare == 0){
632         // Variable found, remove it:
633         xbt_dynar_remove_at(scope->variables, cursor, NULL);
634
635         // and start again:
636         start = 0;
637         end = xbt_dynar_length(scope->variables) - 1;
638       }else if(compare < 0){
639         start = cursor + 1;
640       }else{
641         end = cursor - 1;
642       }
643     }
644
645   }
646
647   // And recursive processing in nested scopes:
648   unsigned cursor = 0;
649   dw_frame_t nested_scope = NULL;
650   xbt_dynar_foreach(scope->scopes, cursor, nested_scope) {
651     // The new scope may be an inlined subroutine, in this case we want to use its
652     // namespaced name in recursive calls:
653     dw_frame_t nested_subprogram = nested_scope->tag == DW_TAG_inlined_subroutine ? nested_scope : subprogram;
654
655     mc_ignore_local_variable_in_scope(var_name, subprogram_name, nested_subprogram, nested_scope);
656   }
657 }
658
659 static void MC_ignore_local_variable_in_object(const char *var_name, const char *subprogram_name, mc_object_info_t info) {
660   xbt_dict_cursor_t cursor2;
661   dw_frame_t frame;
662   char* key;
663   xbt_dict_foreach(info->subprograms, cursor2, key, frame) {
664     mc_ignore_local_variable_in_scope(var_name, subprogram_name, frame, frame);
665   }
666 }
667
668 void MC_ignore_local_variable(const char *var_name, const char *frame_name){
669   
670   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
671
672   if(strcmp(frame_name, "*") == 0)
673     frame_name = NULL;
674
675   MC_SET_RAW_MEM;
676
677   MC_ignore_local_variable_in_object(var_name, frame_name, mc_libsimgrid_info);
678   if(frame_name!=NULL)
679     MC_ignore_local_variable_in_object(var_name, frame_name, mc_binary_info);
680
681   if(!raw_mem_set)
682     MC_UNSET_RAW_MEM;
683
684 }
685
686 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
687
688   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
689
690   MC_SET_RAW_MEM;
691
692   if(stacks_areas == NULL)
693     stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
694   
695   stack_region_t region = NULL;
696   region = xbt_new0(s_stack_region_t, 1);
697   region->address = stack;
698   region->process_name = strdup(name);
699   region->context = context;
700   region->size = size;
701   region->block = ((char*)stack - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
702   xbt_dynar_push(stacks_areas, &region);
703
704   if(!raw_mem_set)
705     MC_UNSET_RAW_MEM;
706 }
707
708 void MC_ignore(void *addr, size_t size){
709
710   int raw_mem_set= (mmalloc_get_current_heap() == raw_heap);
711
712   MC_SET_RAW_MEM;
713
714   if(mc_checkpoint_ignore == NULL)
715     mc_checkpoint_ignore = xbt_dynar_new(sizeof(mc_checkpoint_ignore_region_t), checkpoint_ignore_region_free_voidp);
716
717   mc_checkpoint_ignore_region_t region = xbt_new0(s_mc_checkpoint_ignore_region_t, 1);
718   region->addr = addr;
719   region->size = size;
720
721   if(xbt_dynar_is_empty(mc_checkpoint_ignore)){
722     xbt_dynar_push(mc_checkpoint_ignore, &region);
723   }else{
724      
725     unsigned int cursor = 0;
726     int start = 0;
727     int end = xbt_dynar_length(mc_checkpoint_ignore) -1;
728     mc_checkpoint_ignore_region_t current_region = NULL;
729
730     while(start <= end){
731       cursor = (start + end) / 2;
732       current_region = (mc_checkpoint_ignore_region_t)xbt_dynar_get_as(mc_checkpoint_ignore, cursor, mc_checkpoint_ignore_region_t);
733       if(current_region->addr == addr){
734         if(current_region->size == size){
735           checkpoint_ignore_region_free(region);
736           if(!raw_mem_set)
737             MC_UNSET_RAW_MEM;
738           return;
739         }else if(current_region->size < size){
740           start = cursor + 1;
741         }else{
742           end = cursor - 1;
743         }
744       }else if(current_region->addr < addr){
745           start = cursor + 1;
746       }else{
747         end = cursor - 1;
748       }
749     }
750
751      if(current_region->addr == addr){
752        if(current_region->size < size){
753         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
754       }else{
755         xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
756       }
757     }else if(current_region->addr < addr){
758        xbt_dynar_insert_at(mc_checkpoint_ignore, cursor + 1, &region);
759     }else{
760        xbt_dynar_insert_at(mc_checkpoint_ignore, cursor, &region);
761     }
762   }
763
764   if(!raw_mem_set)
765     MC_UNSET_RAW_MEM;
766 }
767
768 /*******************************  Initialisation of MC *******************************/
769 /*********************************************************************************/
770
771 static void MC_post_process_object_info(mc_object_info_t info) {
772   xbt_dict_cursor_t cursor = NULL;
773   char* key = NULL;
774   dw_type_t type = NULL;
775   xbt_dict_foreach(info->types, cursor, key, type){
776
777     // Resolve full_type:
778     if(type->name && type->byte_size == 0) {
779       for(size_t i=0; i!=mc_object_infos_size; ++i) {
780         dw_type_t same_type =  xbt_dict_get_or_null(mc_object_infos[i]->full_types_by_name, type->name);
781         if(same_type && same_type->name && same_type->byte_size) {
782           type->full_type = same_type;
783           break;
784         }
785       }
786     }
787
788   }
789 }
790
791 static void MC_init_debug_info(void) {
792   XBT_INFO("Get debug information ...");
793
794   memory_map_t maps = MC_get_memory_map();
795
796   /* Get local variables for state equality detection */
797   mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
798   mc_object_infos[0] = mc_binary_info;
799
800   mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
801   mc_object_infos[1] = mc_libsimgrid_info;
802
803   // Use information of the other objects:
804   MC_post_process_object_info(mc_binary_info);
805   MC_post_process_object_info(mc_libsimgrid_info);
806
807   MC_free_memory_map(maps);
808   XBT_INFO("Get debug information done !");
809 }
810
811 void MC_init(){
812
813   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
814
815   compare = 0;
816
817   /* Initialize the data structures that must be persistent across every
818      iteration of the model-checker (in RAW memory) */
819
820   MC_SET_RAW_MEM;
821
822   MC_init_memory_map_info();
823   MC_init_debug_info();
824
825    /* Init parmap */
826   parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
827
828   MC_UNSET_RAW_MEM;
829
830    /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
831   MC_ignore_local_variable("e", "*");
832   MC_ignore_local_variable("__ex_cleanup", "*");
833   MC_ignore_local_variable("__ex_mctx_en", "*");
834   MC_ignore_local_variable("__ex_mctx_me", "*");
835   MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
836   MC_ignore_local_variable("_log_ev", "*");
837   MC_ignore_local_variable("_throw_ctx", "*");
838   MC_ignore_local_variable("ctx", "*");
839
840   MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
841   MC_ignore_local_variable("next_context", "smx_ctx_sysv_suspend_serial");
842   MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
843
844   /* Ignore local variable about time used for tracing */
845   MC_ignore_local_variable("start_time", "*"); 
846
847   MC_ignore_global_variable("compared_pointers");
848   MC_ignore_global_variable("mc_comp_times");
849   MC_ignore_global_variable("mc_snapshot_comparison_time"); 
850   MC_ignore_global_variable("mc_time");
851   MC_ignore_global_variable("smpi_current_rank");
852   MC_ignore_global_variable("counter"); /* Static variable used for tracing */
853   MC_ignore_global_variable("maestro_stack_start");
854   MC_ignore_global_variable("maestro_stack_end");
855   MC_ignore_global_variable("smx_total_comms");
856
857   MC_ignore_heap(&(simix_global->process_to_run), sizeof(simix_global->process_to_run));
858   MC_ignore_heap(&(simix_global->process_that_ran), sizeof(simix_global->process_that_ran));
859   MC_ignore_heap(simix_global->process_to_run, sizeof(*(simix_global->process_to_run)));
860   MC_ignore_heap(simix_global->process_that_ran, sizeof(*(simix_global->process_that_ran)));
861   
862   smx_process_t process;
863   xbt_swag_foreach(process, simix_global->process_list){
864     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
865   }
866
867   if(raw_mem_set)
868     MC_SET_RAW_MEM;
869
870 }
871
872 static void MC_init_dot_output(){ /* FIXME : more colors */
873
874   colors[0] = "blue";
875   colors[1] = "red";
876   colors[2] = "green3";
877   colors[3] = "goldenrod";
878   colors[4] = "brown";
879   colors[5] = "purple";
880   colors[6] = "magenta";
881   colors[7] = "turquoise4";
882   colors[8] = "gray25";
883   colors[9] = "forestgreen";
884   colors[10] = "hotpink";
885   colors[11] = "lightblue";
886   colors[12] = "tan";
887
888   dot_output = fopen(_sg_mc_dot_output_file, "w");
889   
890   if(dot_output == NULL){
891     perror("Error open dot output file");
892     xbt_abort();
893   }
894
895   fprintf(dot_output, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
896
897 }
898
899 /*******************************  Core of MC *******************************/
900 /**************************************************************************/
901
902 void MC_do_the_modelcheck_for_real() {
903
904   MC_SET_RAW_MEM;
905   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
906   MC_UNSET_RAW_MEM;
907   
908   if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
909     if (mc_reduce_kind==e_mc_reduce_unset)
910       mc_reduce_kind=e_mc_reduce_dpor;
911
912     XBT_INFO("Check a safety property");
913     MC_modelcheck_safety();
914
915   } else  {
916
917     if (mc_reduce_kind==e_mc_reduce_unset)
918       mc_reduce_kind=e_mc_reduce_none;
919
920     XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
921     MC_automaton_load(_sg_mc_property_file);
922     MC_modelcheck_liveness();
923   }
924 }
925
926 void MC_modelcheck_safety(void)
927 {
928   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
929
930   /* Check if MC is already initialized */
931   if (initial_state_safety)
932     return;
933
934   mc_time = xbt_new0(double, simix_process_maxpid);
935
936   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
937   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
938
939   /* Initialize the data structures that must be persistent across every
940      iteration of the model-checker (in RAW memory) */
941   
942   MC_SET_RAW_MEM;
943
944   /* Initialize statistics */
945   mc_stats = xbt_new0(s_mc_stats_t, 1);
946   mc_stats->state_size = 1;
947
948   /* Create exploration stack */
949   mc_stack_safety = xbt_fifo_new();
950
951   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
952     MC_init_dot_output();
953
954   MC_UNSET_RAW_MEM;
955
956   if(_sg_mc_visited > 0){
957     MC_init();
958   }else{
959     MC_SET_RAW_MEM;
960     MC_init_memory_map_info();
961     MC_init_debug_info();
962     MC_UNSET_RAW_MEM;
963   }
964
965   MC_dpor_init();
966
967   MC_SET_RAW_MEM;
968   /* Save the initial state */
969   initial_state_safety = xbt_new0(s_mc_global_t, 1);
970   initial_state_safety->snapshot = MC_take_snapshot(0);
971   initial_state_safety->initial_communications_pattern_done = 0;
972   initial_state_safety->comm_deterministic = 1;
973   initial_state_safety->send_deterministic = 1;
974   MC_UNSET_RAW_MEM;
975
976   MC_dpor();
977
978   if(raw_mem_set)
979     MC_SET_RAW_MEM;
980
981   xbt_abort();
982   //MC_exit();
983 }
984
985 void MC_modelcheck_liveness(){
986
987   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
988
989   MC_init();
990
991   mc_time = xbt_new0(double, simix_process_maxpid);
992
993   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
994   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
995  
996   MC_SET_RAW_MEM;
997  
998   /* Initialize statistics */
999   mc_stats = xbt_new0(s_mc_stats_t, 1);
1000   mc_stats->state_size = 1;
1001
1002   /* Create exploration stack */
1003   mc_stack_liveness = xbt_fifo_new();
1004
1005   /* Create the initial state */
1006   initial_state_liveness = xbt_new0(s_mc_global_t, 1);
1007
1008   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
1009     MC_init_dot_output();
1010   
1011   MC_UNSET_RAW_MEM;
1012
1013   MC_ddfs_init();
1014
1015   /* We're done */
1016   MC_print_statistics(mc_stats);
1017   xbt_free(mc_time);
1018
1019   if(raw_mem_set)
1020     MC_SET_RAW_MEM;
1021
1022 }
1023
1024
1025 void MC_exit(void)
1026 {
1027   xbt_free(mc_time);
1028
1029   MC_memory_exit();
1030   //xbt_abort();
1031 }
1032
1033 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max){
1034
1035   return simcall->mc_value;
1036 }
1037
1038
1039 int MC_random(int min, int max)
1040 {
1041   /*FIXME: return mc_current_state->executed_transition->random.value;*/
1042   return simcall_mc_random(min, max);
1043 }
1044
1045 /**
1046  * \brief Schedules all the process that are ready to run
1047  */
1048 void MC_wait_for_requests(void)
1049 {
1050   smx_process_t process;
1051   smx_simcall_t req;
1052   unsigned int iter;
1053
1054   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
1055     SIMIX_process_runall();
1056     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
1057       req = &process->simcall;
1058       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
1059         SIMIX_simcall_pre(req, 0);
1060     }
1061   }
1062 }
1063
1064 int MC_deadlock_check()
1065 {
1066   int deadlock = FALSE;
1067   smx_process_t process;
1068   if(xbt_swag_size(simix_global->process_list)){
1069     deadlock = TRUE;
1070     xbt_swag_foreach(process, simix_global->process_list){
1071       if(process->simcall.call != SIMCALL_NONE
1072          && MC_request_is_enabled(&process->simcall)){
1073         deadlock = FALSE;
1074         break;
1075       }
1076     }
1077   }
1078   return deadlock;
1079 }
1080
1081 /**
1082  * \brief Re-executes from the state at position start all the transitions indicated by
1083  *        a given model-checker stack.
1084  * \param stack The stack with the transitions to execute.
1085  * \param start Start index to begin the re-execution.
1086  */
1087 void MC_replay(xbt_fifo_t stack, int start)
1088 {
1089   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
1090
1091   int value, i = 1, count = 1;
1092   char *req_str;
1093   smx_simcall_t req = NULL, saved_req = NULL;
1094   xbt_fifo_item_t item, start_item;
1095   mc_state_t state;
1096   smx_process_t process = NULL;
1097   int comm_pattern = 0;
1098
1099   XBT_DEBUG("**** Begin Replay ****");
1100
1101   if(start == -1){
1102     /* Restore the initial state */
1103     MC_restore_snapshot(initial_state_safety->snapshot);
1104     /* At the moment of taking the snapshot the raw heap was set, so restoring
1105      * it will set it back again, we have to unset it to continue  */
1106     MC_UNSET_RAW_MEM;
1107   }
1108
1109   start_item = xbt_fifo_get_last_item(stack);
1110   if(start != -1){
1111     while (i != start){
1112       start_item = xbt_fifo_get_prev_item(start_item);
1113       i++;
1114     }
1115   }
1116
1117   MC_SET_RAW_MEM;
1118   xbt_dict_reset(first_enabled_state);
1119   xbt_swag_foreach(process, simix_global->process_list){
1120     if(MC_process_is_enabled(process)){
1121       char *key = bprintf("%lu", process->pid);
1122       char *data = bprintf("%d", count);
1123       xbt_dict_set(first_enabled_state, key, data, NULL);
1124       xbt_free(key);
1125     }
1126   }
1127   if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
1128     xbt_dynar_reset(communications_pattern);
1129   MC_UNSET_RAW_MEM;
1130   
1131
1132   /* Traverse the stack from the state at position start and re-execute the transitions */
1133   for (item = start_item;
1134        item != xbt_fifo_get_first_item(stack);
1135        item = xbt_fifo_get_prev_item(item)) {
1136
1137     state = (mc_state_t) xbt_fifo_get_item_content(item);
1138     saved_req = MC_state_get_executed_request(state, &value);
1139    
1140     MC_SET_RAW_MEM;
1141     char *key = bprintf("%lu", saved_req->issuer->pid);
1142     xbt_dict_remove(first_enabled_state, key); 
1143     xbt_free(key);
1144     MC_UNSET_RAW_MEM;
1145    
1146     if(saved_req){
1147       /* because we got a copy of the executed request, we have to fetch the  
1148          real one, pointed by the request field of the issuer process */
1149       req = &saved_req->issuer->simcall;
1150
1151       /* Debug information */
1152       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1153         req_str = MC_request_to_string(req, value);
1154         XBT_DEBUG("Replay: %s (%p)", req_str, state);
1155         xbt_free(req_str);
1156       }
1157     }
1158
1159     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1160       if(req->call == SIMCALL_COMM_ISEND)
1161         comm_pattern = 1;
1162       else if(req->call == SIMCALL_COMM_IRECV)
1163       comm_pattern = 2;
1164     }
1165
1166     SIMIX_simcall_pre(req, value);
1167
1168     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1169       MC_SET_RAW_MEM;
1170       if(comm_pattern != 0){
1171         get_comm_pattern(communications_pattern, req, comm_pattern);
1172       }
1173       MC_UNSET_RAW_MEM;
1174       comm_pattern = 0;
1175     }
1176     
1177     MC_wait_for_requests();
1178
1179     count++;
1180
1181     MC_SET_RAW_MEM;
1182     /* Insert in dict all enabled processes */
1183     xbt_swag_foreach(process, simix_global->process_list){
1184       if(MC_process_is_enabled(process) /*&& !MC_state_process_is_done(state, process)*/){
1185         char *key = bprintf("%lu", process->pid);
1186         if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
1187           char *data = bprintf("%d", count);
1188           xbt_dict_set(first_enabled_state, key, data, NULL);
1189         }
1190         xbt_free(key);
1191       }
1192     }
1193     MC_UNSET_RAW_MEM;
1194          
1195     /* Update statistics */
1196     mc_stats->visited_states++;
1197     mc_stats->executed_transitions++;
1198
1199   }
1200
1201   XBT_DEBUG("**** End Replay ****");
1202
1203   if(raw_mem)
1204     MC_SET_RAW_MEM;
1205   else
1206     MC_UNSET_RAW_MEM;
1207   
1208
1209 }
1210
1211 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
1212 {
1213
1214   initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1215
1216   int value;
1217   char *req_str;
1218   smx_simcall_t req = NULL, saved_req = NULL;
1219   xbt_fifo_item_t item;
1220   mc_state_t state;
1221   mc_pair_t pair;
1222   int depth = 1;
1223
1224   XBT_DEBUG("**** Begin Replay ****");
1225
1226   /* Restore the initial state */
1227   MC_restore_snapshot(initial_state_liveness->snapshot);
1228
1229   /* At the moment of taking the snapshot the raw heap was set, so restoring
1230    * it will set it back again, we have to unset it to continue  */
1231   if(!initial_state_liveness->raw_mem_set)
1232     MC_UNSET_RAW_MEM;
1233
1234   if(all_stack){
1235
1236     item = xbt_fifo_get_last_item(stack);
1237
1238     while(depth <= xbt_fifo_size(stack)){
1239
1240       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1241       state = (mc_state_t) pair->graph_state;
1242
1243       if(pair->requests > 0){
1244    
1245         saved_req = MC_state_get_executed_request(state, &value);
1246         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1247       
1248         if(saved_req != NULL){
1249           /* because we got a copy of the executed request, we have to fetch the  
1250              real one, pointed by the request field of the issuer process */
1251           req = &saved_req->issuer->simcall;
1252           //XBT_DEBUG("Req->call %u", req->call);
1253   
1254           /* Debug information */
1255           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1256             req_str = MC_request_to_string(req, value);
1257             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1258             xbt_free(req_str);
1259           }
1260   
1261         }
1262  
1263         SIMIX_simcall_pre(req, value);
1264         MC_wait_for_requests();
1265       }
1266
1267       depth++;
1268     
1269       /* Update statistics */
1270       mc_stats->visited_pairs++;
1271       mc_stats->executed_transitions++;
1272
1273       item = xbt_fifo_get_prev_item(item);
1274     }
1275
1276   }else{
1277
1278     /* Traverse the stack from the initial state and re-execute the transitions */
1279     for (item = xbt_fifo_get_last_item(stack);
1280          item != xbt_fifo_get_first_item(stack);
1281          item = xbt_fifo_get_prev_item(item)) {
1282
1283       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1284       state = (mc_state_t) pair->graph_state;
1285
1286       if(pair->requests > 0){
1287    
1288         saved_req = MC_state_get_executed_request(state, &value);
1289         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1290       
1291         if(saved_req != NULL){
1292           /* because we got a copy of the executed request, we have to fetch the  
1293              real one, pointed by the request field of the issuer process */
1294           req = &saved_req->issuer->simcall;
1295           //XBT_DEBUG("Req->call %u", req->call);
1296   
1297           /* Debug information */
1298           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1299             req_str = MC_request_to_string(req, value);
1300             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1301             xbt_free(req_str);
1302           }
1303   
1304         }
1305  
1306         SIMIX_simcall_pre(req, value);
1307         MC_wait_for_requests();
1308       }
1309
1310       depth++;
1311     
1312       /* Update statistics */
1313       mc_stats->visited_pairs++;
1314       mc_stats->executed_transitions++;
1315     }
1316   }  
1317
1318   XBT_DEBUG("**** End Replay ****");
1319
1320   if(initial_state_liveness->raw_mem_set)
1321     MC_SET_RAW_MEM;
1322   else
1323     MC_UNSET_RAW_MEM;
1324   
1325 }
1326
1327 /**
1328  * \brief Dumps the contents of a model-checker's stack and shows the actual
1329  *        execution trace
1330  * \param stack The stack to dump
1331  */
1332 void MC_dump_stack_safety(xbt_fifo_t stack)
1333 {
1334   
1335   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1336
1337   MC_show_stack_safety(stack);
1338
1339   if(!_sg_mc_checkpoint){
1340
1341     mc_state_t state;
1342
1343     MC_SET_RAW_MEM;
1344     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
1345       MC_state_delete(state);
1346     MC_UNSET_RAW_MEM;
1347
1348   }
1349
1350   if(raw_mem_set)
1351     MC_SET_RAW_MEM;
1352   else
1353     MC_UNSET_RAW_MEM;
1354   
1355 }
1356
1357
1358 void MC_show_stack_safety(xbt_fifo_t stack)
1359 {
1360
1361   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1362
1363   MC_SET_RAW_MEM;
1364
1365   int value;
1366   mc_state_t state;
1367   xbt_fifo_item_t item;
1368   smx_simcall_t req;
1369   char *req_str = NULL;
1370   
1371   for (item = xbt_fifo_get_last_item(stack);
1372        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
1373         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1374     req = MC_state_get_executed_request(state, &value);
1375     if(req){
1376       req_str = MC_request_to_string(req, value);
1377       XBT_INFO("%s", req_str);
1378       xbt_free(req_str);
1379     }
1380   }
1381
1382   if(!raw_mem_set)
1383     MC_UNSET_RAW_MEM;
1384 }
1385
1386 void MC_show_deadlock(smx_simcall_t req)
1387 {
1388   /*char *req_str = NULL;*/
1389   XBT_INFO("**************************");
1390   XBT_INFO("*** DEAD-LOCK DETECTED ***");
1391   XBT_INFO("**************************");
1392   XBT_INFO("Locked request:");
1393   /*req_str = MC_request_to_string(req);
1394     XBT_INFO("%s", req_str);
1395     xbt_free(req_str);*/
1396   XBT_INFO("Counter-example execution trace:");
1397   MC_dump_stack_safety(mc_stack_safety);
1398   MC_print_statistics(mc_stats);
1399 }
1400
1401
1402 void MC_show_stack_liveness(xbt_fifo_t stack){
1403   int value;
1404   mc_pair_t pair;
1405   xbt_fifo_item_t item;
1406   smx_simcall_t req;
1407   char *req_str = NULL;
1408   
1409   for (item = xbt_fifo_get_last_item(stack);
1410        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
1411         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1412     req = MC_state_get_executed_request(pair->graph_state, &value);
1413     if(req){
1414       if(pair->requests>0){
1415         req_str = MC_request_to_string(req, value);
1416         XBT_INFO("%s", req_str);
1417         xbt_free(req_str);
1418       }else{
1419         XBT_INFO("End of system requests but evolution in Büchi automaton");
1420       }
1421     }
1422   }
1423 }
1424
1425 void MC_dump_stack_liveness(xbt_fifo_t stack){
1426
1427   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1428
1429   mc_pair_t pair;
1430
1431   MC_SET_RAW_MEM;
1432   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
1433     MC_pair_delete(pair);
1434   MC_UNSET_RAW_MEM;
1435
1436   if(raw_mem_set)
1437     MC_SET_RAW_MEM;
1438
1439 }
1440
1441
1442 void MC_print_statistics(mc_stats_t stats)
1443 {
1444   if(stats->expanded_pairs == 0){
1445     XBT_INFO("Expanded states = %lu", stats->expanded_states);
1446     XBT_INFO("Visited states = %lu", stats->visited_states);
1447   }else{
1448     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
1449     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
1450   }
1451   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
1452   MC_SET_RAW_MEM;
1453   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
1454     fprintf(dot_output, "}\n");
1455     fclose(dot_output);
1456   }
1457   if(initial_state_safety != NULL){
1458     if(_sg_mc_comms_determinism)
1459       XBT_INFO("Communication-deterministic : %s", !initial_state_safety->comm_deterministic ? "No" : "Yes");
1460     if (_sg_mc_send_determinism)
1461       XBT_INFO("Send-deterministic : %s", !initial_state_safety->send_deterministic ? "No" : "Yes");
1462   }
1463   MC_UNSET_RAW_MEM;
1464 }
1465
1466 void MC_assert(int prop)
1467 {
1468   if (MC_is_active() && !prop){
1469     XBT_INFO("**************************");
1470     XBT_INFO("*** PROPERTY NOT VALID ***");
1471     XBT_INFO("**************************");
1472     XBT_INFO("Counter-example execution trace:");
1473     MC_dump_stack_safety(mc_stack_safety);
1474     MC_print_statistics(mc_stats);
1475     xbt_abort();
1476   }
1477 }
1478
1479 void MC_cut(void){
1480   user_max_depth_reached = 1;
1481 }
1482
1483 void MC_process_clock_add(smx_process_t process, double amount)
1484 {
1485   mc_time[process->pid] += amount;
1486 }
1487
1488 double MC_process_clock_get(smx_process_t process)
1489 {
1490   if(mc_time){
1491     if(process != NULL)
1492       return mc_time[process->pid];
1493     else 
1494       return -1;
1495   }else{
1496     return 0;
1497   }
1498 }
1499
1500 void MC_automaton_load(const char *file){
1501
1502   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1503
1504   MC_SET_RAW_MEM;
1505
1506   if (_mc_property_automaton == NULL)
1507     _mc_property_automaton = xbt_automaton_new();
1508   
1509   xbt_automaton_load(_mc_property_automaton,file);
1510
1511   MC_UNSET_RAW_MEM;
1512
1513   if(raw_mem_set)
1514     MC_SET_RAW_MEM;
1515
1516 }
1517
1518 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
1519
1520   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1521
1522   MC_SET_RAW_MEM;
1523
1524   if (_mc_property_automaton == NULL)
1525     _mc_property_automaton = xbt_automaton_new();
1526
1527   xbt_automaton_propositional_symbol_new(_mc_property_automaton,id,fct);
1528
1529   MC_UNSET_RAW_MEM;
1530
1531   if(raw_mem_set)
1532     MC_SET_RAW_MEM;
1533   
1534 }
1535
1536
1537