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