Created
November 12, 2012 04:10
-
-
Save DanielVartanov/4057467 to your computer and use it in GitHub Desktop.
gcc sample.c -o sample -lunwind
This file contains hidden or 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 <stdio.h> | |
#define UNW_LOCAL_ONLY | |
#include <libunwind.h> | |
int print_backtrace() { | |
unw_context_t unwind_context; | |
unw_cursor_t unwind_cursor; | |
unw_getcontext(&unwind_context); | |
unw_init_local(&unwind_cursor, &unwind_context); | |
while (unw_step(&unwind_cursor) > 0) { | |
char proc_name[255]; | |
unw_word_t ip, sp, proc_offset; | |
unw_get_reg(&unwind_cursor, UNW_REG_IP, &ip); | |
unw_get_reg(&unwind_cursor, UNW_REG_SP, &sp); | |
unw_get_proc_name(&unwind_cursor, proc_name, 255, &proc_offset); | |
printf ("[ip = %lx, sp = %lx] %s at %lx\n", (long) ip, (long) sp, proc_name, (long) proc_offset); | |
}; | |
} | |
void three() { | |
print_backtrace(); | |
} | |
void two() { | |
three(); | |
} | |
void one() { | |
two(); | |
} | |
int main() { | |
one(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment