Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr/gitroot/simgrid/simgrid
[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_exit.h"
28 #include "src/mc/Session.hpp"
29 #include "src/mc/Checker.hpp"
30 #include "src/mc/CommunicationDeterminismChecker.hpp"
31 #include "src/mc/SafetyChecker.hpp"
32
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_main, mc, "Entry point for simgrid-mc");
34
35 static inline
36 char** argvdup(int argc, char** argv)
37 {
38   char** argv_copy = xbt_new(char*, argc+1);
39   std::memcpy(argv_copy, argv, sizeof(char*) * argc);
40   argv_copy[argc] = nullptr;
41   return argv_copy;
42 }
43
44 static
45 std::unique_ptr<simgrid::mc::Checker> createChecker(simgrid::mc::Session& session)
46 {
47   if (_sg_mc_comms_determinism || _sg_mc_send_determinism)
48     return std::unique_ptr<simgrid::mc::Checker>(
49       new simgrid::mc::CommunicationDeterminismChecker(session));
50   else if (!_sg_mc_property_file || _sg_mc_property_file[0] == '\0')
51     return std::unique_ptr<simgrid::mc::Checker>(
52       new simgrid::mc::SafetyChecker(session));
53   else
54     return std::unique_ptr<simgrid::mc::Checker>(
55       simgrid::mc::createLivenessChecker(session));
56 }
57
58 int main(int argc, char** argv)
59 {
60   using simgrid::mc::Session;
61
62   try {
63     if (argc < 2)
64       xbt_die("Missing arguments.\n");
65
66     // Currently, we need this before sg_config_init:
67     _sg_do_model_check = 1;
68     mc_mode = MC_MODE_SERVER;
69
70     // The initialisation function can touch argv.
71     // We make a copy of argv before modifying it in order to pass the original
72     // value to the model-checked:
73     char** argv_copy = argvdup(argc, argv);
74     xbt_log_init(&argc, argv);
75     sg_config_init(&argc, argv);
76
77     std::unique_ptr<Session> session =
78       std::unique_ptr<Session>(Session::spawnvp(argv_copy[1], argv_copy+1));
79     free(argv_copy);
80
81     simgrid::mc::session = session.get();
82     std::unique_ptr<simgrid::mc::Checker> checker = createChecker(*session);
83     int res = checker->run();
84     checker = nullptr;
85     session->close();
86     return res;
87   }
88   catch(std::exception& e) {
89     XBT_ERROR("Exception: %s", e.what());
90     return SIMGRID_MC_EXIT_ERROR;
91   }
92   catch(...) {
93     XBT_ERROR("Unknown exception");
94     return SIMGRID_MC_EXIT_ERROR;
95   }
96 }