如何联动期间更换pthread_create的 [英] How to replace pthread_create during linkage

查看:116
本文介绍了如何联动期间更换pthread_create的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要维护所有正在运行的线程的列表,每个线程一些额外的信息。在此回答提到,有可能提供自己pthread_create的版本,并用它来链接程序。
同样重要的是,我想打电话给原来的pthread_create的在它的可重写版本的结尾。

I want to maintain a list of all running threads with some additional information about each thread. In this answer mentioned that it is possible to provide my own version of pthread_create and to link program with it. It is also important that I want to call original pthread_create in the end of my override version of it.

有人能详细解释它如何完成和/或提供一些code例子吗?

Could someone explain in details how it could be done and/or provide some code example?

推荐答案

您可以通过调用查找原始pthread_create的功能符号:

You can lookup the symbol for the original pthread_create-function by calling:

pthread_create_orig = dlsym(RTLD_NEXT, "pthread_create");

然后包装看起来像:

The wrapper would then look like:

#include <dlfcn.h>

int (*pthread_create_orig)(pthread_t *, const pthread_attr_t *, void *(*) (void *), void *);

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start) (void *), void *arg) {
       if (!pthread_create_orig)
           pthread_create_orig = dlsym(RTLD_NEXT, "pthread_create");
       return pthread_create_orig(thread, attr, start, arg);
}

开始你的可执行文件时,编译这是一个共享库和preLOAD它。

Compile this as a shared library and preload it when starting your executable.

说明:通常情况下,则dlsym()的第一个参数是句柄库的dlopen()打开。特殊句柄 RTLD_NEXT 用于搜索该符号的下一个出现的,即它不会被默认链接之一。然后,这是libpthread中的符号,而不是一个在你preloaded库。

Explanation: Usually, the first argument of dlsym() is a handle to a library opened with dlopen(). The special handle RTLD_NEXT is used to search the next occurrence of that symbol, i.e. the one which would not be linked by default. This is then the symbol in libpthread, not the one in your preloaded library.

这篇关于如何联动期间更换pthread_create的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆