0f1fbf2bc75c4c30c7110d3b00c2fc279b9516b4
[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->enabledcount = 1;
22         params->bound = 0;
23         params->verbose = !!DBG_ENABLED();
24         params->uninitvalue = 0;
25         params->maxexecutions = 0;
26 }
27
28 static void print_usage(const char *program_name, struct model_params *params)
29 {
30         /* Reset defaults before printing */
31         param_defaults(params);
32
33         model_print(
34 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
35 "Distributed under the GPLv2\n"
36 "Written by Brian Norris and Brian Demsky\n"
37 "\n"
38 "Usage: %s [MODEL-CHECKER OPTIONS] -- [PROGRAM ARGS]\n"
39 "\n"
40 "MODEL-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
41 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
42 "\n"
43 "Model-checker options:\n"
44 "-h, --help                  Display this help message and exit\n"
45 "-e, --enabled=COUNT         Enabled count.\n"
46 "                              Default: %d\n"
47 "-b, --bound=MAX             Upper length bound.\n"
48 "                              Default: %d\n"
49 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
50 "                              0 is quiet; 1 shows valid executions; 2 is noisy;\n"
51 "                              3 is noisier.\n"
52 "                              Default: %d\n"
53 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
54 "                              uninitialized atomic.\n"
55 "                              Default: %u\n"
56 "-t, --analysis=NAME         Use Analysis Plugin.\n"
57 "-o, --options=NAME          Option for previous analysis plugin.  \n"
58 "-x, --maxexec=NUM           Maximum number of executions.\n"
59 "                            Default: %u\n"
60 "                            -o help for a list of options\n"
61 " --                         Program arguments follow.\n\n",
62                 program_name,
63                 params->enabledcount,
64                 params->bound,
65                 params->verbose,
66                 params->uninitvalue,
67                 params->maxexecutions);
68
69         exit(EXIT_SUCCESS);
70 }
71
72 static void parse_options(struct model_params *params, int argc, char **argv)
73 {
74         const char *shortopts = "ht:o:e:b:u:x:v::";
75         const struct option longopts[] = {
76                 {"help", no_argument, NULL, 'h'},
77                 {"enabled", required_argument, NULL, 'e'},
78                 {"bound", required_argument, NULL, 'b'},
79                 {"verbose", optional_argument, NULL, 'v'},
80                 {"uninitialized", required_argument, NULL, 'u'},
81                 {"analysis", required_argument, NULL, 't'},
82                 {"options", required_argument, NULL, 'o'},
83                 {"maxexecutions", required_argument, NULL, 'x'},
84                 {0, 0, 0, 0} /* Terminator */
85         };
86         int opt, longindex;
87         bool error = false;
88         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
89                 switch (opt) {
90                 case 'h':
91                         print_usage(argv[0], params);
92                         break;
93                 case 'x':
94                         params->maxexecutions = atoi(optarg);
95                         break;
96                 case 'e':
97                         params->enabledcount = atoi(optarg);
98                         break;
99                 case 'b':
100                         params->bound = atoi(optarg);
101                         break;
102                 case 'v':
103                         params->verbose = optarg ? atoi(optarg) : 1;
104                         break;
105                 case 'u':
106                         params->uninitvalue = atoi(optarg);
107                         break;
108 /**             case 't':
109                         if (install_plugin(optarg))
110                                 error = true;
111                         break;
112                 case 'o':
113                         {
114                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
115                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
116                                         error = true;
117                         }
118                         break;
119 */
120                 default: /* '?' */
121                         error = true;
122                         break;
123                 }
124         }
125
126         /* Pass remaining arguments to user program */
127         params->argc = argc - (optind - 1);
128         params->argv = argv + (optind - 1);
129
130         /* Reset program name */
131         params->argv[0] = argv[0];
132
133         /* Reset (global) optind for potential use by user program */
134         optind = 1;
135
136         if (error)
137                 print_usage(argv[0], params);
138 }
139
140 int main_argc;
141 char **main_argv;
142
143 /** The model_main function contains the main model checking loop. */
144 static void model_main()
145 {
146         struct model_params params;
147
148         param_defaults(&params);
149
150         parse_options(&params, main_argc, main_argv);
151
152         //Initialize race detector
153         initRaceDetector();
154
155         snapshot_stack_init();
156
157         model = new ModelChecker(params);       // L: Model thread is created
158 //      install_trace_analyses(model->get_execution());         L: disable plugin
159
160         snapshot_record(0);
161         model->run();
162         delete model;
163
164         DEBUG("Exiting\n");
165 }
166
167 /**
168  * Main function.  Just initializes snapshotting library and the
169  * snapshotting library calls the model_main function.
170  */
171 int main(int argc, char **argv)
172 {
173         main_argc = argc;
174         main_argv = argv;
175
176         /*
177          * If this printf statement is removed, CDSChecker will fail on an
178          * assert on some versions of glibc.  The first time printf is
179          * called, it allocated internal buffers.  We can't easily snapshot
180          * libc since we also use it.
181          */
182
183         printf("CDSChecker\n"
184                                  "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
185                                  "Distributed under the GPLv2\n"
186                                  "Written by Brian Norris and Brian Demsky\n\n");
187
188         /* Configure output redirection for the model-checker */
189         redirect_output();
190
191         /* Let's jump in quickly and start running stuff */
192         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
193 }