Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove the stateful model-checking from the archive. It's not working anymore
[simgrid.git] / src / mc / explo / Exploration.cpp
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/mc/explo/Exploration.hpp"
7 #include "src/mc/mc_config.hpp"
8 #include "src/mc/mc_environ.h"
9 #include "src/mc/mc_exit.hpp"
10 #include "src/mc/mc_private.hpp"
11 #include "xbt/string.hpp"
12
13 #include <sys/wait.h>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_explo, mc, "Generic exploration algorithm of the model-checker");
16
17 namespace simgrid::mc {
18
19 static simgrid::config::Flag<std::string> cfg_dot_output_file{
20     "model-check/dot-output", "Name of dot output file corresponding to graph state", ""};
21
22 Exploration* Exploration::instance_ = nullptr; // singleton instance
23
24 Exploration::Exploration(const std::vector<char*>& args) : remote_app_(std::make_unique<RemoteApp>(args))
25 {
26   xbt_assert(instance_ == nullptr, "Cannot have more than one exploration instance");
27   instance_ = this;
28
29   if (not cfg_dot_output_file.get().empty()) {
30     dot_output_ = fopen(cfg_dot_output_file.get().c_str(), "w");
31     xbt_assert(dot_output_ != nullptr, "Error open dot output file: %s", strerror(errno));
32
33     fprintf(dot_output_, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node "
34                          "[fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
35   }
36 }
37
38 Exploration::~Exploration()
39 {
40   if (dot_output_ != nullptr)
41     fclose(dot_output_);
42   instance_ = nullptr;
43 }
44
45 void Exploration::dot_output(const char* fmt, ...)
46 {
47   if (dot_output_ != nullptr) {
48     va_list ap;
49     va_start(ap, fmt);
50     vfprintf(dot_output_, fmt, ap);
51     va_end(ap);
52     fflush(dot_output_);
53   }
54 }
55
56 void Exploration::log_state()
57 {
58   if (not cfg_dot_output_file.get().empty()) {
59     dot_output("}\n");
60     fclose(dot_output_);
61   }
62   if (getenv(MC_ENV_SYSTEM_STATISTICS)) {
63     int ret = system("free");
64     if (ret != 0)
65       XBT_WARN("Call to system(free) did not return 0, but %d", ret);
66   }
67 }
68 // Make our tests fully reproducible despite the subtle differences of strsignal() across archs
69 static const char* signal_name(int status)
70 {
71   switch (WTERMSIG(status)) {
72     case SIGABRT: // FreeBSD uses "Abort trap" as a strsignal for SIGABRT
73       return "Aborted";
74     case SIGSEGV: // MacOSX uses "Segmentation fault: 11" for SIGKILL
75       return "Segmentation fault";
76     default:
77       return strsignal(WTERMSIG(status));
78   }
79 }
80
81 std::vector<std::string> Exploration::get_textual_trace(int max_elements)
82 {
83   std::vector<std::string> trace;
84   for (auto const& transition : get_record_trace()) {
85     auto const& call_location = transition->get_call_location();
86     if (not call_location.empty())
87       trace.push_back(xbt::string_printf("Actor %ld in %s ==> simcall: %s", transition->aid_, call_location.c_str(),
88                                          transition->to_string().c_str()));
89     else
90       trace.push_back(xbt::string_printf("Actor %ld in simcall %s", transition->aid_, transition->to_string().c_str()));
91     max_elements--;
92     if (max_elements == 0)
93       break;
94   }
95   return trace;
96 }
97
98 XBT_ATTRIB_NORETURN void Exploration::report_crash(int status)
99 {
100   XBT_INFO("**************************");
101   XBT_INFO("** CRASH IN THE PROGRAM **");
102   XBT_INFO("**************************");
103   if (WIFSIGNALED(status))
104     XBT_INFO("From signal: %s", signal_name(status));
105   else if (WIFEXITED(status))
106     XBT_INFO("From exit: %i", WEXITSTATUS(status));
107   if (not xbt_log_no_loc)
108     XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
109
110   XBT_INFO("Counter-example execution trace:");
111   for (auto const& s : get_textual_trace())
112     XBT_INFO("  %s", s.c_str());
113   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
114            "--cfg=model-check/replay:'%s'",
115            get_record_trace().to_string().c_str());
116   log_state();
117
118   throw McError(ExitStatus::PROGRAM_CRASH);
119 }
120 XBT_ATTRIB_NORETURN void Exploration::report_assertion_failure()
121 {
122   XBT_INFO("**************************");
123   XBT_INFO("*** PROPERTY NOT VALID ***");
124   XBT_INFO("**************************");
125   XBT_INFO("Counter-example execution trace:");
126   for (auto const& s : get_textual_trace())
127     XBT_INFO("  %s", s.c_str());
128   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
129            "--cfg=model-check/replay:'%s'",
130            get_record_trace().to_string().c_str());
131   log_state();
132   throw McError(ExitStatus::SAFETY);
133 }
134
135 }; // namespace simgrid::mc