gprof:如何为链接到主程序的共享库中的函数生成调用图

发布于 2024-08-13 11:09:21 字数 437 浏览 3 评论 0原文

我正在Linux环境下工作。我有两个“C”源代码包 train 和 test_train。

  1. train 包编译时会生成 libtrain.so
  2. test_train 链接到 libtrain.so 并生成可执行的 train-test

现在我想使用 gprof 生成一个调用图,它显示主程序中的函数调用顺序以及

我正在编译的 libtrain.so 中的函数调用顺序并使用 -pg 选项链接这两个包,调试级别为 o0。 在我执行 ./train-test 后,生成 gmon.out 。然后我这样做:

$ gprof -q ./train-test gmon.out

这里,输出显示了 train-test 中函数的调用图,但不在 libtrain.so 中,

这可能是什么问题?

I am working on Linux environment. I have two 'C' source packages train and test_train.

  1. train package when compiled generates libtrain.so
  2. test_train links to libtrain.so and generates executable train-test

Now I want to generate a call graph using gprof which shows calling sequence of functions in main program as well as those inside libtrain.so

I am compiling and linking both packages with -pg option and debugging level is o0.
After I do ./train-test , gmon.out is generated. Then I do:

$ gprof -q ./train-test gmon.out

Here, output shows call graph of functions in train-test but not in libtrain.so

What could be the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别挽留 2024-08-20 11:09:21

gprof 不起作用,您需要使用 sprof 代替。我发现这些链接很有帮助:

第二个链接的摘要:

  1. 在调试 (-g) 模式下编译共享库 (libmylib.so)。没有-pg。
  2. export LD_PROFILE_OUTPUT=`pwd`
  3. export LD_PROFILE=libmylib.so
  4. rm -f $LD_PROFILE.profile
  5. 执行加载 libmylib.so 的程序
  6. sprof PATH-TO-LIB/$LD_PROFILE $LD_PROFILE.profile -p >log
  7. 查看日志。

我发现在第 2 步中,它必须是一个现有目录——否则您会收到有用的警告。在第 3 步中,您可能需要将库指定为 libmylib.so.X(甚至可能是 .XY,不确定)——否则您不会收到任何警告。

gprof won't work, you need to use sprof instead. I found these links helpful:

Summary from the 2nd link:

  1. Compile your shared library (libmylib.so) in debug (-g) mode. No -pg.
  2. export LD_PROFILE_OUTPUT=`pwd`
  3. export LD_PROFILE=libmylib.so
  4. rm -f $LD_PROFILE.profile
  5. execute your program that loads libmylib.so
  6. sprof PATH-TO-LIB/$LD_PROFILE $LD_PROFILE.profile -p >log
  7. See the log.

I found that in step 2, it needs to be an existing directory -- otherwise you get a helpful warning. And in step 3, you might need to specify the library as libmylib.so.X (maybe even .X.Y, not sure) -- otherwise you get no warning whatsoever.

倚栏听风 2024-08-20 11:09:21

我正在从 Python 加载我的库,但 sprof 没有任何运气。相反,我使用了 oprofile,它至少位于 Fedora 存储库中:

operf --callgraph /path/to/mybinary

等待您的应用程序完成或执行 Ctl-c 停止分析。现在让我们生成一个配置文件摘要:

opreport --callgraph --symbols

请参阅 文档来解释它。有点乱。在生成的报告中,每个符号都列在其自己的块中。该块的主要符号是未缩进的符号。它上面的项目是调用该函数的函数,下面的项目是被该函数调用的东西。下面部分中的百分比是它在这些被调用者中花费的相对时间量。

I'm loading my library from Python and didn't have any luck with sprof. Instead, I used oprofile, which was in the Fedora repositories, at least:

operf --callgraph /path/to/mybinary

Wait for your application to finish or do Ctl-c to stop profiling. Now let's generate a profile summary:

opreport --callgraph --symbols

See the documentation to interpret it. It's kind of a mess. In the generated report, each symbol is listed in a block of its own. The block's main symbol is the one that's not indented. The items above it are functions that call that function, and the ones below it are the things that get called by it. The percentages in the below section are the relative amount of time it spent in those callees.

傲世九天 2024-08-20 11:09:21

如果您不在 Linux 上(就像我在 Solaris 上一样),那您就运气不好了,因为那里没有 sprof
如果您有库的源代码,您可以通过链接静态库并使用该静态库制作分析二进制文件来解决您的问题。
我设法跟踪对共享库的调用的另一种方法是使用 truss。使用选项 -u [!]lib,...:[:][!]func, ... 可以很好地了解一次运行的调用历史记录。它与分析并不完全相同,但在某些情况下非常有用。

If you're not on Linux (like me on Solaris) you simply out of luck as there is no sprof there.
If you have the sources of your library you can solve your problem by linking a static library and making your profiling binary with that one instead.
Another way I manage to trace calls to shared libraries, is by using truss. With the option -u [!]lib,...:[:][!]func, ... one can get a good picture of the call history of a run. It's not completely the same as profiling but can be very usefull in some scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文