获取源代码函数调用堆栈

========

#include <stdio.h>
#include <execinfo.h>

static int level = 1;
void __attribute__((__no_instrument_function__))

__cyg_profile_func_enter(void *this_func, void *call_site)
{
    printf("pushd %d: \n", level++);
    backtrace_symbols_fd(&this_func, 1, 1);
}
    void __attribute__((__no_instrument_function__))
__cyg_profile_func_exit(void *this_func, void *call_site)
{
    printf("popd  %d: \n", --level);
    backtrace_symbols_fd(&this_func, 1, 1);
}



////////////////////////////////////////////////
void xputs(char *s)
{
    puts(s);
}
main()
{
    xputs("Hello World!");
    return 0;
}


zjw@ubuntu:~/zzz/include$ gcc a.c -rdynamic -finstrument-functions
zjw@ubuntu:~/zzz/include$ ./a.out
pushd 1:
./a.out(main+0x0)[0x80487a7]
pushd 2:
./a.out(xputs+0x0)[0x804876e]
Hello World!
popd  2:
./a.out(xputs+0x0)[0x804876e]
popd  1:
./a.out(main+0x0)[0x80487a7]


参考 http://blog.csdn.net/wind19/article/details/6105617

=========

你可能感兴趣的:(获取源代码函数调用堆栈)