d2d8d4ab835a5d44d35789f1075f8f7d55a09063
[model-checker.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <string.h>
8
9 #include "common.h"
10 #include "output.h"
11
12 #include "datarace.h"
13
14 /* global "model" object */
15 #include "model.h"
16 #include "params.h"
17 #include "snapshot-interface.h"
18 #include "scanalysis.h"
19 #include "plugins.h"
20
21 static void param_defaults(struct model_params *params)
22 {
23         params->maxreads = 0;
24         params->maxfuturedelay = 6;
25         params->fairwindow = 0;
26         params->yieldon = false;
27         params->yieldblock = false;
28         params->enabledcount = 1;
29         params->bound = 0;
30         params->maxfuturevalues = 0;
31         params->expireslop = 4;
32         params->verbose = !!DBG_ENABLED();
33         params->uninitvalue = 0;
34 }
35
36 static void print_usage(const char *program_name, struct model_params *params)
37 {
38         /* Reset defaults before printing */
39         param_defaults(params);
40
41         model_print(
42 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
43 "Distributed under the GPLv2\n"
44 "Written by Brian Norris and Brian Demsky\n"
45 "\n"
46 "Usage: %s [MODEL-CHECKER OPTIONS] -- [PROGRAM ARGS]\n"
47 "\n"
48 "MODLE-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
49 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
50 "\n"
51 "Model-checker options:\n"
52 "-h, --help                  Display this help message and exit\n"
53 "-m, --liveness=NUM          Maximum times a thread can read from the same write\n"
54 "                              while other writes exist.\n"
55 "                              Default: %d\n"
56 "-M, --maxfv=NUM             Maximum number of future values that can be sent to\n"
57 "                              the same read.\n"
58 "                              Default: %d\n"
59 "-s, --maxfvdelay=NUM        Maximum actions that the model checker will wait for\n"
60 "                              a write from the future past the expected number\n"
61 "                              of actions.\n"
62 "                              Default: %d\n"
63 "-S, --fvslop=NUM            Future value expiration sloppiness.\n"
64 "                              Default: %u\n"
65 "-y, --yield                 Enable CHESS-like yield-based fairness support.\n"
66 "                              Default: %s\n"
67 "-Y, --yieldblock            Prohibit an execution from running a yield.\n"
68 "                              Default: %s\n"
69 "-f, --fairness=WINDOW       Specify a fairness window in which actions that are\n"
70 "                              enabled sufficiently many times should receive\n"
71 "                              priority for execution (not recommended).\n"
72 "                              Default: %d\n"
73 "-e, --enabled=COUNT         Enabled count.\n"
74 "                              Default: %d\n"
75 "-b, --bound=MAX             Upper length bound.\n"
76 "                              Default: %d\n"
77 "-v, --verbose               Print verbose execution information.\n"
78 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
79 "                              uninitialized atomic.\n"
80 "                              Default: %u\n"
81 "-t, --analysis=NAME         Use Trace Analysis.\n"
82 "-o, --options=NAME          Options.\n"
83 " --                         Program arguments follow.\n\n",
84                 program_name,
85                 params->maxreads,
86                 params->maxfuturevalues,
87                 params->maxfuturedelay,
88                 params->expireslop,
89                 params->yieldon ? "enabled" : "disabled",
90                 params->yieldblock ? "enabled" : "disabled",
91                 params->fairwindow,
92                 params->enabledcount,
93                 params->bound,
94                 params->uninitvalue);
95         exit(EXIT_SUCCESS);
96 }
97
98 bool install_plugin(char * name) {
99         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
100         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
101
102         for(unsigned int i=0;i<registeredanalysis->size();i++) {
103                 TraceAnalysis * analysis=(*registeredanalysis)[i];
104                 if (strcmp(name, analysis->name())==0) {
105                         installedanalysis->push_back(analysis);
106                         return false;
107                 }
108         }
109         model_print("Analysis %s Not Found\n", name);
110         return true;
111 }
112
113 static void parse_options(struct model_params *params, int argc, char **argv)
114 {
115         const char *shortopts = "hyYt:o:m:M:s:S:f:e:b:u:v::";
116         const struct option longopts[] = {
117                 {"help", no_argument, NULL, 'h'},
118                 {"liveness", required_argument, NULL, 'm'},
119                 {"maxfv", required_argument, NULL, 'M'},
120                 {"maxfvdelay", required_argument, NULL, 's'},
121                 {"fvslop", required_argument, NULL, 'S'},
122                 {"fairness", required_argument, NULL, 'f'},
123                 {"yield", no_argument, NULL, 'y'},
124                 {"yieldblock", no_argument, NULL, 'Y'},
125                 {"enabled", required_argument, NULL, 'e'},
126                 {"bound", required_argument, NULL, 'b'},
127                 {"verbose", optional_argument, NULL, 'v'},
128                 {"uninitialized", optional_argument, NULL, 'u'},
129                 {"analysis", optional_argument, NULL, 't'},
130                 {"options", optional_argument, NULL, 'o'},
131                 {0, 0, 0, 0} /* Terminator */
132         };
133         int opt, longindex;
134         bool error = false;
135         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
136                 switch (opt) {
137                 case 'h':
138                         print_usage(argv[0], params);
139                         break;
140                 case 's':
141                         params->maxfuturedelay = atoi(optarg);
142                         break;
143                 case 'S':
144                         params->expireslop = atoi(optarg);
145                         break;
146                 case 'f':
147                         params->fairwindow = atoi(optarg);
148                         break;
149                 case 'e':
150                         params->enabledcount = atoi(optarg);
151                         break;
152                 case 'b':
153                         params->bound = atoi(optarg);
154                         break;
155                 case 'm':
156                         params->maxreads = atoi(optarg);
157                         break;
158                 case 'M':
159                         params->maxfuturevalues = atoi(optarg);
160                         break;
161                 case 'v':
162                         params->verbose = optarg ? atoi(optarg) : 1;
163                         break;
164                 case 'u':
165                         params->uninitvalue = atoi(optarg);
166                         break;
167                 case 'y':
168                         params->yieldon = true;
169                         break;
170                 case 't':
171                         if (install_plugin(optarg))
172                                 error = true;
173                         break;
174                 case 'o':
175                         {
176                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
177                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
178                                         error = true;
179                         }
180                         break;
181                 case 'Y':
182                         params->yieldblock = true;
183                         break;
184                 default: /* '?' */
185                         error = true;
186                         break;
187                 }
188         }
189
190         /* Pass remaining arguments to user program */
191         params->argc = argc - (optind - 1);
192         params->argv = argv + (optind - 1);
193
194         /* Reset program name */
195         params->argv[0] = argv[0];
196
197         /* Reset (global) optind for potential use by user program */
198         optind = 1;
199
200         if (error)
201                 print_usage(argv[0], params);
202 }
203
204 int main_argc;
205 char **main_argv;
206
207 static void install_trace_analyses(ModelExecution *execution)
208 {
209         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
210         for(unsigned int i=0;i<installedanalysis->size();i++) {
211                 TraceAnalysis * ta=(*installedanalysis)[i];
212                 ta->setExecution(execution);
213                 model->add_trace_analysis(ta);
214         }
215 }
216
217 /** The model_main function contains the main model checking loop. */
218 static void model_main()
219 {
220         struct model_params params;
221
222         param_defaults(&params);
223         register_plugins();
224
225         parse_options(&params, main_argc, main_argv);
226
227         //Initialize race detector
228         initRaceDetector();
229
230         snapshot_stack_init();
231
232         model = new ModelChecker(params);
233         install_trace_analyses(model->get_execution());
234
235         snapshot_record(0);
236         model->run();
237         delete model;
238
239         DEBUG("Exiting\n");
240 }
241
242 /**
243  * Main function.  Just initializes snapshotting library and the
244  * snapshotting library calls the model_main function.
245  */
246 int main(int argc, char **argv)
247 {
248         main_argc = argc;
249         main_argv = argv;
250
251         /* Configure output redirection for the model-checker */
252         redirect_output();
253
254         /* Let's jump in quickly and start running stuff */
255         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
256 }