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