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 "mc_base.h"
8
9 #ifndef _XBT_WIN32
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/time.h>
14 #include <sys/mman.h>
15 #include <libgen.h>
16 #endif
17
18 #include "simgrid/sg_config.h"
19 #include "../surf/surf_private.h"
20 #include "../simix/smx_private.h"
21 #include "xbt/fifo.h"
22 #include "xbt/automaton.h"
23 #include "xbt/dict.h"
24
25 #ifdef HAVE_MC
26 #define UNW_LOCAL_ONLY
27 #include <libunwind.h>
28
29 #include "../xbt/mmalloc/mmprivate.h"
30 #include "mc_object_info.h"
31 #include "mc_comm_pattern.h"
32 #include "mc_request.h"
33 #include "mc_safety.h"
34 #include "mc_memory_map.h"
35 #include "mc_snapshot.h"
36 #include "mc_liveness.h"
37 #include "mc_private.h"
38 #endif
39 #include "mc_record.h"
40
41 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
42                                 "Logging specific to MC (global)");
43
44 double *mc_time = NULL;
45
46 #ifdef HAVE_MC
47 int user_max_depth_reached = 0;
48
49 /* MC global data structures */
50 mc_state_t mc_current_state = NULL;
51 char mc_replay_mode = FALSE;
52
53 __thread mc_comparison_times_t mc_comp_times = NULL;
54 __thread double mc_snapshot_comparison_time;
55 mc_stats_t mc_stats = NULL;
56 mc_global_t initial_global_state = NULL;
57 xbt_fifo_t mc_stack = NULL;
58
59 /* Liveness */
60 xbt_automaton_t _mc_property_automaton = NULL;
61
62 /* Variables */
63 mc_object_info_t mc_libsimgrid_info = NULL;
64 mc_object_info_t mc_binary_info = NULL;
65
66 mc_object_info_t mc_object_infos[2] = { NULL, NULL };
67
68 size_t mc_object_infos_size = 2;
69
70 /* Dot output */
71 FILE *dot_output = NULL;
72 const char *colors[13];
73
74
75 /*******************************  Initialisation of MC *******************************/
76 /*********************************************************************************/
77
78 static void MC_init_dot_output()
79 {                               /* FIXME : more colors */
80
81   colors[0] = "blue";
82   colors[1] = "red";
83   colors[2] = "green3";
84   colors[3] = "goldenrod";
85   colors[4] = "brown";
86   colors[5] = "purple";
87   colors[6] = "magenta";
88   colors[7] = "turquoise4";
89   colors[8] = "gray25";
90   colors[9] = "forestgreen";
91   colors[10] = "hotpink";
92   colors[11] = "lightblue";
93   colors[12] = "tan";
94
95   dot_output = fopen(_sg_mc_dot_output_file, "w");
96
97   if (dot_output == NULL) {
98     perror("Error open dot output file");
99     xbt_abort();
100   }
101
102   fprintf(dot_output,
103           "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");
104
105 }
106
107 static void MC_init_debug_info(void)
108 {
109   XBT_INFO("Get debug information ...");
110
111   memory_map_t maps = MC_get_memory_map();
112
113   /* Get local variables for state equality detection */
114   mc_binary_info = MC_find_object_info(maps, xbt_binary_name, 1);
115   mc_object_infos[0] = mc_binary_info;
116
117   mc_libsimgrid_info = MC_find_object_info(maps, libsimgrid_path, 0);
118   mc_object_infos[1] = mc_libsimgrid_info;
119
120   // Use information of the other objects:
121   MC_post_process_object_info(mc_binary_info);
122   MC_post_process_object_info(mc_libsimgrid_info);
123
124   MC_free_memory_map(maps);
125   XBT_INFO("Get debug information done !");
126 }
127
128
129 mc_model_checker_t mc_model_checker = NULL;
130
131 mc_model_checker_t MC_model_checker_new()
132 {
133   mc_model_checker_t mc = xbt_new0(s_mc_model_checker_t, 1);
134   mc->pages = mc_pages_store_new();
135   mc->fd_clear_refs = -1;
136   mc->fd_pagemap = -1;
137   return mc;
138 }
139
140 void MC_model_checker_delete(mc_model_checker_t mc) {
141   mc_pages_store_delete(mc->pages);
142   if(mc->record)
143     xbt_dynar_free(&mc->record);
144 }
145
146 void MC_init()
147 {
148   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
149
150   mc_time = xbt_new0(double, simix_process_maxpid);
151
152   /* Initialize the data structures that must be persistent across every
153      iteration of the model-checker (in RAW memory) */
154
155   MC_SET_MC_HEAP;
156
157   mc_model_checker = MC_model_checker_new();
158
159   mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
160
161   /* Initialize statistics */
162   mc_stats = xbt_new0(s_mc_stats_t, 1);
163   mc_stats->state_size = 1;
164
165   MC_init_memory_map_info();
166   MC_init_debug_info();         /* FIXME : get debug information only if liveness verification or visited state reduction */
167
168   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0'))
169     MC_init_dot_output();
170
171   /* Init parmap */
172   //parmap = xbt_parmap_mc_new(xbt_os_get_numcores(), XBT_PARMAP_DEFAULT);
173
174   MC_SET_STD_HEAP;
175
176   if (_sg_mc_visited > 0 || _sg_mc_liveness  || _sg_mc_termination) {
177     /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
178     MC_ignore_local_variable("e", "*");
179     MC_ignore_local_variable("__ex_cleanup", "*");
180     MC_ignore_local_variable("__ex_mctx_en", "*");
181     MC_ignore_local_variable("__ex_mctx_me", "*");
182     MC_ignore_local_variable("__xbt_ex_ctx_ptr", "*");
183     MC_ignore_local_variable("_log_ev", "*");
184     MC_ignore_local_variable("_throw_ctx", "*");
185     MC_ignore_local_variable("ctx", "*");
186
187     MC_ignore_local_variable("self", "simcall_BODY_mc_snapshot");
188     MC_ignore_local_variable("next_cont"
189       "ext", "smx_ctx_sysv_suspend_serial");
190     MC_ignore_local_variable("i", "smx_ctx_sysv_suspend_serial");
191
192     /* Ignore local variable about time used for tracing */
193     MC_ignore_local_variable("start_time", "*");
194
195     /* Main MC state: */
196     MC_ignore_global_variable("mc_model_checker");
197     MC_ignore_global_variable("initial_communications_pattern");
198     MC_ignore_global_variable("incomplete_communications_pattern");
199     MC_ignore_global_variable("nb_comm_pattern");
200
201     /* MC __thread variables: */
202     MC_ignore_global_variable("mc_diff_info");
203     MC_ignore_global_variable("mc_comp_times");
204     MC_ignore_global_variable("mc_snapshot_comparison_time");
205
206     /* This MC state is used in MC replay as well: */
207     MC_ignore_global_variable("mc_time");
208
209     /* Static variable used for tracing */
210     MC_ignore_global_variable("counter");
211
212     /* SIMIX */
213     MC_ignore_global_variable("smx_total_comms");
214
215     MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
216
217     smx_process_t process;
218     xbt_swag_foreach(process, simix_global->process_list) {
219       MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
220     }
221   }
222
223   if (raw_mem_set)
224     MC_SET_MC_HEAP;
225
226 }
227
228 /*******************************  Core of MC *******************************/
229 /**************************************************************************/
230
231 static void MC_modelcheck_comm_determinism_init(void)
232 {
233
234   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
235
236   MC_init();
237
238   if (!mc_mem_set)
239     MC_SET_MC_HEAP;
240
241   /* Create exploration stack */
242   mc_stack = xbt_fifo_new();
243
244   MC_SET_STD_HEAP;
245
246   MC_pre_modelcheck_comm_determinism();
247
248   MC_SET_MC_HEAP;
249   initial_global_state = xbt_new0(s_mc_global_t, 1);
250   initial_global_state->snapshot = MC_take_snapshot(0);
251   initial_global_state->initial_communications_pattern_done = 0;
252   initial_global_state->recv_deterministic = 1;
253   initial_global_state->send_deterministic = 1;
254   initial_global_state->recv_diff = NULL;
255   initial_global_state->send_diff = NULL;
256
257   MC_SET_STD_HEAP;
258
259   MC_modelcheck_comm_determinism();
260
261   if(mc_mem_set)
262     MC_SET_MC_HEAP;
263
264 }
265
266 static void MC_modelcheck_safety_init(void)
267 {
268   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
269
270   _sg_mc_safety = 1;
271
272   MC_init();
273
274   if (!mc_mem_set)
275     MC_SET_MC_HEAP;
276
277   /* Create exploration stack */
278   mc_stack = xbt_fifo_new();
279
280   MC_SET_STD_HEAP;
281
282   MC_pre_modelcheck_safety();
283
284   MC_SET_MC_HEAP;
285   /* Save the initial state */
286   initial_global_state = xbt_new0(s_mc_global_t, 1);
287   initial_global_state->snapshot = MC_take_snapshot(0);
288   MC_SET_STD_HEAP;
289
290   MC_modelcheck_safety();
291
292   if (mc_mem_set)
293     MC_SET_MC_HEAP;
294
295   xbt_abort();
296   //MC_exit();
297 }
298
299 static void MC_modelcheck_liveness_init()
300 {
301
302   int mc_mem_set = (mmalloc_get_current_heap() == mc_heap);
303
304   _sg_mc_liveness = 1;
305
306   MC_init();
307
308   if (!mc_mem_set)
309     MC_SET_MC_HEAP;
310
311   /* Create exploration stack */
312   mc_stack = xbt_fifo_new();
313
314   /* Create the initial state */
315   initial_global_state = xbt_new0(s_mc_global_t, 1);
316
317   MC_SET_STD_HEAP;
318
319   MC_pre_modelcheck_liveness();
320
321   /* We're done */
322   MC_print_statistics(mc_stats);
323   xbt_free(mc_time);
324
325   if (mc_mem_set)
326     MC_SET_MC_HEAP;
327
328 }
329
330 void MC_do_the_modelcheck_for_real()
331 {
332
333   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
334     XBT_INFO("Check communication determinism");
335     mc_reduce_kind = e_mc_reduce_none;
336     MC_modelcheck_comm_determinism_init();
337   } else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
338     if(_sg_mc_termination){
339       XBT_INFO("Check non progressive cycles");
340       mc_reduce_kind = e_mc_reduce_none;
341     }else{
342       XBT_INFO("Check a safety property");
343     }
344     MC_modelcheck_safety_init();
345   } else {
346     if (mc_reduce_kind == e_mc_reduce_unset)
347       mc_reduce_kind = e_mc_reduce_none;
348     XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
349     MC_automaton_load(_sg_mc_property_file);
350     MC_modelcheck_liveness_init();
351   }
352 }
353
354
355 void MC_exit(void)
356 {
357   xbt_free(mc_time);
358
359   MC_memory_exit();
360   //xbt_abort();
361 }
362
363 int MC_deadlock_check()
364 {
365   int deadlock = FALSE;
366   smx_process_t process;
367   if (xbt_swag_size(simix_global->process_list)) {
368     deadlock = TRUE;
369     xbt_swag_foreach(process, simix_global->process_list) {
370       if (MC_request_is_enabled(&process->simcall)) {
371         deadlock = FALSE;
372         break;
373       }
374     }
375   }
376   return deadlock;
377 }
378
379 void handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
380
381   switch(call_type) {
382   case MC_CALL_TYPE_NONE:
383     break;
384   case MC_CALL_TYPE_SEND:
385   case MC_CALL_TYPE_RECV:
386     get_comm_pattern(pattern, req, call_type, backtracking);
387     break;
388   case MC_CALL_TYPE_WAIT:
389   case MC_CALL_TYPE_WAITANY:
390     {
391       smx_synchro_t current_comm = NULL;
392       if (call_type == MC_CALL_TYPE_WAIT)
393         current_comm = simcall_comm_wait__get__comm(req);
394       else
395         current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
396       complete_comm_pattern(pattern, current_comm, req->issuer->pid, backtracking);
397     }
398     break;
399   default:
400     xbt_die("Unexpected call type %i", (int)call_type);
401   }
402   
403 }
404
405 static void MC_restore_communications_pattern(mc_state_t state) {
406   mc_list_comm_pattern_t list_process_comm;
407   unsigned int cursor, cursor2;
408   xbt_dynar_foreach(initial_communications_pattern, cursor, list_process_comm){
409     list_process_comm->index_comm = (int)xbt_dynar_get_as(state->index_comm, cursor, int);
410   }
411   mc_comm_pattern_t comm;
412   cursor = 0;
413   xbt_dynar_t initial_incomplete_process_comms, incomplete_process_comms;
414   for(int i=0; i<simix_process_maxpid; i++){
415     initial_incomplete_process_comms = xbt_dynar_get_as(incomplete_communications_pattern, i, xbt_dynar_t);
416     xbt_dynar_reset(initial_incomplete_process_comms);
417     incomplete_process_comms = xbt_dynar_get_as(state->incomplete_comm_pattern, i, xbt_dynar_t);
418     xbt_dynar_foreach(incomplete_process_comms, cursor2, comm) {
419       mc_comm_pattern_t copy_comm = xbt_new0(s_mc_comm_pattern_t, 1);
420       copy_comm->index = comm->index;
421       copy_comm->type = comm->type;
422       copy_comm->comm = comm->comm;
423       copy_comm->rdv = strdup(comm->rdv);
424       copy_comm->data_size = -1;
425       copy_comm->data = NULL;
426       if(comm->type == SIMIX_COMM_SEND){
427         copy_comm->src_proc = comm->src_proc;
428         copy_comm->src_host = comm->src_host;
429         if(comm->data != NULL){
430           copy_comm->data_size = comm->data_size;
431           copy_comm->data = xbt_malloc0(comm->data_size);
432           memcpy(copy_comm->data, comm->data, comm->data_size);
433         }
434       }else{
435         copy_comm->dst_proc = comm->dst_proc;
436         copy_comm->dst_host = comm->dst_host;
437       }
438       xbt_dynar_push(initial_incomplete_process_comms, &copy_comm);
439     }
440   }
441 }
442
443 /**
444  * \brief Re-executes from the state at position start all the transitions indicated by
445  *        a given model-checker stack.
446  * \param stack The stack with the transitions to execute.
447  * \param start Start index to begin the re-execution.
448  */
449 void MC_replay(xbt_fifo_t stack)
450 {
451   int raw_mem = (mmalloc_get_current_heap() == mc_heap);
452
453   int value, count = 1, j;
454   char *req_str;
455   smx_simcall_t req = NULL, saved_req = NULL;
456   xbt_fifo_item_t item, start_item;
457   mc_state_t state;
458   
459   XBT_DEBUG("**** Begin Replay ****");
460
461   /* Intermediate backtracking */
462   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
463     start_item = xbt_fifo_get_first_item(stack);
464     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
465     if(state->system_state){
466       MC_restore_snapshot(state->system_state);
467       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
468         MC_restore_communications_pattern(state);
469       MC_SET_STD_HEAP;
470       return;
471     }
472   }
473
474
475   /* Restore the initial state */
476   MC_restore_snapshot(initial_global_state->snapshot);
477   /* At the moment of taking the snapshot the raw heap was set, so restoring
478    * it will set it back again, we have to unset it to continue  */
479   MC_SET_STD_HEAP;
480
481   start_item = xbt_fifo_get_last_item(stack);
482   
483   MC_SET_MC_HEAP;
484
485   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
486     for (j=0; j<simix_process_maxpid; j++) {
487       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
488       ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t))->index_comm = 0;
489     }
490   }
491
492   MC_SET_STD_HEAP;
493
494   /* Traverse the stack from the state at position start and re-execute the transitions */
495   for (item = start_item;
496        item != xbt_fifo_get_first_item(stack);
497        item = xbt_fifo_get_prev_item(item)) {
498
499     state = (mc_state_t) xbt_fifo_get_item_content(item);
500     saved_req = MC_state_get_executed_request(state, &value);
501     
502     if (saved_req) {
503       /* because we got a copy of the executed request, we have to fetch the  
504          real one, pointed by the request field of the issuer process */
505       req = &saved_req->issuer->simcall;
506
507       /* Debug information */
508       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
509         req_str = MC_request_to_string(req, value);
510         XBT_DEBUG("Replay: %s (%p)", req_str, state);
511         xbt_free(req_str);
512       }
513
514       /* TODO : handle test and testany simcalls */
515       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
516       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
517         call = mc_get_call_type(req);
518
519       SIMIX_simcall_handle(req, value);
520
521       MC_SET_MC_HEAP;
522       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
523         handle_comm_pattern(call, req, value, NULL, 1);
524       MC_SET_STD_HEAP;
525       
526       MC_wait_for_requests();
527
528       count++;
529     }
530
531     /* Update statistics */
532     mc_stats->visited_states++;
533     mc_stats->executed_transitions++;
534
535   }
536
537   XBT_DEBUG("**** End Replay ****");
538
539   if (raw_mem)
540     MC_SET_MC_HEAP;
541   else
542     MC_SET_STD_HEAP;
543
544
545 }
546
547 void MC_replay_liveness(xbt_fifo_t stack)
548 {
549
550   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
551
552   xbt_fifo_item_t item;
553   mc_pair_t pair = NULL;
554   mc_state_t state = NULL;
555   smx_simcall_t req = NULL, saved_req = NULL;
556   int value, depth = 1;
557   char *req_str;
558
559   XBT_DEBUG("**** Begin Replay ****");
560
561   /* Intermediate backtracking */
562   if(_sg_mc_checkpoint > 0) {
563     item = xbt_fifo_get_first_item(stack);
564     pair = (mc_pair_t) xbt_fifo_get_item_content(item);
565     if(pair->graph_state->system_state){
566       MC_restore_snapshot(pair->graph_state->system_state);
567       MC_SET_STD_HEAP;
568       return;
569     }
570   }
571
572   /* Restore the initial state */
573   MC_restore_snapshot(initial_global_state->snapshot);
574
575   /* At the moment of taking the snapshot the raw heap was set, so restoring
576    * it will set it back again, we have to unset it to continue  */
577   if (!initial_global_state->raw_mem_set)
578     MC_SET_STD_HEAP;
579
580     /* Traverse the stack from the initial state and re-execute the transitions */
581     for (item = xbt_fifo_get_last_item(stack);
582          item != xbt_fifo_get_first_item(stack);
583          item = xbt_fifo_get_prev_item(item)) {
584
585       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
586
587       state = (mc_state_t) pair->graph_state;
588
589       if (pair->exploration_started) {
590
591         saved_req = MC_state_get_executed_request(state, &value);
592
593         if (saved_req != NULL) {
594           /* because we got a copy of the executed request, we have to fetch the
595              real one, pointed by the request field of the issuer process */
596           req = &saved_req->issuer->simcall;
597
598           /* Debug information */
599           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
600             req_str = MC_request_to_string(req, value);
601             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
602             xbt_free(req_str);
603           }
604
605         }
606
607         SIMIX_simcall_handle(req, value);
608         MC_wait_for_requests();
609       }
610
611       /* Update statistics */
612       mc_stats->visited_pairs++;
613       mc_stats->executed_transitions++;
614
615       depth++;
616       
617     }
618
619   XBT_DEBUG("**** End Replay ****");
620
621   if (initial_global_state->raw_mem_set)
622     MC_SET_MC_HEAP;
623   else
624     MC_SET_STD_HEAP;
625
626 }
627
628 /**
629  * \brief Dumps the contents of a model-checker's stack and shows the actual
630  *        execution trace
631  * \param stack The stack to dump
632  */
633 void MC_dump_stack_safety(xbt_fifo_t stack)
634 {
635
636   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
637
638   MC_show_stack_safety(stack);
639   
640   mc_state_t state;
641   
642   MC_SET_MC_HEAP;
643   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
644     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
645   MC_SET_STD_HEAP;
646
647   if (raw_mem_set)
648     MC_SET_MC_HEAP;
649   else
650     MC_SET_STD_HEAP;
651
652 }
653
654
655 void MC_show_stack_safety(xbt_fifo_t stack)
656 {
657
658   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
659
660   MC_SET_MC_HEAP;
661
662   int value;
663   mc_state_t state;
664   xbt_fifo_item_t item;
665   smx_simcall_t req;
666   char *req_str = NULL;
667
668   for (item = xbt_fifo_get_last_item(stack);
669        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
670         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
671     req = MC_state_get_executed_request(state, &value);
672     if (req) {
673       req_str = MC_request_to_string(req, value);
674       XBT_INFO("%s", req_str);
675       xbt_free(req_str);
676     }
677   }
678
679   if (!raw_mem_set)
680     MC_SET_STD_HEAP;
681 }
682
683 void MC_show_deadlock(smx_simcall_t req)
684 {
685   /*char *req_str = NULL; */
686   XBT_INFO("**************************");
687   XBT_INFO("*** DEAD-LOCK DETECTED ***");
688   XBT_INFO("**************************");
689   XBT_INFO("Locked request:");
690   /*req_str = MC_request_to_string(req);
691      XBT_INFO("%s", req_str);
692      xbt_free(req_str); */
693   XBT_INFO("Counter-example execution trace:");
694   MC_dump_stack_safety(mc_stack);
695   MC_print_statistics(mc_stats);
696 }
697
698 void MC_show_non_termination(void){
699   XBT_INFO("******************************************");
700   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
701   XBT_INFO("******************************************");
702   XBT_INFO("Counter-example execution trace:");
703   MC_dump_stack_safety(mc_stack);
704   MC_print_statistics(mc_stats);
705 }
706
707
708 void MC_show_stack_liveness(xbt_fifo_t stack)
709 {
710   int value;
711   mc_pair_t pair;
712   xbt_fifo_item_t item;
713   smx_simcall_t req;
714   char *req_str = NULL;
715
716   for (item = xbt_fifo_get_last_item(stack);
717        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
718        item = xbt_fifo_get_prev_item(item)) {
719     req = MC_state_get_executed_request(pair->graph_state, &value);
720     if (req && req->call != SIMCALL_NONE) {
721       req_str = MC_request_to_string(req, value);
722       XBT_INFO("%s", req_str);
723       xbt_free(req_str);
724     }
725   }
726 }
727
728
729 void MC_dump_stack_liveness(xbt_fifo_t stack)
730 {
731
732   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
733
734   mc_pair_t pair;
735
736   MC_SET_MC_HEAP;
737   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
738     MC_pair_delete(pair);
739   MC_SET_STD_HEAP;
740
741   if (raw_mem_set)
742     MC_SET_MC_HEAP;
743
744 }
745
746 void MC_print_statistics(mc_stats_t stats)
747 {
748   xbt_mheap_t previous_heap = mmalloc_get_current_heap();
749
750   if(_sg_mc_comms_determinism) {
751     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
752       XBT_INFO("******************************************************");
753       XBT_INFO("**** Only-send-deterministic communication pattern ****");
754       XBT_INFO("******************************************************");
755       XBT_INFO("%s", initial_global_state->recv_diff);
756     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
757       XBT_INFO("******************************************************");
758       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
759       XBT_INFO("******************************************************");
760       XBT_INFO("%s", initial_global_state->send_diff);
761     }
762   }
763   if (stats->expanded_pairs == 0) {
764     XBT_INFO("Expanded states = %lu", stats->expanded_states);
765     XBT_INFO("Visited states = %lu", stats->visited_states);
766   } else {
767     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
768     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
769   }
770   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
771   MC_SET_MC_HEAP;
772   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
773     fprintf(dot_output, "}\n");
774     fclose(dot_output);
775   }
776   if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
777     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
778     if (_sg_mc_comms_determinism)
779       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
780   }
781   mmalloc_set_current_heap(previous_heap);
782 }
783
784 void MC_assert(int prop)
785 {
786   if (MC_is_active() && !prop) {
787     XBT_INFO("**************************");
788     XBT_INFO("*** PROPERTY NOT VALID ***");
789     XBT_INFO("**************************");
790     XBT_INFO("Counter-example execution trace:");
791     MC_record_dump_path(mc_stack);
792     MC_dump_stack_safety(mc_stack);
793     MC_print_statistics(mc_stats);
794     xbt_abort();
795   }
796 }
797
798 void MC_cut(void)
799 {
800   user_max_depth_reached = 1;
801 }
802
803 void MC_automaton_load(const char *file)
804 {
805
806   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
807
808   MC_SET_MC_HEAP;
809
810   if (_mc_property_automaton == NULL)
811     _mc_property_automaton = xbt_automaton_new();
812
813   xbt_automaton_load(_mc_property_automaton, file);
814
815   MC_SET_STD_HEAP;
816
817   if (raw_mem_set)
818     MC_SET_MC_HEAP;
819
820 }
821
822 void MC_automaton_new_propositional_symbol(const char *id, void *fct)
823 {
824
825   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
826
827   MC_SET_MC_HEAP;
828
829   if (_mc_property_automaton == NULL)
830     _mc_property_automaton = xbt_automaton_new();
831
832   xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
833
834   MC_SET_STD_HEAP;
835
836   if (raw_mem_set)
837     MC_SET_MC_HEAP;
838
839 }
840
841 void MC_dump_stacks(FILE* file)
842 {
843   int raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
844   MC_SET_MC_HEAP;
845
846   int nstack = 0;
847   stack_region_t current_stack;
848   unsigned cursor;
849   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
850     unw_context_t * context = (unw_context_t *)current_stack->context;
851     fprintf(file, "Stack %i:\n", nstack);
852
853     int nframe = 0;
854     char buffer[100];
855     unw_cursor_t c;
856     unw_init_local (&c, context);
857     unw_word_t off;
858     do {
859       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
860       fprintf(file, "  %i: %s\n", nframe, name);
861       ++nframe;
862     } while(unw_step(&c));
863
864     ++nstack;
865   }
866
867   if (raw_mem_set)
868     MC_SET_MC_HEAP;
869 }
870 #endif
871
872 double MC_process_clock_get(smx_process_t process)
873 {
874   if (mc_time) {
875     if (process != NULL)
876       return mc_time[process->pid];
877     else
878       return -1;
879   } else {
880     return 0;
881   }
882 }
883
884 void MC_process_clock_add(smx_process_t process, double amount)
885 {
886   mc_time[process->pid] += amount;
887 }