Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
laste batch tonight
[simgrid.git] / src / mc / checker / simgrid_mc.cpp
1 /* Copyright (c) 2015-2018. 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
57   try {
58     if (argc < 2)
59       xbt_die("Missing arguments.\n");
60
61     // Currently, we need this before sg_config_init:
62     _sg_do_model_check = 1;
63
64     // The initialization function can touch argv.
65     // We make a copy of argv before modifying it in order to pass the original
66     // value to the model-checked:
67     char** argv_copy = argvdup(argc, argv);
68     xbt_log_init(&argc, argv);
69     sg_config_init(&argc, argv);
70
71     std::unique_ptr<Session> session =
72       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
73     delete[] argv_copy;
74
75     simgrid::mc::session = session.get();
76     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
77     int res = SIMGRID_MC_EXIT_SUCCESS;
78     try {
79       checker->run();
80     } catch (simgrid::mc::DeadlockError& de) {
81       res = SIMGRID_MC_EXIT_DEADLOCK;
82     } catch (simgrid::mc::TerminationError& te) {
83       res = SIMGRID_MC_EXIT_NON_TERMINATION;
84     } catch (simgrid::mc::LivenessError& le) {
85       res = SIMGRID_MC_EXIT_LIVENESS;
86     }
87     checker = nullptr;
88     session->close();
89     return res;
90   }
91   catch(std::exception& e) {
92     XBT_ERROR("Exception: %s", e.what());
93     return SIMGRID_MC_EXIT_ERROR;
94   }
95   catch(...) {
96     XBT_ERROR("Unknown exception");
97     return SIMGRID_MC_EXIT_ERROR;
98   }
99 }