Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Centralize definitions for the name of environment variables used by the MC.
[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
12 #if SIMGRID_HAVE_STATEFUL_MC
13 #include "src/mc/sosp/RemoteProcessMemory.hpp"
14 #endif
15
16 #include <sys/wait.h>
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_explo, mc, "Generic exploration algorithm of the model-checker");
19
20 namespace simgrid::mc {
21
22 static simgrid::config::Flag<std::string> cfg_dot_output_file{
23     "model-check/dot-output", "Name of dot output file corresponding to graph state", ""};
24
25 Exploration* Exploration::instance_ = nullptr; // singleton instance
26
27 Exploration::Exploration(const std::vector<char*>& args, bool need_memory_introspection)
28     : remote_app_(std::make_unique<RemoteApp>(args, need_memory_introspection))
29 {
30   xbt_assert(instance_ == nullptr, "Cannot have more than one exploration instance");
31   instance_ = this;
32
33   if (not cfg_dot_output_file.get().empty()) {
34     dot_output_ = fopen(cfg_dot_output_file.get().c_str(), "w");
35     xbt_assert(dot_output_ != nullptr, "Error open dot output file: %s", strerror(errno));
36
37     fprintf(dot_output_, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node "
38                          "[fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
39   }
40 }
41
42 Exploration::~Exploration()
43 {
44   if (dot_output_ != nullptr)
45     fclose(dot_output_);
46   instance_ = nullptr;
47 }
48
49 void Exploration::dot_output(const char* fmt, ...)
50 {
51   if (dot_output_ != nullptr) {
52     va_list ap;
53     va_start(ap, fmt);
54     vfprintf(dot_output_, fmt, ap);
55     va_end(ap);
56     fflush(dot_output_);
57   }
58 }
59
60 void Exploration::log_state()
61 {
62   if (not cfg_dot_output_file.get().empty()) {
63     dot_output("}\n");
64     fclose(dot_output_);
65   }
66   if (getenv(MC_ENV_SYSTEM_STATISTICS)) {
67     int ret = system("free");
68     if (ret != 0)
69       XBT_WARN("Call to system(free) did not return 0, but %d", ret);
70   }
71 }
72 // Make our tests fully reproducible despite the subtle differences of strsignal() across archs
73 static const char* signal_name(int status)
74 {
75   switch (WTERMSIG(status)) {
76     case SIGABRT: // FreeBSD uses "Abort trap" as a strsignal for SIGABRT
77       return "Aborted";
78     case SIGSEGV: // MacOSX uses "Segmentation fault: 11" for SIGKILL
79       return "Segmentation fault";
80     default:
81       return strsignal(WTERMSIG(status));
82   }
83 }
84 XBT_ATTRIB_NORETURN void Exploration::report_crash(int status)
85 {
86   XBT_INFO("**************************");
87   XBT_INFO("** CRASH IN THE PROGRAM **");
88   XBT_INFO("**************************");
89   if (WIFSIGNALED(status))
90     XBT_INFO("From signal: %s", signal_name(status));
91   else if (WIFEXITED(status))
92     XBT_INFO("From exit: %i", WEXITSTATUS(status));
93   if (not xbt_log_no_loc)
94     XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
95
96   XBT_INFO("Counter-example execution trace:");
97   for (auto const& s : get_textual_trace())
98     XBT_INFO("  %s", s.c_str());
99   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
100            "--cfg=model-check/replay:'%s'",
101            get_record_trace().to_string().c_str());
102   log_state();
103   if (xbt_log_no_loc) {
104     XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
105   } else {
106 #if SIMGRID_HAVE_STATEFUL_MC
107     const auto* memory = get_remote_app().get_remote_process_memory();
108     if (memory) {
109       XBT_INFO("Stack trace:");
110       memory->dump_stack();
111     } else
112 #endif
113       XBT_INFO("Stack trace not shown because there is no memory introspection.");
114   }
115
116   system_exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
117 }
118 XBT_ATTRIB_NORETURN void Exploration::report_assertion_failure()
119 {
120   XBT_INFO("**************************");
121   XBT_INFO("*** PROPERTY NOT VALID ***");
122   XBT_INFO("**************************");
123   XBT_INFO("Counter-example execution trace:");
124   for (auto const& s : get_textual_trace())
125     XBT_INFO("  %s", s.c_str());
126   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
127            "--cfg=model-check/replay:'%s'",
128            get_record_trace().to_string().c_str());
129   log_state();
130   system_exit(SIMGRID_MC_EXIT_SAFETY);
131 }
132
133 void Exploration::system_exit(int status) const
134 {
135   ::exit(status);
136 }
137
138 }; // namespace simgrid::mc