Skip to content

Instantly share code, notes, and snippets.

@moonheart08
Created February 21, 2018 19:01
Show Gist options
  • Save moonheart08/733415ae14eb1d4cc1079ab3b51c293f to your computer and use it in GitHub Desktop.
Save moonheart08/733415ae14eb1d4cc1079ab3b51c293f to your computer and use it in GitHub Desktop.
void print_backtrace() {
unw_cursor_t cursor; unw_context_t uc;
unw_word_t ip, sp, offset;
char buffer[1024]; // Lets make sure there's lots of space.
unw_getcontext(&uc);
unw_init_local(&cursor, &uc);
while (unw_step(&cursor) > 0) {
unw_get_reg(&cursor, UNW_REG_IP, &ip);
unw_get_reg(&cursor, UNW_REG_SP, &sp);
if (!unw_get_proc_name(&cursor, buffer, 1024, &offset)) {
printf("%s (Address: %p, Stack: %lx)\n", buffer, (void*) ip, (long) sp);
} else {
printf("Failed to symbolize (Address: %p, Stack: %lx)\n", (void*) ip, (long) sp);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment