Logo AND Algorithmique Numérique Distribuée

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