#define UNW_LOCAL_ONLY #include #include #include // Function to print the current stack trace void show_backtrace(void) { unw_cursor_t cursor; unw_context_t context; unw_word_t ip, sp; char sym[256]; unw_word_t offset; // Initialize cursor to current frame for local unwinding unw_getcontext(&context); unw_init_local(&cursor, &context); printf("--- Stack Trace ---\n"); // Unwind frames one by one while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); // Try to get the function name if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { printf("0x%lx: (%s+0x%lx)\n", (long)ip, sym, (long)offset); } else { printf("0x%lx: -- no symbol name found --\n", (long)ip); } } printf("-------------------\n"); } void function_c(void) { show_backtrace(); } void function_b(void) { function_c(); } void function_a(void) { function_b(); } int main(int argc, char **argv) { function_a(); return 0; }