栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

跟踪Linux中本地函数调用的工具

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

跟踪Linux中本地函数调用的工具

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

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

给定

#include <iostream>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 facBreakpoint 1 at 0x804866a: file test.cpp, line 4.(gdb) commands 1Type commands for when breakpoint 1 is hit, one per line.End with a line saying just "end".>silent>bt 1>c>end(gdb) runStarting program: /home/js/cpp/a.out#0  fac (n=0) at test.cpp:41#0  fac (n=1) at test.cpp:4#0  fac (n=0) at test.cpp:41#0  fac (n=2) at test.cpp:4#0  fac (n=1) at test.cpp:4#0  fac (n=0) at test.cpp:42#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:46Program exited normally.(gdb)

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

tmp=$(mktemp)readelf -s ./a.out | gawk '{   if($4 == "FUNC" && $2 != 0) {     print "# pre 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(

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399045.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号