more implementation of scanalysis...
[model-checker.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6
7 #include "common.h"
8 #include "output.h"
9
10 #include "datarace.h"
11
12 /* global "model" object */
13 #include "model.h"
14 #include "snapshot-interface.h"
15 #include "scanalysis.h"
16
17 static void param_defaults(struct model_params *params)
18 {
19         params->maxreads = 0;
20         params->maxfuturedelay = 6;
21         params->fairwindow = 0;
22         params->yieldon = false;
23         params->sc_trace_analysis = false;
24         params->enabledcount = 1;
25         params->bound = 0;
26         params->maxfuturevalues = 0;
27         params->expireslop = 4;
28         params->verbose = !!DBG_ENABLED();
29         params->uninitvalue = 0;
30 }
31
32 static void print_usage(struct model_params *params)
33 {
34         /* Reset defaults before printing */
35         param_defaults(params);
36
37         model_print(
38 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
39 "Distributed under the GPLv2\n"
40 "Written by Brian Norris and Brian Demsky\n"
41 "\n"
42 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
43 "\n"
44 "Options:\n"
45 "-h                    Display this help message and exit\n"
46 "-m                    Maximum times a thread can read from the same write\n"
47 "                      while other writes exist. Default: %d\n"
48 "-M                    Maximum number of future values that can be sent to\n"
49 "                      the same read. Default: %d\n"
50 "-s                    Maximum actions that the model checker will wait for\n"
51 "                      a write from the future past the expected number of\n"
52 "                      actions. Default: %d\n"
53 "-S                    Future value expiration sloppiness. Default: %u\n"
54 "-f                    Specify a fairness window in which actions that are\n"
55 "                      enabled sufficiently many times should receive\n"
56 "                      priority for execution. Default: %d\n"
57 "-y                    Turn on CHESS yield-based fairness support.\n"
58 "                      Default: %d\n"
59 "-e                    Enabled count. Default: %d\n"
60 "-b                    Upper length bound. Default: %d\n"
61 "-v                    Print verbose execution information.\n"
62 "-u                    Value for uninitialized reads. Default: %u\n"
63 "-c                    Use SC Trace Analysis.\n"
64 "--                    Program arguments follow.\n\n",
65 params->maxreads, params->maxfuturevalues, params->maxfuturedelay, params->expireslop, params->fairwindow, params->yieldon, params->enabledcount, params->bound, params->uninitvalue);
66         exit(EXIT_SUCCESS);
67 }
68
69 static void parse_options(struct model_params *params, int argc, char **argv)
70 {
71         const char *shortopts = "hycm:M:s:S:f:e:b:u:v";
72         int opt;
73         bool error = false;
74         while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
75                 switch (opt) {
76                 case 'h':
77                         print_usage(params);
78                         break;
79                 case 's':
80                         params->maxfuturedelay = atoi(optarg);
81                         break;
82                 case 'S':
83                         params->expireslop = atoi(optarg);
84                         break;
85                 case 'f':
86                         params->fairwindow = atoi(optarg);
87                         break;
88                 case 'e':
89                         params->enabledcount = atoi(optarg);
90                         break;
91                 case 'b':
92                         params->bound = atoi(optarg);
93                         break;
94                 case 'm':
95                         params->maxreads = atoi(optarg);
96                         break;
97                 case 'M':
98                         params->maxfuturevalues = atoi(optarg);
99                         break;
100                 case 'v':
101                         params->verbose = 1;
102                         break;
103                 case 'u':
104                         params->uninitvalue = atoi(optarg);
105                         break;
106                 case 'c':
107                         params->sc_trace_analysis = true;
108                         break;
109                 case 'y':
110                         params->yieldon = true;
111                         break;
112                 default: /* '?' */
113                         error = true;
114                         break;
115                 }
116         }
117
118         /* Pass remaining arguments to user program */
119         params->argc = argc - (optind - 1);
120         params->argv = argv + (optind - 1);
121
122         /* Reset program name */
123         params->argv[0] = argv[0];
124
125         /* Reset (global) optind for potential use by user program */
126         optind = 1;
127
128         if (error)
129                 print_usage(params);
130 }
131
132 int main_argc;
133 char **main_argv;
134
135 void install_trace_analyses() {
136         if (model->params.sc_trace_analysis)
137                 model->add_trace_analysis(new SCAnalysis());
138 }
139
140 /** The model_main function contains the main model checking loop. */
141 static void model_main()
142 {
143         struct model_params params;
144
145         param_defaults(&params);
146
147         parse_options(&params, main_argc, main_argv);
148
149         //Initialize race detector
150         initRaceDetector();
151
152         snapshot_stack_init();
153
154         model = new ModelChecker(params);
155         install_trace_analyses();
156
157         snapshot_record(0);
158         model->run();
159         delete model;
160
161         DEBUG("Exiting\n");
162 }
163
164 /**
165  * Main function.  Just initializes snapshotting library and the
166  * snapshotting library calls the model_main function.
167  */
168 int main(int argc, char **argv)
169 {
170         main_argc = argc;
171         main_argv = argv;
172
173         /* Configure output redirection for the model-checker */
174         redirect_output();
175
176         /* Let's jump in quickly and start running stuff */
177         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
178 }