Spectra
monitorCreator.h
1 
8 #ifndef DP_MONITORCREATOR_H
9 #define DP_MONITORCREATOR_H
10 
11 #include <string>
12 #include <fstream>
13 #include <utility>
14 #include <stack>
15 #include <regex>
16 #include <cstdio>
17 #include "formulaAnalyser.h"
18 
19 #define GENERATED_FILES_NAME "monitor"
20 
27 long fileSize(const char *filename);
28 
34 private:
35  std::string formulaFile; /*< A name of specification file containing the formulae to be monitored */
36 
37 public:
41  explicit monitorCreator(std::string);
42 
57  bool printMonitor(const std::string &defsFile);
58 
67  void generateHeaderFile(const std::string &defsFile, const std::string &comment) const;
68 
76  void generateMonitorImplementation(formulaAnalyser &analyser, const std::string &comment);
77 
85  void printVarNamesEnum(std::set<std::string> varNames, FILE *monitor);
86 
98  void printMonitorStructure(FILE *monitor, std::vector<formula> formulae,
99  std::vector<std::string> contents, std::vector<std::string> varsDefinitions,
100  int childrenCnt);
101 
110  void printPreparation(FILE *monitor, std::vector<formula> formulae);
111 
120  void printProcessing(FILE *monitor, std::vector<formula> formulae);
121 
122 
130  std::string getCommentForGeneratedFiles(const std::string &defsFile);
131 
138  void replaceLabelsInFileWithMonitorCalls(const std::string &filename);
139 
146  void replaceInstrumentationComments(const std::vector<std::string> &srcList);
147 
148  void setFormulaFile(const std::string &formulaFile);
149 
150 };
151 
152 /*********** Templates for generating code of the monitor **********/
153 
154 #define HEADER_FILE "%s\n" /* comment - when and how was this generated */\
155  "#ifndef MONITOR_H\n"\
156  "#define MONITOR_H\n\n"\
157  "#include <stdio.h>\n"\
158  "#include <string.h>\n"\
159  "#include <stdbool.h>\n"\
160  "#include \"%s\"\n\n" /* name of given header file with definitions */ \
161  "#ifdef __cplusplus\n"\
162  "extern \"C\" {\n#endif\n\n"\
163  " bool monitorVerify();\n\n"\
164  "#ifdef __cplusplus\n}\n#endif\n\n"\
165  "#endif"
166 
167 #define MONITOR_C_HEADER "%s\n" /* comment - when and how was this generated */\
168 "#include \"" GENERATED_FILES_NAME ".h\"\n\n"\
169 "#ifdef __cplusplus\n"\
170 "extern \"C\" {\n"\
171 "#endif\n\n"
172 
173 #define ENUM_VAR_NAMES "\ntypedef bool (*fce)();\n"\
174 "\n"\
175 "enum VarNames { %s };\n\n"
176 
177 #define MONITOR_STRUCT_DECLARATION "typedef struct {\n"\
178 " fce values[%d];\n" /* count of state variables */ \
179 " const char* formulaeContents[%d];\n" /* count of formulae */\
180 " bool pre[%d], now[%d], check[%d];\n" /* 3x count of formulae */\
181 " int since[%d];\n" /* count of formulae */\
182 " int children[%d][%d];\n"/* count of formulae, max. count of children */\
183 " int chCnt[%d];\n" /* count of formulae */\
184 " int hb;\n"\
185 "} monitor;\n\n"
186 
187 #define MONITOR_STRUCT_DEFINITION "monitor M = {\n"\
188 " {%s\n" /* values of state variables */\
189 " },\n" \
190 " {%s\n" /* strings representing contents of formulae */\
191 " },\n" \
192 " {false},\n" /* pre */\
193 " {false},\n" /* now */\
194 " {false},\n" /* check */\
195 " {0},\n" /* since */\
196 " {%s\n"/* lists of children */\
197 " },\n" \
198 " { %s },\n"/* counts of children */\
199 " 0\n" /* initial heartbeat number */\
200 "};\n\n" \
201 "bool initiated = false;\n\n"
202 
203 #define MONITOR_VERIFY_INIT "bool monitorVerify() {\n" \
204 " if (!initiated) {prepare();}\n" \
205 " else {\n" \
206 " for (int i = 0; i < %d; i++) {\n" /* count of formulae */\
207 " if (M.pre[i] != M.now[i])\n"\
208 " M.since[i] = M.hb;\n\n"\
209 " M.pre[i] = M.now[i]; \n" \
210 " }\n" \
211 " M.hb++;\n" \
212 " }\n\n" \
213 "\n"
214 
215 #define MONITOR_VERIFY_CHECK_AND_FINALIZE "\n" \
216 " for (int i = 0; i < %d; i++) {\n" /* count of formulae */\
217 " if ((M.check[i] || i == 0) && M.now[i] != M.pre[i]) {\n" \
218 " M.check[i] = true;\n" \
219 " for (int j = 0; j < M.chCnt[i]; j++) {\n" \
220 " M.check[M.children[i][j]] = true;\n" \
221 " }\n" \
222 " } else M.check[i] = false;\n" \
223 " }\n" \
224 "\n" \
225 " if (!M.now[0]) printFailReport();\n" \
226 " return M.now[0];\n" \
227 "}\n"
228 
229 #define FAIL_REPORT_METHOD "\n"\
230 "void printFailReport() {\n"\
231 " fprintf(stderr,\"Verification failed after round #%%d!\\nRelevant changes:\\n\", M.hb);\n"\
232 "\n"\
233 " int i;\n"\
234 " /*< Print all changes in formulae values that happened in the failure heartbeat */\n"\
235 " for (i = %d; i > 0 ; i--) {\n" /* count of formulae - 1 -> the root is not printed*/\
236 " if (M.check[i]) {\n"\
237 " fprintf(stderr, M.formulaeContents[i], (M.now[i]?\"true\":\"false\"), (M.pre[i]?\"true\":\"false\"),"\
238 " M.hb - M.since[i]); \n"\
239 " }\n"\
240 " }\n"\
241 "}\n\n"
242 
243 #define RESET_METHOD "\n"\
244 "void reset() {\n"\
245 " initiated = false;"\
246 "}\n\n"
247 
248 #endif //DP_MONITORCREATOR_H
void printProcessing(FILE *monitor, std::vector< formula > formulae)
Definition: monitorCreator.cpp:247
monitorCreator(std::string)
Definition: monitorCreator.cpp:24
bool printMonitor(const std::string &defsFile)
Definition: monitorCreator.cpp:43
Definition: monitorCreator.h:33
Definition: formulaAnalyser.h:29
void replaceInstrumentationComments(const std::vector< std::string > &srcList)
Definition: monitorCreator.cpp:365
std::string getCommentForGeneratedFiles(const std::string &defsFile)
Definition: monitorCreator.cpp:272
void printPreparation(FILE *monitor, std::vector< formula > formulae)
Definition: monitorCreator.cpp:223
void generateMonitorImplementation(formulaAnalyser &analyser, const std::string &comment)
Definition: monitorCreator.cpp:88
void printMonitorStructure(FILE *monitor, std::vector< formula > formulae, std::vector< std::string > contents, std::vector< std::string > varsDefinitions, int childrenCnt)
Definition: monitorCreator.cpp:155
void printVarNamesEnum(std::set< std::string > varNames, FILE *monitor)
Definition: monitorCreator.cpp:134
void replaceLabelsInFileWithMonitorCalls(const std::string &filename)
Definition: monitorCreator.cpp:287
void generateHeaderFile(const std::string &defsFile, const std::string &comment) const
Definition: monitorCreator.cpp:71