main: fixup parameter parsing for user program
[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 <threads.h>
8 #include "common.h"
9 #include "threads-model.h"
10
11 #include "datarace.h"
12
13 /* global "model" object */
14 #include "model.h"
15 #include "snapshot-interface.h"
16
17 static void param_defaults(struct model_params * params) {
18         params->maxreads = 0;
19         params->maxfuturedelay = 100;
20         params->fairwindow = 0;
21         params->enabledcount = 1;
22         params->bound = 0;
23 }
24
25 static void print_usage(struct model_params *params) {
26         /* Reset defaults before printing */
27         param_defaults(params);
28
29         printf(
30 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
31 "\n"
32 "Options:\n"
33 "-h                    Display this help message and exit\n"
34 "-m                    Maximum times a thread can read from the same write\n"
35 "                      while other writes exist. Default: %d\n"
36 "-s                    Maximum actions that the model checker will wait for\n"
37 "                      a write from the future past the expected number of\n"
38 "                      actions. Default: %d\n"
39 "-f                    Specify a fairness window in which actions that are\n"
40 "                      enabled sufficiently many times should receive\n"
41 "                      priority for execution. Default: %d\n"
42 "-e                    Enabled count. Default: %d\n"
43 "-b                    Upper length bound. Default: %d\n"
44 "--                    Program arguments follow.\n\n",
45 params->maxreads, params->maxfuturedelay, params->fairwindow, params->enabledcount, params->bound);
46         exit(EXIT_SUCCESS);
47 }
48
49 static void parse_options(struct model_params *params, int *argc, char ***argv) {
50         const char *shortopts = "hm:s:f:e:b:";
51         int opt;
52         bool error = false;
53         while (!error && (opt = getopt(*argc, *argv, shortopts)) != -1) {
54                 switch (opt) {
55                 case 'h':
56                         print_usage(params);
57                         break;
58                 case 's':
59                         params->maxfuturedelay = atoi(optarg);
60                         break;
61                 case 'f':
62                         params->fairwindow = atoi(optarg);
63                         break;
64                 case 'e':
65                         params->enabledcount = atoi(optarg);
66                         break;
67                 case 'b':
68                         params->bound = atoi(optarg);
69                         break;
70                 case 'm':
71                         params->maxreads = atoi(optarg);
72                         break;
73                 default: /* '?' */
74                         error = true;
75                         break;
76                 }
77         }
78         (*argv)[optind - 1] = (*argv)[0];
79         (*argc) -= (optind - 1);
80         (*argv) += (optind - 1);
81         optind = 1;
82
83         if (error)
84                 print_usage(params);
85 }
86
87 int main_argc;
88 char **main_argv;
89
90 /** Wrapper to run the user's main function, with appropriate arguments */
91 void wrapper_user_main(void *)
92 {
93         user_main(main_argc, main_argv);
94 }
95
96 /** The model_main function contains the main model checking loop. */
97 static void model_main() {
98         thrd_t user_thread;
99         struct model_params params;
100
101         param_defaults(&params);
102
103         parse_options(&params, &main_argc, &main_argv);
104
105         //Initialize race detector
106         initRaceDetector();
107
108         //Create the singleton SnapshotStack object
109         snapshotObject = new SnapshotStack();
110
111         model = new ModelChecker(params);
112
113         snapshotObject->snapshotStep(0);
114         do {
115                 /* Start user program */
116                 model->add_thread(new Thread(&user_thread, &wrapper_user_main, NULL));
117
118                 /* Wait for all threads to complete */
119                 model->finish_execution();
120         } while (model->next_execution());
121
122         delete model;
123
124         DEBUG("Exiting\n");
125 }
126
127 /**
128  * Main function.  Just initializes snapshotting library and the
129  * snapshotting library calls the model_main function.
130  */
131 int main(int argc, char ** argv) {
132         main_argc = argc;
133         main_argv = argv;
134
135         /* Let's jump in quickly and start running stuff */
136         initSnapshotLibrary(10000, 1024, 1024, 4000, &model_main);
137 }