Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6efe84181fffe982e08568793acf932bf428c70b
[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_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
72 XBT_ATTRIB_NORETURN void Exploration::report_crash(int status)
73 {
74   XBT_INFO("**************************");
75   XBT_INFO("** CRASH IN THE PROGRAM **");
76   XBT_INFO("**************************");
77   if (WIFSIGNALED(status)) // FreeBSD use "Abort trap" as a strsignal for SIGABRT that is part of our tests
78     XBT_INFO("From signal: %s", WTERMSIG(status) == SIGABRT ? "Aborted" : strsignal(WTERMSIG(status)));
79   else if (WIFEXITED(status))
80     XBT_INFO("From exit: %i", WEXITSTATUS(status));
81   if (not xbt_log_no_loc)
82     XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
83
84   XBT_INFO("Counter-example execution trace:");
85   for (auto const& s : get_textual_trace())
86     XBT_INFO("  %s", s.c_str());
87   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
88            "--cfg=model-check/replay:'%s'",
89            get_record_trace().to_string().c_str());
90   log_state();
91   if (xbt_log_no_loc) {
92     XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
93   } else {
94 #if SIMGRID_HAVE_MC
95     const auto* memory = get_remote_app().get_remote_process_memory();
96     if (memory) {
97       XBT_INFO("Stack trace:");
98       memory->dump_stack();
99     } else
100 #endif
101       XBT_INFO("Stack trace not shown because there is no memory introspection.");
102   }
103
104   system_exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
105 }
106 XBT_ATTRIB_NORETURN void Exploration::report_assertion_failure()
107 {
108   XBT_INFO("**************************");
109   XBT_INFO("*** PROPERTY NOT VALID ***");
110   XBT_INFO("**************************");
111   XBT_INFO("Counter-example execution trace:");
112   for (auto const& s : get_textual_trace())
113     XBT_INFO("  %s", s.c_str());
114   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
115            "--cfg=model-check/replay:'%s'",
116            get_record_trace().to_string().c_str());
117   log_state();
118   system_exit(SIMGRID_MC_EXIT_SAFETY);
119 }
120
121 void Exploration::system_exit(int status) const
122 {
123   ::exit(status);
124 }
125
126 }; // namespace simgrid::mc