获取源代码函数调用堆栈

========

#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.youkuaiyun.com/wind19/article/details/6105617

=========

转载于:https://my.oschina.net/plp626/blog/200305

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值