Fix gcc uninitialized warning in FreeBSD zio_crypt.c
[zfs.git] / lib / libspl / backtrace.c
blob6e8b3b12122de17059727dce65ae28899c96f9ac
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2024, Rob Norris <robn@despairlabs.com>
23 * Copyright (c) 2024, Klara Inc.
26 #include <sys/backtrace.h>
27 #include <sys/types.h>
28 #include <sys/debug.h>
29 #include <unistd.h>
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.
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))
43 #if defined(HAVE_LIBUNWIND)
44 #define UNW_LOCAL_ONLY
45 #include <libunwind.h>
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.
52 static size_t
53 spl_bt_u64_to_hex_str(uint64_t v, size_t n, char *buf, size_t buflen)
55 static const char hexdigits[] = {
56 '0', '1', '2', '3', '4', '5', '6', '7',
57 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
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;
72 return (pos);
75 void
76 libspl_backtrace(int fd)
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;
85 /* Snapshot the current frame and state. */
86 unw_getcontext(&uc);
89 * TODO: walk back to the frame that tripped the assertion / the place
90 * where the signal was recieved.
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.
98 spl_bt_write(fd, "Registers:\n");
100 /* Initialise a frame cursor, starting at the current frame */
101 unw_init_local(&cp, &uc);
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.
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.
121 uint_t cols = 0;
122 for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) {
124 * Get the value. Any error probably means the register
125 * doesn't exist, and we skip it.
127 if (unw_get_reg(&cp, regnum, &v) < 0)
128 continue;
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.
135 const char *name = unw_regname(regnum);
136 for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}
137 if (n == 0) {
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.
143 buf[0] = '?';
144 n = spl_bt_u64_to_hex_str(regnum, 2,
145 &buf[1], sizeof (buf)-1) + 1;
146 name = buf;
150 * Two spaces of padding before each column, plus extra
151 * spaces to align register names shorter than three chars.
153 spl_bt_write_n(fd, " ", 5-MIN(n, 3));
155 /* Register name and column punctuation */
156 spl_bt_write_n(fd, name, n);
157 spl_bt_write(fd, ": 0x");
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.
167 n = spl_bt_u64_to_hex_str(v, 16, buf, sizeof (buf));
168 spl_bt_write_n(fd, buf, n);
170 /* Every third column, emit a newline */
171 if (!(++cols % 3))
172 spl_bt_write(fd, "\n");
175 /* If we finished before the third column, emit a newline. */
176 if (cols % 3)
177 spl_bt_write(fd, "\n");
179 /* Now the main event, the backtrace. */
180 spl_bt_write(fd, "Call trace:\n");
182 /* Reset the cursor to the top again. */
183 unw_init_local(&cp, &uc);
185 do {
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.
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;
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, "] ");
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.
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.
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.
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);
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, "???");
230 #ifdef HAVE_LIBUNWIND_ELF
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.
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);
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, ")");
250 #endif
251 spl_bt_write(fd, "\n");
252 } while (unw_step(&cp) > 0);
254 #elif defined(HAVE_BACKTRACE)
255 #include <execinfo.h>
257 void
258 libspl_backtrace(int fd)
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);
265 #else
266 void
267 libspl_backtrace(int fd __maybe_unused)
270 #endif