Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Dump the current trace when the model-checked crashes
[simgrid.git] / src / mc / mc_global.cpp
1 /* Copyright (c) 2008-2015. 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 <cinttypes>
8 #include <cassert>
9 #include <cstddef>
10 #include <cstdint>
11
12 #include <vector>
13
14 #include "mc_base.h"
15
16 #include "mc/mc.h"
17
18 #ifndef _WIN32
19 #include <unistd.h>
20 #include <sys/wait.h>
21 #include <sys/time.h>
22 #endif
23
24 #include "src/simix/smx_process_private.h"
25
26 #if HAVE_MC
27 #include <libunwind.h>
28 #include "src/mc/mc_comm_pattern.h"
29 #include "src/mc/mc_request.h"
30 #include "src/mc/mc_safety.h"
31 #include "src/mc/mc_snapshot.h"
32 #include "src/mc/LivenessChecker.hpp"
33 #include "src/mc/mc_private.h"
34 #include "src/mc/mc_unw.h"
35 #include "src/mc/mc_smx.h"
36 #endif
37
38 #include "src/mc/mc_record.h"
39 #include "src/mc/mc_protocol.h"
40 #include "src/mc/Client.hpp"
41
42 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
43
44 e_mc_mode_t mc_mode;
45
46 namespace simgrid {
47 namespace mc {
48
49 std::vector<double> processes_time;
50
51 }
52 }
53
54 #if HAVE_MC
55 int user_max_depth_reached = 0;
56
57 /* MC global data structures */
58 mc_state_t mc_current_state = nullptr;
59 char mc_replay_mode = false;
60
61 mc_stats_t mc_stats = nullptr;
62 mc_global_t initial_global_state = nullptr;
63 xbt_fifo_t mc_stack = nullptr;
64
65 /* Liveness */
66
67 namespace simgrid {
68 namespace mc {
69
70 xbt_automaton_t property_automaton = nullptr;
71
72 }
73 }
74
75 /* Dot output */
76 FILE *dot_output = nullptr;
77
78
79 /*******************************  Initialisation of MC *******************************/
80 /*********************************************************************************/
81
82 void MC_init_dot_output()
83 {
84   dot_output = fopen(_sg_mc_dot_output_file, "w");
85
86   if (dot_output == nullptr) {
87     perror("Error open dot output file");
88     xbt_abort();
89   }
90
91   fprintf(dot_output,
92           "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");
93
94 }
95
96 /*******************************  Core of MC *******************************/
97 /**************************************************************************/
98
99 void MC_run()
100 {
101   simgrid::mc::processes_time.resize(simix_process_maxpid);
102   MC_ignore_heap(simgrid::mc::processes_time.data(),
103     simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
104   smx_process_t process;
105   xbt_swag_foreach(process, simix_global->process_list)
106     MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
107   simgrid::mc::Client::get()->mainLoop();
108   simgrid::mc::processes_time.clear();
109 }
110
111 /**
112  * \brief Re-executes from the state at position start all the transitions indicated by
113  *        a given model-checker stack.
114  * \param stack The stack with the transitions to execute.
115  * \param start Start index to begin the re-execution.
116  */
117 void MC_replay(xbt_fifo_t stack)
118 {
119   int value, count = 1;
120   char *req_str;
121   smx_simcall_t req = nullptr, saved_req = NULL;
122   xbt_fifo_item_t item, start_item;
123   mc_state_t state;
124   
125   XBT_DEBUG("**** Begin Replay ****");
126
127   /* Intermediate backtracking */
128   if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
129     start_item = xbt_fifo_get_first_item(stack);
130     state = (mc_state_t)xbt_fifo_get_item_content(start_item);
131     if(state->system_state){
132       simgrid::mc::restore_snapshot(state->system_state);
133       if(_sg_mc_comms_determinism || _sg_mc_send_determinism) 
134         MC_restore_communications_pattern(state);
135       return;
136     }
137   }
138
139
140   /* Restore the initial state */
141   simgrid::mc::restore_snapshot(initial_global_state->snapshot);
142   /* At the moment of taking the snapshot the raw heap was set, so restoring
143    * it will set it back again, we have to unset it to continue  */
144
145   start_item = xbt_fifo_get_last_item(stack);
146
147   if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
148     // int n = xbt_dynar_length(incomplete_communications_pattern);
149     unsigned n = MC_smx_get_maxpid();
150     assert(n == xbt_dynar_length(incomplete_communications_pattern));
151     assert(n == xbt_dynar_length(initial_communications_pattern));
152     for (unsigned j=0; j < n ; j++) {
153       xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
154       xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
155     }
156   }
157
158   /* Traverse the stack from the state at position start and re-execute the transitions */
159   for (item = start_item;
160        item != xbt_fifo_get_first_item(stack);
161        item = xbt_fifo_get_prev_item(item)) {
162
163     state = (mc_state_t) xbt_fifo_get_item_content(item);
164     saved_req = MC_state_get_executed_request(state, &value);
165     
166     if (saved_req) {
167       /* because we got a copy of the executed request, we have to fetch the  
168          real one, pointed by the request field of the issuer process */
169
170       const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
171       req = &issuer->simcall;
172
173       /* Debug information */
174       if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
175         req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
176         XBT_DEBUG("Replay: %s (%p)", req_str, state);
177         xbt_free(req_str);
178       }
179
180       /* TODO : handle test and testany simcalls */
181       e_mc_call_type_t call = MC_CALL_TYPE_NONE;
182       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
183         call = MC_get_call_type(req);
184
185       simgrid::mc::handle_simcall(req, value);
186
187       if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
188         MC_handle_comm_pattern(call, req, value, nullptr, 1);
189
190       mc_model_checker->wait_for_requests();
191
192       count++;
193     }
194
195     /* Update statistics */
196     mc_stats->visited_states++;
197     mc_stats->executed_transitions++;
198
199   }
200
201   XBT_DEBUG("**** End Replay ****");
202 }
203
204 void MC_show_deadlock(void)
205 {
206   XBT_INFO("**************************");
207   XBT_INFO("*** DEAD-LOCK DETECTED ***");
208   XBT_INFO("**************************");
209   XBT_INFO("Counter-example execution trace:");
210   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
211     XBT_INFO("%s", s.c_str());
212   MC_print_statistics(mc_stats);
213 }
214
215 void MC_print_statistics(mc_stats_t stats)
216 {
217   if(_sg_mc_comms_determinism) {
218     if (!initial_global_state->recv_deterministic && initial_global_state->send_deterministic){
219       XBT_INFO("******************************************************");
220       XBT_INFO("**** Only-send-deterministic communication pattern ****");
221       XBT_INFO("******************************************************");
222       XBT_INFO("%s", initial_global_state->recv_diff);
223     }else if(!initial_global_state->send_deterministic && initial_global_state->recv_deterministic) {
224       XBT_INFO("******************************************************");
225       XBT_INFO("**** Only-recv-deterministic communication pattern ****");
226       XBT_INFO("******************************************************");
227       XBT_INFO("%s", initial_global_state->send_diff);
228     }
229   }
230
231   if (stats->expanded_pairs == 0) {
232     XBT_INFO("Expanded states = %lu", stats->expanded_states);
233     XBT_INFO("Visited states = %lu", stats->visited_states);
234   } else {
235     XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
236     XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
237   }
238   XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
239   if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
240     fprintf(dot_output, "}\n");
241     fclose(dot_output);
242   }
243   if (initial_global_state != nullptr && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
244     XBT_INFO("Send-deterministic : %s", !initial_global_state->send_deterministic ? "No" : "Yes");
245     if (_sg_mc_comms_determinism)
246       XBT_INFO("Recv-deterministic : %s", !initial_global_state->recv_deterministic ? "No" : "Yes");
247   }
248   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
249     int ret=system("free");
250     if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
251   }
252 }
253
254 void MC_automaton_load(const char *file)
255 {
256   if (simgrid::mc::property_automaton == nullptr)
257     simgrid::mc::property_automaton = xbt_automaton_new();
258
259   xbt_automaton_load(simgrid::mc::property_automaton, file);
260 }
261
262 namespace simgrid {
263 namespace mc {
264
265 void dumpStack(FILE* file, unw_cursor_t cursor)
266 {
267   int nframe = 0;
268   char buffer[100];
269
270   unw_word_t off;
271   do {
272     const char * name = !unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
273 #if defined(__x86_64__)
274     unw_word_t rip = 0;
275     unw_word_t rsp = 0;
276     unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
277     unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
278     fprintf(file, "  %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
279       nframe, name, (std::uint64_t) rip, (std::uint64_t) rsp);
280 #else
281     fprintf(file, "  %i: %s\n", nframe, name);
282 #endif
283     ++nframe;
284   } while(unw_step(&cursor));
285 }
286
287 }
288 }
289
290 static void MC_dump_stacks(FILE* file)
291 {
292   int nstack = 0;
293   for (auto const& stack : mc_model_checker->process().stack_areas()) {
294     fprintf(file, "Stack %i:\n", nstack++);
295
296     simgrid::mc::UnwindContext context;
297     unw_context_t raw_context =
298       mc_model_checker->process().read<unw_context_t>(
299         simgrid::mc::remote((unw_context_t *)stack.context));
300     context.initialize(&mc_model_checker->process(), &raw_context);
301
302     unw_cursor_t cursor = context.cursor();
303     simgrid::mc::dumpStack(file, cursor);
304   }
305 }
306 #endif
307
308 double MC_process_clock_get(smx_process_t process)
309 {
310   if (simgrid::mc::processes_time.empty())
311     return 0;
312   if (process != nullptr)
313     return simgrid::mc::processes_time[process->pid];
314   return -1;
315 }
316
317 void MC_process_clock_add(smx_process_t process, double amount)
318 {
319   simgrid::mc::processes_time[process->pid] += amount;
320 }
321
322 #if HAVE_MC
323 void MC_report_assertion_error(void)
324 {
325   XBT_INFO("**************************");
326   XBT_INFO("*** PROPERTY NOT VALID ***");
327   XBT_INFO("**************************");
328   XBT_INFO("Counter-example execution trace:");
329   simgrid::mc::dumpRecordPath();
330   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
331     XBT_INFO("%s", s.c_str());
332   MC_print_statistics(mc_stats);
333 }
334
335 void MC_report_crash(int status)
336 {
337   XBT_INFO("**************************");
338   XBT_INFO("** CRASH IN THE PROGRAM **");
339   XBT_INFO("**************************");
340   if (WIFSIGNALED(status))
341     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
342   else if (WIFEXITED(status))
343     XBT_INFO("From exit: %i", WEXITSTATUS(status));
344   if (WCOREDUMP(status))
345     XBT_INFO("A core dump was generated by the system.");
346   else
347     XBT_INFO("No core dump was generated by the system.");
348   XBT_INFO("Counter-example execution trace:");
349   simgrid::mc::dumpRecordPath();
350   for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
351     XBT_INFO("%s", s.c_str());
352   MC_print_statistics(mc_stats);
353   XBT_INFO("Stack trace:");
354   mc_model_checker->process().dumpStack();
355 }
356
357 #endif