1,简介示例
1.1,代码先行
获取并打印函数调用堆栈信息,仅实现与 glibc中backtrace系列相同的功能,这个示例多了获取寄存器的值,结合glibc 的 backtrace 将比较容易理解,
hello_libunwind.c
            
            
              cpp
              
              
            
          
          #include<stdio.h>
#define UNW_LOCAL_ONLY
#include<libunwind.h>
void do_unwind()
{
	unw_cursor_t cursor;
	unw_context_t context;
	unw_getcontext(&context);
	unw_init_local(&cursor, &context);
	while(unw_step(&cursor)>0)
	{
		unw_word_t offset, ip, eax, ebx, ecx, edx;
		char func_sym[64];
		
		
		unw_get_reg(&cursor, UNW_REG_IP, &ip);
		unw_get_reg(&cursor, UNW_X86_64_RAX, &eax);
		unw_get_reg(&cursor, UNW_X86_64_RBX, &ebx);
		unw_get_reg(&cursor, UNW_X86_64_RCX, &ecx);
		unw_get_reg(&cursor, UNW_X86_64_RDX, &edx);
		func_sym[0] = '\0';
		(void) unw_get_proc_name(&cursor, func_sym, sizeof(func_sym), &offset);
		printf("%p : (%s+0x%x) [%p] ", ip, func_sym, offset, ip);
		printf("EAX=0X%08X EBX=0X%08X ECX=0X%08X EDX=0X%08X\n", eax, ebx, ecx, edx);
		printf("");
	}
}
void xxx()
{
}
void aaa()
{
	xxx();
	do_unwind();
}
void bbb()
{
	xxx();
	xxx();
	aaa();
}
void ccc()
{
	xxx();
	xxx();
	xxx();
	bbb();
}
int main()
{
	ccc();
	
	return 0;
}
        1.2,编译运行
Makefile
            
            
              cpp
              
              
            
          
          hello_libunwind: 
%: %.c
	gcc $< -o $@  -lunwind 
        $ make
运行:
$ ./hello_libunwind
效果:
