linux - 使用 g++ 在 Linux 上使用 Valgrind 拦截全局函数

标签 linux gcc g++ valgrind interception

根据 their example,我正在尝试使用 Valgrind 拦截函数.

我可以在使用 gcc 构建时拦截全局函数,但是当我使用 g++ 编译相同的代码时,拦截不起作用。

我应该指定的编译器标志有什么特别之处吗?

这是我的示例应用:

#include <stdio.h>
#include "valgrind.h"

__attribute__ ((noinline))
void foo()
{
    printf("inside foo\n");
}

void I_WRAP_SONAME_FNNAME_ZU(NONE,foo)()
{
   OrigFn fn;
   VALGRIND_GET_ORIG_FN(fn);
   printf("*** Before foo()\n");
   CALL_FN_v_v(fn);
   printf("*** After foo()\n");
}

int main()
{
    foo();
    return 0;
}

使用 GCC 编译时,输出为:

*** Before foo()
inside foo
*** After foo()

但是当用g++编译时,输出只是

inside foo

最佳答案

G++ 在没有 extern "C" 的情况下对函数进行名称重整。所以你应该找到一个错位的名称(例如使用 nm object)并在你的 valgrind 代码中使用它。或者您可以重写目标程序以使用 extern "C" 函数。

关于linux - 使用 g++ 在 Linux 上使用 Valgrind 拦截全局函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5056165/

相关文章:

c++ - 哪个库包含 regexec()、regfree()、regcomp()?

linux - 用于切换用户的 Bash 脚本

python - 在 Linux 中使用 python 正则表达式未检测到表情符号

linux - 用awk实现tail

c++ - 使用 g++ 链接编译指示

C++文件编译: -L and -I arguments don't work for boost library

linux - 使用 watch 每 N 秒更新一次在 linux 中显示的图像失败

c++ - Visual Studio 中的 gcc -Wall -pedantic -Wextra

c++ - 是否可以在 gcc 中关闭对 "and"/"or" boolean 运算符使用的支持?

c - GCC C编译器警告 "warning: control reaches..."