backtrace: rework for signal safety
Mostly, try a lot harder to not allocate anything. Sponsored-by: Klara, Inc. Sponsored-by: Wasabi Technology, Inc. Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: Rob Norris <rob.norris@klarasystems.com> Closes #16181
This commit is contained in:
parent
3974ef045e
commit
1ea8c59441
|
@ -845,7 +845,7 @@ static void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
struct sigaction action;
|
struct sigaction action;
|
||||||
|
|
||||||
libspl_backtrace();
|
libspl_backtrace(STDERR_FILENO);
|
||||||
dump_debug_buffer();
|
dump_debug_buffer();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -623,7 +623,7 @@ static void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
struct sigaction action;
|
struct sigaction action;
|
||||||
|
|
||||||
libspl_backtrace();
|
libspl_backtrace(STDERR_FILENO);
|
||||||
dump_debug_buffer();
|
dump_debug_buffer();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -103,7 +103,7 @@ libspl_assertf(const char *file, const char *func, int line,
|
||||||
getpid(), libspl_getprogname(),
|
getpid(), libspl_getprogname(),
|
||||||
libspl_gettid(), tname);
|
libspl_gettid(), tname);
|
||||||
|
|
||||||
libspl_backtrace();
|
libspl_backtrace(STDERR_FILENO);
|
||||||
|
|
||||||
#if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)
|
#if !__has_feature(attribute_analyzer_noreturn) && !defined(__COVERITY__)
|
||||||
if (libspl_assert_ok) {
|
if (libspl_assert_ok) {
|
||||||
|
|
|
@ -24,57 +24,94 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/backtrace.h>
|
#include <sys/backtrace.h>
|
||||||
#include <stdio.h>
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* libspl_backtrace() must be safe to call from inside a signal hander. This
|
||||||
|
* mostly means it must not allocate, and so we can't use things like printf.
|
||||||
|
*/
|
||||||
|
|
||||||
#if defined(HAVE_LIBUNWIND)
|
#if defined(HAVE_LIBUNWIND)
|
||||||
#define UNW_LOCAL_ONLY
|
#define UNW_LOCAL_ONLY
|
||||||
#include <libunwind.h>
|
#include <libunwind.h>
|
||||||
|
|
||||||
void
|
static size_t
|
||||||
libspl_backtrace(void)
|
libspl_u64_to_hex_str(uint64_t v, size_t digits, char *buf, size_t buflen)
|
||||||
{
|
{
|
||||||
|
static const char hexdigits[] = {
|
||||||
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||||
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t pos = 0;
|
||||||
|
boolean_t want = (digits == 0);
|
||||||
|
for (int i = 15; i >= 0; i--) {
|
||||||
|
const uint64_t d = v >> (i * 4) & 0xf;
|
||||||
|
if (!want && (d != 0 || digits > i))
|
||||||
|
want = B_TRUE;
|
||||||
|
if (want) {
|
||||||
|
buf[pos++] = hexdigits[d];
|
||||||
|
if (pos == buflen)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
libspl_backtrace(int fd)
|
||||||
|
{
|
||||||
|
ssize_t ret __attribute__((unused));
|
||||||
unw_context_t uc;
|
unw_context_t uc;
|
||||||
unw_cursor_t cp;
|
unw_cursor_t cp;
|
||||||
unw_word_t ip, off;
|
unw_word_t loc;
|
||||||
char funcname[128];
|
char buf[128];
|
||||||
#ifdef HAVE_LIBUNWIND_ELF
|
size_t n;
|
||||||
char objname[128];
|
|
||||||
unw_word_t objoff;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
fprintf(stderr, "Call trace:\n");
|
ret = write(fd, "Call trace:\n", 12);
|
||||||
unw_getcontext(&uc);
|
unw_getcontext(&uc);
|
||||||
unw_init_local(&cp, &uc);
|
unw_init_local(&cp, &uc);
|
||||||
while (unw_step(&cp) > 0) {
|
while (unw_step(&cp) > 0) {
|
||||||
unw_get_reg(&cp, UNW_REG_IP, &ip);
|
unw_get_reg(&cp, UNW_REG_IP, &loc);
|
||||||
unw_get_proc_name(&cp, funcname, sizeof (funcname), &off);
|
ret = write(fd, " [0x", 5);
|
||||||
|
n = libspl_u64_to_hex_str(loc, 10, buf, sizeof (buf));
|
||||||
|
ret = write(fd, buf, n);
|
||||||
|
ret = write(fd, "] ", 2);
|
||||||
|
unw_get_proc_name(&cp, buf, sizeof (buf), &loc);
|
||||||
|
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
|
||||||
|
ret = write(fd, buf, n);
|
||||||
|
ret = write(fd, "+0x", 3);
|
||||||
|
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
|
||||||
|
ret = write(fd, buf, n);
|
||||||
#ifdef HAVE_LIBUNWIND_ELF
|
#ifdef HAVE_LIBUNWIND_ELF
|
||||||
unw_get_elf_filename(&cp, objname, sizeof (objname), &objoff);
|
ret = write(fd, " (in ", 5);
|
||||||
fprintf(stderr, " [0x%08lx] %s+0x%2lx (in %s +0x%2lx)\n",
|
unw_get_elf_filename(&cp, buf, sizeof (buf), &loc);
|
||||||
ip, funcname, off, objname, objoff);
|
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
|
||||||
#else
|
ret = write(fd, buf, n);
|
||||||
fprintf(stderr, " [0x%08lx] %s+0x%2lx\n", ip, funcname, off);
|
ret = write(fd, " +0x", 4);
|
||||||
|
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
|
||||||
|
ret = write(fd, buf, n);
|
||||||
|
ret = write(fd, ")", 1);
|
||||||
#endif
|
#endif
|
||||||
|
ret = write(fd, "\n", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#elif defined(HAVE_BACKTRACE)
|
#elif defined(HAVE_BACKTRACE)
|
||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
|
|
||||||
void
|
void
|
||||||
libspl_backtrace(void)
|
libspl_backtrace(int fd)
|
||||||
{
|
{
|
||||||
void *btptrs[100];
|
ssize_t ret __attribute__((unused));
|
||||||
size_t nptrs = backtrace(btptrs, 100);
|
void *btptrs[64];
|
||||||
char **bt = backtrace_symbols(btptrs, nptrs);
|
size_t nptrs = backtrace(btptrs, 64);
|
||||||
fprintf(stderr, "Call trace:\n");
|
ret = write(fd, "Call trace:\n", 12);
|
||||||
for (size_t i = 0; i < nptrs; i++)
|
backtrace_symbols_fd(btptrs, nptrs, fd);
|
||||||
fprintf(stderr, " %s\n", bt[i]);
|
|
||||||
free(bt);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void
|
void
|
||||||
libspl_backtrace(void)
|
libspl_backtrace(int fd __maybe_unused)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,6 @@
|
||||||
#ifndef _LIBSPL_SYS_BACKTRACE_H
|
#ifndef _LIBSPL_SYS_BACKTRACE_H
|
||||||
#define _LIBSPL_SYS_BACKTRACE_H
|
#define _LIBSPL_SYS_BACKTRACE_H
|
||||||
|
|
||||||
void libspl_backtrace(void);
|
void libspl_backtrace(int fd);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue