Logo AND Algorithmique Numérique Distribuée

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