Logo AND Algorithmique Numérique Distribuée

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