add traceanalysis support
[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 #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 " --                         Program arguments follow.\n\n",
83                 program_name,
84                 params->maxreads,
85                 params->maxfuturevalues,
86                 params->maxfuturedelay,
87                 params->expireslop,
88                 params->yieldon ? "enabled" : "disabled",
89                 params->yieldblock ? "enabled" : "disabled",
90                 params->fairwindow,
91                 params->enabledcount,
92                 params->bound,
93                 params->uninitvalue);
94         exit(EXIT_SUCCESS);
95 }
96
97 bool install_plugin(char * name) {
98         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
99         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
100
101         for(unsigned int i=0;i<registeredanalysis->size();i++) {
102                 TraceAnalysis * analysis=(*registeredanalysis)[i];
103                 if (strcmp(name, analysis->name())==0) {
104                         installedanalysis->push_back(analysis);
105                         return false;
106                 }
107         }
108         model_print("Analysis %s Not Found\n", name);
109         return true;
110 }
111
112 static void parse_options(struct model_params *params, int argc, char **argv)
113 {
114         const char *shortopts = "hyYt:m:M:s:S:f:e:b:u:v::";
115         const struct option longopts[] = {
116                 {"help", no_argument, NULL, 'h'},
117                 {"liveness", required_argument, NULL, 'm'},
118                 {"maxfv", required_argument, NULL, 'M'},
119                 {"maxfvdelay", required_argument, NULL, 's'},
120                 {"fvslop", required_argument, NULL, 'S'},
121                 {"fairness", required_argument, NULL, 'f'},
122                 {"yield", no_argument, NULL, 'y'},
123                 {"yieldblock", no_argument, NULL, 'Y'},
124                 {"enabled", required_argument, NULL, 'e'},
125                 {"bound", required_argument, NULL, 'b'},
126                 {"verbose", optional_argument, NULL, 'v'},
127                 {"uninitialized", optional_argument, NULL, 'u'},
128                 {"analysis", optional_argument, NULL, 't'},
129                 {0, 0, 0, 0} /* Terminator */
130         };
131         int opt, longindex;
132         bool error = false;
133         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
134                 switch (opt) {
135                 case 'h':
136                         print_usage(argv[0], params);
137                         break;
138                 case 's':
139                         params->maxfuturedelay = atoi(optarg);
140                         break;
141                 case 'S':
142                         params->expireslop = atoi(optarg);
143                         break;
144                 case 'f':
145                         params->fairwindow = atoi(optarg);
146                         break;
147                 case 'e':
148                         params->enabledcount = atoi(optarg);
149                         break;
150                 case 'b':
151                         params->bound = atoi(optarg);
152                         break;
153                 case 'm':
154                         params->maxreads = atoi(optarg);
155                         break;
156                 case 'M':
157                         params->maxfuturevalues = atoi(optarg);
158                         break;
159                 case 'v':
160                         params->verbose = optarg ? atoi(optarg) : 1;
161                         break;
162                 case 'u':
163                         params->uninitvalue = atoi(optarg);
164                         break;
165                 case 'y':
166                         params->yieldon = true;
167                         break;
168                 case 't':
169                         if (install_plugin(optarg))
170                                 error = true;
171                         break;
172                 case 'Y':
173                         params->yieldblock = true;
174                         break;
175                 default: /* '?' */
176                         error = true;
177                         break;
178                 }
179         }
180
181         /* Pass remaining arguments to user program */
182         params->argc = argc - (optind - 1);
183         params->argv = argv + (optind - 1);
184
185         /* Reset program name */
186         params->argv[0] = argv[0];
187
188         /* Reset (global) optind for potential use by user program */
189         optind = 1;
190
191         if (error)
192                 print_usage(argv[0], params);
193 }
194
195 int main_argc;
196 char **main_argv;
197
198 static void install_trace_analyses(ModelExecution *execution)
199 {
200         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
201         for(unsigned int i=0;i<installedanalysis->size();i++) {
202                 TraceAnalysis * ta=(*installedanalysis)[i];
203                 ta->setExecution(execution);
204                 model->add_trace_analysis(ta);
205         }
206 }
207
208 /** The model_main function contains the main model checking loop. */
209 static void model_main()
210 {
211         struct model_params params;
212
213         param_defaults(&params);
214         register_plugins();
215
216         parse_options(&params, main_argc, main_argv);
217
218         //Initialize race detector
219         initRaceDetector();
220
221         snapshot_stack_init();
222
223         model = new ModelChecker(params);
224         install_trace_analyses(model->get_execution());
225
226         snapshot_record(0);
227         model->run();
228         delete model;
229
230         DEBUG("Exiting\n");
231 }
232
233 /**
234  * Main function.  Just initializes snapshotting library and the
235  * snapshotting library calls the model_main function.
236  */
237 int main(int argc, char **argv)
238 {
239         main_argc = argc;
240         main_argv = argv;
241
242         /* Configure output redirection for the model-checker */
243         redirect_output();
244
245         /* Let's jump in quickly and start running stuff */
246         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
247 }