Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move comm determinism code in mc_comm_determinism.c
[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 void MC_do_the_modelcheck_for_real()
215 {
216   MC_init_mode();
217
218   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
219     // TODO, move this part in the MCer process
220     if (mc_mode == MC_MODE_SERVER)
221       MC_server_loop(mc_server);
222     else {
223       XBT_INFO("Check communication determinism");
224       mc_reduce_kind = e_mc_reduce_none;
225       MC_modelcheck_comm_determinism();
226     }
227   }
228
229   else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0') {
230     if(_sg_mc_termination)
231       mc_reduce_kind = e_mc_reduce_none;
232     else if (mc_reduce_kind == e_mc_reduce_unset)
233       mc_reduce_kind = e_mc_reduce_dpor;
234     _sg_mc_safety = 1;
235     if (mc_mode == MC_MODE_SERVER || mc_mode == MC_MODE_STANDALONE) {
236       if (_sg_mc_termination)
237         XBT_INFO("Check non progressive cycles");
238       else
239         XBT_INFO("Check a safety property");
240       MC_wait_for_requests();
241       MC_modelcheck_safety();
242     }
243     else {
244       // Most of this is not needed:
245       MC_init();
246       // Main event loop:
247       MC_client_main_loop();
248     }
249   }
250
251   else {
252     if (mc_reduce_kind == e_mc_reduce_unset)
253       mc_reduce_kind = e_mc_reduce_none;
254     if (mc_mode == MC_MODE_SERVER || mc_mode == MC_MODE_STANDALONE) {
255       XBT_INFO("Check the liveness property %s", _sg_mc_property_file);
256       MC_automaton_load(_sg_mc_property_file);
257       MC_modelcheck_liveness();
258     } else {
259       MC_init();
260       MC_client_main_loop();
261     }
262   }
263 }
264
265
266 void MC_exit(void)
267 {
268   xbt_free(mc_time);
269
270   MC_memory_exit();
271   //xbt_abort();
272 }
273
274 int MC_deadlock_check()
275 {
276   if (mc_mode == MC_MODE_SERVER) {
277     int res;
278     if ((res = MC_protocol_send_simple_message(mc_model_checker->process.socket,
279       MC_MESSAGE_DEADLOCK_CHECK)))
280       xbt_die("Could not check deadlock state");
281     s_mc_int_message_t message;
282     ssize_t s = MC_receive_message(mc_model_checker->process.socket, &message, sizeof(message), 0);
283     if (s == -1)
284       xbt_die("Could not receive message");
285     else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
286       xbt_die("%s received unexpected message %s (%i, size=%i) "
287         "expected MC_MESSAGE_DEADLOCK_CHECK_REPLY (%i, size=%i)",
288         MC_mode_name(mc_mode),
289         MC_message_type_name(message.type), (int) message.type, (int) s,
290         (int) MC_MESSAGE_DEADLOCK_CHECK_REPLY, (int) sizeof(message)
291         );
292     }
293     else
294       return message.value;
295   }
296
297   int deadlock = FALSE;
298   smx_process_t process;
299   if (xbt_swag_size(simix_global->process_list)) {
300     deadlock = TRUE;
301     MC_EACH_SIMIX_PROCESS(process,
302       if (MC_process_is_enabled(process)) {
303         deadlock = FALSE;
304         break;
305       }
306     );
307   }
308   return deadlock;
309 }
310
311 void MC_handle_comm_pattern(e_mc_call_type_t call_type, smx_simcall_t req, int value, xbt_dynar_t pattern, int backtracking) {
312
313   switch(call_type) {
314   case MC_CALL_TYPE_NONE:
315     break;
316   case MC_CALL_TYPE_SEND:
317   case MC_CALL_TYPE_RECV:
318     MC_get_comm_pattern(pattern, req, call_type, backtracking);
319     break;
320   case MC_CALL_TYPE_WAIT:
321   case MC_CALL_TYPE_WAITANY:
322     {
323       smx_synchro_t current_comm = NULL;
324       if (call_type == MC_CALL_TYPE_WAIT)
325         current_comm = simcall_comm_wait__get__comm(req);
326       else
327         current_comm = xbt_dynar_get_as(simcall_comm_waitany__get__comms(req), value, smx_synchro_t);
328       MC_complete_comm_pattern(pattern, current_comm, req->issuer->pid, backtracking);
329     }
330     break;
331   default:
332     xbt_die("Unexpected call type %i", (int)call_type);
333   }
334   
335 }
336
337 /**
338  * \brief Re-executes from the state at position start all the transitions indicated by
339  *        a given model-checker stack.
340  * \param stack The stack with the transitions to execute.
341  * \param start Start index to begin the re-execution.
342  */
343 void MC_replay(xbt_fifo_t stack)
344 {
345   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
346
347   int value, count = 1, j;
348   char *req_str;
349   smx_simcall_t req = NULL, saved_req = NULL;
350   xbt_fifo_item_t item, start_item;
351   mc_state_t state;
352   
353   XBT_DEBUG("**** Begin Replay ****");
354
355   /* Intermediate backtracking */
356   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
357     start_item = xbt_fifo_get_first_item(stack);
358     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
359     if(state->system_state){
360       MC_restore_snapshot(state->system_state);
361       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
362         MC_restore_communications_pattern(state);
363       MC_SET_STD_HEAP;
364       return;
365     }
366   }
367
368
369   /* Restore the initial state */
370   MC_restore_snapshot(initial_global_state->snapshot);
371   /* At the moment of taking the snapshot the raw heap was set, so restoring
372    * it will set it back again, we have to unset it to continue  */
373   MC_SET_STD_HEAP;
374
375   start_item = xbt_fifo_get_last_item(stack);
376   
377   MC_SET_MC_HEAP;
378
379   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
380     for (j=0; j < simix_process_maxpid; j++) {
381       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
382       ((mc_list_comm_pattern_t)xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t))->index_comm = 0;
383     }
384   }
385
386   MC_SET_STD_HEAP;
387
388   /* Traverse the stack from the state at position start and re-execute the transitions */
389   for (item = start_item;
390        item != xbt_fifo_get_first_item(stack);
391        item = xbt_fifo_get_prev_item(item)) {
392
393     state = (mc_state_t) xbt_fifo_get_item_content(item);
394     saved_req = MC_state_get_executed_request(state, &value);
395     
396     if (saved_req) {
397       /* because we got a copy of the executed request, we have to fetch the  
398          real one, pointed by the request field of the issuer process */
399
400       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
401       req = &issuer->simcall;
402
403       /* Debug information */
404       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
405         req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
406         XBT_DEBUG("Replay: %s (%p)", req_str, state);
407         xbt_free(req_str);
408       }
409
410       /* TODO : handle test and testany simcalls */
411       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
412       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
413         call = MC_get_call_type(req);
414
415       MC_simcall_handle(req, value);
416
417       MC_SET_MC_HEAP;
418       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
419         MC_handle_comm_pattern(call, req, value, NULL, 1);
420       MC_SET_STD_HEAP;
421       
422       MC_wait_for_requests();
423
424       count++;
425     }
426
427     /* Update statistics */
428     mc_stats->visited_states++;
429     mc_stats->executed_transitions++;
430
431   }
432
433   XBT_DEBUG("**** End Replay ****");
434   mmalloc_set_current_heap(heap);
435 }
436
437 void MC_replay_liveness(xbt_fifo_t stack)
438 {
439
440   initial_global_state->raw_mem_set = (mmalloc_get_current_heap() == mc_heap);
441
442   xbt_fifo_item_t item;
443   mc_pair_t pair = NULL;
444   mc_state_t state = NULL;
445   smx_simcall_t req = NULL, saved_req = NULL;
446   int value, depth = 1;
447   char *req_str;
448
449   XBT_DEBUG("**** Begin Replay ****");
450
451   /* Intermediate backtracking */
452   if(_sg_mc_checkpoint > 0) {
453     item = xbt_fifo_get_first_item(stack);
454     pair = (mc_pair_t) xbt_fifo_get_item_content(item);
455     if(pair->graph_state->system_state){
456       MC_restore_snapshot(pair->graph_state->system_state);
457       MC_SET_STD_HEAP;
458       return;
459     }
460   }
461
462   /* Restore the initial state */
463   MC_restore_snapshot(initial_global_state->snapshot);
464
465   /* At the moment of taking the snapshot the raw heap was set, so restoring
466    * it will set it back again, we have to unset it to continue  */
467   if (!initial_global_state->raw_mem_set)
468     MC_SET_STD_HEAP;
469
470     /* Traverse the stack from the initial state and re-execute the transitions */
471     for (item = xbt_fifo_get_last_item(stack);
472          item != xbt_fifo_get_first_item(stack);
473          item = xbt_fifo_get_prev_item(item)) {
474
475       pair = (mc_pair_t) xbt_fifo_get_item_content(item);
476
477       state = (mc_state_t) pair->graph_state;
478
479       if (pair->exploration_started) {
480
481         saved_req = MC_state_get_executed_request(state, &value);
482
483         if (saved_req != NULL) {
484           /* because we got a copy of the executed request, we have to fetch the
485              real one, pointed by the request field of the issuer process */
486           const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
487           req = &issuer->simcall;
488
489           /* Debug information */
490           if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
491             req_str = MC_request_to_string(req, value, MC_REQUEST_SIMIX);
492             XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
493             xbt_free(req_str);
494           }
495
496         }
497
498         MC_simcall_handle(req, value);
499         MC_wait_for_requests();
500       }
501
502       /* Update statistics */
503       mc_stats->visited_pairs++;
504       mc_stats->executed_transitions++;
505
506       depth++;
507       
508     }
509
510   XBT_DEBUG("**** End Replay ****");
511
512   if (initial_global_state->raw_mem_set)
513     MC_SET_MC_HEAP;
514   else
515     MC_SET_STD_HEAP;
516
517 }
518
519 /**
520  * \brief Dumps the contents of a model-checker's stack and shows the actual
521  *        execution trace
522  * \param stack The stack to dump
523  */
524 void MC_dump_stack_safety(xbt_fifo_t stack)
525 {
526   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
527
528   MC_show_stack_safety(stack);
529   
530   mc_state_t state;
531   
532   MC_SET_MC_HEAP;
533   while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
534     MC_state_delete(state, !state->in_visited_states ? 1 : 0);
535   mmalloc_set_current_heap(heap);
536 }
537
538
539 void MC_show_stack_safety(xbt_fifo_t stack)
540 {
541   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
542
543   int value;
544   mc_state_t state;
545   xbt_fifo_item_t item;
546   smx_simcall_t req;
547   char *req_str = NULL;
548
549   for (item = xbt_fifo_get_last_item(stack);
550        (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
551         : (NULL)); item = xbt_fifo_get_prev_item(item)) {
552     req = MC_state_get_executed_request(state, &value);
553     if (req) {
554       req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
555       XBT_INFO("%s", req_str);
556       xbt_free(req_str);
557     }
558   }
559
560   mmalloc_set_current_heap(heap);
561 }
562
563 void MC_show_deadlock(smx_simcall_t req)
564 {
565   /*char *req_str = NULL; */
566   XBT_INFO("**************************");
567   XBT_INFO("*** DEAD-LOCK DETECTED ***");
568   XBT_INFO("**************************");
569   XBT_INFO("Locked request:");
570   /*req_str = MC_request_to_string(req);
571      XBT_INFO("%s", req_str);
572      xbt_free(req_str); */
573   XBT_INFO("Counter-example execution trace:");
574   MC_dump_stack_safety(mc_stack);
575   MC_print_statistics(mc_stats);
576 }
577
578 void MC_show_non_termination(void){
579   XBT_INFO("******************************************");
580   XBT_INFO("*** NON-PROGRESSIVE CYCLE DETECTED ***");
581   XBT_INFO("******************************************");
582   XBT_INFO("Counter-example execution trace:");
583   MC_dump_stack_safety(mc_stack);
584   MC_print_statistics(mc_stats);
585 }
586
587
588 void MC_show_stack_liveness(xbt_fifo_t stack)
589 {
590   int value;
591   mc_pair_t pair;
592   xbt_fifo_item_t item;
593   smx_simcall_t req;
594   char *req_str = NULL;
595
596   for (item = xbt_fifo_get_last_item(stack);
597        (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item))) : (NULL));
598        item = xbt_fifo_get_prev_item(item)) {
599     req = MC_state_get_executed_request(pair->graph_state, &value);
600     if (req && req->call != SIMCALL_NONE) {
601       req_str = MC_request_to_string(req, value, MC_REQUEST_EXECUTED);
602       XBT_INFO("%s", req_str);
603       xbt_free(req_str);
604     }
605   }
606 }
607
608
609 void MC_dump_stack_liveness(xbt_fifo_t stack)
610 {
611   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
612   mc_pair_t pair;
613   while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
614     MC_pair_delete(pair);
615   mmalloc_set_current_heap(heap);
616 }
617
618 void MC_print_statistics(mc_stats_t stats)
619 {
620   if(_sg_mc_comms_determinism) {
621     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
622       XBT_INFO("******************************************************");
623       XBT_INFO("**** Only-send-deterministic communication pattern ****");
624       XBT_INFO("******************************************************");
625       XBT_INFO("%s", initial_global_state->recv_diff);
626     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
627       XBT_INFO("******************************************************");
628       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
629       XBT_INFO("******************************************************");
630       XBT_INFO("%s", initial_global_state->send_diff);
631     }
632   }
633
634   if (stats->expanded_pairs == 0) {
635     XBT_INFO("Expanded states = %lu", stats->expanded_states);
636     XBT_INFO("Visited states = %lu", stats->visited_states);
637   } else {
638     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
639     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
640   }
641   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
642   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
643   if ((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0] != '\0')) {
644     fprintf(dot_output, "}\n");
645     fclose(dot_output);
646   }
647   if (initial_global_state != NULL && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
648     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
649     if (_sg_mc_comms_determinism)
650       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
651   }
652   mmalloc_set_current_heap(heap);
653 }
654
655 void MC_automaton_load(const char *file)
656 {
657   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
658
659   if (_mc_property_automaton == NULL)
660     _mc_property_automaton = xbt_automaton_new();
661
662   xbt_automaton_load(_mc_property_automaton, file);
663   mmalloc_set_current_heap(heap);
664 }
665
666 static void register_symbol(xbt_automaton_propositional_symbol_t symbol)
667 {
668   if (mc_mode != MC_MODE_CLIENT)
669     return;
670   s_mc_register_symbol_message_t message;
671   message.type = MC_MESSAGE_REGISTER_SYMBOL;
672   const char* name = xbt_automaton_propositional_symbol_get_name(symbol);
673   if (strlen(name) + 1 > sizeof(message.name))
674     xbt_die("Symbol is too long");
675   strncpy(message.name, name, sizeof(message.name));
676   message.callback = xbt_automaton_propositional_symbol_get_callback(symbol);
677   message.data = xbt_automaton_propositional_symbol_get_data(symbol);
678   MC_client_send_message(&message, sizeof(message));
679 }
680
681 void MC_automaton_new_propositional_symbol(const char *id, int(*fct)(void))
682 {
683   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
684
685   if (_mc_property_automaton == NULL)
686     _mc_property_automaton = xbt_automaton_new();
687
688   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new(_mc_property_automaton, id, fct);
689   register_symbol(symbol);
690   mmalloc_set_current_heap(heap);
691 }
692
693 void MC_automaton_new_propositional_symbol_pointer(const char *id, int* value)
694 {
695   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
696   if (_mc_property_automaton == NULL)
697     _mc_property_automaton = xbt_automaton_new();
698   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_pointer(_mc_property_automaton, id, value);
699   register_symbol(symbol);
700   mmalloc_set_current_heap(heap);
701 }
702
703 void MC_automaton_new_propositional_symbol_callback(const char* id,
704   xbt_automaton_propositional_symbol_callback_type callback,
705   void* data, xbt_automaton_propositional_symbol_free_function_type free_function)
706 {
707   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
708   if (_mc_property_automaton == NULL)
709     _mc_property_automaton = xbt_automaton_new();
710   xbt_automaton_propositional_symbol_t symbol = xbt_automaton_propositional_symbol_new_callback(
711     _mc_property_automaton, id, callback, data, free_function);
712   register_symbol(symbol);
713   mmalloc_set_current_heap(heap);
714 }
715
716 // TODO, fix cross-process access (this function is not used)
717 void MC_dump_stacks(FILE* file)
718 {
719   xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
720
721   int nstack = 0;
722   stack_region_t current_stack;
723   unsigned cursor;
724   xbt_dynar_foreach(stacks_areas, cursor, current_stack) {
725     unw_context_t * context = (unw_context_t *)current_stack->context;
726     fprintf(file, "Stack %i:\n", nstack);
727
728     int nframe = 0;
729     char buffer[100];
730     unw_cursor_t c;
731     unw_init_local (&c, context);
732     unw_word_t off;
733     do {
734       const char * name = !unw_get_proc_name(&c, buffer, 100, &off) ? buffer : "?";
735 #if defined(__x86_64__)
736       unw_word_t rip = 0;
737       unw_word_t rsp = 0;
738       unw_get_reg(&c, UNW_X86_64_RIP, &rip);
739       unw_get_reg(&c, UNW_X86_64_RSP, &rsp);
740       fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
741         nframe, name, rip, rsp);
742 #else
743       fprintf(file, "  %i: %s\n", nframe, name);
744 #endif
745       ++nframe;
746     } while(unw_step(&c));
747
748     ++nstack;
749   }
750   mmalloc_set_current_heap(heap);
751 }
752 #endif
753
754 double MC_process_clock_get(smx_process_t process)
755 {
756   if (mc_time) {
757     if (process != NULL)
758       return mc_time[process->pid];
759     else
760       return -1;
761   } else {
762     return 0;
763   }
764 }
765
766 void MC_process_clock_add(smx_process_t process, double amount)
767 {
768   mc_time[process->pid] += amount;
769 }
770
771 void MC_report_assertion_error(void)
772 {
773   XBT_INFO("**************************");
774   XBT_INFO("*** PROPERTY NOT VALID ***");
775   XBT_INFO("**************************");
776   XBT_INFO("Counter-example execution trace:");
777   MC_record_dump_path(mc_stack);
778   MC_dump_stack_safety(mc_stack);
779   MC_print_statistics(mc_stats);
780 }