开发者

Intercepting global functions with Valgrind on Linux using g++

开发者 https://www.devze.com 2023-02-12 17:54 出处:网络
I\'m trying to intercept a function with Valgrind, according to their example. I am able to do the interception of glo开发者_运维技巧bal function when building with gcc, however when I compile the sa

I'm trying to intercept a function with Valgrind, according to their example.

I am able to do the interception of glo开发者_运维技巧bal function when building with gcc, however when I compile the same code with g++, interception doesn't work.

Is there anything special in the compiler flags I should specify?

Here's my sample app:

#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;
}

When compiled with GCC, the output is:

*** Before foo()

inside foo

*** After foo()

However when compiled with g++, the output is simply

inside foo


G++ does a name mangling for function without extern "C". So you should to find a mangled name (e.g. with nm object) and use it in your valgrind code. Or you can rewrite your target program to use an extern "C" function.

0

精彩评论

暂无评论...
验证码 换一张
取 消