Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stringify (a lot)
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2017. 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 <exception>
8
9 #include <cstdlib>
10 #include <cstdio>
11 #include <cstring>
12
13 #include <utility>
14
15 #include <unistd.h>
16
17 #include <xbt/log.h>
18
19 #include "simgrid/sg_config.h"
20 #include "src/xbt_modinter.h"
21
22 #include "src/mc/Session.hpp"
23 #include "src/mc/checker/Checker.hpp"
24 #include "src/mc/mc_base.h"
25 #include "src/mc/mc_comm_pattern.hpp"
26 #include "src/mc/mc_exit.hpp"
27 #include "src/mc/mc_private.hpp"
28 #include "src/mc/mc_safety.hpp"
29 #include "src/mc/remote/mc_protocol.h"
30
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
32 extern std::string _sg_mc_property_file;
33
34 static inline
35 char** argvdup(int argc, char** argv)
36 {
37   char** argv_copy = new char*[argc + 1];
38   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
39   argv_copy[argc] = nullptr;
40   return argv_copy;
41 }
42
43 static std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
44 {
45   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
46     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
47   else if (_sg_mc_property_file.empty())
48     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker(session));
49   else
50     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker(session));
51 }
52
53 int main(int argc, char** argv)
54 {
55   using simgrid::mc::Session;
56   XBT_LOG_CONNECT(mc_main);
57
58   try {
59     if (argc < 2)
60       xbt_die("Missing arguments.\n");
61
62     // Currently, we need this before sg_config_init:
63     _sg_do_model_check = 1;
64
65     // The initialization function can touch argv.
66     // We make a copy of argv before modifying it in order to pass the original
67     // value to the model-checked:
68     char** argv_copy = argvdup(argc, argv);
69     xbt_log_init(&argc, argv);
70     sg_config_init(&argc, argv);
71
72     std::unique_ptr<Session> session =
73       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
74     delete[] argv_copy;
75
76     simgrid::mc::session = session.get();
77     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
78     int res = SIMGRID_MC_EXIT_SUCCESS;
79     try {
80       checker->run();
81     } catch (simgrid::mc::DeadlockError& de) {
82       res = SIMGRID_MC_EXIT_DEADLOCK;
83     } catch (simgrid::mc::TerminationError& te) {
84       res = SIMGRID_MC_EXIT_NON_TERMINATION;
85     } catch (simgrid::mc::LivenessError& le) {
86       res = SIMGRID_MC_EXIT_LIVENESS;
87     }
88     checker = nullptr;
89     session->close();
90     return res;
91   }
92   catch(std::exception& e) {
93     XBT_ERROR("Exception: %s", e.what());
94     return SIMGRID_MC_EXIT_ERROR;
95   }
96   catch(...) {
97     XBT_ERROR("Unknown exception");
98     return SIMGRID_MC_EXIT_ERROR;
99   }
100 }