Intercepting global functions with Valgrind on Linux using g ++

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

I can intercept a global function when building with gcc, however, when I compile the same code with g ++, the interception does not work.

Is there anything special about compiler flags that I should specify?

Here is my sample application:

#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 compiling with GCC, the output is:

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

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

inside foo

+4
source share
1 answer

g ++ executes the name for the function without extern "C" . Therefore, you must find the warped name (e.g. using nm object ) and use it in your valgrind code. Or you can rewrite the target program to use the extern "C" function.

+1
source

Source: https://habr.com/ru/post/1340512/


All Articles