f830655c10dcc5e57ae234ed478c8e32863d703d
[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
27 #ifndef _WIN32
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #endif
31
32 #if defined(_MSC_VER)
33 // At least this is not in MSVC++ 2013.
34 # define __PRETTY_FUNCTION__ __FUNCTION__
35 #endif
36
37 // from libs/sha1/sha1.h
38 class SHA1;
39
40 YOSYS_NAMESPACE_BEGIN
41
42 #define S__LINE__sub2(x) #x
43 #define S__LINE__sub1(x) S__LINE__sub2(x)
44 #define S__LINE__ S__LINE__sub1(__LINE__)
45
46 struct log_cmd_error_exception { };
47
48 extern std::vector<FILE*> log_files;
49 extern std::vector<std::ostream*> log_streams;
50 extern std::map<std::string, std::set<std::string>> log_hdump;
51 extern bool log_hdump_all;
52 extern FILE *log_errfile;
53 extern SHA1 *log_hasher;
54
55 extern bool log_time;
56 extern bool log_error_stderr;
57 extern bool log_cmd_error_throw;
58 extern bool log_quiet_warnings;
59 extern int log_verbose_level;
60 extern string log_last_error;
61
62 void logv(const char *format, va_list ap);
63 void logv_header(RTLIL::Design *design, const char *format, va_list ap);
64 void logv_warning(const char *format, va_list ap);
65 YS_NORETURN void logv_error(const char *format, va_list ap) YS_ATTRIBUTE(noreturn);
66
67 void log(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
68 void log_header(RTLIL::Design *design, const char *format, ...) YS_ATTRIBUTE(format(printf, 2, 3));
69 void log_warning(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2));
70 YS_NORETURN void log_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
71 YS_NORETURN void log_cmd_error(const char *format, ...) YS_ATTRIBUTE(format(printf, 1, 2), noreturn);
72
73 void log_spacer();
74 void log_push();
75 void log_pop();
76
77 void log_backtrace(const char *prefix, int levels);
78 void log_reset_stack();
79 void log_flush();
80
81 const char *log_signal(const RTLIL::SigSpec &sig, bool autoint = true);
82 const char *log_id(RTLIL::IdString id);
83
84 template<typename T> static inline const char *log_id(T *obj) {
85 return log_id(obj->name);
86 }
87
88 void log_module(RTLIL::Module *module, std::string indent = "");
89 void log_cell(RTLIL::Cell *cell, std::string indent = "");
90
91 #ifndef NDEBUG
92 static inline void log_assert_worker(bool cond, const char *expr, const char *file, int line) {
93 if (!cond) log_error("Assert `%s' failed in %s:%d.\n", expr, file, line);
94 }
95 # define log_assert(_assert_expr_) YOSYS_NAMESPACE_PREFIX log_assert_worker(_assert_expr_, #_assert_expr_, __FILE__, __LINE__)
96 #else
97 # define log_assert(_assert_expr_)
98 #endif
99
100 #define log_abort() YOSYS_NAMESPACE_PREFIX log_error("Abort in %s:%d.\n", __FILE__, __LINE__)
101 #define log_ping() YOSYS_NAMESPACE_PREFIX log("-- %s:%d %s --\n", __FILE__, __LINE__, __PRETTY_FUNCTION__)
102
103
104 // ---------------------------------------------------
105 // This is the magic behind the code coverage counters
106 // ---------------------------------------------------
107
108 #if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
109
110 #define cover(_id) do { \
111 static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1), used)) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \
112 __d.counter++; \
113 } while (0)
114
115 struct CoverData {
116 const char *file, *func, *id;
117 int line, counter;
118 } YS_ATTRIBUTE(packed);
119
120 // this two symbols are created by the linker for the "yosys_cover_list" ELF section
121 extern "C" struct CoverData __start_yosys_cover_list[];
122 extern "C" struct CoverData __stop_yosys_cover_list[];
123
124 extern dict<std::string, std::pair<std::string, int>> extra_coverage_data;
125
126 void cover_extra(std::string parent, std::string id, bool increment = true);
127 dict<std::string, std::pair<std::string, int>> get_coverage_data();
128
129 #define cover_list(_id, ...) do { cover(_id); \
130 std::string r = cover_list_worker(_id, __VA_ARGS__); \
131 log_assert(r.empty()); \
132 } while (0)
133
134 static inline std::string cover_list_worker(std::string, std::string last) {
135 return last;
136 }
137
138 template<typename... T>
139 std::string cover_list_worker(std::string prefix, std::string first, T... rest) {
140 std::string selected = cover_list_worker(prefix, rest...);
141 cover_extra(prefix, prefix + "." + first, first == selected);
142 return first == selected ? "" : selected;
143 }
144
145 #else
146 # define cover(...) do { } while (0)
147 # define cover_list(...) do { } while (0)
148 #endif
149
150
151 // ------------------------------------------------------------
152 // everything below this line are utilities for troubleshooting
153 // ------------------------------------------------------------
154
155 // simple timer for performance measurements
156 // toggle the '#if 1' to get a baseline for the performance penalty added by the measurement
157 struct PerformanceTimer
158 {
159 #if 1
160 int64_t total_ns;
161
162 PerformanceTimer() {
163 total_ns = 0;
164 }
165
166 static int64_t query() {
167 # if _WIN32
168 return 0;
169 # elif defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
170 struct timespec ts;
171 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
172 return int64_t(ts.tv_sec)*1000000000 + ts.tv_nsec;
173 # elif defined(RUSAGE_SELF)
174 struct rusage rusage;
175 int64_t t;
176 if (getrusage(RUSAGE_SELF, &rusage) == -1) {
177 log_cmd_error("getrusage failed!\n");
178 log_abort();
179 }
180 t = 1000000000ULL * (int64_t) rusage.ru_utime.tv_sec + (int64_t) rusage.ru_utime.tv_usec * 1000ULL;
181 t += 1000000000ULL * (int64_t) rusage.ru_stime.tv_sec + (int64_t) rusage.ru_stime.tv_usec * 1000ULL;
182 return t;
183 # else
184 # error Dont know how to measure per-process CPU time. Need alternative method (times()/clocks()/gettimeofday()?).
185 # endif
186 }
187
188 void reset() {
189 total_ns = 0;
190 }
191
192 void begin() {
193 total_ns -= query();
194 }
195
196 void end() {
197 total_ns += query();
198 }
199
200 float sec() const {
201 return total_ns * 1e-9f;
202 }
203 #else
204 static int64_t query() { return 0; }
205 void reset() { }
206 void begin() { }
207 void end() { }
208 float sec() const { return 0; }
209 #endif
210 };
211
212 // simple API for quickly dumping values when debugging
213
214 static inline void log_dump_val_worker(short v) { log("%d", v); }
215 static inline void log_dump_val_worker(unsigned short v) { log("%u", v); }
216 static inline void log_dump_val_worker(int v) { log("%d", v); }
217 static inline void log_dump_val_worker(unsigned int v) { log("%u", v); }
218 static inline void log_dump_val_worker(long int v) { log("%ld", v); }
219 static inline void log_dump_val_worker(unsigned long int v) { log("%lu", v); }
220 #ifndef _WIN32
221 static inline void log_dump_val_worker(long long int v) { log("%lld", v); }
222 static inline void log_dump_val_worker(unsigned long long int v) { log("%lld", v); }
223 #endif
224 static inline void log_dump_val_worker(char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
225 static inline void log_dump_val_worker(unsigned char c) { log(c >= 32 && c < 127 ? "'%c'" : "'\\x%02x'", c); }
226 static inline void log_dump_val_worker(bool v) { log("%s", v ? "true" : "false"); }
227 static inline void log_dump_val_worker(double v) { log("%f", v); }
228 static inline void log_dump_val_worker(char *v) { log("%s", v); }
229 static inline void log_dump_val_worker(const char *v) { log("%s", v); }
230 static inline void log_dump_val_worker(std::string v) { log("%s", v.c_str()); }
231 static inline void log_dump_val_worker(PerformanceTimer p) { log("%f seconds", p.sec()); }
232 static inline void log_dump_args_worker(const char *p YS_ATTRIBUTE(unused)) { log_assert(*p == 0); }
233 void log_dump_val_worker(RTLIL::IdString v);
234 void log_dump_val_worker(RTLIL::SigSpec v);
235
236 template<typename K, typename T, typename OPS>
237 static inline void log_dump_val_worker(dict<K, T, OPS> &v) {
238 log("{");
239 bool first = true;
240 for (auto &it : v) {
241 log(first ? " " : ", ");
242 log_dump_val_worker(it.first);
243 log(": ");
244 log_dump_val_worker(it.second);
245 first = false;
246 }
247 log(" }");
248 }
249
250 template<typename K, typename OPS>
251 static inline void log_dump_val_worker(pool<K, OPS> &v) {
252 log("{");
253 bool first = true;
254 for (auto &it : v) {
255 log(first ? " " : ", ");
256 log_dump_val_worker(it);
257 first = false;
258 }
259 log(" }");
260 }
261
262 template<typename T>
263 static inline void log_dump_val_worker(T *ptr) { log("%p", ptr); }
264
265 template<typename T, typename ... Args>
266 void log_dump_args_worker(const char *p, T first, Args ... args)
267 {
268 int next_p_state = 0;
269 const char *next_p = p;
270 while (*next_p && (next_p_state != 0 || *next_p != ',')) {
271 if (*next_p == '"')
272 do {
273 next_p++;
274 while (*next_p == '\\' && *(next_p + 1))
275 next_p += 2;
276 } while (*next_p && *next_p != '"');
277 if (*next_p == '\'') {
278 next_p++;
279 if (*next_p == '\\')
280 next_p++;
281 if (*next_p)
282 next_p++;
283 }
284 if (*next_p == '(' || *next_p == '[' || *next_p == '{')
285 next_p_state++;
286 if ((*next_p == ')' || *next_p == ']' || *next_p == '}') && next_p_state > 0)
287 next_p_state--;
288 next_p++;
289 }
290 log("\n\t%.*s => ", int(next_p - p), p);
291 if (*next_p == ',')
292 next_p++;
293 while (*next_p == ' ' || *next_p == '\t' || *next_p == '\r' || *next_p == '\n')
294 next_p++;
295 log_dump_val_worker(first);
296 log_dump_args_worker(next_p, args ...);
297 }
298
299 #define log_dump(...) do { \
300 log("DEBUG DUMP IN %s AT %s:%d:", __PRETTY_FUNCTION__, __FILE__, __LINE__); \
301 log_dump_args_worker(#__VA_ARGS__, __VA_ARGS__); \
302 log("\n"); \
303 } while (0)
304
305 YOSYS_NAMESPACE_END
306
307 #endif