2024-05-10 04:26:11 +03:00
|
|
|
/*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License (the "License").
|
|
|
|
* You may not use this file except in compliance with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
|
|
|
|
* or https://opensource.org/licenses/CDDL-1.0.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
|
|
|
|
* If applicable, add the following below this CDDL HEADER, with the
|
|
|
|
* fields enclosed by brackets "[]" replaced with your own identifying
|
|
|
|
* information: Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
|
|
|
|
* Copyright (c) 2024, Klara Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/backtrace.h>
|
2024-05-10 06:04:14 +03:00
|
|
|
#include <sys/types.h>
|
2024-10-18 05:49:41 +03:00
|
|
|
#include <sys/debug.h>
|
2024-05-10 06:04:14 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/*
|
2024-10-18 05:49:41 +03:00
|
|
|
* Output helpers. libspl_backtrace() must not block, must be thread-safe and
|
|
|
|
* must be safe to call from a signal handler. At least, that means not having
|
|
|
|
* printf, so we end up having to call write() directly on the fd. That's
|
|
|
|
* awkward, as we always have to pass through a length, and some systems will
|
|
|
|
* complain if we don't consume the return. So we have some macros to make
|
|
|
|
* things a little more palatable.
|
2024-05-10 06:04:14 +03:00
|
|
|
*/
|
2024-10-18 05:49:41 +03:00
|
|
|
#define spl_bt_write_n(fd, s, n) \
|
|
|
|
do { ssize_t r __maybe_unused = write(fd, s, n); } while (0)
|
|
|
|
#define spl_bt_write(fd, s) spl_bt_write_n(fd, s, sizeof (s))
|
2024-05-10 04:26:11 +03:00
|
|
|
|
|
|
|
#if defined(HAVE_LIBUNWIND)
|
|
|
|
#define UNW_LOCAL_ONLY
|
|
|
|
#include <libunwind.h>
|
|
|
|
|
2024-10-18 06:30:05 +03:00
|
|
|
/*
|
|
|
|
* Convert `v` to ASCII hex characters. The bottom `n` nybbles (4-bits ie one
|
|
|
|
* hex digit) will be written, up to `buflen`. The buffer will not be
|
|
|
|
* null-terminated. Returns the number of digits written.
|
|
|
|
*/
|
2024-05-10 06:04:14 +03:00
|
|
|
static size_t
|
2024-10-18 06:30:05 +03:00
|
|
|
spl_bt_u64_to_hex_str(uint64_t v, size_t n, char *buf, size_t buflen)
|
2024-05-10 06:04:14 +03:00
|
|
|
{
|
|
|
|
static const char hexdigits[] = {
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t pos = 0;
|
2024-10-18 06:30:05 +03:00
|
|
|
boolean_t want = (n == 0);
|
2024-05-10 06:04:14 +03:00
|
|
|
for (int i = 15; i >= 0; i--) {
|
|
|
|
const uint64_t d = v >> (i * 4) & 0xf;
|
2024-10-18 06:30:05 +03:00
|
|
|
if (!want && (d != 0 || n > i))
|
2024-05-10 06:04:14 +03:00
|
|
|
want = B_TRUE;
|
|
|
|
if (want) {
|
|
|
|
buf[pos++] = hexdigits[d];
|
|
|
|
if (pos == buflen)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (pos);
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:26:11 +03:00
|
|
|
void
|
2024-05-10 06:04:14 +03:00
|
|
|
libspl_backtrace(int fd)
|
2024-05-10 04:26:11 +03:00
|
|
|
{
|
|
|
|
unw_context_t uc;
|
|
|
|
unw_cursor_t cp;
|
2024-10-15 14:18:19 +03:00
|
|
|
unw_word_t v;
|
2024-05-10 06:04:14 +03:00
|
|
|
char buf[128];
|
2024-10-15 14:18:19 +03:00
|
|
|
size_t n, c;
|
2024-05-10 04:26:11 +03:00
|
|
|
|
|
|
|
unw_getcontext(&uc);
|
2024-10-15 14:18:19 +03:00
|
|
|
|
2024-05-10 04:26:11 +03:00
|
|
|
unw_init_local(&cp, &uc);
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "Registers:\n");
|
2024-10-15 14:18:19 +03:00
|
|
|
c = 0;
|
|
|
|
for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) {
|
|
|
|
if (unw_get_reg(&cp, regnum, &v) < 0)
|
|
|
|
continue;
|
|
|
|
const char *name = unw_regname(regnum);
|
|
|
|
for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}
|
|
|
|
if (n == 0) {
|
|
|
|
buf[0] = '?';
|
2024-10-18 06:30:05 +03:00
|
|
|
n = spl_bt_u64_to_hex_str(regnum, 2,
|
2024-10-15 14:18:19 +03:00
|
|
|
&buf[1], sizeof (buf)-1) + 1;
|
|
|
|
name = buf;
|
|
|
|
}
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, " ", 5-MIN(n, 3));
|
|
|
|
spl_bt_write_n(fd, name, n);
|
|
|
|
spl_bt_write(fd, ": 0x");
|
2024-10-18 06:30:05 +03:00
|
|
|
n = spl_bt_u64_to_hex_str(v, 18, buf, sizeof (buf));
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
2024-10-15 14:18:19 +03:00
|
|
|
if (!(++c % 3))
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "\n");
|
2024-10-15 14:18:19 +03:00
|
|
|
}
|
|
|
|
if (c % 3)
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "\n");
|
2024-10-15 14:18:19 +03:00
|
|
|
|
|
|
|
unw_init_local(&cp, &uc);
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "Call trace:\n");
|
2024-05-10 04:26:11 +03:00
|
|
|
while (unw_step(&cp) > 0) {
|
2024-10-15 14:18:19 +03:00
|
|
|
unw_get_reg(&cp, UNW_REG_IP, &v);
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, " [0x");
|
2024-10-18 06:30:05 +03:00
|
|
|
n = spl_bt_u64_to_hex_str(v, 18, buf, sizeof (buf));
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
|
|
|
spl_bt_write(fd, "] ");
|
2024-10-15 14:18:19 +03:00
|
|
|
unw_get_proc_name(&cp, buf, sizeof (buf), &v);
|
2024-05-10 06:04:14 +03:00
|
|
|
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
|
|
|
spl_bt_write(fd, "+0x");
|
2024-10-18 06:30:05 +03:00
|
|
|
n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
2024-05-10 04:26:11 +03:00
|
|
|
#ifdef HAVE_LIBUNWIND_ELF
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, " (in ");
|
2024-10-15 14:18:19 +03:00
|
|
|
unw_get_elf_filename(&cp, buf, sizeof (buf), &v);
|
2024-05-10 06:04:14 +03:00
|
|
|
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
|
|
|
spl_bt_write(fd, " +0x");
|
2024-10-18 06:30:05 +03:00
|
|
|
n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write_n(fd, buf, n);
|
|
|
|
spl_bt_write(fd, ")");
|
2024-05-10 04:26:11 +03:00
|
|
|
#endif
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "\n");
|
2024-05-10 04:26:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(HAVE_BACKTRACE)
|
|
|
|
#include <execinfo.h>
|
|
|
|
|
|
|
|
void
|
2024-05-10 06:04:14 +03:00
|
|
|
libspl_backtrace(int fd)
|
2024-05-10 04:26:11 +03:00
|
|
|
{
|
2024-05-10 06:04:14 +03:00
|
|
|
void *btptrs[64];
|
|
|
|
size_t nptrs = backtrace(btptrs, 64);
|
2024-10-18 05:49:41 +03:00
|
|
|
spl_bt_write(fd, "Call trace:\n");
|
2024-05-10 06:04:14 +03:00
|
|
|
backtrace_symbols_fd(btptrs, nptrs, fd);
|
2024-05-10 04:26:11 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
2024-05-10 06:04:14 +03:00
|
|
|
libspl_backtrace(int fd __maybe_unused)
|
2024-05-10 04:26:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|