From 1b864b5527535efbdc21224e39dbbac94e0e8cba Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 28 Nov 2011 18:12:56 -0800 Subject: [PATCH] Add backtrace function. May only work on Linux but still very useful. --- xwords4/linux/Makefile | 2 +- xwords4/linux/linuxutl.c | 18 ++++++++++++++++++ xwords4/linux/xptypes.h | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/xwords4/linux/Makefile b/xwords4/linux/Makefile index 2f90dc32e..f5e870d2a 100644 --- a/xwords4/linux/Makefile +++ b/xwords4/linux/Makefile @@ -20,7 +20,7 @@ ifeq ($(MEMDEBUG),TRUE) DEFINES = -DMEM_DEBUG -DDEBUG -DENABLE_LOGGING -DNUMBER_KEY_AS_INDEX DEFINES += -DCOMMS_CHECKSUM CFLAGS += -g $(GPROFFLAG) -Wall -Wunused-parameter -Wcast-align -Werror -O0 -CFLAGS += -DDEBUG_TS +CFLAGS += -DDEBUG_TS -rdynamic PLATFORM = obj_linux_memdbg else DEFINES = diff --git a/xwords4/linux/linuxutl.c b/xwords4/linux/linuxutl.c index 886231376..3593cd45c 100644 --- a/xwords4/linux/linuxutl.c +++ b/xwords4/linux/linuxutl.c @@ -25,6 +25,9 @@ #include #include #include /* BAD: use glib to support utf8 */ +#ifdef DEBUG +# include /* for backtrace */ +#endif #include "linuxutl.h" #include "main.h" @@ -54,6 +57,21 @@ linux_debugf( const char* format, ... ) fprintf( stderr, "%s\n", buf ); } + +void +linux_backtrace( void ) +{ + void* buffer[128]; + int nFound = backtrace( buffer, VSIZE(buffer) ); + XP_ASSERT( nFound < VSIZE(buffer) ); + char** traces = backtrace_symbols( buffer, nFound ); + + XP_U16 ii; + for ( ii = 0; ii < nFound; ++ii ) { + XP_LOGF( "trace[%.2d]: %s", ii, traces[ii] ); + } + free( traces ); +} #endif #ifndef MEM_DEBUG diff --git a/xwords4/linux/xptypes.h b/xwords4/linux/xptypes.h index 075120e11..5aa150573 100644 --- a/xwords4/linux/xptypes.h +++ b/xwords4/linux/xptypes.h @@ -115,8 +115,11 @@ void linux_lowerstr( XP_UCHAR* str ); #ifdef DEBUG # define XP_ASSERT(b) assert(b) +void linux_backtrace( void ); +# define XP_BACKTRACE linux_backtrace #else # define XP_ASSERT(b) +# define XP_BACKTRACE #endif #define DGRAM_TYPE SOCK_DGRAM