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