#include #include void show_stack_by_direct_access(long * stack_p, int num) { long * cur_stack_p; int i; cur_stack_p = stack_p; for(i = 0; i < num; i++) { printf("0x%lx ", *cur_stack_p); cur_stack_p = cur_stack_p - 1; } } int third_function(int num, int num2) { unw_cursor_t cursor; unw_context_t context; unw_proc_info_t proc_info; int reg_ip_ok, reg_sp_ok, proc_name_ok; size_t name_size = 255; char name[255]; unw_word_t ip, sp; unw_word_t greg; printf("... the last parameters should be %i and %i.\n", num, num2); printf("Back tracing...\n\n"); unw_tdep_getcontext(&context); unw_init_local(&cursor, &context); do { proc_name_ok = unw_get_proc_name(&cursor, (char *)&name, name_size, NULL); if(proc_name_ok == 0) { printf("function %s(...)\n", name); reg_ip_ok = unw_get_reg(&cursor, UNW_REG_IP, &ip); reg_sp_ok = unw_get_reg(&cursor, UNW_REG_SP, &sp); if(reg_ip_ok == 0 && reg_sp_ok == 0) { printf("ip = 0x%lx, sp = 0x%lx\n", (long) ip, (long) sp); } else { printf("Error getting IP and/or SP register(s)."); } printf("stack: "); show_stack_by_direct_access((long *)sp, 8); printf(" ...\n\n"); } } while (unw_step(&cursor) > 0); printf("backtrace finished.\n\n"); } int second_function(int num, int num2) { third_function(num + 1, num2 * 2); } int first_function(int num) { second_function(num * 2, num + 1); } int main(int argc, char ** argv) { int pid; printf("Testing basic features of libunwind.\n"); first_function(2); return 0; }