bug fix
[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 "plugins.h"
19
20 void param_defaults(struct model_params *params)
21 {
22         params->verbose = !!DBG_ENABLED();
23         params->maxexecutions = 10;
24         params->traceminsize = 0;
25         params->checkthreshold = 500000;
26         params->removevisible = false;
27         params->nofork = false;
28 }
29
30 static void print_usage(struct model_params *params)
31 {
32         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
33         /* Reset defaults before printing */
34         param_defaults(params);
35
36         model_print(
37                 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
38                 "Distributed under the GPLv2\n"
39                 "Written by Brian Norris and Brian Demsky\n"
40                 "\n"
41                 "Usage: C11TESTER=[MODEL-CHECKER OPTIONS]\n"
42                 "\n"
43                 "MODEL-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
44                 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
45                 "\n"
46                 "Model-checker options:\n"
47                 "-h, --help                  Display this help message and exit\n"
48                 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
49                 "                              0 is quiet; 1 shows valid executions; 2 is noisy;\n"
50                 "                              3 is noisier.\n"
51                 "                              Default: %d\n"
52                 "-t, --analysis=NAME         Use Analysis Plugin.\n"
53                 "-o, --options=NAME          Option for previous analysis plugin.  \n"
54                 "-x, --maxexec=NUM           Maximum number of executions.\n"
55                 "                            Default: %u\n"
56                 "                            -o help for a list of options\n"
57                 "-n                          No fork\n"
58                 "-m, --minsize=NUM           Minimum number of actions to keep\n"
59                 "                            Default: %u\n"
60                 "-f, --freqfree=NUM          Frequency to free actions\n"
61                 "                            Default: %u\n"
62                 "-r, --removevisible         Free visible writes\n",
63                 params->verbose,
64                 params->maxexecutions,
65                 params->traceminsize,
66                 params->checkthreshold);
67         model_print("Analysis plugins:\n");
68         for(unsigned int i=0;i<registeredanalysis->size();i++) {
69                 TraceAnalysis * analysis=(*registeredanalysis)[i];
70                 model_print("%s\n", analysis->name());
71         }
72         exit(EXIT_SUCCESS);
73 }
74
75 bool install_plugin(char * name) {
76         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
77         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
78
79         for(unsigned int i=0;i<registeredanalysis->size();i++) {
80                 TraceAnalysis * analysis=(*registeredanalysis)[i];
81                 if (strcmp(name, analysis->name())==0) {
82                         installedanalysis->push_back(analysis);
83                         return false;
84                 }
85         }
86         model_print("Analysis %s Not Found\n", name);
87         return true;
88 }
89
90 void parse_options(struct model_params *params) {
91         const char *shortopts = "hrnt:o:x:v:m:f:";
92         const struct option longopts[] = {
93                 {"help", no_argument, NULL, 'h'},
94                 {"removevisible", no_argument, NULL, 'r'},
95                 {"analysis", required_argument, NULL, 't'},
96                 {"options", required_argument, NULL, 'o'},
97                 {"maxexecutions", required_argument, NULL, 'x'},
98                 {"verbose", optional_argument, NULL, 'v'},
99                 {"minsize", required_argument, NULL, 'm'},
100                 {"freqfree", required_argument, NULL, 'f'},
101                 {0, 0, 0, 0}    /* Terminator */
102         };
103         int opt, longindex;
104         bool error = false;
105         char * options = getenv("C11TESTER");
106
107         if (options == NULL)
108                 return;
109         int argc = 1;
110         int index;
111         for(index = 0;options[index]!=0;index++) {
112                 if (options[index] == ' ')
113                         argc++;
114         }
115         argc++; //first parameter is executable name
116         char optcpy[index + 1];
117         memcpy(optcpy, options, index+1);
118         char * argv[argc + 1];
119         argv[0] = NULL;
120         argv[1] = optcpy;
121         int count = 2;
122         for(index = 0;optcpy[index]!=0;index++) {
123                 if (optcpy[index] == ' ') {
124                         argv[count++] = &optcpy[index+1];
125                         optcpy[index] = 0;
126                 }
127         }
128
129         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
130                 switch (opt) {
131                 case 'h':
132                         print_usage(params);
133                         break;
134                 case 'n':
135                         params->nofork = true;
136                         break;
137                 case 'x':
138                         params->maxexecutions = atoi(optarg);
139                         break;
140                 case 'v':
141                         params->verbose = optarg ? atoi(optarg) : 1;
142                         break;
143                 case 't':
144                         if (install_plugin(optarg))
145                                 error = true;
146                         break;
147                 case 'm':
148                         params->traceminsize = atoi(optarg);
149                         break;
150                 case 'f':
151                         params->checkthreshold = atoi(optarg);
152                         break;
153                 case 'r':
154                         params->removevisible = true;
155                         break;
156                 case 'o':
157                 {
158                         ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
159                         if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
160                                 error = true;
161                 }
162                 break;
163                 default:        /* '?' */
164                         error = true;
165                         break;
166                 }
167         }
168
169         /* Special value to reset implementation as described by Linux man page.  */
170         optind = 0;
171
172         if (error)
173                 print_usage(params);
174 }
175
176 void install_trace_analyses(ModelExecution *execution) {
177         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
178         for(unsigned int i=0;i<installedanalysis->size();i++) {
179                 TraceAnalysis * ta=(*installedanalysis)[i];
180                 ta->setExecution(execution);
181                 model->add_trace_analysis(ta);
182                 /** Call the installation event for each installed plugin */
183                 ta->actionAtInstallation();
184         }
185 }