Logo AND Algorithmique Numérique Distribuée

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