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