Logo AND Algorithmique Numérique Distribuée

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