Hello, I am trying to intercept libpthred( POSIX ) function calls using LD_PRELOAD technique. First I tried to create my own shared library having the same functin names as in the pthread library. Here is an example of my own created pthread_create function which first gets the address of original pthread_create function in the pthread library, then it calls the original function in the pthread library using the function pointer.
#include <stdio.h>
#include <dlfcn.h>
#include <pthread.h>
int pthread_create (pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void*), void *arg )
{
int return_value;
static int (*fptr) () = 0;
printf( "\n this is the test of LD_PRELOAD" );
char *lError = dlerror();
if( fptr == 0 )
{
fptr = ( int (*) () ) dlsym( RTLD_NEXT, "pthread_create" );
if( fptr == 0 )
{
(void) printf( "Error dlopen: %s", dlerror() );
return 0;
}
}
return (return_value);
}
I tried to compile it in this way:
g++ -fPIC -g -rdynamic -c -Wall ldtest.C -lrt -lpthread -ldl
But its giving the following error
ldtest.C:26: error: too many arguments to function
Line 26 is this line in the program.
return_value = (*fptr)( thread, attr, start_routine, arg );
Can anyone please tell me what is the problem here Or is this the right way to compile it?