[util] Add a wrapper for libgcc's backtrace functions

Right now, just backtrace_pcinfo is supported, but it's enough for
testing.
This commit is contained in:
Bill Currie 2023-12-04 17:53:12 +09:00
parent b001bf20c0
commit e30f2cbacc
9 changed files with 131 additions and 1 deletions

5
config.d/backtrace.m4 Normal file
View file

@ -0,0 +1,5 @@
AC_CHECK_LIB([backtrace], [backtrace_create_state],
[HAVE_BACKTRACE=yes
BACKTRACE_LIBS=-lbacktrace],
[HAVE_BACKTRACE=no])
AC_SUBST(BACKTRACE_LIBS)

View file

@ -141,6 +141,7 @@ m4_include(config.d/paths.m4)
m4_include(config.d/build_control.m4)
m4_include(config.d/qfcc.m4)
m4_include(config.d/tracy.m4)
m4_include(config.d/backtrace.m4)
AC_ARG_ENABLE(static-doc,
AS_HELP_STRING([--enable-static-doc],
@ -177,6 +178,7 @@ case "${host}" in
[fontconfig, HAVE_FONTCONFIG],
[freetype, HAVE_FREETYPE],
[harfbuzz, HAVE_HARFBUZZ],
[backtrace, HAVE_BACKTRACE],
])
;;
*linux*)
@ -201,6 +203,7 @@ case "${host}" in
[libfontconfig-dev, HAVE_FONTCONFIG],
[libfreetype-dev, HAVE_FREETYPE],
[libharfbuzz-dev, HAVE_HARFBUZZ],
[backtrace, HAVE_BACKTRACE],
])
;;
esac

View file

@ -1,5 +1,6 @@
include_qf = \
include/QF/alloc.h \
include/QF/backtrace.h \
include/QF/bspfile.h \
include/QF/cbuf.h \
include/QF/cdaudio.h \

40
include/QF/backtrace.h Normal file
View file

@ -0,0 +1,40 @@
/*
backtrace.h
Support for dumping backtrace information.
Copyright (C) 2023 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2023/12/04
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifndef __QF_backtrace_h
#define __QF_backtrace_h
#include <stdint.h>
typedef struct dstring_s dstring_t;
void BT_Init (const char *filename);
void BT_pcInfo (dstring_t *str, uintptr_t pc);
#endif// __QF_backtrace_h

View file

@ -37,9 +37,10 @@ getopt=
endif
libs_util_libQFutil_la_LDFLAGS= $(lib_ldflags)
libs_util_libQFutil_la_LIBADD= $(util_asm) $(Z_LIBS) $(DL_LIBS) $(WIN32_LIBS)
libs_util_libQFutil_la_LIBADD= $(util_asm) $(Z_LIBS) $(BACKTRACE_LIBS) $(DL_LIBS) $(WIN32_LIBS)
libs_util_libQFutil_la_DEPENDENCIES= $(util_asm)
libs_util_libQFutil_la_SOURCES= \
libs/util/backtrace.c \
libs/util/bsearch.c \
libs/util/bspfile.c \
libs/util/buildnum.c \

76
libs/util/backtrace.c Normal file
View file

@ -0,0 +1,76 @@
/*
backtrace.c
Support for dumping backtrace information.
Copyright (C) 2023 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2023/12/04
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <inttypes.h>
#include <string.h>
#include <backtrace.h>
#include "QF/backtrace.h"
#include "QF/dstring.h"
#include "QF/sys.h"
static struct backtrace_state *bt_state;
static void bt_error (void *data, const char *msg, int errnum)
{
if (errnum > 0) {
Sys_Printf ("%s: %s\n", msg, strerror (errnum));
} else if (errnum < 0) {
Sys_Printf ("%s: no symbol table\n", msg);
}
}
void
BT_Init (const char *filename)
{
bt_state = backtrace_create_state (filename, 1, bt_error, 0);
}
static int
bt_print (void *data, uintptr_t pc, const char *fname, int lineno,
const char *func)
{
dstring_t *str = data;
dasprintf (str, "(%"PRIxPTR") %s:%s:%d", pc, func, fname, lineno);
return 0;
}
void
BT_pcInfo (dstring_t *str, uintptr_t pc)
{
if (bt_state) {
backtrace_pcinfo (bt_state, pc, bt_print, 0, str);
} else {
dasprintf (str, "(%"PRIxPTR")", pc);
}
}

View file

@ -28,6 +28,7 @@
# include "config.h"
#endif
#include "QF/backtrace.h"
#include "QF/dstring.h"
#include "QF/Vulkan/barrier.h"

View file

@ -40,6 +40,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include <errno.h>
#include <string.h>
#include "QF/backtrace.h"
#include "QF/cbuf.h"
#include "QF/draw.h"
#include "QF/image.h"
@ -343,6 +344,7 @@ BI_Graphics_Init (progs_t *pr)
qwaq_thread_t *thread = PR_Resources_Find (pr, "qwaq_thread");
PR_RegisterBuiltins (pr, builtins, 0);
BT_Init (this_program);
QFS_Init (thread->hunk, "nq");
PI_Init ();

View file

@ -30,6 +30,7 @@ void BI_Curses_Init (progs_t *pr);
void BI_TermInput_Init (progs_t *pr);
void QWAQ_EditBuffer_Init (progs_t *pr);
extern struct cbuf_s *qwaq_cbuf;
extern const char *this_program;
qwaq_thread_t *create_thread (void *(*thread_func) (qwaq_thread_t *), void *);
int qwaq_init_threads (qwaq_thread_set_t *thread_data);