From dd5a56128a6f2a2e468d36af70280d05955572c0 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Tue, 9 May 2023 05:30:16 +0000 Subject: [PATCH 1/5] [YOSYS][Issue 3594] Print backtrace on abort/assert #3594 ERROR: No such command: prox (type help for a command overview) Error while executing script: Running script on file demo.ys --------------------------------- 1 read_verilog ./tests/lut/map_not.v 2 opt 3 proc_clean -->4 prox --------------------------------- --- kernel/log.cc | 15 +++++++++++++++ kernel/log.h | 1 + kernel/yosys.cc | 13 ++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/kernel/log.cc b/kernel/log.cc index 75a1ffb45..bf1795517 100644 --- a/kernel/log.cc +++ b/kernel/log.cc @@ -354,6 +354,21 @@ static void logv_error_with_prefix(const char *prefix, log_check_expected(); + int lineno = 1; + if ( !log_line_number.empty() ) { + log("Error while executing script:\n\t"); + log( log_line_number.begin()->c_str() ); + log("\n"); + log_line_number.erase(log_line_number.begin()); + log("---------------------------------\n"); + for(const std::string& script_line : log_line_number) { + const char* pfx = (lineno == log_line_number.size()) ? "-->" : " "; + log("%s%d %s\n",pfx,lineno++,script_line.c_str()); + } + log("---------------------------------\n"); + } + + if (log_error_atexit) log_error_atexit(); diff --git a/kernel/log.h b/kernel/log.h index 3a6ec8758..dfcbc1137 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -95,6 +95,7 @@ YOSYS_NAMESPACE_BEGIN struct log_cmd_error_exception { }; extern std::vector log_files; +extern std::vector log_line_number; extern std::vector log_streams; extern std::vector log_scratchpads; extern std::map> log_hdump; diff --git a/kernel/yosys.cc b/kernel/yosys.cc index bd8dded4b..46d341645 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -359,7 +359,7 @@ int run_command(const std::string &command, std::function log_line_number; bool run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *from_to_label) { + log_line_number.clear(); if (design == nullptr) design = yosys_design; @@ -1150,7 +1152,6 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi { std::string run_from, run_to; bool from_to_active = true; - if (from_to_label != NULL) { size_t pos = from_to_label->find(':'); if (pos == std::string::npos) { @@ -1175,13 +1176,19 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi if (f == NULL) log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno)); + { + std::stringstream ss; + ss << "Running " << command << " on file '" << filename << "'"; + log_line_number.push_back(ss.str()); + } FILE *backup_script_file = Frontend::current_script_file; Frontend::current_script_file = f; try { std::string command; while (fgetline(f, command)) { - while (!command.empty() && command[command.size()-1] == '\\') { + log_line_number.push_back(command); + while (!command.empty() && command[command.size()-1] == '\\') { std::string next_line; if (!fgetline(f, next_line)) break; From 8b2994a05ae2fb33c19877633dea08e7d26e51ad Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Tue, 9 May 2023 05:30:16 +0000 Subject: [PATCH 2/5] [YOSYS][Issue 3594] Print backtrace on abort/assert #3594 ERROR: No such command: prox (type help for a command overview) Error while executing script: Running script on file demo.ys --------------------------------- 1 read_verilog ./tests/lut/map_not.v 2 opt 3 proc_clean -->4 prox --------------------------------- --- kernel/log.cc | 15 +++++++++++++++ kernel/log.h | 1 + kernel/yosys.cc | 13 ++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/kernel/log.cc b/kernel/log.cc index 75a1ffb45..bf1795517 100644 --- a/kernel/log.cc +++ b/kernel/log.cc @@ -354,6 +354,21 @@ static void logv_error_with_prefix(const char *prefix, log_check_expected(); + int lineno = 1; + if ( !log_line_number.empty() ) { + log("Error while executing script:\n\t"); + log( log_line_number.begin()->c_str() ); + log("\n"); + log_line_number.erase(log_line_number.begin()); + log("---------------------------------\n"); + for(const std::string& script_line : log_line_number) { + const char* pfx = (lineno == log_line_number.size()) ? "-->" : " "; + log("%s%d %s\n",pfx,lineno++,script_line.c_str()); + } + log("---------------------------------\n"); + } + + if (log_error_atexit) log_error_atexit(); diff --git a/kernel/log.h b/kernel/log.h index 3a6ec8758..dfcbc1137 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -95,6 +95,7 @@ YOSYS_NAMESPACE_BEGIN struct log_cmd_error_exception { }; extern std::vector log_files; +extern std::vector log_line_number; extern std::vector log_streams; extern std::vector log_scratchpads; extern std::map> log_hdump; diff --git a/kernel/yosys.cc b/kernel/yosys.cc index bd8dded4b..46d341645 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -359,7 +359,7 @@ int run_command(const std::string &command, std::function log_line_number; bool run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *from_to_label) { + log_line_number.clear(); if (design == nullptr) design = yosys_design; @@ -1150,7 +1152,6 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi { std::string run_from, run_to; bool from_to_active = true; - if (from_to_label != NULL) { size_t pos = from_to_label->find(':'); if (pos == std::string::npos) { @@ -1175,13 +1176,19 @@ bool run_frontend(std::string filename, std::string command, RTLIL::Design *desi if (f == NULL) log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno)); + { + std::stringstream ss; + ss << "Running " << command << " on file '" << filename << "'"; + log_line_number.push_back(ss.str()); + } FILE *backup_script_file = Frontend::current_script_file; Frontend::current_script_file = f; try { std::string command; while (fgetline(f, command)) { - while (!command.empty() && command[command.size()-1] == '\\') { + log_line_number.push_back(command); + while (!command.empty() && command[command.size()-1] == '\\') { std::string next_line; if (!fgetline(f, next_line)) break; From 7c4609aa7a0569b76d4423cff2523703d8f178e6 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Wed, 10 May 2023 17:54:36 +0000 Subject: [PATCH 3/5] [Backtrace] enable compile flag to trigger on at-exit; 6 frame minimum shows clean atexit when there is not much of a stack Obtained 6 stack frames. 1 | ./yosys(_Z17yosys_print_tracev+0x2e) [0x55c7a207830a] 2 | ./yosys(_Z12yosys_atexitv+0x9) [0x55c7a2078386] 3 | ./yosys(main+0x1c33) [0x55c7a207a020] 4 | /lib/x86_64-linux-gnu/libc.so.6(+0x23510) [0x7fae3a623510] 5 | /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x89) [0x7fae3a6235c9] 6 | ./yosys(_start+0x25) [0x55c7a2078215] --- Makefile | 8 ++++++++ kernel/driver.cc | 28 ++++++++++++++++++++++++++++ kernel/log.h | 1 + 3 files changed, 37 insertions(+) diff --git a/Makefile b/Makefile index c622d3bd7..0f1630e37 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ DISABLE_VERIFIC_VHDL := 0 ENABLE_COVER := 1 ENABLE_LIBYOSYS := 0 ENABLE_ZLIB := 1 +ENABLE_BACKTRACE := 0 # python wrappers ENABLE_PYOSYS := 0 @@ -68,6 +69,8 @@ ifeq ($(ENABLE_PYOSYS),1) ENABLE_LIBYOSYS := 1 endif + + BINDIR := $(PREFIX)/bin LIBDIR := $(PREFIX)/lib/$(PROGRAM_PREFIX)yosys DATDIR := $(PREFIX)/share/$(PROGRAM_PREFIX)yosys @@ -125,6 +128,11 @@ PKG_CONFIG_PATH := $(BREW_PREFIX)/libffi/lib/pkgconfig:$(PKG_CONFIG_PATH) PKG_CONFIG_PATH := $(BREW_PREFIX)/tcl-tk/lib/pkgconfig:$(PKG_CONFIG_PATH) export PATH := $(BREW_PREFIX)/bison/bin:$(BREW_PREFIX)/gettext/bin:$(BREW_PREFIX)/flex/bin:$(PATH) +ifeq ($(ENABLE_BACKTRACE),1) +CXXFLAGS += -DYOSYS_BACKTRACE +endif + + # macports search paths else ifneq ($(shell :; command -v port),) PORT_PREFIX := $(patsubst %/bin/port,%,$(shell :; command -v port)) diff --git a/kernel/driver.cc b/kernel/driver.cc index ef8e77924..c9e48177d 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -83,6 +83,32 @@ int getopt(int argc, char **argv, const char *optstring) } #endif +#if !defined(_WIN32) || defined(__MINGW32__) || defined(YOSYS_BACKTRACE) +#include +void yosys_print_trace (void) +{ + void *array[32] = {0,}; + char **strings; + + int size = backtrace (array, 32); + strings = backtrace_symbols (array, size); + if (strings != NULL && size > 6) + { + + fprintf (stderr,"Obtained %d stack frames.\n", size); + for (int i = 0; i < size; i++) + fprintf(stderr,"%d | %s\n", i+1, strings[i]); + free (strings); + } + +} +#else +void yosys_print_trace() +{ + fprintf(stderr,"Backtrace not available on this platform,\n"); +} +#endif + USING_YOSYS_NAMESPACE @@ -174,6 +200,8 @@ extern "C" { void yosys_atexit() { + + yosys_print_trace(); #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE) if (!yosys_history_file.empty()) { #if defined(YOSYS_ENABLE_READLINE) diff --git a/kernel/log.h b/kernel/log.h index dfcbc1137..26053a540 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -43,6 +43,7 @@ # endif #endif + #if defined(_MSC_VER) // At least this is not in MSVC++ 2013. # define __PRETTY_FUNCTION__ __FUNCTION__ From 721bd82d6f37dd648d53d8f168c02f0bd0ca3b58 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Sat, 13 May 2023 00:49:05 -0700 Subject: [PATCH 4/5] yosys> read_verilog /tmp/powerlog/cloud/Powerlog_43077FB1-54B7-4948-9CE7-C8A2DBD19124.tar.gz Found gzip magic in file /tmp/powerlog/cloud/Powerlog_43077FB1-54B7-4948-9CE7-C8A2DBD19124.tar.gz, decompressing using zlib. 1. Executing Verilog-2005 frontend: /tmp/powerlog/cloud/Powerlog_43077FB1-54B7-4948-9CE7-C8A2DBD19124.tar.gz Parsing Verilog input from `/tmp/powerlog/cloud/Powerlog_43077FB1-54B7-4948-9CE7-C8A2DBD19124.tar.gz' to AST representation. /tmp/powerlog/cloud/Powerlog_43077FB1-54B7-4948-9CE7-C8A2DBD19124.tar.gz:1: ERROR: syntax error, unexpected TOK_ID Obtained 13 stack frames. 1 | 0 yosys 0x00000001001644ac _Z17yosys_print_tracev + 88 2 | 1 yosys 0x0000000100164568 _Z12yosys_atexitv + 16 3 | 2 yosys 0x0000000100205124 _ZN5YosysL22logv_error_with_prefixEPKcS1_Pc + 840 4 | 3 yosys 0x00000001002052f4 _ZN5Yosys14log_file_errorERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEEiPKcz + 96 5 | 4 yosys 0x00000001003bc694 _ZNSt3__110__list_impINS_6vectorINS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEENS5_IS7_EEEENS5_IS9_EEE5clearEv + 0 6 | 5 yosys 0x000000010039772c _Z24frontend_verilog_yyparsev + 2164 7 | 6 yosys 0x00000001003be520 _ZN5Yosys15VerilogFrontend7executeERPNSt3__113basic_istreamIcNS1_11char_traitsIcEEEENS1_12basic_stringIcS4_NS1_9allocatorIcEEEENS1_6vectorISB_NS9_ISB_EEEEPNS_5RTLIL6DesignE + 5556 8 | 7 yosys 0x0000000100171c60 _ZN5Yosys8Frontend7executeENSt3__16vectorINS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEENS6_IS8_EEEEPNS_5RTLIL6DesignE + 172 9 | 8 yosys 0x00000001001709a4 _ZN5Yosys4Pass4callEPNS_5RTLIL6DesignENSt3__16vectorINS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEENS9_ISB_EEEE + 532 10 | 9 yosys 0x0000000100170464 _ZN5Yosys4Pass4callEPNS_5RTLIL6DesignENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEE + 1080 11 | 10 yosys 0x000000010021479c _ZN5Yosys5shellEPNS_5RTLIL6DesignE + 320 12 | 11 yosys 0x0000000100165374 main + 3444 13 | 12 dyld 0x00000001016710f4 start + 520 --- kernel/driver.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/driver.cc b/kernel/driver.cc index c9e48177d..ab67b66de 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -83,7 +83,7 @@ int getopt(int argc, char **argv, const char *optstring) } #endif -#if !defined(_WIN32) || defined(__MINGW32__) || defined(YOSYS_BACKTRACE) +#if defined (__linux__) || defined(__FreeBSD__) || defined(YOSYS_BACKTRACE) #include void yosys_print_trace (void) { From 026cbcdd28a311643e4f6984403063a19a212b14 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Wed, 17 May 2023 16:49:50 +0000 Subject: [PATCH 5/5] keep script-stack error logging --- Makefile | 8 -------- kernel/driver.cc | 28 ---------------------------- 2 files changed, 36 deletions(-) diff --git a/Makefile b/Makefile index 0f1630e37..c622d3bd7 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,6 @@ DISABLE_VERIFIC_VHDL := 0 ENABLE_COVER := 1 ENABLE_LIBYOSYS := 0 ENABLE_ZLIB := 1 -ENABLE_BACKTRACE := 0 # python wrappers ENABLE_PYOSYS := 0 @@ -69,8 +68,6 @@ ifeq ($(ENABLE_PYOSYS),1) ENABLE_LIBYOSYS := 1 endif - - BINDIR := $(PREFIX)/bin LIBDIR := $(PREFIX)/lib/$(PROGRAM_PREFIX)yosys DATDIR := $(PREFIX)/share/$(PROGRAM_PREFIX)yosys @@ -128,11 +125,6 @@ PKG_CONFIG_PATH := $(BREW_PREFIX)/libffi/lib/pkgconfig:$(PKG_CONFIG_PATH) PKG_CONFIG_PATH := $(BREW_PREFIX)/tcl-tk/lib/pkgconfig:$(PKG_CONFIG_PATH) export PATH := $(BREW_PREFIX)/bison/bin:$(BREW_PREFIX)/gettext/bin:$(BREW_PREFIX)/flex/bin:$(PATH) -ifeq ($(ENABLE_BACKTRACE),1) -CXXFLAGS += -DYOSYS_BACKTRACE -endif - - # macports search paths else ifneq ($(shell :; command -v port),) PORT_PREFIX := $(patsubst %/bin/port,%,$(shell :; command -v port)) diff --git a/kernel/driver.cc b/kernel/driver.cc index ab67b66de..ef8e77924 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -83,32 +83,6 @@ int getopt(int argc, char **argv, const char *optstring) } #endif -#if defined (__linux__) || defined(__FreeBSD__) || defined(YOSYS_BACKTRACE) -#include -void yosys_print_trace (void) -{ - void *array[32] = {0,}; - char **strings; - - int size = backtrace (array, 32); - strings = backtrace_symbols (array, size); - if (strings != NULL && size > 6) - { - - fprintf (stderr,"Obtained %d stack frames.\n", size); - for (int i = 0; i < size; i++) - fprintf(stderr,"%d | %s\n", i+1, strings[i]); - free (strings); - } - -} -#else -void yosys_print_trace() -{ - fprintf(stderr,"Backtrace not available on this platform,\n"); -} -#endif - USING_YOSYS_NAMESPACE @@ -200,8 +174,6 @@ extern "C" { void yosys_atexit() { - - yosys_print_trace(); #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE) if (!yosys_history_file.empty()) { #if defined(YOSYS_ENABLE_READLINE)