fe84184a57e8c6b49d2e3e35bf6ad5f62a263c82
2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 #include "kernel/yosys.h"
21 #include "libs/sha1/sha1.h"
22 #include "backends/ilang/ilang_backend.h"
24 #if !defined(_WIN32) || defined(__MINGW32__)
25 # include <sys/time.h>
41 std::vector
<FILE*> log_files
;
42 std::vector
<std::ostream
*> log_streams
;
43 std::map
<std::string
, std::set
<std::string
>> log_hdump
;
44 bool log_hdump_all
= false;
45 FILE *log_errfile
= NULL
;
46 SHA1
*log_hasher
= NULL
;
48 bool log_time
= false;
49 bool log_error_stderr
= false;
50 bool log_cmd_error_throw
= false;
51 bool log_quiet_warnings
= false;
52 int log_verbose_level
;
53 string log_last_error
;
55 vector
<int> header_count
;
56 pool
<RTLIL::IdString
> log_id_cache
;
57 vector
<shared_str
> string_buf
;
58 int string_buf_index
= -1;
60 static struct timeval initial_tv
= { 0, 0 };
61 static bool next_print_log
= false;
62 static int log_newline_count
= 0;
64 #if defined(_WIN32) && !defined(__MINGW32__)
65 // this will get time information and return it in timeval, simulating gettimeofday()
66 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
68 LARGE_INTEGER counter
;
71 QueryPerformanceFrequency(&freq
);
72 QueryPerformanceCounter(&counter
);
74 counter
.QuadPart
*= 1000000;
75 counter
.QuadPart
/= freq
.QuadPart
;
77 tv
->tv_sec
= long(counter
.QuadPart
/ 1000000);
78 tv
->tv_usec
= counter
.QuadPart
% 1000000;
84 void logv(const char *format
, va_list ap
)
86 while (format
[0] == '\n' && format
[1] != 0) {
91 std::string str
= vstringf(format
, ap
);
96 size_t nnl_pos
= str
.find_last_not_of('\n');
97 if (nnl_pos
== std::string::npos
)
98 log_newline_count
+= GetSize(str
);
100 log_newline_count
= GetSize(str
) - nnl_pos
- 1;
103 log_hasher
->update(str
);
107 std::string time_str
;
109 if (next_print_log
|| initial_tv
.tv_sec
== 0) {
110 next_print_log
= false;
112 gettimeofday(&tv
, NULL
);
113 if (initial_tv
.tv_sec
== 0)
115 if (tv
.tv_usec
< initial_tv
.tv_usec
) {
117 tv
.tv_usec
+= 1000000;
119 tv
.tv_sec
-= initial_tv
.tv_sec
;
120 tv
.tv_usec
-= initial_tv
.tv_usec
;
121 time_str
+= stringf("[%05d.%06d] ", int(tv
.tv_sec
), int(tv
.tv_usec
));
124 if (format
[0] && format
[strlen(format
)-1] == '\n')
125 next_print_log
= true;
127 for (auto f
: log_files
)
128 fputs(time_str
.c_str(), f
);
130 for (auto f
: log_streams
)
134 for (auto f
: log_files
)
135 fputs(str
.c_str(), f
);
137 for (auto f
: log_streams
)
141 void logv_header(RTLIL::Design
*design
, const char *format
, va_list ap
)
143 bool pop_errfile
= false;
146 if (header_count
.size() > 0)
147 header_count
.back()++;
149 if (int(header_count
.size()) <= log_verbose_level
&& log_errfile
!= NULL
) {
150 log_files
.push_back(log_errfile
);
154 std::string header_id
;
156 for (int c
: header_count
)
157 header_id
+= stringf("%s%d", header_id
.empty() ? "" : ".", c
);
159 log("%s. ", header_id
.c_str());
164 log_hdump
[header_id
].insert("yosys_dump_" + header_id
+ ".il");
166 if (log_hdump
.count(header_id
) && design
!= nullptr)
167 for (auto &filename
: log_hdump
.at(header_id
)) {
168 log("Dumping current design to '%s'.\n", filename
.c_str());
169 Pass::call(design
, {"dump", "-o", filename
});
173 log_files
.pop_back();
176 void logv_warning(const char *format
, va_list ap
)
178 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
179 log_files
.push_back(log_errfile
);
185 if (log_errfile
!= NULL
&& !log_quiet_warnings
)
186 log_files
.pop_back();
189 void logv_error(const char *format
, va_list ap
)
192 auto backup_log_files
= log_files
;
195 if (log_errfile
!= NULL
)
196 log_files
.push_back(log_errfile
);
198 if (log_error_stderr
)
199 for (auto &f
: log_files
)
203 log_last_error
= vstringf(format
, ap
);
204 log("ERROR: %s", log_last_error
.c_str());
208 log_files
= backup_log_files
;
215 void log(const char *format
, ...)
218 va_start(ap
, format
);
223 void log_header(RTLIL::Design
*design
, const char *format
, ...)
226 va_start(ap
, format
);
227 logv_header(design
, format
, ap
);
231 void log_warning(const char *format
, ...)
234 va_start(ap
, format
);
235 logv_warning(format
, ap
);
239 void log_error(const char *format
, ...)
242 va_start(ap
, format
);
243 logv_error(format
, ap
);
246 void log_cmd_error(const char *format
, ...)
249 va_start(ap
, format
);
251 if (log_cmd_error_throw
) {
252 log_last_error
= vstringf(format
, ap
);
253 log("ERROR: %s", log_last_error
.c_str());
255 throw log_cmd_error_exception();
258 logv_error(format
, ap
);
263 while (log_newline_count
< 2)
269 header_count
.push_back(0);
274 header_count
.pop_back();
275 log_id_cache
.clear();
277 string_buf_index
= -1;
281 #if defined(__linux__) && defined(YOSYS_ENABLE_PLUGINS)
282 void log_backtrace(const char *prefix
, int levels
)
284 if (levels
<= 0) return;
289 if ((p
= __builtin_extract_return_addr(__builtin_return_address(0))) && dladdr(p
, &dli
)) {
290 log("%sframe #1: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
292 log("%sframe #1: ---\n", prefix
);
296 if (levels
<= 1) return;
298 if ((p
= __builtin_extract_return_addr(__builtin_return_address(1))) && dladdr(p
, &dli
)) {
299 log("%sframe #2: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
301 log("%sframe #2: ---\n", prefix
);
305 if (levels
<= 2) return;
307 if ((p
= __builtin_extract_return_addr(__builtin_return_address(2))) && dladdr(p
, &dli
)) {
308 log("%sframe #3: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
310 log("%sframe #3: ---\n", prefix
);
314 if (levels
<= 3) return;
316 if ((p
= __builtin_extract_return_addr(__builtin_return_address(3))) && dladdr(p
, &dli
)) {
317 log("%sframe #4: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
319 log("%sframe #4: ---\n", prefix
);
323 if (levels
<= 4) return;
325 if ((p
= __builtin_extract_return_addr(__builtin_return_address(4))) && dladdr(p
, &dli
)) {
326 log("%sframe #5: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
328 log("%sframe #5: ---\n", prefix
);
332 if (levels
<= 5) return;
334 if ((p
= __builtin_extract_return_addr(__builtin_return_address(5))) && dladdr(p
, &dli
)) {
335 log("%sframe #6: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
337 log("%sframe #6: ---\n", prefix
);
341 if (levels
<= 6) return;
343 if ((p
= __builtin_extract_return_addr(__builtin_return_address(6))) && dladdr(p
, &dli
)) {
344 log("%sframe #7: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
346 log("%sframe #7: ---\n", prefix
);
350 if (levels
<= 7) return;
352 if ((p
= __builtin_extract_return_addr(__builtin_return_address(7))) && dladdr(p
, &dli
)) {
353 log("%sframe #8: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
355 log("%sframe #8: ---\n", prefix
);
359 if (levels
<= 8) return;
361 if ((p
= __builtin_extract_return_addr(__builtin_return_address(8))) && dladdr(p
, &dli
)) {
362 log("%sframe #9: %p %s(%p) %s(%p)\n", prefix
, p
, dli
.dli_fname
, dli
.dli_fbase
, dli
.dli_sname
, dli
.dli_saddr
);
364 log("%sframe #9: ---\n", prefix
);
368 if (levels
<= 9) return;
371 void log_backtrace(const char*, int) { }
374 void log_reset_stack()
376 while (header_count
.size() > 1)
377 header_count
.pop_back();
378 log_id_cache
.clear();
380 string_buf_index
= -1;
386 for (auto f
: log_files
)
389 for (auto f
: log_streams
)
393 void log_dump_val_worker(RTLIL::IdString v
) {
394 log("%s", log_id(v
));
397 void log_dump_val_worker(RTLIL::SigSpec v
) {
398 log("%s", log_signal(v
));
401 const char *log_signal(const RTLIL::SigSpec
&sig
, bool autoint
)
403 std::stringstream buf
;
404 ILANG_BACKEND::dump_sigspec(buf
, sig
, autoint
);
406 if (string_buf
.size() < 100) {
407 string_buf
.push_back(buf
.str());
408 return string_buf
.back().c_str();
410 if (++string_buf_index
== 100)
411 string_buf_index
= 0;
412 string_buf
[string_buf_index
] = buf
.str();
413 return string_buf
[string_buf_index
].c_str();
417 const char *log_id(RTLIL::IdString str
)
419 log_id_cache
.insert(str
);
420 const char *p
= str
.c_str();
423 if (p
[1] == '$' || p
[1] == '\\' || p
[1] == 0)
425 if (p
[1] >= '0' && p
[1] <= '9')
430 void log_cell(RTLIL::Cell
*cell
, std::string indent
)
432 std::stringstream buf
;
433 ILANG_BACKEND::dump_cell(buf
, indent
, cell
);
434 log("%s", buf
.str().c_str());
437 // ---------------------------------------------------
438 // This is the magic behind the code coverage counters
439 // ---------------------------------------------------
440 #if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
442 dict
<std::string
, std::pair
<std::string
, int>> extra_coverage_data
;
444 void cover_extra(std::string parent
, std::string id
, bool increment
) {
445 if (extra_coverage_data
.count(id
) == 0) {
446 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++)
448 extra_coverage_data
[id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
449 log_assert(extra_coverage_data
.count(id
));
452 extra_coverage_data
[id
].second
++;
455 dict
<std::string
, std::pair
<std::string
, int>> get_coverage_data()
457 dict
<std::string
, std::pair
<std::string
, int>> coverage_data
;
459 for (auto &it
: pass_register
) {
460 std::string key
= stringf("passes.%s", it
.first
.c_str());
461 coverage_data
[key
].first
= stringf("%s:%d:%s", __FILE__
, __LINE__
, __FUNCTION__
);
462 coverage_data
[key
].second
+= it
.second
->call_counter
;
465 for (auto &it
: extra_coverage_data
) {
466 if (coverage_data
.count(it
.first
))
467 log_warning("found duplicate coverage id \"%s\".\n", it
.first
.c_str());
468 coverage_data
[it
.first
].first
= it
.second
.first
;
469 coverage_data
[it
.first
].second
+= it
.second
.second
;
472 for (CoverData
*p
= __start_yosys_cover_list
; p
!= __stop_yosys_cover_list
; p
++) {
473 if (coverage_data
.count(p
->id
))
474 log_warning("found duplicate coverage id \"%s\".\n", p
->id
);
475 coverage_data
[p
->id
].first
= stringf("%s:%d:%s", p
->file
, p
->line
, p
->func
);
476 coverage_data
[p
->id
].second
+= p
->counter
;
479 for (auto &it
: coverage_data
)
480 if (!it
.second
.first
.compare(0, strlen(YOSYS_SRC
"/"), YOSYS_SRC
"/"))
481 it
.second
.first
= it
.second
.first
.substr(strlen(YOSYS_SRC
"/"));
483 return coverage_data
;