Logo AND Algorithmique Numérique Distribuée

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