Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move another function out of ModelChecker
[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 #include "src/mc/sosp/RemoteProcessMemory.hpp"
11
12 #include <sys/wait.h>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_explo, mc, "Generic exploration algorithm of the model-checker");
15
16 namespace simgrid::mc {
17
18 static simgrid::config::Flag<std::string> cfg_dot_output_file{
19     "model-check/dot-output", "Name of dot output file corresponding to graph state", ""};
20
21 Exploration::Exploration(const std::vector<char*>& args) : remote_app_(std::make_unique<RemoteApp>(args))
22 {
23   mc_model_checker->set_exploration(this);
24
25   if (not cfg_dot_output_file.get().empty()) {
26     dot_output_ = fopen(cfg_dot_output_file.get().c_str(), "w");
27     xbt_assert(dot_output_ != nullptr, "Error open dot output file: %s", strerror(errno));
28
29     fprintf(dot_output_, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.25; edge [fontsize=12]; node "
30                          "[fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
31   }
32 }
33
34 Exploration::~Exploration()
35 {
36   if (dot_output_ != nullptr)
37     fclose(dot_output_);
38 }
39
40 void Exploration::dot_output(const char* fmt, ...)
41 {
42   if (dot_output_ != nullptr) {
43     va_list ap;
44     va_start(ap, fmt);
45     vfprintf(dot_output_, fmt, ap);
46     va_end(ap);
47     fflush(dot_output_);
48   }
49 }
50
51 void Exploration::log_state()
52 {
53   if (not cfg_dot_output_file.get().empty()) {
54     dot_output("}\n");
55     fclose(dot_output_);
56   }
57   if (getenv("SIMGRID_MC_SYSTEM_STATISTICS")) {
58     int ret = system("free");
59     if (ret != 0)
60       XBT_WARN("Call to system(free) did not return 0, but %d", ret);
61   }
62 }
63
64 void Exploration::report_crash(int status)
65 {
66   XBT_INFO("**************************");
67   XBT_INFO("** CRASH IN THE PROGRAM **");
68   XBT_INFO("**************************");
69   if (WIFSIGNALED(status))
70     XBT_INFO("From signal: %s", strsignal(WTERMSIG(status)));
71   else if (WIFEXITED(status))
72     XBT_INFO("From exit: %i", WEXITSTATUS(status));
73   if (not xbt_log_no_loc)
74     XBT_INFO("%s core dump was generated by the system.", WCOREDUMP(status) ? "A" : "No");
75
76   XBT_INFO("Counter-example execution trace:");
77   for (auto const& s : get_textual_trace())
78     XBT_INFO("  %s", s.c_str());
79   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
80            "--cfg=model-check/replay:'%s'",
81            get_record_trace().to_string().c_str());
82   log_state();
83   if (xbt_log_no_loc) {
84     XBT_INFO("Stack trace not displayed because you passed --log=no_loc");
85   } else {
86     XBT_INFO("Stack trace:");
87     get_remote_app().get_remote_process_memory().dump_stack();
88   }
89
90   get_remote_app().get_remote_process_memory().terminate();
91   system_exit(SIMGRID_MC_EXIT_PROGRAM_CRASH);
92 }
93 void Exploration::report_assertion_failure()
94 {
95   XBT_INFO("**************************");
96   XBT_INFO("*** PROPERTY NOT VALID ***");
97   XBT_INFO("**************************");
98   XBT_INFO("Counter-example execution trace:");
99   for (auto const& s : get_textual_trace())
100     XBT_INFO("  %s", s.c_str());
101   XBT_INFO("You can debug the problem (and see the whole details) by rerunning out of simgrid-mc with "
102            "--cfg=model-check/replay:'%s'",
103            get_record_trace().to_string().c_str());
104   log_state();
105   system_exit(SIMGRID_MC_EXIT_SAFETY);
106 }
107
108 void Exploration::system_exit(int status)
109 {
110   get_remote_app().shutdown();
111   ::exit(status);
112 }
113
114 }; // namespace simgrid::mc