有没有办法找到当前 pthread 持有的 pthread_mutexes 列表?

Is there a way to find a list of pthread_mutexes held by the current pthread?

我已经尝试在 pthread_mutex 上创建一个包装器,它维护当前线程持有的所有互斥锁的列表。但是,我也使用某些库代码,为此我不可能使用这个包装器。有什么方法可以获取通用 pthread_mutexes 的列表? FWIW,我正在使用 Linux 主机。

您可以插入(例如,通过 LD_PRELOAD<dlfcn.h>pthread_mutex_lock()pthread_mutex_trylock()pthread_mutex_unlock() 函数与您自己的那个调用底层函数,但也维护每个线程 array/chain 持有的互斥锁地址。 (可以使用GCC提供的__thread storage class关键字,等同于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编译成一个库,那么我们可以在这里实现默认函数,让插入库也插入它们。) 这样,link 目标二进制文件也足以对抗 libld (-ldl)。

held.h,声明上面提供的接口held.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;
}

通过运行ning make clean all编译库和示例程序。

如果您运行示例./example,它只会输出“互斥跟踪不可用”。

如果您 运行 带有插入库的示例 LD_PRELOAD=./libheld.so ./example,它会输出主线程持有的(默认 5)互斥量。

如果您想了解 SPDX-License-Identifier 评论,他们此代码是根据 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 纪元以来的秒数)。