您可以插入(例如通过LD_PRELOAD 和<dlfcn.h>)pthread_mutex_lock()、pthread_mutex_trylock() 和pthread_mutex_unlock() 函数与您自己调用底层函数,但也维护每个线程数组/chain 持有的互斥地址。 (可以使用GCC提供的__thread存储类关键字,相当于C11的_Thread_local,声明per-thread变量。)
这是一个经过验证的工作示例。
Makefile,构建设置库和示例程序:
# SPDX-License-Identifier: CC0-1.0
CC := gcc
CFLAGS := -Wall -Wextra -O2
LDFLAGS := -pthread -ldl
BINS := libheld.so example
.PHONY: all clean run
all: clean $(BINS)
clean:
rm -f *.o $(BINS)
run: libheld.so example
env LD_PRELOAD=./libheld.so ./example
%.o: %.c
$(CC) $(CFLAGS) -c $^
libheld.so: libheld.c
$(CC) $(CFLAGS) -fPIC $^ -shared -Wl,-soname,$@ -ldl -o $@
example: example.o held.o
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
请注意,Makefiles 中的缩进必须使用 Tabs,因此如果您复制粘贴上述内容,则需要修复缩进,例如通过运行 sed -e 's|^ *|\t|' -i Makefile。
libheld.c,中间库(libheld.so)的实现:
// SPDX-License-Identifier: CC0-1.0
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
static int (*original_lock)(pthread_mutex_t *) = NULL;
static int (*original_trylock)(pthread_mutex_t *) = NULL;
static int (*original_unlock)(pthread_mutex_t *) = NULL;
static int held_err = 0;
static __thread size_t held_max = 0;
static __thread size_t held_num = 0;
static __thread pthread_mutex_t **held_ptr = NULL;
int libheld_errno(void)
{
return __atomic_load_n(&held_err, __ATOMIC_SEQ_CST);
}
size_t libheld_count(void)
{
return held_num;
}
pthread_mutex_t *libheld_mutex(size_t i)
{
return (i < held_num) ? held_ptr[i] : NULL;
}
int pthread_mutex_lock(pthread_mutex_t *m)
{
int (*lock)(pthread_mutex_t *);
__atomic_load(&original_lock, &lock, __ATOMIC_SEQ_CST);
if (!lock) {
lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
if (!lock) {
__atomic_store_n(&held_err, ENOSYS, __ATOMIC_SEQ_CST);
return ENOSYS;
}
__atomic_store(&original_lock, &lock, __ATOMIC_SEQ_CST);
}
int retval = lock(m);
if (retval)
return retval;
if (held_num >= held_max) {
const int saved_errno = errno;
const size_t temp_max = (held_num | 127) + 121;
void *temp_ptr;
temp_ptr = realloc(held_ptr, temp_max * sizeof held_ptr[0]);
if (!temp_ptr) {
__atomic_store_n(&held_err, ENOMEM, __ATOMIC_SEQ_CST);
errno = saved_errno;
return 0;
}
held_max = temp_max;
held_ptr = temp_ptr;
}
held_ptr[held_num++] = m;
return 0;
}
int pthread_mutex_trylock(pthread_mutex_t *m)
{
int (*trylock)(pthread_mutex_t *);
__atomic_load(&original_trylock, &trylock, __ATOMIC_SEQ_CST);
if (!trylock) {
trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
if (!trylock) {
__atomic_store_n(&held_err, ENOSYS, __ATOMIC_SEQ_CST);
return ENOSYS;
}
__atomic_store(&original_trylock, &trylock, __ATOMIC_SEQ_CST);
}
int retval = trylock(m);
if (retval)
return retval;
if (held_num >= held_max) {
const int saved_errno = errno;
const size_t temp_max = (held_num | 127) + 121;
void *temp_ptr;
temp_ptr = realloc(held_ptr, temp_max * sizeof held_ptr[0]);
if (!temp_ptr) {
__atomic_store_n(&held_err, ENOMEM, __ATOMIC_SEQ_CST);
errno = saved_errno;
return 0;
}
held_max = temp_max;
held_ptr = temp_ptr;
}
held_ptr[held_num++] = m;
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *m)
{
int (*unlock)(pthread_mutex_t *);
__atomic_load(&original_unlock, &unlock, __ATOMIC_SEQ_CST);
if (!unlock) {
unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
if (!unlock) {
__atomic_store_n(&held_err, ENOSYS, __ATOMIC_SEQ_CST);
return ENOSYS;
}
__atomic_store(&original_unlock, &unlock, __ATOMIC_SEQ_CST);
}
int retval = unlock(m);
if (retval)
return retval;
size_t i = 0;
while (i < held_num) {
if (held_ptr[i] == m) {
held_num--;
if (i < held_num) {
memmove(held_ptr + i, held_ptr + i + 1, (held_num - i) * sizeof held_ptr[0]);
}
} else {
i++;
}
}
return 0;
}
held.c,实现获取互斥量跟踪信息所需的接口(互斥量跟踪是否可用,当前线程持有多少个互斥量,这些互斥量的地址):
// SPDX-License-Identifier: CC0-1.0
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <stdlib.h>
#include <pthread.h>
#include <dlfcn.h>
#include <errno.h>
static int libheld_errno_default(void)
{
return ENOSYS;
}
static size_t libheld_count_default(void)
{
return 0;
}
static pthread_mutex_t *libheld_mutex_default(size_t i)
{
(void)i; /* Silences warning about unused parameter; generates no code. */
return NULL;
}
static int (*held_errno_func)(void) = NULL;
static size_t (*held_count_func)(void) = NULL;
static pthread_mutex_t *(*held_mutex_func)(size_t) = NULL;
int held_errno(void)
{
int (*errno_func)(void);
__atomic_load(&held_errno_func, &errno_func, __ATOMIC_SEQ_CST);
if (!held_errno_func) {
errno_func = dlsym(RTLD_DEFAULT, "libheld_errno");
if (!errno_func)
errno_func = libheld_errno_default;
__atomic_store(&held_errno_func, &errno_func, __ATOMIC_SEQ_CST);
}
return errno_func();
}
size_t held_count(void)
{
size_t (*count_func)(void);
__atomic_load(&held_count_func, &count_func, __ATOMIC_SEQ_CST);
if (!count_func) {
count_func = dlsym(RTLD_DEFAULT, "libheld_count");
if (!count_func)
count_func = libheld_count_default;
__atomic_store(&held_count_func, &count_func, __ATOMIC_SEQ_CST);
}
return count_func();
}
pthread_mutex_t *held_mutex(size_t i)
{
pthread_mutex_t *(*mutex_func)(size_t);
__atomic_load(&held_mutex_func, &mutex_func, __ATOMIC_SEQ_CST);
if (!mutex_func) {
mutex_func = dlsym(RTLD_DEFAULT, "libheld_mutex");
if (!mutex_func)
mutex_func = libheld_mutex_default;
__atomic_store(&held_mutex_func, &mutex_func, __ATOMIC_SEQ_CST);
}
return mutex_func(i);
}
注意,因为直到运行时我们才知道互斥跟踪是否可用以及插入库是否加载,我们需要使用来获取插入库本身提供的功能。 (如果我们把held.c编译成一个库,那么我们可以在这里实现默认函数,让插入库也插入它们。)
这样,也可以将目标二进制文件与 libld (-ldl) 链接起来。
held.h,声明上述hold.c提供的接口:
// SPDX-License-Identifier: CC0-1.0
#ifndef HELD_H
#define HELD_H
#include <stdlib.h>
#include <pthread.h>
/* Returns zero if mutex tracking is available, nonzero otherwise. */
extern int held_errno(void);
/* Returns the number of mutexes held by the current thread. */
extern size_t held_count(void);
/* Returns a pointer to the i'th mutex (0 up to held_count()-1) held by the current thread. */
extern pthread_mutex_t *held_mutex(size_t);
#endif /* HELD_H */
example.c,展示了一个跟踪持有的互斥锁的例子:
// SPDX-License-Identifier: CC0-1.0
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include "held.h"
#ifndef LOCKS
#define LOCKS 5
#endif
void print_held(FILE *out)
{
if (!out)
out = stdout;
int err = held_errno();
if (err == ENOSYS) {
fprintf(out, "Mutex tracking is not available.\n");
return;
} else
if (err) {
fprintf(out, "Error in mutex tracking: %s.\n", strerror(err));
return;
}
size_t n = held_count();
if (n > 0) {
fprintf(out, "%zu %s held by current thread:\n", n, (n > 1) ? "mutexes" : "mutex");
for (size_t i = 0; i < n; i++) {
fprintf(out, "\t%p\n", held_mutex(i));
}
} else {
fprintf(out, "No mutexes held by current thread.\n");
}
}
int main(void)
{
pthread_mutex_t lock[LOCKS];
size_t i;
for (i = 0; i < LOCKS; i++)
pthread_mutex_init(lock + i, NULL);
for (i = 0; i < LOCKS; i++)
pthread_mutex_lock(lock + i);
print_held(stdout);
for (i = 0; i < LOCKS; i++)
pthread_mutex_unlock(lock + i);
print_held(stdout);
return EXIT_SUCCESS;
}
运行make clean all编译库和示例程序。
如果您运行示例./example,它只会输出“互斥跟踪不可用。”
如果您使用插入库 LD_PRELOAD=./libheld.so ./example 运行示例,它会输出主线程持有的(默认 5 个)互斥体。
如果您想了解 SPDX-License-Identifier cmets,他们的此代码已根据 Creative Commons Zero v1.0 Universal 许可证获得许可。这意味着您可以使用此代码做任何您想做的事情,但我不提供任何保证。
更简单的外部方法是在 ltrace 下运行未修改的二进制文件,即ltrace -fttt -o logfile -e pthread_mutex_lock+pthread_mutex_trylock+pthread_mutex_unlock ./example
以便 ltrace 将每个 pthread_mutex_lock、pthread_mutex_trylock 和 pthread_mutex_unlock 调用记录到带有时间戳(自 Unix 纪元以来的秒数)的日志文件中。