Skip to content

Commit 0a001f3

Browse files
robnbehlendorf
authored andcommitted
libspl/backtrace: dump registers in libunwind backtraces
More useful stuff, especially when trying to follow a disassembly. 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]> Closes #16653
1 parent 27e8f56 commit 0a001f3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

lib/libspl/backtrace.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,58 @@ libspl_backtrace(int fd)
6565
ssize_t ret __attribute__((unused));
6666
unw_context_t uc;
6767
unw_cursor_t cp;
68-
unw_word_t loc;
68+
unw_word_t v;
6969
char buf[128];
70-
size_t n;
70+
size_t n, c;
7171

72-
ret = write(fd, "Call trace:\n", 12);
7372
unw_getcontext(&uc);
73+
7474
unw_init_local(&cp, &uc);
75+
ret = write(fd, "Registers:\n", 11);
76+
c = 0;
77+
for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) {
78+
if (unw_get_reg(&cp, regnum, &v) < 0)
79+
continue;
80+
const char *name = unw_regname(regnum);
81+
for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}
82+
if (n == 0) {
83+
buf[0] = '?';
84+
n = libspl_u64_to_hex_str(regnum, 2,
85+
&buf[1], sizeof (buf)-1) + 1;
86+
name = buf;
87+
}
88+
ret = write(fd, " ", 5-MIN(n, 3));
89+
ret = write(fd, name, n);
90+
ret = write(fd, ": 0x", 4);
91+
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
92+
ret = write(fd, buf, n);
93+
if (!(++c % 3))
94+
ret = write(fd, "\n", 1);
95+
}
96+
if (c % 3)
97+
ret = write(fd, "\n", 1);
98+
99+
unw_init_local(&cp, &uc);
100+
ret = write(fd, "Call trace:\n", 12);
75101
while (unw_step(&cp) > 0) {
76-
unw_get_reg(&cp, UNW_REG_IP, &loc);
102+
unw_get_reg(&cp, UNW_REG_IP, &v);
77103
ret = write(fd, " [0x", 5);
78-
n = libspl_u64_to_hex_str(loc, 10, buf, sizeof (buf));
104+
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
79105
ret = write(fd, buf, n);
80106
ret = write(fd, "] ", 2);
81-
unw_get_proc_name(&cp, buf, sizeof (buf), &loc);
107+
unw_get_proc_name(&cp, buf, sizeof (buf), &v);
82108
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
83109
ret = write(fd, buf, n);
84110
ret = write(fd, "+0x", 3);
85-
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
111+
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
86112
ret = write(fd, buf, n);
87113
#ifdef HAVE_LIBUNWIND_ELF
88114
ret = write(fd, " (in ", 5);
89-
unw_get_elf_filename(&cp, buf, sizeof (buf), &loc);
115+
unw_get_elf_filename(&cp, buf, sizeof (buf), &v);
90116
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
91117
ret = write(fd, buf, n);
92118
ret = write(fd, " +0x", 4);
93-
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
119+
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
94120
ret = write(fd, buf, n);
95121
ret = write(fd, ")", 1);
96122
#endif

0 commit comments

Comments
 (0)