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