Created
June 15, 2023 05:30
-
-
Save tanishiking/59bde50f19610cb9fe370112d28dd61e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libunwind.h> | |
#include <stdio.h> | |
// Call this function to get a backtrace. | |
void backtrace() { | |
unw_cursor_t cursor; | |
unw_context_t context; | |
// Initialize cursor to current frame for local unwinding. | |
unw_getcontext(&context); | |
unw_init_local(&cursor, &context); | |
// Unwind frames one by one, going up the frame stack. | |
while (unw_step(&cursor) > 0) { | |
unw_word_t offset, pc; | |
unw_get_reg(&cursor, UNW_REG_IP, &pc); | |
if (pc == 0) { | |
break; | |
} | |
printf("0x%lx:", pc); | |
char sym[256]; | |
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { | |
printf(" (%s+0x%lx)\n", sym, offset); | |
} else { | |
printf(" -- error: unable to obtain symbol name for this frame\n"); | |
} | |
} | |
} | |
void foo() { | |
backtrace(); // <-------- backtrace here! | |
} | |
void bar() { | |
foo(); | |
} | |
int main(int argc, char **argv) { | |
bar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment