1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
26 #include "src/simix/smx_process_private.h"
29 #include <libunwind.h>
30 #include "src/mc/mc_comm_pattern.h"
31 #include "src/mc/mc_request.h"
32 #include "src/mc/mc_safety.h"
33 #include "src/mc/mc_snapshot.h"
34 #include "src/mc/mc_private.h"
35 #include "src/mc/mc_unw.h"
36 #include "src/mc/mc_smx.h"
37 #include "src/mc/Checker.hpp"
40 #include "src/mc/mc_record.h"
41 #include "src/mc/mc_protocol.h"
42 #include "src/mc/Client.hpp"
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc, "Logging specific to MC (global)");
51 std::vector<double> processes_time;
57 int user_max_depth_reached = 0;
59 /* MC global data structures */
60 simgrid::mc::State* mc_current_state = nullptr;
61 char mc_replay_mode = false;
63 mc_stats_t mc_stats = nullptr;
70 std::unique_ptr<s_mc_global_t> initial_global_state = nullptr;
71 xbt_automaton_t property_automaton = nullptr;
77 FILE *dot_output = nullptr;
80 /******************************* Initialisation of MC *******************************/
81 /*********************************************************************************/
83 void MC_init_dot_output()
85 dot_output = fopen(_sg_mc_dot_output_file, "w");
87 if (dot_output == nullptr) {
88 perror("Error open dot output file");
93 "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");
97 /******************************* Core of MC *******************************/
98 /**************************************************************************/
102 simgrid::mc::processes_time.resize(simix_process_maxpid);
103 MC_ignore_heap(simgrid::mc::processes_time.data(),
104 simgrid::mc::processes_time.size() * sizeof(simgrid::mc::processes_time[0]));
105 smx_process_t process;
106 xbt_swag_foreach(process, simix_global->process_list)
107 MC_ignore_heap(&(process->process_hookup), sizeof(process->process_hookup));
108 simgrid::mc::Client::get()->mainLoop();
109 simgrid::mc::processes_time.clear();
116 * \brief Re-executes from the state at position start all the transitions indicated by
117 * a given model-checker stack.
118 * \param stack The stack with the transitions to execute.
119 * \param start Start index to begin the re-execution.
121 void replay(std::list<std::unique_ptr<simgrid::mc::State>> const& stack)
123 XBT_DEBUG("**** Begin Replay ****");
125 /* Intermediate backtracking */
126 if(_sg_mc_checkpoint > 0 || _sg_mc_termination || _sg_mc_visited > 0) {
127 simgrid::mc::State* state = stack.back().get();
128 if (state->system_state) {
129 simgrid::mc::restore_snapshot(state->system_state);
130 if(_sg_mc_comms_determinism || _sg_mc_send_determinism)
131 MC_restore_communications_pattern(state);
137 /* Restore the initial state */
138 simgrid::mc::restore_snapshot(simgrid::mc::initial_global_state->snapshot);
139 /* At the moment of taking the snapshot the raw heap was set, so restoring
140 * it will set it back again, we have to unset it to continue */
142 if (_sg_mc_comms_determinism || _sg_mc_send_determinism) {
143 // int n = xbt_dynar_length(incomplete_communications_pattern);
144 unsigned n = MC_smx_get_maxpid();
145 assert(n == xbt_dynar_length(incomplete_communications_pattern));
146 assert(n == xbt_dynar_length(initial_communications_pattern));
147 for (unsigned j=0; j < n ; j++) {
148 xbt_dynar_reset((xbt_dynar_t)xbt_dynar_get_as(incomplete_communications_pattern, j, xbt_dynar_t));
149 xbt_dynar_get_as(initial_communications_pattern, j, mc_list_comm_pattern_t)->index_comm = 0;
155 /* Traverse the stack from the state at position start and re-execute the transitions */
156 for (std::unique_ptr<simgrid::mc::State> const& state : stack) {
157 if (state == stack.back())
161 smx_simcall_t saved_req = MC_state_get_executed_request(state.get(), &value);
164 /* because we got a copy of the executed request, we have to fetch the
165 real one, pointed by the request field of the issuer process */
167 const smx_process_t issuer = MC_smx_simcall_get_issuer(saved_req);
168 smx_simcall_t req = &issuer->simcall;
170 /* Debug information */
171 if (XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)) {
172 char* req_str = simgrid::mc::request_to_string(req, value, simgrid::mc::RequestType::simix);
173 XBT_DEBUG("Replay: %s (%p)", req_str, state.get());
177 /* TODO : handle test and testany simcalls */
178 e_mc_call_type_t call = MC_CALL_TYPE_NONE;
179 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
180 call = MC_get_call_type(req);
182 simgrid::mc::handle_simcall(req, value);
184 if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
185 MC_handle_comm_pattern(call, req, value, nullptr, 1);
187 mc_model_checker->wait_for_requests();
192 /* Update statistics */
193 mc_stats->visited_states++;
194 mc_stats->executed_transitions++;
198 XBT_DEBUG("**** End Replay ****");
204 void MC_show_deadlock(void)
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);
215 void MC_print_statistics(mc_stats_t stats)
217 if(_sg_mc_comms_determinism) {
218 if (!simgrid::mc::initial_global_state->recv_deterministic &&
219 simgrid::mc::initial_global_state->send_deterministic){
220 XBT_INFO("******************************************************");
221 XBT_INFO("**** Only-send-deterministic communication pattern ****");
222 XBT_INFO("******************************************************");
223 XBT_INFO("%s", simgrid::mc::initial_global_state->recv_diff);
224 }else if(!simgrid::mc::initial_global_state->send_deterministic &&
225 simgrid::mc::initial_global_state->recv_deterministic) {
226 XBT_INFO("******************************************************");
227 XBT_INFO("**** Only-recv-deterministic communication pattern ****");
228 XBT_INFO("******************************************************");
229 XBT_INFO("%s", simgrid::mc::initial_global_state->send_diff);
233 if (stats->expanded_pairs == 0) {
234 XBT_INFO("Expanded states = %lu", stats->expanded_states);
235 XBT_INFO("Visited states = %lu", stats->visited_states);
237 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
238 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
240 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
241 if ((_sg_mc_dot_output_file != nullptr) && (_sg_mc_dot_output_file[0] != '\0')) {
242 fprintf(dot_output, "}\n");
245 if (simgrid::mc::initial_global_state != nullptr
246 && (_sg_mc_comms_determinism || _sg_mc_send_determinism)) {
247 XBT_INFO("Send-deterministic : %s",
248 !simgrid::mc::initial_global_state->send_deterministic ? "No" : "Yes");
249 if (_sg_mc_comms_determinism)
250 XBT_INFO("Recv-deterministic : %s",
251 !simgrid::mc::initial_global_state->recv_deterministic ? "No" : "Yes");
253 if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")){
254 int ret=system("free");
255 if(ret!=0)XBT_WARN("system call did not return 0, but %d",ret);
259 void MC_automaton_load(const char *file)
261 if (simgrid::mc::property_automaton == nullptr)
262 simgrid::mc::property_automaton = xbt_automaton_new();
264 xbt_automaton_load(simgrid::mc::property_automaton, file);
270 void dumpStack(FILE* file, unw_cursor_t cursor)
277 const char * name = !unw_get_proc_name(&cursor, buffer, 100, &off) ? buffer : "?";
281 // Demangle C++ names:
282 char* realname = abi::__cxa_demangle(name, 0, 0, &status);
284 #if defined(__x86_64__)
287 unw_get_reg(&cursor, UNW_X86_64_RIP, &rip);
288 unw_get_reg(&cursor, UNW_X86_64_RSP, &rsp);
289 fprintf(file, " %i: %s (RIP=0x%" PRIx64 " RSP=0x%" PRIx64 ")\n",
290 nframe, realname ? realname : name, (std::uint64_t) rip, (std::uint64_t) rsp);
292 fprintf(file, " %i: %s\n", nframe, realname ? realname : name);
297 } while(unw_step(&cursor));
303 static void MC_dump_stacks(FILE* file)
306 for (auto const& stack : mc_model_checker->process().stack_areas()) {
307 fprintf(file, "Stack %i:\n", nstack++);
309 simgrid::mc::UnwindContext context;
310 unw_context_t raw_context =
311 mc_model_checker->process().read<unw_context_t>(
312 simgrid::mc::remote((unw_context_t *)stack.context));
313 context.initialize(&mc_model_checker->process(), &raw_context);
315 unw_cursor_t cursor = context.cursor();
316 simgrid::mc::dumpStack(file, cursor);
321 double MC_process_clock_get(smx_process_t process)
323 if (simgrid::mc::processes_time.empty())
325 if (process != nullptr)
326 return simgrid::mc::processes_time[process->pid];
330 void MC_process_clock_add(smx_process_t process, double amount)
332 simgrid::mc::processes_time[process->pid] += amount;
336 void MC_report_assertion_error(void)
338 XBT_INFO("**************************");
339 XBT_INFO("*** PROPERTY NOT VALID ***");
340 XBT_INFO("**************************");
341 XBT_INFO("Counter-example execution trace:");
342 simgrid::mc::dumpRecordPath();
343 for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
344 XBT_INFO("%s", s.c_str());
345 MC_print_statistics(mc_stats);
348 void MC_report_crash(int status)
350 XBT_INFO("**************************");
351 XBT_INFO("** CRASH IN THE PROGRAM **");
352 XBT_INFO("**************************");
353 if (WIFSIGNALED(status))
354 XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
355 else if (WIFEXITED(status))
356 XBT_INFO("From exit: %i", WEXITSTATUS(status));
357 if (WCOREDUMP(status))
358 XBT_INFO("A core dump was generated by the system.");
360 XBT_INFO("No core dump was generated by the system.");
361 XBT_INFO("Counter-example execution trace:");
362 simgrid::mc::dumpRecordPath();
363 for (auto& s : mc_model_checker->getChecker()->getTextualTrace())
364 XBT_INFO("%s", s.c_str());
365 MC_print_statistics(mc_stats);
366 XBT_INFO("Stack trace:");
367 mc_model_checker->process().dumpStack();