Prevent double error message
[yosys.git] / kernel / log.h
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/yosys.h"
21
22 #ifndef LOG_H
23 #define LOG_H
24
25 #include <time.h>
26 #include <regex>
27
28 #ifndef _WIN32
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 #endif
32
33 #if defined(_MSC_VER)
34 // At least this is not in MSVC++ 2013.
35 # define __PRETTY_FUNCTION__ __FUNCTION__
36 #endif
37
38 // from libs/sha1/sha1.h
39 class SHA1;
40
41 YOSYS_NAMESPACE_BEGIN
42
43 #define S__LINE__sub2(x) #x
44 #define S__LINE__sub1(x) S__LINE__sub2(x)
45 #define S__LINE__ S__LINE__sub1(__LINE__)
46
47 struct log_cmd_error_exception { };
48
49 extern std::vector<FILE*> log_files;
50 extern std::vector<std::ostream*> log_streams;
51 extern std::map<std::string, std::set<std::string>> log_hdump;
52 extern std::vector<std::regex> log_warn_regexes, log_nowarn_regexes, log_werror_regexes;
53 extern std::set<std::string> log_warnings, log_experimentals, log_experimentals_ignored;
54 extern int log_warnings_count;
55 extern bool log_expect_no_warnings;
56 extern bool log_hdump_all;
57 extern FILE *log_errfile;
58 extern SHA1 *log_hasher;
59
60 extern bool log_time;
61 extern bool log_error_stderr;
62 extern bool log_cmd_error_throw;
63 extern bool log_quiet_warnings;
64 extern int log_verbose_level;
65 extern string log_last_error;
66 extern void (*log_error_atexit)();
67
68 extern int log_make_debug;
69 extern int log_force_debug;
70 extern int log_debug_suppressed;
71
72 void logv(const char *format, va_list ap);
73 void logv_header(RTLIL::Design *design, const char *format, va_list ap);
74 void logv_warning(const char *format, va_list ap);
75 void logv_warning_noprefix(const char *format, va_list ap);
76 YS_NORETURN void logv_error(const char *format, va_list ap) YS_ATTRIBUTE(noreturn);
77
78 void log(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
79 void log_header(RTLIL::Design *design, const char *format, ...) YS_ATTRIBUTE(format(printf, 2, 3));
80 void log_warning(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
81 void log_experimental(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
82
83 // Log with filename to report a problem in a source file.
84 void log_file_warning(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
85 void log_file_info(const std::string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4));
86
87 void log_warning_noprefix(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
88 YS_NORETURN void log_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
89 void log_file_error(const string &filename, int lineno, const char *format, ...) YS_ATTRIBUTE(format(printf, 3, 4), noreturn);
90 YS_NORETURN void log_cmd_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
91
92 #ifndef NDEBUG
93 static inline bool ys_debug(int n = 0) { if (log_force_debug) return true; log_debug_suppressed += n; return false; }
94 # define log_debug(...) do { if (ys_debug(1)) log(__VA_ARGS__); } while (0)
95 #else
96 static inline bool ys_debug(int = 0) { return false; }
97 # define log_debug(_fmt, ...) do { } while (0)
98 #endif
99
100 static inline void log_suppressed() {
101 if (log_debug_suppressed && !log_make_debug) {
102 log("<suppressed ~%d debug messages>\n", log_debug_suppressed);
103 log_debug_suppressed = 0;
104 }
105 }
106
107 struct LogMakeDebugHdl {
108 bool status = false;
109 LogMakeDebugHdl(bool start_on = false) {
110 if (start_on)
111 on();
112 }
113 ~LogMakeDebugHdl() {
114 off();
115 }
116 void on() {
117 if (status) return;
118 status=true;
119 log_make_debug++;
120 }
121 void off_silent() {
122 if (!status) return;
123 status=false;
124 log_make_debug--;
125 }
126 void off() {
127 off_silent();
128 }
129 };
130
131 void log_spacer();
132 void log_push();
133 void log_pop();
134
135 void log_backtrace(const char *prefix, int levels);
136 void log_reset_stack();
137 void log_flush();
138
139 struct LogExpectedItem
140 {
141 LogExpectedItem(std::string pattern, int expected) :
142 expected_count(expected),
143 current_count(0),
144 pattern(pattern)
145 {
146 }
147
148 int expected_count;
149 int current_count;
150 std::string pattern;
151 };
152
153 extern std::vector<std::pair<std::regex,LogExpectedItem>> log_expect_log, log_expect_warning, log_expect_error;
154 void log_check_expected();
155
156 const char *log_signal(const RTLIL::SigSpec &sig, bool autoint = true);
157 const char *log_const(const RTLIL::Const &value, bool autoint = true);
158 const char *log_id(RTLIL::IdString id);
159
160 template<typename T> static inline const char *log_id(T *obj, const char *nullstr = nullptr) {
161 if (nullstr && obj == nullptr)
162 return nullstr;
163 return log_id(obj->name);
164 }
165
166 void log_module(RTLIL::Module *module, std::string indent = "");
167 void log_cell(RTLIL::Cell *cell, std::string indent = "");
168 void log_wire(RTLIL::Wire *wire, std::string indent = "");
169
170 #ifndef NDEBUG
171 static inline void log_assert_worker(bool cond, const char *expr, const char *file, int line) {
172 if (!cond) log_error("Assert `%s' failed in %s:%d.\n", expr, file, line);
173 }
174 # define log_assert(_assert_expr_) YOSYS_NAMESPACE_PREFIX log_assert_worker(_assert_expr_, #_assert_expr_, __FILE__, __LINE__)
175 #else
176 # define log_assert(_assert_expr_)
177 #endif
178
179 #define log_abort() YOSYS_NAMESPACE_PREFIX log_error("Abort in %s:%d.\n", __FILE__, __LINE__)
180 #define log_ping() YOSYS_NAMESPACE_PREFIX log("-- %s:%d %s --\n", __FILE__, __LINE__, __PRETTY_FUNCTION__)
181
182
183 // ---------------------------------------------------
184 // This is the magic behind the code coverage counters
185 // ---------------------------------------------------
186
187 #if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
188
189 #define cover(_id) do { \
190 static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1), used)) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
191 __d.counter++; \
192 } while (0)
193
194 struct CoverData {
195 const char *file, *func, *id;
196 int line, counter;
197 } YS_ATTRIBUTE(packed);
198
199 // this two symbols are created by the linker for the "yosys_cover_list" ELF section
200 extern "C" struct CoverData __start_yosys_cover_list[];
201 extern "C" struct CoverData __stop_yosys_cover_list[];
202
203 extern dict<std::string, std::pair<std::string, int>> extra_coverage_data;
204
205 void cover_extra(std::string parent, std::string id, bool increment = true);
206 dict<std::string, std::pair<std::string, int>> get_coverage_data();
207
208 #define cover_list(_id, ...) do { cover(_id); \
209 std::string r = cover_list_worker(_id, __VA_ARGS__); \
210 log_assert(r.empty()); \
211 } while (0)
212
213 static inline std::string cover_list_worker(std::string, std::string last) {
214 return last;
215 }
216
217 template<typename... T>
218 std::string cover_list_worker(std::string prefix, std::string first, T... rest) {
219 std::string selected = cover_list_worker(prefix, rest...);
220 cover_extra(prefix, prefix + "." + first, first == selected);
221 return first == selected ? "" : selected;
222 }
223
224 #else
225 # define cover(...) do { } while (0)
226 # define cover_list(...) do { } while (0)
227 #endif
228
229
230 // ------------------------------------------------------------
231 // everything below this line are utilities for troubleshooting
232 // ------------------------------------------------------------
233
234 // simple timer for performance measurements
235 // toggle the '#if 1' to get a baseline for the performance penalty added by the measurement
236 struct PerformanceTimer
237 {
238 #if 1
239 int64_t total_ns;
240
241 PerformanceTimer() {
242 total_ns = 0;
243 }
244
245 static int64_t query() {
246 # ifdef _WIN32
247 return 0;
248 # elif defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
249 struct timespec ts;
250 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
251 return int64_t(ts.tv_sec)*1000000000 + ts.tv_nsec;
252 # elif defined(RUSAGE_SELF)
253 struct rusage rusage;
254 int64_t t;
255 if (getrusage(RUSAGE_SELF, &rusage) == -1) {
256 log_cmd_error("getrusage failed!\n");
257 log_abort();
258 }
259 t = 1000000000ULL * (int64_t) rusage.ru_utime.tv_sec + (int64_t) rusage.ru_utime.tv_usec * 1000ULL;
260 t += 1000000000ULL * (int64_t) rusage.ru_stime.tv_sec + (int64_t) rusage.ru_stime.tv_usec * 1000ULL;
261 return t;
262 # else
263 # error "Don't know how to measure per-process CPU time. Need alternative method (times()/clocks()/gettimeofday()?)."
264 # endif
265 }
266
267 void reset() {
268 total_ns = 0;
269 }
270
271 void begin() {
272 total_ns -= query();
273 }
274
275 void end() {
276 total_ns += query();
277 }
278
279 float sec() const {
280 return total_ns * 1e-9f;
281 }
282 #else
283 static int64_t query() { return 0; }
284 void reset() { }
285 void begin() { }
286 void end() { }
287 float sec() const { return 0; }
288 #endif
289 };
290
291 // simple API for quickly dumping values when debugging
292
293 static inline void log_dump_val_worker(short v) { log("%d", v); }
294 static inline void log_dump_val_worker(unsigned short v) { log("%u", v); }
295 static inline void log_dump_val_worker(int v) { log("%d", v); }
296 static inline void log_dump_val_worker(unsigned int v) { log("%u", v); }
297 static inline void log_dump_val_worker(long int v) { log("%ld", v); }
298 static inline void log_dump_val_worker(unsigned long int v) { log("%lu", v); }
299 #ifndef _WIN32
300 static inline void log_dump_val_worker(long long int v) { log("%lld", v); }
301 static inline void log_dump_val_worker(unsigned long long int v) { log("%lld", v); }
302 #endif
303 static inline void log_dump_val_worker(char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
304 static inline void log_dump_val_worker(unsigned char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
305 static inline void log_dump_val_worker(bool v) { log("%s", v ? "true" : "false"); }
306 static inline void log_dump_val_worker(double v) { log("%f", v); }
307 static inline void log_dump_val_worker(char *v) { log("%s", v); }
308 static inline void log_dump_val_worker(const char *v) { log("%s", v); }
309 static inline void log_dump_val_worker(std::string v) { log("%s", v.c_str()); }
310 static inline void log_dump_val_worker(PerformanceTimer p) { log("%f seconds", p.sec()); }
311 static inline void log_dump_args_worker(const char *p YS_ATTRIBUTE(unused)) { log_assert(*p == 0); }
312 void log_dump_val_worker(RTLIL::IdString v);
313 void log_dump_val_worker(RTLIL::SigSpec v);
314 void log_dump_val_worker(RTLIL::State v);
315
316 template<typename K, typename T, typename OPS>
317 static inline void log_dump_val_worker(dict<K, T, OPS> &v) {
318 log("{");
319 bool first = true;
320 for (auto &it : v) {
321 log(first ? " " : ", ");
322 log_dump_val_worker(it.first);
323 log(": ");
324 log_dump_val_worker(it.second);
325 first = false;
326 }
327 log(" }");
328 }
329
330 template<typename K, typename OPS>
331 static inline void log_dump_val_worker(pool<K, OPS> &v) {
332 log("{");
333 bool first = true;
334 for (auto &it : v) {
335 log(first ? " " : ", ");
336 log_dump_val_worker(it);
337 first = false;
338 }
339 log(" }");
340 }
341
342 template<typename T>
343 static inline void log_dump_val_worker(T *ptr) { log("%p", ptr); }
344
345 template<typename T, typename ... Args>
346 void log_dump_args_worker(const char *p, T first, Args ... args)
347 {
348 int next_p_state = 0;
349 const char *next_p = p;
350 while (*next_p && (next_p_state != 0 || *next_p != ',')) {
351 if (*next_p == '"')
352 do {
353 next_p++;
354 while (*next_p == '\\' && *(next_p + 1))
355 next_p += 2;
356 } while (*next_p && *next_p != '"');
357 if (*next_p == '\'') {
358 next_p++;
359 if (*next_p == '\\')
360 next_p++;
361 if (*next_p)
362 next_p++;
363 }
364 if (*next_p == '(' || *next_p == '[' || *next_p == '{')
365 next_p_state++;
366 if ((*next_p == ')' || *next_p == ']' || *next_p == '}') && next_p_state > 0)
367 next_p_state--;
368 next_p++;
369 }
370 log("\n\t%.*s => ", int(next_p - p), p);
371 if (*next_p == ',')
372 next_p++;
373 while (*next_p == ' ' || *next_p == '\t' || *next_p == '\r' || *next_p == '\n')
374 next_p++;
375 log_dump_val_worker(first);
376 log_dump_args_worker(next_p, args ...);
377 }
378
379 #define log_dump(...) do { \
380 log("DEBUG DUMP IN %s AT %s:%d:", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
381 log_dump_args_worker(#__VA_ARGS__, __VA_ARGS__); \
382 log("\n"); \
383 } while (0)
384
385 YOSYS_NAMESPACE_END
386
387 #endif