Get rid of uninitialized actions and just use non-atomic writes instead
[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->nofork = false;
25 }
26
27 static void print_usage(struct model_params *params)
28 {
29         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
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: C11TESTER=[MODEL-CHECKER OPTIONS]\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                 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
46                 "                              0 is quiet; 1 shows valid executions; 2 is noisy;\n"
47                 "                              3 is noisier.\n"
48                 "                              Default: %d\n"
49                 "-t, --analysis=NAME         Use Analysis Plugin.\n"
50                 "-o, --options=NAME          Option for previous analysis plugin.  \n"
51                 "-x, --maxexec=NUM           Maximum number of executions.\n"
52                 "                            Default: %u\n"
53                 "                            -o help for a list of options\n"
54                 "-n                          No fork\n\n",
55                 params->verbose,
56                 params->maxexecutions);
57         model_print("Analysis plugins:\n");
58         for(unsigned int i=0;i<registeredanalysis->size();i++) {
59                 TraceAnalysis * analysis=(*registeredanalysis)[i];
60                 model_print("%s\n", analysis->name());
61         }
62         exit(EXIT_SUCCESS);
63 }
64
65 bool install_plugin(char * name) {
66         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
67         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
68
69         for(unsigned int i=0;i<registeredanalysis->size();i++) {
70                 TraceAnalysis * analysis=(*registeredanalysis)[i];
71                 if (strcmp(name, analysis->name())==0) {
72                         installedanalysis->push_back(analysis);
73                         return false;
74                 }
75         }
76         model_print("Analysis %s Not Found\n", name);
77         return true;
78 }
79
80 void parse_options(struct model_params *params) {
81         const char *shortopts = "hnt:o:x:v::";
82         const struct option longopts[] = {
83                 {"help", no_argument, NULL, 'h'},
84                 {"verbose", optional_argument, NULL, 'v'},
85                 {"analysis", required_argument, NULL, 't'},
86                 {"options", required_argument, NULL, 'o'},
87                 {"maxexecutions", required_argument, NULL, 'x'},
88                 {0, 0, 0, 0}    /* Terminator */
89         };
90         int opt, longindex;
91         int tmpoptind = optind;
92         bool error = false;
93         char * options = getenv("C11TESTER");
94
95         if (options == NULL)
96                 return;
97         int argc = 1;
98         int index;
99         for(index = 0;options[index]!=0;index++) {
100                 if (options[index] == ' ')
101                         argc++;
102         }
103         argc++; //first parameter is executable name
104         char optcpy[index + 1];
105         memcpy(optcpy, options, index+1);
106         char * argv[argc + 1];
107         argv[0] = NULL;
108         argv[1] = optcpy;
109         int count = 2;
110         for(index = 0;optcpy[index]!=0;index++) {
111                 if (optcpy[index] == ' ') {
112                         argv[count++] = &optcpy[index+1];
113                         optcpy[index] = 0;
114                 }
115         }
116
117         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
118                 switch (opt) {
119                 case 'h':
120                         print_usage(params);
121                         break;
122                 case 'n':
123                         params->nofork = true;
124                         break;
125                 case 'x':
126                         params->maxexecutions = atoi(optarg);
127                         break;
128                 case 'v':
129                         params->verbose = optarg ? atoi(optarg) : 1;
130                         break;
131                 case 't':
132                         if (install_plugin(optarg))
133                                 error = true;
134                         break;
135                 case 'o':
136                 {
137                         ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
138                         if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
139                                 error = true;
140                 }
141                 break;
142                 default:        /* '?' */
143                         error = true;
144                         break;
145                 }
146         }
147
148         /* Restore (global) optind for potential use by user program */
149         optind = tmpoptind;
150
151         if (error)
152                 print_usage(params);
153 }
154
155 void install_trace_analyses(ModelExecution *execution) {
156         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
157         for(unsigned int i=0;i<installedanalysis->size();i++) {
158                 TraceAnalysis * ta=(*installedanalysis)[i];
159                 ta->setExecution(execution);
160                 model->add_trace_analysis(ta);
161                 /** Call the installation event for each installed plugin */
162                 ta->actionAtInstallation();
163         }
164 }