Commit 848daf27 authored by Pawel Wodkowski's avatar Pawel Wodkowski Committed by Jim Harris
Browse files

log: add backtrace option



Add '--enable-log-bt=lvl' option to show simple backtrace in following
form:

thread.c: 346:spdk_io_device_register: *ERROR*: io_device 0xf2ef80
already registered
*ERROR*: === BACKTRACE START ===
*ERROR*:   1: spdk_io_device_register() at 0x6d64df
*ERROR*:   2:  spdk_copy_engine_initialize() at 0x71059c
*ERROR*:   3:   spdk_copy_engine_subsystem_initialize() at 0x572ed0
*ERROR*:   4:    spdk_subsystem_init_next() at 0x6ca756
*ERROR*:   5:     spdk_subsystem_verify() at 0x6caba7
*ERROR*:   6:      _spdk_event_queue_run_batch() at 0x6c1ffa
*ERROR*:   7:       _spdk_reactor_run() at 0x6c5349
*ERROR*:   8:        spdk_reactors_start() at 0x6c784f
*ERROR*:   9:         spdk_app_start() at 0x6bf18e
*ERROR*: === BACKTRACE END ===

This adds additional libunwind dependency so don't enable by default.

Change-Id: Ice93d7571a000d8a57d2fedda7670c9a0b6ff7b7
Signed-off-by: default avatarPawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/419726


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 5d53d43e
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@ CONFIG_PREFIX?=/usr/local
# Build with debug logging. Turn off for performance testing and normal usage
CONFIG_DEBUG?=n

# Show backtrace when logging message at level <= lvl (ERROR, WARN, NOTICE, DEBUG)
#CONFIG_LOG_BACKTRACE?=lvl

# Treat warnings as errors (fail the build on any warning).
CONFIG_WERROR?=n

+8 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@ function usage()
	echo " --prefix=path             Configure installation prefix (default: /usr/local)"
	echo ""
	echo " --enable-debug            Configure for debug builds"
	echo " --enable-log-bt=lvl       Show backtrace using libunwind when logging message at level <= lvl."
	echo "                           Valid values are: ERROR, WARN, NOTICE, DEBUG."
	echo " --enable-werror           Treat compiler warnings as errors"
	echo " --enable-asan             Enable address sanitizer"
	echo " --enable-ubsan            Enable undefined behavior sanitizer"
@@ -89,6 +91,9 @@ for i in "$@"; do
		--disable-debug)
			CONFIG_DEBUG=n
			;;
		--enable-log-bt=*)
			CONFIG_LOG_BACKTRACE=${i#*=}
			;;
		--enable-asan)
			CONFIG_ASAN=y
			;;
@@ -295,6 +300,9 @@ fi
if [ -n "$CONFIG_DEBUG" ]; then
	echo "CONFIG_DEBUG?=$CONFIG_DEBUG" >> CONFIG.local
fi
if [ -n "$CONFIG_LOG_BACKTRACE" ]; then
	echo "CONFIG_LOG_BACKTRACE?=$CONFIG_LOG_BACKTRACE" >> CONFIG.local
fi
if [ -n "$CONFIG_WERROR" ]; then
	echo "CONFIG_WERROR?=$CONFIG_WERROR" >> CONFIG.local
fi
+44 −0
Original line number Diff line number Diff line
@@ -35,6 +35,11 @@

#include "spdk_internal/log.h"

#ifdef SPDK_LOG_BACKTRACE_LVL
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#endif

static const char *const spdk_level_names[] = {
	[SPDK_LOG_ERROR]	= "ERROR",
	[SPDK_LOG_WARN]		= "WARNING",
@@ -57,6 +62,44 @@ spdk_log_close(void)
	closelog();
}

#ifdef SPDK_LOG_BACKTRACE_LVL
static void
spdk_log_unwind_stack(FILE *fp, enum spdk_log_level level)
{
	unw_error_t err;
	unw_cursor_t cursor;
	unw_context_t uc;
	unw_word_t ip;
	unw_word_t offp;
	char f_name[64];
	int frame;

	if (level > SPDK_LOG_BACKTRACE_LVL) {
		return;
	}

	unw_getcontext(&uc);
	unw_init_local(&cursor, &uc);
	fprintf(fp, "*%s*: === BACKTRACE START ===\n", spdk_level_names[level]);

	unw_step(&cursor);
	for (frame = 1; unw_step(&cursor) > 0; frame++) {
		unw_get_reg(&cursor, UNW_REG_IP, &ip);
		err = unw_get_proc_name(&cursor, f_name, sizeof(f_name), &offp);
		if (err || strcmp(f_name, "main") == 0) {
			break;
		}

		fprintf(fp, "*%s*: %3d: %*s%s() at %#lx\n", spdk_level_names[level], frame, frame - 1, "", f_name,
			(unsigned long)ip);
	}
	fprintf(fp, "*%s*: === BACKTRACE END ===\n", spdk_level_names[level]);
}

#else
#define spdk_log_unwind_stack(fp, lvl)
#endif

void
spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
	 const char *format, ...)
@@ -87,6 +130,7 @@ spdk_log(enum spdk_log_level level, const char *file, const int line, const char

	if (level <= g_spdk_log_print_level) {
		fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
		spdk_log_unwind_stack(stderr, level);
	}

	if (level <= g_spdk_log_level) {
+4 −0
Original line number Diff line number Diff line
@@ -193,6 +193,10 @@ CXXFLAGS += $(COMMON_CFLAGS) -std=c++0x
SYS_LIBS += -lrt
SYS_LIBS += -luuid
SYS_LIBS += -lcrypto
ifneq ($(CONFIG_LOG_BACKTRACE),)
SYS_LIBS += -lunwind
COMMON_CFLAGS += -DSPDK_LOG_BACKTRACE_LVL=SPDK_LOG_$(CONFIG_LOG_BACKTRACE)
endif

MAKEFLAGS += --no-print-directory

+6 −0
Original line number Diff line number Diff line
@@ -13,6 +13,8 @@ if [ -s /etc/redhat-release ]; then
		git astyle python-pep8 lcov python clang-analyzer libuuid-devel \
		sg3_utils libiscsi-devel
	yum install -y --allowerasing openssl-devel
	# Additional (optional) dependencies for showing backtrace in logs
	yum install libunwind-devel
	# Additional dependencies for NVMe over Fabrics
	yum install -y libibverbs-devel librdmacm-devel
	# Additional dependencies for DPDK
@@ -27,6 +29,8 @@ elif [ -f /etc/debian_version ]; then
	# Includes Ubuntu, Debian
	apt-get install -y gcc g++ make libcunit1-dev libaio-dev libssl-dev \
		git astyle pep8 lcov clang uuid-dev sg3-utils libiscsi-dev
	# Additional (optional) dependencies for showing backtrace in logs
	apt-get install libunwind-dev
	# Additional dependencies for NVMe over Fabrics
	apt-get install -y libibverbs-dev librdmacm-dev
	# Additional dependencies for DPDK
@@ -40,6 +44,8 @@ elif [ -f /etc/debian_version ]; then
elif [ -f /etc/SuSE-release ]; then
	zypper install -y gcc gcc-c++ make cunit-devel libaio-devel libopenssl-devel \
		git-core lcov python-base python-pep8 libuuid-devel sg3_utils
	# Additional (optional) dependencies for showing backtrace in logs
	zypper install libunwind-devel
	# Additional dependencies for NVMe over Fabrics
	zypper install -y rdma-core-devel
	# Additional dependencies for DPDK
Loading