From e514a5d43c2c3477941e2548d8a01be0e1294b0e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 9 Apr 2023 12:55:09 +0930 Subject: [PATCH] common: lookup function for symnames. Signed-off-by: Rusty Russell --- common/daemon.c | 34 ++++++++++++++++++++++++++++++++++ common/daemon.h | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/common/daemon.c b/common/daemon.c index 83b0ff12a..e8f40f534 100644 --- a/common/daemon.c +++ b/common/daemon.c @@ -38,6 +38,35 @@ void send_backtrace(const char *why) backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL); } +static void extract_symname(void *data, uintptr_t pc, + const char *symname, + uintptr_t symval, + uintptr_t symsize) +{ + const char **ret = data; + + /* ret is context to alloc off, and value to set */ + if (symname) + *ret = tal_strdup(*ret, symname); + else + *ret = NULL; +} + +const char *backtrace_symname(const tal_t *ctx, const void *addr) +{ + const char *ret = ctx; + if (!backtrace_state) + return tal_fmt(ctx, "%p (backtrace disabled)", addr); + + if (!backtrace_syminfo(backtrace_state, (uintptr_t)addr, + extract_symname, NULL, &ret)) + ret = NULL; + + if (ret) + return ret; + return tal_fmt(ctx, "%p", addr); +} + static void crashdump(int sig) { char why[100]; @@ -71,6 +100,11 @@ static void crashlog_activate(void) void send_backtrace(const char *why) { } + +const char *backtrace_symname(const tal_t *ctx, const void *addr) +{ + return "unknown (backtrace unsupported)"; +} #endif int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout) diff --git a/common/daemon.h b/common/daemon.h index b1703685d..2db8cf437 100644 --- a/common/daemon.h +++ b/common/daemon.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_DAEMON_H #define LIGHTNING_COMMON_DAEMON_H #include "config.h" +#include #include /* Common setup for all daemons */ @@ -14,6 +15,9 @@ int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout); /* Print a backtrace to stderr, and via backtrace_print */ void send_backtrace(const char *why); +/* Try to extract a name for this function/var/etc */ +const char *backtrace_symname(const tal_t *ctx, const void *addr); + /* Shutdown for a valgrind-clean exit (frees everything) */ void daemon_shutdown(void);