Intercepting global functions with Valgrind on Linux using g++

511 views Asked by At

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

I am able to do the interception of global 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

1

There are 1 answers

0
osgx On

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.