| 13 | namespace crash_handler_libunwind { |
| 14 | |
| 15 | inline void print_stacktrace_libunwind() { |
| 16 | // Use libunwind for better stack traces |
| 17 | unw_cursor_t cursor; |
| 18 | unw_context_t context; |
| 19 | |
| 20 | unw_getcontext(&context); |
| 21 | unw_init_local(&cursor, &context); |
| 22 | |
| 23 | int depth = 0; |
| 24 | printf("Stack trace (libunwind):\n"); |
| 25 | while (unw_step(&cursor) > 0) { |
| 26 | unw_word_t offset, pc; |
| 27 | char sym[256]; |
| 28 | |
| 29 | unw_get_reg(&cursor, UNW_REG_IP, &pc); |
| 30 | if (pc == 0) { |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | printf("#%-2d 0x%lx:", depth++, pc); |
| 35 | |
| 36 | if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { |
| 37 | printf(" (%s+0x%lx)\n", sym, offset); |
| 38 | } else { |
| 39 | printf(" -- symbol not found\n"); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | inline void crash_handler(int sig) { |
| 45 | // Prevent recursion if handler crashes |
no test coverage detected