Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement I/O as asynchronous activities
[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.hpp"
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
33 static inline
34 char** argvdup(int argc, char** argv)
35 {
36   char** argv_copy = new char*[argc + 1];
37   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
38   argv_copy[argc] = nullptr;
39   return argv_copy;
40 }
41
42 static std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
43 {
44   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
45     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createCommunicationDeterminismChecker(session));
46   else if (_sg_mc_property_file.get().empty())
47     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createSafetyChecker(session));
48   else
49     return std::unique_ptr<simgrid::mc::Checker>(simgrid::mc::createLivenessChecker(session));
50 }
51
52 int main(int argc, char** argv)
53 {
54   using simgrid::mc::Session;
55
56   try {
57     if (argc < 2)
58       xbt_die("Missing arguments.\n");
59
60     // Currently, we need this before sg_config_init:
61     _sg_do_model_check = 1;
62
63     // The initialization function can touch argv.
64     // We make a copy of argv before modifying it in order to pass the original
65     // value to the model-checked:
66     char** argv_copy = argvdup(argc, argv);
67     xbt_log_init(&argc, argv);
68     sg_config_init(&argc, argv);
69
70     std::unique_ptr<Session> session =
71       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
72     delete[] argv_copy;
73
74     simgrid::mc::session = session.get();
75     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
76     int res = SIMGRID_MC_EXIT_SUCCESS;
77     try {
78       checker->run();
79     } catch (simgrid::mc::DeadlockError& de) {
80       res = SIMGRID_MC_EXIT_DEADLOCK;
81     } catch (simgrid::mc::TerminationError& te) {
82       res = SIMGRID_MC_EXIT_NON_TERMINATION;
83     } catch (simgrid::mc::LivenessError& le) {
84       res = SIMGRID_MC_EXIT_LIVENESS;
85     }
86     checker = nullptr;
87     session->close();
88     return res;
89   }
90   catch(std::exception& e) {
91     XBT_ERROR("Exception: %s", e.what());
92     return SIMGRID_MC_EXIT_ERROR;
93   }
94   catch(...) {
95     XBT_ERROR("Unknown exception");
96     return SIMGRID_MC_EXIT_ERROR;
97   }
98 }