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