Logo AND Algorithmique Numérique Distribuée

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