Skip to content

Commit c7e47b3

Browse files
robnbehlendorf
authored andcommitted
libspl/backtrace: helper macros for output
My eyes are going blurry looking at all those write calls. This is much nicer. Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Tino Reichardt <[email protected]> Signed-off-by: Rob Norris <[email protected]> Close #16653
1 parent 0a001f3 commit c7e47b3

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

lib/libspl/backtrace.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525

2626
#include <sys/backtrace.h>
2727
#include <sys/types.h>
28+
#include <sys/debug.h>
2829
#include <unistd.h>
2930

3031
/*
31-
* libspl_backtrace() must be safe to call from inside a signal hander. This
32-
* mostly means it must not allocate, and so we can't use things like printf.
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.
3338
*/
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))
3442

3543
#if defined(HAVE_LIBUNWIND)
3644
#define UNW_LOCAL_ONLY
@@ -62,7 +70,6 @@ libspl_u64_to_hex_str(uint64_t v, size_t digits, char *buf, size_t buflen)
6270
void
6371
libspl_backtrace(int fd)
6472
{
65-
ssize_t ret __attribute__((unused));
6673
unw_context_t uc;
6774
unw_cursor_t cp;
6875
unw_word_t v;
@@ -72,7 +79,7 @@ libspl_backtrace(int fd)
7279
unw_getcontext(&uc);
7380

7481
unw_init_local(&cp, &uc);
75-
ret = write(fd, "Registers:\n", 11);
82+
spl_bt_write(fd, "Registers:\n");
7683
c = 0;
7784
for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) {
7885
if (unw_get_reg(&cp, regnum, &v) < 0)
@@ -85,42 +92,42 @@ libspl_backtrace(int fd)
8592
&buf[1], sizeof (buf)-1) + 1;
8693
name = buf;
8794
}
88-
ret = write(fd, " ", 5-MIN(n, 3));
89-
ret = write(fd, name, n);
90-
ret = write(fd, ": 0x", 4);
95+
spl_bt_write_n(fd, " ", 5-MIN(n, 3));
96+
spl_bt_write_n(fd, name, n);
97+
spl_bt_write(fd, ": 0x");
9198
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
92-
ret = write(fd, buf, n);
99+
spl_bt_write_n(fd, buf, n);
93100
if (!(++c % 3))
94-
ret = write(fd, "\n", 1);
101+
spl_bt_write(fd, "\n");
95102
}
96103
if (c % 3)
97-
ret = write(fd, "\n", 1);
104+
spl_bt_write(fd, "\n");
98105

99106
unw_init_local(&cp, &uc);
100-
ret = write(fd, "Call trace:\n", 12);
107+
spl_bt_write(fd, "Call trace:\n");
101108
while (unw_step(&cp) > 0) {
102109
unw_get_reg(&cp, UNW_REG_IP, &v);
103-
ret = write(fd, " [0x", 5);
110+
spl_bt_write(fd, " [0x");
104111
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
105-
ret = write(fd, buf, n);
106-
ret = write(fd, "] ", 2);
112+
spl_bt_write_n(fd, buf, n);
113+
spl_bt_write(fd, "] ");
107114
unw_get_proc_name(&cp, buf, sizeof (buf), &v);
108115
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
109-
ret = write(fd, buf, n);
110-
ret = write(fd, "+0x", 3);
116+
spl_bt_write_n(fd, buf, n);
117+
spl_bt_write(fd, "+0x");
111118
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
112-
ret = write(fd, buf, n);
119+
spl_bt_write_n(fd, buf, n);
113120
#ifdef HAVE_LIBUNWIND_ELF
114-
ret = write(fd, " (in ", 5);
121+
spl_bt_write(fd, " (in ");
115122
unw_get_elf_filename(&cp, buf, sizeof (buf), &v);
116123
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
117-
ret = write(fd, buf, n);
118-
ret = write(fd, " +0x", 4);
124+
spl_bt_write_n(fd, buf, n);
125+
spl_bt_write(fd, " +0x");
119126
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
120-
ret = write(fd, buf, n);
121-
ret = write(fd, ")", 1);
127+
spl_bt_write_n(fd, buf, n);
128+
spl_bt_write(fd, ")");
122129
#endif
123-
ret = write(fd, "\n", 1);
130+
spl_bt_write(fd, "\n");
124131
}
125132
}
126133
#elif defined(HAVE_BACKTRACE)
@@ -129,15 +136,12 @@ libspl_backtrace(int fd)
129136
void
130137
libspl_backtrace(int fd)
131138
{
132-
ssize_t ret __attribute__((unused));
133139
void *btptrs[64];
134140
size_t nptrs = backtrace(btptrs, 64);
135-
ret = write(fd, "Call trace:\n", 12);
141+
spl_bt_write(fd, "Call trace:\n");
136142
backtrace_symbols_fd(btptrs, nptrs, fd);
137143
}
138144
#else
139-
#include <sys/debug.h>
140-
141145
void
142146
libspl_backtrace(int fd __maybe_unused)
143147
{

0 commit comments

Comments
 (0)