1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * 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.LICENSE 9 * or https://opensource.org/licenses/CDDL-1.0. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2024, Rob Norris <robn@despairlabs.com> 23 * Copyright (c) 2024, Klara Inc. 24 */ 25 26 #include <sys/backtrace.h> 27 #include <sys/types.h> 28 #include <sys/debug.h> 29 #include <unistd.h> 30 31 /* 32 * Output helpers. libspl_backtrace() must not block, must be thread-safe and 33 * must be safe to call from a signal handler. At least, that means not having 34 * printf, so we end up having to call write() directly on the fd. That's 35 * awkward, as we always have to pass through a length, and some systems will 36 * complain if we don't consume the return. So we have some macros to make 37 * things a little more palatable. 38 */ 39 #define spl_bt_write_n(fd, s, n) \ 40 do { 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)) 42 43 #if defined(HAVE_LIBUNWIND) 44 #define UNW_LOCAL_ONLY 45 #include <libunwind.h> 46 47 /* 48 * Convert `v` to ASCII hex characters. The bottom `n` nybbles (4-bits ie one 49 * hex digit) will be written, up to `buflen`. The buffer will not be 50 * null-terminated. Returns the number of digits written. 51 */ 52 static size_t 53 spl_bt_u64_to_hex_str(uint64_t v, size_t n, char *buf, size_t buflen) 54 { 55 static const char hexdigits[] = { 56 '0', '1', '2', '3', '4', '5', '6', '7', 57 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 58 }; 59 60 size_t pos = 0; 61 boolean_t want = (n == 0); 62 for (int i = 15; i >= 0; i--) { 63 const uint64_t d = v >> (i * 4) & 0xf; 64 if (!want && (d != 0 || n > i)) 65 want = B_TRUE; 66 if (want) { 67 buf[pos++] = hexdigits[d]; 68 if (pos == buflen) 69 break; 70 } 71 } 72 return (pos); 73 } 74 75 void 76 libspl_backtrace(int fd) 77 { 78 unw_context_t uc; 79 unw_cursor_t cp; 80 unw_word_t v; 81 char buf[128]; 82 size_t n; 83 int err; 84 85 /* Snapshot the current frame and state. */ 86 unw_getcontext(&uc); 87 88 /* 89 * TODO: walk back to the frame that tripped the assertion / the place 90 * where the signal was recieved. 91 */ 92 93 /* 94 * Register dump. We're going to loop over all the registers in the 95 * top frame, and show them, with names, in a nice three-column 96 * layout, which keeps us within 80 columns. 97 */ 98 spl_bt_write(fd, "Registers:\n"); 99 100 /* Initialise a frame cursor, starting at the current frame */ 101 unw_init_local(&cp, &uc); 102 103 /* 104 * libunwind's list of possible registers for this architecture is an 105 * enum, unw_regnum_t. UNW_TDEP_LAST_REG is the highest-numbered 106 * register in that list, however, not all register numbers in this 107 * range are defined by the architecture, and not all defined registers 108 * will be present on every implementation of that architecture. 109 * Moreover, libunwind provides nice names for most, but not all 110 * registers, but these are hardcoded; a name being available does not 111 * mean that register is available. 112 * 113 * So, we have to pull this all together here. We try to get the value 114 * of every possible register. If we get a value for it, then the 115 * register must exist, and so we get its name. If libunwind has no 116 * name for it, we synthesize something. These cases should be rare, 117 * and they're usually for uninteresting or niche registers, so it 118 * shouldn't really matter. We can see the value, and that's the main 119 * thing. 120 */ 121 uint_t cols = 0; 122 for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) { 123 /* 124 * Get the value. Any error probably means the register 125 * doesn't exist, and we skip it. 126 */ 127 if (unw_get_reg(&cp, regnum, &v) < 0) 128 continue; 129 130 /* 131 * Register name. If libunwind doesn't have a name for it, 132 * it will return "???". As a shortcut, we just treat '?' 133 * is an alternate end-of-string character. 134 */ 135 const char *name = unw_regname(regnum); 136 for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {} 137 if (n == 0) { 138 /* 139 * No valid name, so make one of the form "?xx", where 140 * "xx" is the two-char hex of libunwind's register 141 * number. 142 */ 143 buf[0] = '?'; 144 n = spl_bt_u64_to_hex_str(regnum, 2, 145 &buf[1], sizeof (buf)-1) + 1; 146 name = buf; 147 } 148 149 /* 150 * Two spaces of padding before each column, plus extra 151 * spaces to align register names shorter than three chars. 152 */ 153 spl_bt_write_n(fd, " ", 5-MIN(n, 3)); 154 155 /* Register name and column punctuation */ 156 spl_bt_write_n(fd, name, n); 157 spl_bt_write(fd, ": 0x"); 158 159 /* 160 * Convert register value (from unw_get_reg()) to hex. We're 161 * assuming that all registers are 64-bits wide, which is 162 * probably fine for any general-purpose registers on any 163 * machine currently in use. A more generic way would be to 164 * look at the width of unw_word_t, but that would also 165 * complicate the column code a bit. This is fine. 166 */ 167 n = spl_bt_u64_to_hex_str(v, 16, buf, sizeof (buf)); 168 spl_bt_write_n(fd, buf, n); 169 170 /* Every third column, emit a newline */ 171 if (!(++cols % 3)) 172 spl_bt_write(fd, "\n"); 173 } 174 175 /* If we finished before the third column, emit a newline. */ 176 if (cols % 3) 177 spl_bt_write(fd, "\n"); 178 179 /* Now the main event, the backtrace. */ 180 spl_bt_write(fd, "Call trace:\n"); 181 182 /* Reset the cursor to the top again. */ 183 unw_init_local(&cp, &uc); 184 185 do { 186 /* 187 * Getting the IP should never fail; libunwind handles it 188 * specially, because its used a lot internally. Still, no 189 * point being silly about it, as the last thing we want is 190 * our crash handler to crash. So if it ever does fail, we'll 191 * show an error line, but keep going to the next frame. 192 */ 193 if (unw_get_reg(&cp, UNW_REG_IP, &v) < 0) { 194 spl_bt_write(fd, " [couldn't get IP register; " 195 "corrupt frame?]"); 196 continue; 197 } 198 199 /* IP & punctuation */ 200 n = spl_bt_u64_to_hex_str(v, 16, buf, sizeof (buf)); 201 spl_bt_write(fd, " [0x"); 202 spl_bt_write_n(fd, buf, n); 203 spl_bt_write(fd, "] "); 204 205 /* 206 * Function ("procedure") name for the current frame. `v` 207 * receives the offset from the named function to the IP, which 208 * we show as a "+offset" suffix. 209 * 210 * If libunwind can't determine the name, we just show "???" 211 * instead. We've already displayed the IP above; that will 212 * have to do. 213 * 214 * unw_get_proc_name() will return ENOMEM if the buffer is too 215 * small, instead truncating the name. So we treat that as a 216 * success and use whatever is in the buffer. 217 */ 218 err = unw_get_proc_name(&cp, buf, sizeof (buf), &v); 219 if (err == 0 || err == -UNW_ENOMEM) { 220 for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {} 221 spl_bt_write_n(fd, buf, n); 222 223 /* Offset from proc name */ 224 spl_bt_write(fd, "+0x"); 225 n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf)); 226 spl_bt_write_n(fd, buf, n); 227 } else 228 spl_bt_write(fd, "???"); 229 230 #ifdef HAVE_LIBUNWIND_ELF 231 /* 232 * Newer libunwind has unw_get_elf_filename(), which gets 233 * the name of the ELF object that the frame was executing in. 234 * Like `unw_get_proc_name()`, `v` recieves the offset within 235 * the file, and UNW_ENOMEM indicates that a truncate filename 236 * was left in the buffer. 237 */ 238 err = unw_get_elf_filename(&cp, buf, sizeof (buf), &v); 239 if (err == 0 || err == -UNW_ENOMEM) { 240 for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {} 241 spl_bt_write(fd, " (in "); 242 spl_bt_write_n(fd, buf, n); 243 244 /* Offset within file */ 245 spl_bt_write(fd, " +0x"); 246 n = spl_bt_u64_to_hex_str(v, 2, buf, sizeof (buf)); 247 spl_bt_write_n(fd, buf, n); 248 spl_bt_write(fd, ")"); 249 } 250 #endif 251 spl_bt_write(fd, "\n"); 252 } while (unw_step(&cp) > 0); 253 } 254 #elif defined(HAVE_BACKTRACE) 255 #include <execinfo.h> 256 257 void 258 libspl_backtrace(int fd) 259 { 260 void *btptrs[64]; 261 size_t nptrs = backtrace(btptrs, 64); 262 spl_bt_write(fd, "Call trace:\n"); 263 backtrace_symbols_fd(btptrs, nptrs, fd); 264 } 265 #else 266 void 267 libspl_backtrace(int fd __maybe_unused) 268 { 269 } 270 #endif 271