Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e153b0c9a2827f83e77d1b4f55e7ffe119660343
[simgrid.git] / src / mc / simgrid_mc.cpp
1 /* Copyright (c) 2015. 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/mc_base.h"
23 #include "src/mc/mc_private.h"
24 #include "src/mc/mc_protocol.h"
25 #include "src/mc/mc_safety.h"
26 #include "src/mc/mc_comm_pattern.h"
27 #include "src/mc/mc_liveness.h"
28 #include "src/mc/mc_exit.h"
29 #include "src/mc/Session.hpp"
30 #include "src/mc/Checker.hpp"
31
32 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
33
34 static
35 char** argvdup(int argc, char** argv)
36 {
37   char** argv_copy = xbt_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
44 std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
45 {
46   using simgrid::mc::Session;
47   using simgrid::mc::FunctionalChecker;
48
49   std::function<int(Session& session)> code;
50   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
51     code = [](Session& session) {
52       return MC_modelcheck_comm_determinism(); };
53   else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
54     code = [](Session& session) {
55       return simgrid::mc::modelcheck_safety(); };
56   else
57     code = [](Session& session) {
58       return simgrid::mc::modelcheck_liveness(); };
59
60   return std::unique_ptr<simgrid::mc::Checker>(
61     new FunctionalChecker(session, std::move(code)));
62 }
63
64 int main(int argc, char** argv)
65 {
66   using simgrid::mc::Session;
67
68   try {
69     if (argc < 2)
70       xbt_die("Missing arguments.\n");
71
72     // Currently, we need this before sg_config_init:
73     _sg_do_model_check = 1;
74     mc_mode = MC_MODE_SERVER;
75
76     // The initialisation function can touch argv.
77     // We need to keep the original parameters in order to pass them to the
78     // model-checked process so we make a copy of them:
79     int argc_copy = argc;
80     char** argv_copy = argvdup(argc, argv);
81     xbt_log_init(&argc_copy, argv_copy);
82     sg_config_init(&argc_copy, argv_copy);
83
84     std::unique_ptr<Session> session =
85       std::unique_ptr<Session>(Session::spawnvp(argv[1], argv+1));
86     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
87     int res = checker->run();
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 }