Skip to content

Commit

Permalink
libspl/backtrace: dump registers in libunwind backtraces
Browse files Browse the repository at this point in the history
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 openzfs#16653
  • Loading branch information
robn authored and behlendorf committed Oct 21, 2024
1 parent 36a67b5 commit bcd61d9
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions lib/libspl/backtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,58 @@ libspl_backtrace(int fd)
ssize_t ret __attribute__((unused));
unw_context_t uc;
unw_cursor_t cp;
unw_word_t loc;
unw_word_t v;
char buf[128];
size_t n;
size_t n, c;

ret = write(fd, "Call trace:\n", 12);
unw_getcontext(&uc);

unw_init_local(&cp, &uc);
ret = write(fd, "Registers:\n", 11);
c = 0;
for (uint_t regnum = 0; regnum <= UNW_TDEP_LAST_REG; regnum++) {
if (unw_get_reg(&cp, regnum, &v) < 0)
continue;
const char *name = unw_regname(regnum);
for (n = 0; name[n] != '\0' && name[n] != '?'; n++) {}
if (n == 0) {
buf[0] = '?';
n = libspl_u64_to_hex_str(regnum, 2,
&buf[1], sizeof (buf)-1) + 1;
name = buf;
}
ret = write(fd, " ", 5-MIN(n, 3));
ret = write(fd, name, n);
ret = write(fd, ": 0x", 4);
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
ret = write(fd, buf, n);
if (!(++c % 3))
ret = write(fd, "\n", 1);
}
if (c % 3)
ret = write(fd, "\n", 1);

unw_init_local(&cp, &uc);
ret = write(fd, "Call trace:\n", 12);
while (unw_step(&cp) > 0) {
unw_get_reg(&cp, UNW_REG_IP, &loc);
unw_get_reg(&cp, UNW_REG_IP, &v);
ret = write(fd, " [0x", 5);
n = libspl_u64_to_hex_str(loc, 10, buf, sizeof (buf));
n = libspl_u64_to_hex_str(v, 18, buf, sizeof (buf));
ret = write(fd, buf, n);
ret = write(fd, "] ", 2);
unw_get_proc_name(&cp, buf, sizeof (buf), &loc);
unw_get_proc_name(&cp, buf, sizeof (buf), &v);
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
ret = write(fd, buf, n);
ret = write(fd, "+0x", 3);
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
ret = write(fd, buf, n);
#ifdef HAVE_LIBUNWIND_ELF
ret = write(fd, " (in ", 5);
unw_get_elf_filename(&cp, buf, sizeof (buf), &loc);
unw_get_elf_filename(&cp, buf, sizeof (buf), &v);
for (n = 0; n < sizeof (buf) && buf[n] != '\0'; n++) {}
ret = write(fd, buf, n);
ret = write(fd, " +0x", 4);
n = libspl_u64_to_hex_str(loc, 2, buf, sizeof (buf));
n = libspl_u64_to_hex_str(v, 2, buf, sizeof (buf));
ret = write(fd, buf, n);
ret = write(fd, ")", 1);
#endif
Expand Down

0 comments on commit bcd61d9

Please sign in to comment.