Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further tidy the includes in MC
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/sg_config.hpp"
8 #include "src/mc/Session.hpp"
9 #include "src/mc/checker/Checker.hpp"
10 #include "src/mc/mc_config.hpp"
11 #include "src/mc/mc_exit.hpp"
12
13 #include <cstring>
14 #include <memory>
15
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
17
18 static inline
19 char** argvdup(int argc, char** argv)
20 {
21   char** argv_copy = new char*[argc + 1];
22   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
23   argv_copy[argc] = nullptr;
24   return argv_copy;
25 }
26
27 static std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
28 {
29   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
30     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
31   else if (_sg_mc_property_file.get().empty())
32     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker(session));
33   else
34     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker(session));
35 }
36
37 int main(int argc, char** argv)
38 {
39   using simgrid::mc::Session;
40
41   try {
42     if (argc < 2)
43       xbt_die("Missing arguments.\n");
44
45     // Currently, we need this before sg_config_init:
46     _sg_do_model_check = 1;
47
48     // The initialization function can touch argv.
49     // We make a copy of argv before modifying it in order to pass the original
50     // value to the model-checked:
51     char** argv_copy = argvdup(argc, argv);
52     xbt_log_init(&argc, argv);
53     sg_config_init(&argc, argv);
54
55     std::unique_ptr<Session> session =
56       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
57     delete[] argv_copy;
58
59     simgrid::mc::session = session.get();
60     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
61     int res = SIMGRID_MC_EXIT_SUCCESS;
62     try {
63       checker->run();
64     } catch (const simgrid::mc::DeadlockError&) {
65       res = SIMGRID_MC_EXIT_DEADLOCK;
66     } catch (const simgrid::mc::TerminationError&) {
67       res = SIMGRID_MC_EXIT_NON_TERMINATION;
68     } catch (const simgrid::mc::LivenessError&) {
69       res = SIMGRID_MC_EXIT_LIVENESS;
70     }
71     checker = nullptr;
72     session->close();
73     return res;
74   }
75   catch(std::exception& e) {
76     XBT_ERROR("Exception: %s", e.what());
77     return SIMGRID_MC_EXIT_ERROR;
78   }
79   catch(...) {
80     XBT_ERROR("Unknown exception");
81     return SIMGRID_MC_EXIT_ERROR;
82   }
83 }