开发者

what is the error in my program?

开发者 https://www.devze.com 2022-12-18 05:13 出处:网络
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

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_N开发者_C百科EXT, "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?


There's no such line 26 in the code that you posted. Please, post relevant code.

If the fptr you use in line 26 is declared exactly as the one in your pthread_create above (i.e. with empty parameter list ()), then the code will not compile as C++, since in C++ the () as parameter list means "no parameters at all". And you are trying to pass arguments, which is what causes the error. Declare fptr with the proper parameter list if you want your code to work as C++.

The declaration with () as parameter list will work in C code, since in C it means "unspecified parameters", but not in C++.


You've declared fptr as a pointer to a function that takes no arguments and returns an int. Thus, gcc complains when you try calling it with three arguments.

Try redeclaring fptr with the proper arguments.

0

精彩评论

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