From 7c4609aa7a0569b76d4423cff2523703d8f178e6 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Wed, 10 May 2023 17:54:36 +0000 Subject: [PATCH] [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__