4

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

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
  • 2
    On my system, when compiling with C++, I get an object name of `_Z3foov` for the function. (C++ [mangles](http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B) C names.) (Use `readelf -s file` to dump the symbol table.) *But*, I couldn't get the correct result by simply replacing `foo` with `_Z3foov` in my program. So I'm very confused. Thanks for showing me this, though. :) – sarnold Feb 20 '11 at 09:35
  • Have you tried `extern "C"`-ing the function wrapper? There may be an issue because the `I_WRAP_SONAME_FNNAME_ZU` function name is being mangled. – jswolf19 Feb 20 '11 at 10:17

1 Answers1

2

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.

osgx
  • 90,338
  • 53
  • 357
  • 513