C++ 调用覆盖函数库

C++ 调用覆盖函数库,c++,c,C++,C,我想使用一个个人的“pthread_self”函数。我想重写“pthread_self()”并在最后调用真正的“pthread_self”。 在C/C++中可能吗 例如: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/syscall.h> #include <sys/types.h> #include <unistd.h> #

我想使用一个个人的“pthread_self”函数。我想重写“pthread_self()”并在最后调用真正的“pthread_self”。 在C/C++中可能吗

例如:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>

pthread_t pthread_self(void)
{
  // Do something else.....
  // ...
  // And finally call to de real pthread_self():
  return ::pthread_self();  // This doesn't work
}

main()
{
    printf("\nThis is my main thread: %lu\n", pthread_self());
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
pthread_t pthread_self(无效)
{
//做点别的。。。。。
// ...
//最后调用de real pthread_self():
return::pthread_self();//这不起作用
}
main()
{
printf(“\n这是我的主线程:%lu\n”,pthread_self());
}

代码似乎是C89,没有
main的返回类型。因此,我建议您删除函数
pthread_self(void)
pthread_self()
前面的
操作符,然后重新编译


还可以使用其他名称调用
pthread\u self()
,例如
pthread\u self\u 2(void)

是否允许使用虚拟变量(从不使用)重载
pthread\u self()

产生

$ g++ test.cpp -lpthread

$ ./a.out
int main()
pthread_t pthread_self(bool)
int main()

$ 
其他两种方法可能是:

  • 编写一个函数
    my\u pthread\u self()
    ,在任何地方使用它,并让它调用real
    pthread\u self()

  • 拥有一个宏
    PTHREAD\u SELF
    ,该宏调用包装器,该包装器内部调用real
    PTHREAD\u SELF()


以下内容有点粗糙,但应在C中使用。将以下内容放在标题中,必须在
之前或之后包含(但决不能在pthread.h之后):

在相应的.c文件中:

#include <pthread>
#include "wrapper.h"
#undef pthread_self    // undo the hack locally

pthread_t _pthread_self(void)
{
  // Do something else.....
  // ...
  // And finally call to the real pthread_self():
  return pthread_self();
}
#包括
#包括“wrapper.h”
#undef pthread_self//在本地撤消攻击
pthread_t_pthread_self(无效)
{
//做点别的。。。。。
// ...
//最后调用真正的pthread_self():
返回pthread_self();
}
现在,当您在.c文件以外的其他文件中编写
pthread\u self
时,它将扩展到包装器函数。在上面的.c文件中,由于没有完成此攻击,包装器中的调用将调用库函数。

应该会给您一些想法。如果您可以控制可执行文件的链接,则无需使用LD_PRELOAD。

多亏了大家。
主要基于这两个链接和@Carl Norum和@Arkadyi的展示,我向您展示了一个可能的解决方案:

我有两个源文件和一个生成文件:

mipthread\u self.C

#include <stdio.h>
#include <pthread.h>

#if defined (__STDC__) || defined (__cplusplus) || defined (c__plusplus)

#if defined (__cplusplus) || defined (c__plusplus)
extern "C" {
#endif

pthread_t __real_pthread_self();

pthread_t __wrap_pthread_self(void)
{
   printf("This is mipthread_self %lu\n", __real_pthread_self());
   return __real_pthread_self();
}

#if defined (__cplusplus) || defined (c__plusplus)
}
#endif

#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>


void* start_function(void* value)
{
    printf("Thread start function is running for: %u\n", (unsigned int)pthread_self());
    sleep(5);
    pthread_exit(value);
}

int main()
{
    int res;
    pthread_t thread1, thread2;
    void* threadReturnValue;

    res = pthread_create(&thread1, NULL, start_function, (void*)"thread-one");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread1 created with id: %u\n", (unsigned int)thread1);

    res = pthread_create(&thread2, NULL, start_function, (void*)"thread-two");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread2 created with id: %u\n", (unsigned int)thread2);

    res = pthread_join(thread1, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    res = pthread_join(thread2, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    return 0;
}
BIN=test

CFLAGS=-g -m32 -fPIC -Wall $(DEFINES)
TIPO=-m32

PWD=`pwd`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)

INCLUDES=
LD_LIBRARIES=-L$(DIR_OBJ) -lmipthread -Xlinker --wrap -Xlinker pthread_self -lpthread -lstdc++


$(BIN): libmipthread.a
        @echo ""
        @echo "Se va a generar el binario $@"
        @echo ""
        gcc $(CFLAGS) test.C -o $(DIR_BIN)/$@ $(LD_LIBRARIES)

libmipthread.a: mipthread_self.o
        ar crv $(DIR_OBJ)/$@ $(DIR_OBJ)/$<

mipthread_self.o: mipthread_self.C
        gcc $(CFLAGS) -c ${PWD}/$< -o $(DIR_OBJ)/$@ $(INCLUDES)

clean:
        rm $(DIR_OBJ)/*.o
        rm $(DIR_OBJ)/*.a
        rm $(DIR_OBJ)/test
#包括
#包括
#如果定义(u STDC_uu)|定义(u cplusplus)|定义(c_uplusplus)
#如果已定义(uu cplusplus)|已定义(c_uplusplus)
外部“C”{
#恩迪夫
pthread_t_uureal_pthread_self();
pthread\u t\u wrap\u pthread\u self(无效)
{
printf(“这是mipthread\u self%lu\n”,\uuuu real\upthread\u self());
返回uu real_pthread_self();
}
#如果已定义(uu cplusplus)|已定义(c_uplusplus)
}
#恩迪夫
#恩迪夫
测试.C

#include <stdio.h>
#include <pthread.h>

#if defined (__STDC__) || defined (__cplusplus) || defined (c__plusplus)

#if defined (__cplusplus) || defined (c__plusplus)
extern "C" {
#endif

pthread_t __real_pthread_self();

pthread_t __wrap_pthread_self(void)
{
   printf("This is mipthread_self %lu\n", __real_pthread_self());
   return __real_pthread_self();
}

#if defined (__cplusplus) || defined (c__plusplus)
}
#endif

#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>


void* start_function(void* value)
{
    printf("Thread start function is running for: %u\n", (unsigned int)pthread_self());
    sleep(5);
    pthread_exit(value);
}

int main()
{
    int res;
    pthread_t thread1, thread2;
    void* threadReturnValue;

    res = pthread_create(&thread1, NULL, start_function, (void*)"thread-one");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread1 created with id: %u\n", (unsigned int)thread1);

    res = pthread_create(&thread2, NULL, start_function, (void*)"thread-two");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread2 created with id: %u\n", (unsigned int)thread2);

    res = pthread_join(thread1, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    res = pthread_join(thread2, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    return 0;
}
BIN=test

CFLAGS=-g -m32 -fPIC -Wall $(DEFINES)
TIPO=-m32

PWD=`pwd`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)

INCLUDES=
LD_LIBRARIES=-L$(DIR_OBJ) -lmipthread -Xlinker --wrap -Xlinker pthread_self -lpthread -lstdc++


$(BIN): libmipthread.a
        @echo ""
        @echo "Se va a generar el binario $@"
        @echo ""
        gcc $(CFLAGS) test.C -o $(DIR_BIN)/$@ $(LD_LIBRARIES)

libmipthread.a: mipthread_self.o
        ar crv $(DIR_OBJ)/$@ $(DIR_OBJ)/$<

mipthread_self.o: mipthread_self.C
        gcc $(CFLAGS) -c ${PWD}/$< -o $(DIR_OBJ)/$@ $(INCLUDES)

clean:
        rm $(DIR_OBJ)/*.o
        rm $(DIR_OBJ)/*.a
        rm $(DIR_OBJ)/test
#包括
#包括
#包括
#包括
void*start_函数(void*value)
{
printf(“线程启动函数正在为:%u\n运行,(unsigned int)pthread_self());
睡眠(5);
pthread_exit(值);
}
int main()
{
国际关系;
pthread_t thread1,thread2;
void*threadReturnValue;
res=pthread_create(&thread1,NULL,start_函数,(void*)“thread one”);
如果(res!=0){
perror(“创建线程失败”);
退出(退出失败);
}
printf(“创建的线程1的id为:%u\n”,(未签名int)线程1);
res=pthread_create(&thread2,NULL,start_函数,(void*)“thread two”);
如果(res!=0){
perror(“创建线程失败”);
退出(退出失败);
}
printf(“创建的线程2的id为:%u\n”,(未签名的int)Thread2);
res=pthread_join(thread1和threadReturnValue);
如果(res!=0){
perror(“螺纹连接失败”);
退出(退出失败);
}
printf(“%s已加入。\n”,(char*)threadReturnValue);
res=pthread_join(thread2和threadReturnValue);
如果(res!=0){
perror(“螺纹连接失败”);
退出(退出失败);
}
printf(“%s已加入。\n”,(char*)threadReturnValue);
返回0;
}
以及生成文件

#include <stdio.h>
#include <pthread.h>

#if defined (__STDC__) || defined (__cplusplus) || defined (c__plusplus)

#if defined (__cplusplus) || defined (c__plusplus)
extern "C" {
#endif

pthread_t __real_pthread_self();

pthread_t __wrap_pthread_self(void)
{
   printf("This is mipthread_self %lu\n", __real_pthread_self());
   return __real_pthread_self();
}

#if defined (__cplusplus) || defined (c__plusplus)
}
#endif

#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>


void* start_function(void* value)
{
    printf("Thread start function is running for: %u\n", (unsigned int)pthread_self());
    sleep(5);
    pthread_exit(value);
}

int main()
{
    int res;
    pthread_t thread1, thread2;
    void* threadReturnValue;

    res = pthread_create(&thread1, NULL, start_function, (void*)"thread-one");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread1 created with id: %u\n", (unsigned int)thread1);

    res = pthread_create(&thread2, NULL, start_function, (void*)"thread-two");
    if (res != 0) {
        perror("Creation of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("Thread2 created with id: %u\n", (unsigned int)thread2);

    res = pthread_join(thread1, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    res = pthread_join(thread2, &threadReturnValue);
    if (res != 0) {
        perror("Joining of thread failed");
        exit(EXIT_FAILURE);
    }
    printf("%s joined.\n", (char*)threadReturnValue);

    return 0;
}
BIN=test

CFLAGS=-g -m32 -fPIC -Wall $(DEFINES)
TIPO=-m32

PWD=`pwd`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)

INCLUDES=
LD_LIBRARIES=-L$(DIR_OBJ) -lmipthread -Xlinker --wrap -Xlinker pthread_self -lpthread -lstdc++


$(BIN): libmipthread.a
        @echo ""
        @echo "Se va a generar el binario $@"
        @echo ""
        gcc $(CFLAGS) test.C -o $(DIR_BIN)/$@ $(LD_LIBRARIES)

libmipthread.a: mipthread_self.o
        ar crv $(DIR_OBJ)/$@ $(DIR_OBJ)/$<

mipthread_self.o: mipthread_self.C
        gcc $(CFLAGS) -c ${PWD}/$< -o $(DIR_OBJ)/$@ $(INCLUDES)

clean:
        rm $(DIR_OBJ)/*.o
        rm $(DIR_OBJ)/*.a
        rm $(DIR_OBJ)/test
BIN=测试
CFLAGS=-g-m32-fPIC-Wall$(定义)
TIPO=-m32
PWD=`PWD`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)
包括=
LD_LIBRARIES=-L$(DIR_OBJ)-lmipthread-Xlinker--wrap-Xlinker pthread_self-lpthread-lstdc++
$(BIN):libmipthread.a
@回声“”
@echo“Se va a generar el binario$@”
@回声“”
gcc$(CFLAGS)test.C-o$(DIR_BIN)/$@$(LD_库)
libmipthread.a:mipthread\u self.o
ar crv$(直接目标)/$@$(直接目标)/$<
mipthread\u self.o:mipthread\u self.C
gcc$(CFLAGS)-c${PWD}/$<-o$(DIR_OBJ)/$@$(包括)
清洁:
rm$(DIR_OBJ)/*.o
rm$(DIR_OBJ)/*.a
rm$(DIR_OBJ)/测试

<代码> > NETCODER,他使用了<代码>:: >所以它绝对是C++。人,你似乎没有抓住要点。他试图重写一个不可能的自由函数。@alaguna,这当然是可能的。例如,binutils ld
-wrap
。@aleguna的可能重复:这当然是可能的,尤其是对于共享库。是的,但这并不能解决问题,你会得到无限递归。@netcoder他在递归函数之前做了一些事情-这可能是早期的
返回
?查看他的评论我假设通过
并最终调用de-real pthread_self():
他指的是调用
libpthread
的版本,而不是他自己的版本。@Aniket不,正如他所说,他想绕过已经存在的函数。@Aniket,他试图在最后调用posix库提供的“default”函数。这在C语言中是不可能的(他没有明确指定他想要使用什么,看起来很模糊)。此外,这意味着每次调用都要更改代码,这显然是他试图避免的,因为否则他可以轻松地用不同的名称编写包装(或者我不认为这样的包装有问题)谢谢。我想一个C和C++的解决方案。如果不可能,那么只能在C++中。正如@leemes所说,我不想