猿问

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

我正在寻找可以跟踪可执行文件中本地定义的函数的ltrace或strace之类的工具。ltrace仅跟踪动态库调用,而strace仅跟踪系统调用。例如,给定以下C程序:


#include <stdio.h>


int triple ( int x )

{

  return 3 * x;

}


int main (void)

{

  printf("%d\n", triple(10));

  return 0;

}

使用来运行程序ltrace将显示对的调用,printf因为这是标准库函数(在我的系统上是动态库),并且strace将显示启动代码,用于实现printf的系统调用以及关闭代码的所有系统调用。 ,但是我想要让我知道该函数triple已被调用的东西。假设优化编译器未内联局部函数,并且二进制文件未剥离(删除符号),是否有工具可以做到这一点?


编辑


一些澄清:


如果该工具还提供非本地功能的跟踪信息,也可以。

我不想在支持特定工具的情况下重新编译程序,可执行文件中的符号信息就足够了。

如果可以像使用ltrace / strace一样使用该工具附加到现有进程,我将非常好。


动漫人物
浏览 619回答 3
3回答

www说

System Tap可以在现代的Linux机器(Fedora 10,RHEL 5等)上使用。首先下载para-callgraph.stp脚本。然后运行:$ sudo stap para-callgraph.stp 'process("/bin/ls").function("*")' -c /bin/ls0&nbsp; &nbsp; ls(12631):->main argc=0x1 argv=0x7fff1ec3b038276&nbsp; ls(12631): ->human_options spec=0x0 opts=0x61a28c block_size=0x61a290365&nbsp; ls(12631): <-human_options return=0x0496&nbsp; ls(12631): ->clone_quoting_options o=0x0657&nbsp; ls(12631):&nbsp; ->xmemdup p=0x61a600 s=0x28815&nbsp; ls(12631):&nbsp; &nbsp;->xmalloc n=0x28908&nbsp; ls(12631):&nbsp; &nbsp;<-xmalloc return=0x1efe540950&nbsp; ls(12631):&nbsp; <-xmemdup return=0x1efe540990&nbsp; ls(12631): <-clone_quoting_options return=0x1efe5401030 ls(12631): ->get_quoting_style o=0x1efe540

茅侃侃

假设您可以使用gcc选项重新编译(无需更改源代码)要跟踪的代码-finstrument-functions,则可以使用etrace来获取函数调用图。输出如下所示:\-- main|&nbsp; &nbsp;\-- Crumble_make_apple_crumble|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy_stuff|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_buy|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_prepare_apples|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_skin_and_dice|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_mix|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_finalize|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_put|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_put|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_cook|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_put|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;\-- Crumble_bake在Solaris上,truss(等效于strace)可以过滤要跟踪的库。当我发现strace不具备这种功能时,我感到很惊讶。
随时随地看视频慕课网APP
我要回答