linux 查看函数调用,跟踪Linux中本地函数调用的工具

假设只希望收到有关特定功能的通知,可以这样进行:

使用调试信息进行编译(由于您已经具有符号信息,因此您可能还具有足够的调试功能)

给定

#include

int fac(int n) {

if(n == 0)

return 1;

return n * fac(n-1);

}

int main()

{

for(int i=0;i<4;i++)

std::cout << fac(i) << std::endl;

}

使用gdb跟踪:

[js@HOST2 cpp]$ g++ -g3 test.cpp

[js@HOST2 cpp]$ gdb ./a.out

(gdb) b fac

Breakpoint 1 at 0x804866a: file test.cpp, line 4.

(gdb) commands 1

Type commands for when breakpoint 1 is hit, one per line.

End with a line saying just "end".

>silent

>bt 1

>c

>end

(gdb) run

Starting program: /home/js/cpp/a.out

#0  fac (n=0) at test.cpp:4

1

#0  fac (n=1) at test.cpp:4

#0  fac (n=0) at test.cpp:4

1

#0  fac (n=2) at test.cpp:4

#0  fac (n=1) at test.cpp:4

#0  fac (n=0) at test.cpp:4

2

#0  fac (n=3) at test.cpp:4

#0  fac (n=2) at test.cpp:4

#0  fac (n=1) at test.cpp:4

#0  fac (n=0) at test.cpp:4

6

Program exited normally.

(gdb)

这是我收集所有函数地址的方法:

tmp=$(mktemp)

readelf -s ./a.out | gawk '

{

if($4 == "FUNC" && $2 != 0) {

print "# code for " $NF;

print "b *0x" $2;

print "commands";

print "silent";

print "bt 1";

print "c";

print "end";

print "";

}

}' > $tmp;

gdb --command=$tmp ./a.out;

rm -f $tmp

请注意,除了打印当前frame(bt 1)之外,您还可以执行任何操作,打印某些全局值,执行一些shell命令,或者在命中该fatal_bomb_exploded函数时发送邮件:)不幸的是,gcc输出了一些“当前语言已更改”消息之间。但这很容易被窃听。没什么大不了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值