Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[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("counter"); /* Static variable used for tracing */
852   MC_ignore_global_variable("maestro_stack_start");
853   MC_ignore_global_variable("maestro_stack_end");
854   MC_ignore_global_variable("smx_total_comms");
855
856   MC_ignore_heap(&(simix_global->process_to_run), sizeof(simix_global->process_to_run));
857   MC_ignore_heap(&(simix_global->process_that_ran), sizeof(simix_global->process_that_ran));
858   MC_ignore_heap(simix_global->process_to_run, sizeof(*(simix_global->process_to_run)));
859   MC_ignore_heap(simix_global->process_that_ran, sizeof(*(simix_global->process_that_ran)));
860   
861   smx_process_t process;
862   xbt_swag_foreach(process, simix_global->process_list){
863     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
864   }
865
866   if(raw_mem_set)
867     MC_SET_RAW_MEM;
868
869 }
870
871 static void MC_init_dot_output(){ /* FIXME : more colors */
872
873   colors[0] = "blue";
874   colors[1] = "red";
875   colors[2] = "green3";
876   colors[3] = "goldenrod";
877   colors[4] = "brown";
878   colors[5] = "purple";
879   colors[6] = "magenta";
880   colors[7] = "turquoise4";
881   colors[8] = "gray25";
882   colors[9] = "forestgreen";
883   colors[10] = "hotpink";
884   colors[11] = "lightblue";
885   colors[12] = "tan";
886
887   dot_output = fopen(_sg_mc_dot_output_file, "w");
888   
889   if(dot_output == NULL){
890     perror("Error open dot output file");
891     xbt_abort();
892   }
893
894   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");
895
896 }
897
898 /*******************************  Core of MC *******************************/
899 /**************************************************************************/
900
901 void MC_do_the_modelcheck_for_real() {
902
903   MC_SET_RAW_MEM;
904   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
905   MC_UNSET_RAW_MEM;
906   
907   if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
908     if (mc_reduce_kind==e_mc_reduce_unset)
909       mc_reduce_kind=e_mc_reduce_dpor;
910
911     XBT_INFO("Check a safety property");
912     MC_modelcheck_safety();
913
914   } else  {
915
916     if (mc_reduce_kind==e_mc_reduce_unset)
917       mc_reduce_kind=e_mc_reduce_none;
918
919     XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
920     MC_automaton_load(_sg_mc_property_file);
921     MC_modelcheck_liveness();
922   }
923 }
924
925 void MC_modelcheck_safety(void)
926 {
927   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
928
929   /* Check if MC is already initialized */
930   if (initial_state_safety)
931     return;
932
933   mc_time = xbt_new0(double, simix_process_maxpid);
934
935   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
936   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
937
938   /* Initialize the data structures that must be persistent across every
939      iteration of the model-checker (in RAW memory) */
940   
941   MC_SET_RAW_MEM;
942
943   /* Initialize statistics */
944   mc_stats = xbt_new0(s_mc_stats_t, 1);
945   mc_stats->state_size = 1;
946
947   /* Create exploration stack */
948   mc_stack_safety = xbt_fifo_new();
949
950   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
951     MC_init_dot_output();
952
953   MC_UNSET_RAW_MEM;
954
955   if(_sg_mc_visited > 0){
956     MC_init();
957   }else{
958     MC_SET_RAW_MEM;
959     MC_init_memory_map_info();
960     MC_init_debug_info();
961     MC_UNSET_RAW_MEM;
962   }
963
964   MC_dpor_init();
965
966   MC_SET_RAW_MEM;
967   /* Save the initial state */
968   initial_state_safety = xbt_new0(s_mc_global_t, 1);
969   initial_state_safety->snapshot = MC_take_snapshot(0);
970   initial_state_safety->initial_communications_pattern_done = 0;
971   initial_state_safety->comm_deterministic = 1;
972   initial_state_safety->send_deterministic = 1;
973   MC_UNSET_RAW_MEM;
974
975   MC_dpor();
976
977   if(raw_mem_set)
978     MC_SET_RAW_MEM;
979
980   xbt_abort();
981   //MC_exit();
982 }
983
984 void MC_modelcheck_liveness(){
985
986   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
987
988   MC_init();
989
990   mc_time = xbt_new0(double, simix_process_maxpid);
991
992   /* mc_time refers to clock for each process -> ignore it for heap comparison */  
993   MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
994  
995   MC_SET_RAW_MEM;
996  
997   /* Initialize statistics */
998   mc_stats = xbt_new0(s_mc_stats_t, 1);
999   mc_stats->state_size = 1;
1000
1001   /* Create exploration stack */
1002   mc_stack_liveness = xbt_fifo_new();
1003
1004   /* Create the initial state */
1005   initial_state_liveness = xbt_new0(s_mc_global_t, 1);
1006
1007   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0'))
1008     MC_init_dot_output();
1009   
1010   MC_UNSET_RAW_MEM;
1011
1012   MC_ddfs_init();
1013
1014   /* We're done */
1015   MC_print_statistics(mc_stats);
1016   xbt_free(mc_time);
1017
1018   if(raw_mem_set)
1019     MC_SET_RAW_MEM;
1020
1021 }
1022
1023
1024 void MC_exit(void)
1025 {
1026   xbt_free(mc_time);
1027
1028   MC_memory_exit();
1029   //xbt_abort();
1030 }
1031
1032 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max){
1033
1034   return simcall->mc_value;
1035 }
1036
1037
1038 int MC_random(int min, int max)
1039 {
1040   /*FIXME: return mc_current_state->executed_transition->random.value;*/
1041   return simcall_mc_random(min, max);
1042 }
1043
1044 /**
1045  * \brief Schedules all the process that are ready to run
1046  */
1047 void MC_wait_for_requests(void)
1048 {
1049   smx_process_t process;
1050   smx_simcall_t req;
1051   unsigned int iter;
1052
1053   while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
1054     SIMIX_process_runall();
1055     xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
1056       req = &process->simcall;
1057       if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
1058         SIMIX_simcall_pre(req, 0);
1059     }
1060   }
1061 }
1062
1063 int MC_deadlock_check()
1064 {
1065   int deadlock = FALSE;
1066   smx_process_t process;
1067   if(xbt_swag_size(simix_global->process_list)){
1068     deadlock = TRUE;
1069     xbt_swag_foreach(process, simix_global->process_list){
1070       if(process->simcall.call != SIMCALL_NONE
1071          && MC_request_is_enabled(&process->simcall)){
1072         deadlock = FALSE;
1073         break;
1074       }
1075     }
1076   }
1077   return deadlock;
1078 }
1079
1080 /**
1081  * \brief Re-executes from the state at position start all the transitions indicated by
1082  *        a given model-checker stack.
1083  * \param stack The stack with the transitions to execute.
1084  * \param start Start index to begin the re-execution.
1085  */
1086 void MC_replay(xbt_fifo_t stack, int start)
1087 {
1088   int raw_mem = (mmalloc_get_current_heap() == raw_heap);
1089
1090   int value, i = 1, count = 1;
1091   char *req_str;
1092   smx_simcall_t req = NULL, saved_req = NULL;
1093   xbt_fifo_item_t item, start_item;
1094   mc_state_t state;
1095   smx_process_t process = NULL;
1096   int comm_pattern = 0;
1097
1098   XBT_DEBUG("**** Begin Replay ****");
1099
1100   if(start == -1){
1101     /* Restore the initial state */
1102     MC_restore_snapshot(initial_state_safety->snapshot);
1103     /* At the moment of taking the snapshot the raw heap was set, so restoring
1104      * it will set it back again, we have to unset it to continue  */
1105     MC_UNSET_RAW_MEM;
1106   }
1107
1108   start_item = xbt_fifo_get_last_item(stack);
1109   if(start != -1){
1110     while (i != start){
1111       start_item = xbt_fifo_get_prev_item(start_item);
1112       i++;
1113     }
1114   }
1115
1116   MC_SET_RAW_MEM;
1117
1118   if(mc_reduce_kind ==  e_mc_reduce_dpor){
1119     xbt_dict_reset(first_enabled_state);
1120     xbt_swag_foreach(process, simix_global->process_list){
1121       if(MC_process_is_enabled(process)){
1122       char *key = bprintf("%lu", process->pid);
1123       char *data = bprintf("%d", count);
1124       xbt_dict_set(first_enabled_state, key, data, NULL);
1125       xbt_free(key);
1126       }
1127     }
1128   }
1129
1130   if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1131     xbt_dynar_reset(communications_pattern);
1132     xbt_dynar_reset(incomplete_communications_pattern);
1133   }
1134
1135   MC_UNSET_RAW_MEM;
1136   
1137
1138   /* Traverse the stack from the state at position start and re-execute the transitions */
1139   for (item = start_item;
1140        item != xbt_fifo_get_first_item(stack);
1141        item = xbt_fifo_get_prev_item(item)) {
1142
1143     state = (mc_state_t) xbt_fifo_get_item_content(item);
1144     saved_req = MC_state_get_executed_request(state, &value);
1145    
1146     if(mc_reduce_kind ==  e_mc_reduce_dpor){
1147       MC_SET_RAW_MEM;
1148       char *key = bprintf("%lu", saved_req->issuer->pid);
1149       xbt_dict_remove(first_enabled_state, key); 
1150       xbt_free(key);
1151       MC_UNSET_RAW_MEM;
1152     }
1153    
1154     if(saved_req){
1155       /* because we got a copy of the executed request, we have to fetch the  
1156          real one, pointed by the request field of the issuer process */
1157       req = &saved_req->issuer->simcall;
1158
1159       /* Debug information */
1160       if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1161         req_str = MC_request_to_string(req, value);
1162         XBT_DEBUG("Replay: %s (%p)", req_str, state);
1163         xbt_free(req_str);
1164       }
1165     }
1166
1167     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1168       if(req->call == SIMCALL_COMM_ISEND)
1169         comm_pattern = 1;
1170       else if(req->call == SIMCALL_COMM_IRECV)
1171         comm_pattern = 2;
1172     }
1173
1174     SIMIX_simcall_pre(req, value);
1175
1176     if(_sg_mc_comms_determinism || _sg_mc_send_determinism){
1177       MC_SET_RAW_MEM;
1178       if(comm_pattern != 0){
1179         get_comm_pattern(communications_pattern, req, comm_pattern);
1180       }
1181       MC_UNSET_RAW_MEM;
1182       comm_pattern = 0;
1183     }
1184     
1185     MC_wait_for_requests();
1186
1187     count++;
1188
1189     if(mc_reduce_kind ==  e_mc_reduce_dpor){
1190       MC_SET_RAW_MEM;
1191       /* Insert in dict all enabled processes */
1192       xbt_swag_foreach(process, simix_global->process_list){
1193         if(MC_process_is_enabled(process) /*&& !MC_state_process_is_done(state, process)*/){
1194           char *key = bprintf("%lu", process->pid);
1195           if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
1196             char *data = bprintf("%d", count);
1197             xbt_dict_set(first_enabled_state, key, data, NULL);
1198           }
1199           xbt_free(key);
1200         }
1201       }
1202       MC_UNSET_RAW_MEM;
1203     }
1204          
1205     /* Update statistics */
1206     mc_stats->visited_states++;
1207     mc_stats->executed_transitions++;
1208
1209   }
1210
1211   XBT_DEBUG("**** End Replay ****");
1212
1213   if(raw_mem)
1214     MC_SET_RAW_MEM;
1215   else
1216     MC_UNSET_RAW_MEM;
1217   
1218
1219 }
1220
1221 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
1222 {
1223
1224   initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1225
1226   int value;
1227   char *req_str;
1228   smx_simcall_t req = NULL, saved_req = NULL;
1229   xbt_fifo_item_t item;
1230   mc_state_t state;
1231   mc_pair_t pair;
1232   int depth = 1;
1233
1234   XBT_DEBUG("**** Begin Replay ****");
1235
1236   /* Restore the initial state */
1237   MC_restore_snapshot(initial_state_liveness->snapshot);
1238
1239   /* At the moment of taking the snapshot the raw heap was set, so restoring
1240    * it will set it back again, we have to unset it to continue  */
1241   if(!initial_state_liveness->raw_mem_set)
1242     MC_UNSET_RAW_MEM;
1243
1244   if(all_stack){
1245
1246     item = xbt_fifo_get_last_item(stack);
1247
1248     while(depth <= xbt_fifo_size(stack)){
1249
1250       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1251       state = (mc_state_t) pair->graph_state;
1252
1253       if(pair->requests > 0){
1254    
1255         saved_req = MC_state_get_executed_request(state, &value);
1256         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1257       
1258         if(saved_req != NULL){
1259           /* because we got a copy of the executed request, we have to fetch the  
1260              real one, pointed by the request field of the issuer process */
1261           req = &saved_req->issuer->simcall;
1262           //XBT_DEBUG("Req->call %u", req->call);
1263   
1264           /* Debug information */
1265           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1266             req_str = MC_request_to_string(req, value);
1267             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1268             xbt_free(req_str);
1269           }
1270   
1271         }
1272  
1273         SIMIX_simcall_pre(req, value);
1274         MC_wait_for_requests();
1275       }
1276
1277       depth++;
1278     
1279       /* Update statistics */
1280       mc_stats->visited_pairs++;
1281       mc_stats->executed_transitions++;
1282
1283       item = xbt_fifo_get_prev_item(item);
1284     }
1285
1286   }else{
1287
1288     /* Traverse the stack from the initial state and re-execute the transitions */
1289     for (item = xbt_fifo_get_last_item(stack);
1290          item != xbt_fifo_get_first_item(stack);
1291          item = xbt_fifo_get_prev_item(item)) {
1292
1293       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
1294       state = (mc_state_t) pair->graph_state;
1295
1296       if(pair->requests > 0){
1297    
1298         saved_req = MC_state_get_executed_request(state, &value);
1299         //XBT_DEBUG("SavedReq->call %u", saved_req->call);
1300       
1301         if(saved_req != NULL){
1302           /* because we got a copy of the executed request, we have to fetch the  
1303              real one, pointed by the request field of the issuer process */
1304           req = &saved_req->issuer->simcall;
1305           //XBT_DEBUG("Req->call %u", req->call);
1306   
1307           /* Debug information */
1308           if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
1309             req_str = MC_request_to_string(req, value);
1310             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
1311             xbt_free(req_str);
1312           }
1313   
1314         }
1315  
1316         SIMIX_simcall_pre(req, value);
1317         MC_wait_for_requests();
1318       }
1319
1320       depth++;
1321     
1322       /* Update statistics */
1323       mc_stats->visited_pairs++;
1324       mc_stats->executed_transitions++;
1325     }
1326   }  
1327
1328   XBT_DEBUG("**** End Replay ****");
1329
1330   if(initial_state_liveness->raw_mem_set)
1331     MC_SET_RAW_MEM;
1332   else
1333     MC_UNSET_RAW_MEM;
1334   
1335 }
1336
1337 /**
1338  * \brief Dumps the contents of a model-checker's stack and shows the actual
1339  *        execution trace
1340  * \param stack The stack to dump
1341  */
1342 void MC_dump_stack_safety(xbt_fifo_t stack)
1343 {
1344   
1345   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1346
1347   MC_show_stack_safety(stack);
1348
1349   if(!_sg_mc_checkpoint){
1350
1351     mc_state_t state;
1352
1353     MC_SET_RAW_MEM;
1354     while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
1355       MC_state_delete(state);
1356     MC_UNSET_RAW_MEM;
1357
1358   }
1359
1360   if(raw_mem_set)
1361     MC_SET_RAW_MEM;
1362   else
1363     MC_UNSET_RAW_MEM;
1364   
1365 }
1366
1367
1368 void MC_show_stack_safety(xbt_fifo_t stack)
1369 {
1370
1371   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1372
1373   MC_SET_RAW_MEM;
1374
1375   int value;
1376   mc_state_t state;
1377   xbt_fifo_item_t item;
1378   smx_simcall_t req;
1379   char *req_str = NULL;
1380   
1381   for (item = xbt_fifo_get_last_item(stack);
1382        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
1383         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1384     req = MC_state_get_executed_request(state, &value);
1385     if(req){
1386       req_str = MC_request_to_string(req, value);
1387       XBT_INFO("%s", req_str);
1388       xbt_free(req_str);
1389     }
1390   }
1391
1392   if(!raw_mem_set)
1393     MC_UNSET_RAW_MEM;
1394 }
1395
1396 void MC_show_deadlock(smx_simcall_t req)
1397 {
1398   /*char *req_str = NULL;*/
1399   XBT_INFO("**************************");
1400   XBT_INFO("*** DEAD-LOCK DETECTED ***");
1401   XBT_INFO("**************************");
1402   XBT_INFO("Locked request:");
1403   /*req_str = MC_request_to_string(req);
1404     XBT_INFO("%s", req_str);
1405     xbt_free(req_str);*/
1406   XBT_INFO("Counter-example execution trace:");
1407   MC_dump_stack_safety(mc_stack_safety);
1408   MC_print_statistics(mc_stats);
1409 }
1410
1411
1412 void MC_show_stack_liveness(xbt_fifo_t stack){
1413   int value;
1414   mc_pair_t pair;
1415   xbt_fifo_item_t item;
1416   smx_simcall_t req;
1417   char *req_str = NULL;
1418   
1419   for (item = xbt_fifo_get_last_item(stack);
1420        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
1421         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
1422     req = MC_state_get_executed_request(pair->graph_state, &value);
1423     if(req){
1424       if(pair->requests>0){
1425         req_str = MC_request_to_string(req, value);
1426         XBT_INFO("%s", req_str);
1427         xbt_free(req_str);
1428       }else{
1429         XBT_INFO("End of system requests but evolution in Büchi automaton");
1430       }
1431     }
1432   }
1433 }
1434
1435 void MC_dump_stack_liveness(xbt_fifo_t stack){
1436
1437   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1438
1439   mc_pair_t pair;
1440
1441   MC_SET_RAW_MEM;
1442   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
1443     MC_pair_delete(pair);
1444   MC_UNSET_RAW_MEM;
1445
1446   if(raw_mem_set)
1447     MC_SET_RAW_MEM;
1448
1449 }
1450
1451
1452 void MC_print_statistics(mc_stats_t stats)
1453 {
1454   xbt_mheap_t previous_heap = mmalloc_get_current_heap();
1455
1456   if(stats->expanded_pairs == 0){
1457     XBT_INFO("Expanded states = %lu", stats->expanded_states);
1458     XBT_INFO("Visited states = %lu", stats->visited_states);
1459   }else{
1460     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
1461     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
1462   }
1463   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
1464
1465   MC_SET_RAW_MEM;
1466   if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
1467     fprintf(dot_output, "}\n");
1468     fclose(dot_output);
1469   }
1470   if(initial_state_safety != NULL){
1471     if(_sg_mc_comms_determinism)
1472       XBT_INFO("Communication-deterministic : %s", !initial_state_safety->comm_deterministic ? "No" : "Yes");
1473     if (_sg_mc_send_determinism)
1474       XBT_INFO("Send-deterministic : %s", !initial_state_safety->send_deterministic ? "No" : "Yes");
1475   }
1476   mmalloc_set_current_heap(previous_heap);
1477 }
1478
1479 void MC_assert(int prop)
1480 {
1481   if (MC_is_active() && !prop){
1482     XBT_INFO("**************************");
1483     XBT_INFO("*** PROPERTY NOT VALID ***");
1484     XBT_INFO("**************************");
1485     XBT_INFO("Counter-example execution trace:");
1486     MC_dump_stack_safety(mc_stack_safety);
1487     MC_print_statistics(mc_stats);
1488     xbt_abort();
1489   }
1490 }
1491
1492 void MC_cut(void){
1493   user_max_depth_reached = 1;
1494 }
1495
1496 void MC_process_clock_add(smx_process_t process, double amount)
1497 {
1498   mc_time[process->pid] += amount;
1499 }
1500
1501 double MC_process_clock_get(smx_process_t process)
1502 {
1503   if(mc_time){
1504     if(process != NULL)
1505       return mc_time[process->pid];
1506     else 
1507       return -1;
1508   }else{
1509     return 0;
1510   }
1511 }
1512
1513 void MC_automaton_load(const char *file){
1514
1515   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1516
1517   MC_SET_RAW_MEM;
1518
1519   if (_mc_property_automaton == NULL)
1520     _mc_property_automaton = xbt_automaton_new();
1521   
1522   xbt_automaton_load(_mc_property_automaton,file);
1523
1524   MC_UNSET_RAW_MEM;
1525
1526   if(raw_mem_set)
1527     MC_SET_RAW_MEM;
1528
1529 }
1530
1531 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
1532
1533   int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1534
1535   MC_SET_RAW_MEM;
1536
1537   if (_mc_property_automaton == NULL)
1538     _mc_property_automaton = xbt_automaton_new();
1539
1540   xbt_automaton_propositional_symbol_new(_mc_property_automaton,id,fct);
1541
1542   MC_UNSET_RAW_MEM;
1543
1544   if(raw_mem_set)
1545     MC_SET_RAW_MEM;
1546   
1547 }
1548
1549
1550