1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved. */
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. */
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"
13 #if SIMGRID_HAVE_STATEFUL_MC
14 #include "src/mc/sosp/RemoteProcessMemory.hpp"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_explo, mc, "Generic exploration algorithm of the model-checker");
21 namespace simgrid::mc {
23 static simgrid::config::Flag<std::string> cfg_dot_output_file{
24 "model-check/dot-output", "Name of dot output file corresponding to graph state", ""};
26 Exploration* Exploration::instance_ = nullptr; // singleton instance
28 Exploration::Exploration(const std::vector<char*>& args, bool need_memory_introspection)
29 : remote_app_(std::make_unique<RemoteApp>(args, need_memory_introspection))
31 xbt_assert(instance_ == nullptr, "Cannot have more than one exploration instance");
34 if (not cfg_dot_output_file.get().empty()) {
35 dot_output_ = fopen(cfg_dot_output_file.get().c_str(), "w");
36 xbt_assert(dot_output_ != nullptr, "Error open dot output file: %s", strerror(errno));
38 fprintf(dot_output_, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node "
39 "[fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
43 Exploration::~Exploration()
45 if (dot_output_ != nullptr)
50 void Exploration::dot_output(const char* fmt, ...)
52 if (dot_output_ != nullptr) {
55 vfprintf(dot_output_, fmt, ap);
61 void Exploration::log_state()
63 if (not cfg_dot_output_file.get().empty()) {
67 if (getenv(MC_ENV_SYSTEM_STATISTICS)) {
68 int ret = system("free");
70 XBT_WARN("Call to system(free) did not return 0, but %d", ret);
73 // Make our tests fully reproducible despite the subtle differences of strsignal() across archs
74 static const char* signal_name(int status)
76 switch (WTERMSIG(status)) {
77 case SIGABRT: // FreeBSD uses "Abort trap" as a strsignal for SIGABRT
79 case SIGSEGV: // MacOSX uses "Segmentation fault: 11" for SIGKILL
80 return "Segmentation fault";
82 return strsignal(WTERMSIG(status));
86 std::vector<std::string> Exploration::get_textual_trace()
88 std::vector<std::string> trace;
89 for (auto const& transition : get_record_trace())
90 trace.push_back(xbt::string_printf("%ld: %s", transition->aid_, transition->to_string().c_str()));
94 XBT_ATTRIB_NORETURN void Exploration::report_crash(int status)
96 XBT_INFO("**************************");
97 XBT_INFO("** CRASH IN THE PROGRAM **");
98 XBT_INFO("**************************");
99 if (WIFSIGNALED(status))
100 XBT_INFO("From signal: %s", signal_name(status));
101 else if (WIFEXITED(status))
102 XBT_INFO("From exit: %i", WEXITSTATUS(status));
103 if (not xbt_log_no_loc)
104 XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
106 XBT_INFO("Counter-example execution trace:");
107 for (auto const& s : get_textual_trace())
108 XBT_INFO(" %s", s.c_str());
109 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
110 "--cfg=model-check/replay:'%s'",
111 get_record_trace().to_string().c_str());
113 if (xbt_log_no_loc) {
114 XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
116 #if SIMGRID_HAVE_STATEFUL_MC
117 const auto* memory = get_remote_app().get_remote_process_memory();
119 XBT_INFO("Stack trace:");
120 memory->dump_stack();
123 XBT_INFO("Stack trace not shown because there is no memory introspection.");
126 throw McError(ExitStatus::PROGRAM_CRASH);
128 XBT_ATTRIB_NORETURN void Exploration::report_assertion_failure()
130 XBT_INFO("**************************");
131 XBT_INFO("*** PROPERTY NOT VALID ***");
132 XBT_INFO("**************************");
133 XBT_INFO("Counter-example execution trace:");
134 for (auto const& s : get_textual_trace())
135 XBT_INFO(" %s", s.c_str());
136 XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
137 "--cfg=model-check/replay:'%s'",
138 get_record_trace().to_string().c_str());
140 throw McError(ExitStatus::SAFETY);
143 }; // namespace simgrid::mc