remove plugins
[c11tester.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
19 static void param_defaults(struct model_params *params)
20 {
21         params->maxreads = 0;
22         params->maxfuturedelay = 6;
23         params->fairwindow = 0;
24         params->yieldon = false;
25         params->yieldblock = false;
26         params->enabledcount = 1;
27         params->bound = 0;
28         params->maxfuturevalues = 0;
29         params->expireslop = 4;
30         params->verbose = !!DBG_ENABLED();
31         params->uninitvalue = 0;
32         params->maxexecutions = 0;
33 }
34
35 static void print_usage(const char *program_name, struct model_params *params)
36 {
37         /* Reset defaults before printing */
38         param_defaults(params);
39
40         model_print(
41 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
42 "Distributed under the GPLv2\n"
43 "Written by Brian Norris and Brian Demsky\n"
44 "\n"
45 "Usage: %s [MODEL-CHECKER OPTIONS] -- [PROGRAM ARGS]\n"
46 "\n"
47 "MODEL-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
48 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
49 "\n"
50 "Model-checker options:\n"
51 "-h, --help                  Display this help message and exit\n"
52 "-m, --liveness=NUM          Maximum times a thread can read from the same write\n"
53 "                              while other writes exist.\n"
54 "                              Default: %d\n"
55 "-M, --maxfv=NUM             Maximum number of future values that can be sent to\n"
56 "                              the same read.\n"
57 "                              Default: %d\n"
58 "-s, --maxfvdelay=NUM        Maximum actions that the model checker will wait for\n"
59 "                              a write from the future past the expected number\n"
60 "                              of actions.\n"
61 "                              Default: %d\n"
62 "-S, --fvslop=NUM            Future value expiration sloppiness.\n"
63 "                              Default: %u\n"
64 "-y, --yield                 Enable CHESS-like yield-based fairness support\n"
65 "                              (requires thrd_yield() in test program).\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[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
78 "                              0 is quiet; 1 shows valid executions; 2 is noisy;\n"
79 "                              3 is noisier.\n"
80 "                              Default: %d\n"
81 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
82 "                              uninitialized atomic.\n"
83 "                              Default: %u\n"
84 "-t, --analysis=NAME         Use Analysis Plugin.\n"
85 "-o, --options=NAME          Option for previous analysis plugin.  \n"
86 "-x, --maxexec=NUM           Maximum number of executions.\n"
87 "                            Default: %u\n"
88 "                            -o help for a list of options\n"
89 " --                         Program arguments follow.\n\n",
90                 program_name,
91                 params->maxreads,
92                 params->maxfuturevalues,
93                 params->maxfuturedelay,
94                 params->expireslop,
95                 params->yieldon ? "enabled" : "disabled",
96                 params->yieldblock ? "enabled" : "disabled",
97                 params->fairwindow,
98                 params->enabledcount,
99                 params->bound,
100                 params->verbose,
101     params->uninitvalue,
102                 params->maxexecutions);
103         model_print("Analysis plugins:\n");
104
105         exit(EXIT_SUCCESS);
106 }
107
108 static void parse_options(struct model_params *params, int argc, char **argv)
109 {
110         const char *shortopts = "hyYt:o:m:M:s:S:f:e:b:u:x:v::";
111         const struct option longopts[] = {
112                 {"help", no_argument, NULL, 'h'},
113                 {"liveness", required_argument, NULL, 'm'},
114                 {"maxfv", required_argument, NULL, 'M'},
115                 {"maxfvdelay", required_argument, NULL, 's'},
116                 {"fvslop", required_argument, NULL, 'S'},
117                 {"fairness", required_argument, NULL, 'f'},
118                 {"yield", no_argument, NULL, 'y'},
119                 {"yieldblock", no_argument, NULL, 'Y'},
120                 {"enabled", required_argument, NULL, 'e'},
121                 {"bound", required_argument, NULL, 'b'},
122                 {"verbose", optional_argument, NULL, 'v'},
123                 {"uninitialized", required_argument, NULL, 'u'},
124                 {"analysis", required_argument, NULL, 't'},
125                 {"options", required_argument, NULL, 'o'},
126                 {"maxexecutions", required_argument, NULL, 'x'},
127                 {0, 0, 0, 0} /* Terminator */
128         };
129         int opt, longindex;
130         bool error = false;
131         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
132                 switch (opt) {
133                 case 'h':
134                         print_usage(argv[0], params);
135                         break;
136                 case 'x':
137                         params->maxexecutions = atoi(optarg);
138                         break;
139                 case 's':
140                         params->maxfuturedelay = atoi(optarg);
141                         break;
142                 case 'S':
143                         params->expireslop = atoi(optarg);
144                         break;
145                 case 'f':
146                         params->fairwindow = atoi(optarg);
147                         break;
148                 case 'e':
149                         params->enabledcount = atoi(optarg);
150                         break;
151                 case 'b':
152                         params->bound = atoi(optarg);
153                         break;
154                 case 'm':
155                         params->maxreads = atoi(optarg);
156                         break;
157                 case 'M':
158                         params->maxfuturevalues = atoi(optarg);
159                         break;
160                 case 'v':
161                         params->verbose = optarg ? atoi(optarg) : 1;
162                         break;
163                 case 'u':
164                         params->uninitvalue = atoi(optarg);
165                         break;
166                 case 'y':
167                         params->yieldon = true;
168                         break;
169 /**             case 't':
170                         if (install_plugin(optarg))
171                                 error = true;
172                         break;
173                 case 'o':
174                         {
175                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
176                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
177                                         error = true;
178                         }
179                         break;
180 */
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 /** The model_main function contains the main model checking loop. */
208 static void model_main()
209 {
210         struct model_params params;
211
212         param_defaults(&params);
213
214         parse_options(&params, main_argc, main_argv);
215
216         //Initialize race detector
217         initRaceDetector();
218
219         snapshot_stack_init();
220
221         model = new ModelChecker(params);       // L: Model thread is created
222 //      install_trace_analyses(model->get_execution());         L: disable plugin
223
224         snapshot_record(0);
225         model->run();
226         delete model;
227
228         DEBUG("Exiting\n");
229 }
230
231 /**
232  * Main function.  Just initializes snapshotting library and the
233  * snapshotting library calls the model_main function.
234  */
235 int main(int argc, char **argv)
236 {
237         main_argc = argc;
238         main_argv = argv;
239
240         /*
241          * If this printf statement is removed, CDSChecker will fail on an
242          * assert on some versions of glibc.  The first time printf is
243          * called, it allocated internal buffers.  We can't easily snapshot
244          * libc since we also use it.
245          */
246
247         printf("CDSChecker\n"
248                                  "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
249                                  "Distributed under the GPLv2\n"
250                                  "Written by Brian Norris and Brian Demsky\n\n");
251
252         /* Configure output redirection for the model-checker */
253         redirect_output();
254
255         /* Let's jump in quickly and start running stuff */
256         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
257 }