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