|
37 | 37 | #include <errno.h> |
38 | 38 | #ifdef __GLIBC__ |
39 | 39 | #include <execinfo.h> |
| 40 | +#elif defined(HAVE_LIBUNWIND) |
| 41 | +#define UNW_LOCAL_ONLY |
| 42 | +#include <libunwind.h> |
40 | 43 | #endif |
41 | 44 | #include <fcntl.h> |
42 | 45 | #include <getopt.h> |
@@ -176,6 +179,24 @@ void print_backtrace (void) { |
176 | 179 | kwrite (2, "\n------- Stack Backtrace -------\n", 33); |
177 | 180 | backtrace_symbols_fd (buffer, nptrs, 2); |
178 | 181 | kwrite (2, "-------------------------------\n", 32); |
| 182 | +#elif defined(HAVE_LIBUNWIND) |
| 183 | + kwrite (2, "\n------- Stack Backtrace -------\n", 33); |
| 184 | + unw_cursor_t cursor; |
| 185 | + unw_context_t context; |
| 186 | + unw_getcontext (&context); |
| 187 | + unw_init_local (&cursor, &context); |
| 188 | + while (unw_step (&cursor) > 0) { |
| 189 | + unw_word_t offset, pc; |
| 190 | + char fname[128]; |
| 191 | + unw_get_reg (&cursor, UNW_REG_IP, &pc); |
| 192 | + fname[0] = '\0'; |
| 193 | + unw_get_proc_name (&cursor, fname, sizeof (fname), &offset); |
| 194 | + char line[256]; |
| 195 | + int len = snprintf (line, sizeof (line), " %s (+0x%lx) [0x%lx]\n", |
| 196 | + fname[0] ? fname : "???", (long)offset, (long)pc); |
| 197 | + if (len > 0) kwrite (2, line, len); |
| 198 | + } |
| 199 | + kwrite (2, "-------------------------------\n", 32); |
179 | 200 | #else |
180 | 201 | kwrite (2, "\n(stack trace unavailable on musl)\n", 35); |
181 | 202 | #endif |
|
0 commit comments