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