Path: blob/main/sys/contrib/openzfs/lib/libspl/backtrace.c
48378 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright (c) 2024, Rob Norris <[email protected]>23* Copyright (c) 2024, Klara Inc.24*/2526#include <sys/backtrace.h>27#include <sys/types.h>28#include <sys/debug.h>29#include <unistd.h>3031/*32* Output helpers. libspl_backtrace() must not block, must be thread-safe and33* must be safe to call from a signal handler. At least, that means not having34* printf, so we end up having to call write() directly on the fd. That's35* awkward, as we always have to pass through a length, and some systems will36* complain if we don't consume the return. So we have some macros to make37* things a little more palatable.38*/39#define spl_bt_write_n(fd, s, n) \40do { ssize_t r __maybe_unused = write(fd, s, n); } while (0)41#define spl_bt_write(fd, s) spl_bt_write_n(fd, s, sizeof (s)-1)4243#ifdef HAVE_LIBUNWIND44/*45* libunwind-gcc and libunwind-llvm both list registers using an enum,46* unw_regnum_t, however they indicate the highest numbered register for47* a given architecture in different ways. We can check which one is defined48* and mark which libunwind is in use49*/50#ifdef IS_LIBUNWIND_LLVM51#include <libunwind.h>52#define LAST_REG_INDEX _LIBUNWIND_HIGHEST_DWARF_REGISTER53#else54/*55* Need to define UNW_LOCAL_ONLY before importing libunwind.h56* if using libgcc libunwind.57*/58#define UNW_LOCAL_ONLY59#include <libunwind.h>60#define LAST_REG_INDEX UNW_TDEP_LAST_REG61#endif626364/*65* Convert `v` to ASCII hex characters. The bottom `n` nybbles (4-bits ie one66* hex digit) will be written, up to `buflen`. The buffer will not be67* null-terminated. Returns the number of digits written.68*/69static size_t70spl_bt_u64_to_hex_str(uint64_t v, size_t n, char *buf, size_t buflen)71{72static const char hexdigits[] = {73'0', '1', '2', '3', '4', '5', '6', '7',74'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'75};7677size_t pos = 0;78boolean_t want = (n == 0);79for (int i = 15; i >= 0; i--) {80const uint64_t d = v >> (i * 4) & 0xf;81if (!want && (d != 0 || n > i))82want = B_TRUE;83if (want) {84buf[pos++] = hexdigits[d];85if (pos == buflen)86break;87}88}89return (pos);90}9192void93libspl_backtrace(int fd)94{95unw_context_t uc;96unw_cursor_t cp;97unw_word_t v;98char buf[128];99size_t n;100int err;101102/* Snapshot the current frame and state. */103unw_getcontext(&uc);104105/*106* TODO: walk back to the frame that tripped the assertion / the place107* where the signal was recieved.108*/109110/*111* Register dump. We're going to loop over all the registers in the112* top frame, and show them, with names, in a nice three-column113* layout, which keeps us within 80 columns.114*/115spl_bt_write(fd, "Registers:\n");116117/* Initialise a frame cursor, starting at the current frame */118unw_init_local(&cp, &uc);119120/*121* Iterate over all registers for the architecture. We've figured122* out the highest number above, however, not all register numbers in123* this range are defined by the architecture, and not all defined124* registers will be present on every implementation of that125* architecture. Moreover, libunwind provides nice names for most, but126* not all registers, but these are hardcoded; a name being available127* does not mean that register is available.128*129* So, we have to pull this all together here. We try to get the value130* of every possible register. If we get a value for it, then the131* register must exist, and so we get its name. If libunwind has no132* name for it, we synthesize something. These cases should be rare,133* and they're usually for uninteresting or niche registers, so it134* shouldn't really matter. We can see the value, and that's the main135* thing.136*/137uint_t cols = 0;138for (uint_t regnum = 0; regnum <= LAST_REG_INDEX; regnum++) {139/*140* Get the value. Any error probably means the register141* doesn't exist, and we skip it. LLVM libunwind iterates over142* fp registers in the same list, however they have to be143* accessed using unw_get_fpreg instead. Here, we just ignore144* them.145*/146#ifdef IS_LIBUNWIND_LLVM147if (unw_is_fpreg(&cp, regnum) ||148unw_get_reg(&cp, regnum, &v) < 0)149continue;150#else151if (unw_get_reg(&cp, regnum, &v) < 0)152continue;153#endif154155/*156* Register name. If GCC libunwind doesn't have a name for it,157* it will return "???". As a shortcut, we just treat '?'158* is an alternate end-of-string character. LLVM libunwind will159* return the string 'unknown register', which we detect by160* checking if the register name is longer than 5 characters.161*/162#ifdef IS_LIBUNWIND_LLVM163const char *name = unw_regname(&cp, regnum);164#else165const char *name = unw_regname(regnum);166#endif167for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}168if (n == 0 || n > 5) {169/*170* No valid name, or likely llvm_libunwind returned171* unknown_register, so make one of the form "?xx",172* where "xx" is the two-char hex of libunwind's173* register number.174*/175buf[0] = '?';176n = spl_bt_u64_to_hex_str(regnum, 2,177&buf[1], sizeof (buf)-1) + 1;178name = buf;179}180181/*182* Two spaces of padding before each column, plus extra183* spaces to align register names shorter than three chars.184*/185spl_bt_write_n(fd, " ", 5-MIN(n, 3));186187/* Register name and column punctuation */188spl_bt_write_n(fd, name, n);189spl_bt_write(fd, ": 0x");190191/*192* Convert register value (from unw_get_reg()) to hex. We're193* assuming that all registers are 64-bits wide, which is194* probably fine for any general-purpose registers on any195* machine currently in use. A more generic way would be to196* look at the width of unw_word_t, but that would also197* complicate the column code a bit. This is fine.198*/199n = spl_bt_u64_to_hex_str(v, 16, buf, sizeof (buf));200spl_bt_write_n(fd, buf, n);201202/* Every third column, emit a newline */203if (!(++cols % 3))204spl_bt_write(fd, "\n");205}206207/* If we finished before the third column, emit a newline. */208if (cols % 3)209spl_bt_write(fd, "\n");210211/* Now the main event, the backtrace. */212spl_bt_write(fd, "Call trace:\n");213214/* Reset the cursor to the top again. */215unw_init_local(&cp, &uc);216217do {218/*219* Getting the IP should never fail; libunwind handles it220* specially, because its used a lot internally. Still, no221* point being silly about it, as the last thing we want is222* our crash handler to crash. So if it ever does fail, we'll223* show an error line, but keep going to the next frame.224*/225if (unw_get_reg(&cp, UNW_REG_IP, &v) < 0) {226spl_bt_write(fd, " [couldn't get IP register; "227"corrupt frame?]");228continue;229}230231/* IP & punctuation */232n = spl_bt_u64_to_hex_str(v, 16, buf, sizeof (buf));233spl_bt_write(fd, " [0x");234spl_bt_write_n(fd, buf, n);235spl_bt_write(fd, "] ");236237/*238* Function ("procedure") name for the current frame. `v`239* receives the offset from the named function to the IP, which240* we show as a "+offset" suffix.241*242* If libunwind can't determine the name, we just show "???"243* instead. We've already displayed the IP above; that will244* have to do.245*246* unw_get_proc_name() will return ENOMEM if the buffer is too247* small, instead truncating the name. So we treat that as a248* success and use whatever is in the buffer.249*/250err = unw_get_proc_name(&cp, buf, sizeof (buf), &v);251if (err == 0 || err == -UNW_ENOMEM) {252for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}253spl_bt_write_n(fd, buf, n);254255/* Offset from proc name */256spl_bt_write(fd, "+0x");257n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));258spl_bt_write_n(fd, buf, n);259} else260spl_bt_write(fd, "???");261262#ifdef HAVE_LIBUNWIND_ELF263/*264* Newer libunwind has unw_get_elf_filename(), which gets265* the name of the ELF object that the frame was executing in.266* Like `unw_get_proc_name()`, `v` recieves the offset within267* the file, and UNW_ENOMEM indicates that a truncate filename268* was left in the buffer.269*/270err = unw_get_elf_filename(&cp, buf, sizeof (buf), &v);271if (err == 0 || err == -UNW_ENOMEM) {272for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}273spl_bt_write(fd, " (in ");274spl_bt_write_n(fd, buf, n);275276/* Offset within file */277spl_bt_write(fd, " +0x");278n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf));279spl_bt_write_n(fd, buf, n);280spl_bt_write(fd, ")");281}282#endif283spl_bt_write(fd, "\n");284} while (unw_step(&cp) > 0);285}286#elif defined(HAVE_BACKTRACE)287#include <execinfo.h>288289void290libspl_backtrace(int fd)291{292void *btptrs[64];293size_t nptrs = backtrace(btptrs, 64);294spl_bt_write(fd, "Call trace:\n");295backtrace_symbols_fd(btptrs, nptrs, fd);296}297#else298void299libspl_backtrace(int fd __maybe_unused)300{301}302#endif303304305