无需重新编译主程序即可对共享对象进行配置

Thi*_*ilo 5 c++ postgresql profiling shared-libraries callgrind

我目前正在开发一个用于加载到PostgreSQL的共享lilbrary(作为C语言函数,请参见此处).现在我想在不重新编译PostgreSQL本身的情况下分析这个库中的函数.

我尝试使用callgrind

valgrind --tool=callgrind path/to/postgres arguments-to-postgres
Run Code Online (Sandbox Code Playgroud)

这给了我PostgreSQL本身的分析信息,但无法记录我感兴趣的共享库.

我也试过sprof,但我不知道如何让那个工作.

任何想法都会受到高度关注.

PS:请不要建议只在调试器中暂停应用程序.功能运行时间低于0.01秒我需要更多详细结果.

Gea*_*phy 2

使用 callgrind 应该可以按预期工作。为了测试这一点,我使用简单的库和主函数Makefile 设置了一个简单的项目:

CFLAGS=-fpic
exe:exe.o lib.so
        cc -o exe exe.o lib.so
lib.so:lib.o
        cc -shared lib.o -o lib.so
clean:
        rm -f exe lib.so *.o
Run Code Online (Sandbox Code Playgroud)

lib.c 是一个简单的库,包含 2 个函数:

#include <stdio.h>
void someOtherFunction() { printf("someOtherFunction\n"); }
void someFunction() { printf("someFunction\n"); someOtherFunction(); }
Run Code Online (Sandbox Code Playgroud)

exe.c 是一个非常简单的可执行文件:

int someFunction();
void main() { someFunction(); }
Run Code Online (Sandbox Code Playgroud)

使用 Makefile 构建可执行文件并使用 valgrind 运行它,如下所示:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD valgrind --tool=callgrind ./exe
Run Code Online (Sandbox Code Playgroud)

如果检查 callgrind 输出,您将在共享库中找到这两个函数的分析数据。如果您看不到这些功能,您可能正在使用不支持此功能的非标准环境。我使用的是 Linux Mint 11 x64,带有最新的补丁。