Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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/checker/Checker.hpp"
9 #include "src/mc/mc_exit.hpp"
10
11 #include <cstring>
12 #include <memory>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
15
16 static inline
17 char** argvdup(int argc, char** argv)
18 {
19   char** argv_copy = new char*[argc + 1];
20   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
21   argv_copy[argc] = nullptr;
22   return argv_copy;
23 }
24
25 static std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
26 {
27   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
28     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
29   else if (_sg_mc_property_file.get().empty())
30     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker(session));
31   else
32     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker(session));
33 }
34
35 int main(int argc, char** argv)
36 {
37   using simgrid::mc::Session;
38
39   try {
40     if (argc < 2)
41       xbt_die("Missing arguments.\n");
42
43     // Currently, we need this before sg_config_init:
44     _sg_do_model_check = 1;
45
46     // The initialization function can touch argv.
47     // We make a copy of argv before modifying it in order to pass the original
48     // value to the model-checked:
49     char** argv_copy = argvdup(argc, argv);
50     xbt_log_init(&argc, argv);
51     sg_config_init(&argc, argv);
52
53     std::unique_ptr<Session> session =
54       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
55     delete[] argv_copy;
56
57     simgrid::mc::session = session.get();
58     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
59     int res = SIMGRID_MC_EXIT_SUCCESS;
60     try {
61       checker->run();
62     } catch (simgrid::mc::DeadlockError& de) {
63       res = SIMGRID_MC_EXIT_DEADLOCK;
64     } catch (simgrid::mc::TerminationError& te) {
65       res = SIMGRID_MC_EXIT_NON_TERMINATION;
66     } catch (simgrid::mc::LivenessError& le) {
67       res = SIMGRID_MC_EXIT_LIVENESS;
68     }
69     checker = nullptr;
70     session->close();
71     return res;
72   }
73   catch(std::exception& e) {
74     XBT_ERROR("Exception: %s", e.what());
75     return SIMGRID_MC_EXIT_ERROR;
76   }
77   catch(...) {
78     XBT_ERROR("Unknown exception");
79     return SIMGRID_MC_EXIT_ERROR;
80   }
81 }