Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

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

I have tried creating a wrapper over pthread_mutex that maintains a list of all mutexes held by the current thread. However, I also utilize certain library code, for which, I can't possibly use this wrapper. Is there any way to do get this list for generic pthread_mutexes? FWIW, I am using a Linux Host.

question from:https://stackoverflow.com/questions/65904672/is-there-a-way-to-find-a-list-of-pthread-mutexes-held-by-the-current-pthread

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can interpose (via LD_PRELOAD and <dlfcn.h>, for example) the pthread_mutex_lock(), pthread_mutex_trylock(), and pthread_mutex_unlock() functions with your own that call the underlying functions, but also maintain a per-thread array/chain of held mutex addresses. (You can use the __thread storage class keyword provided by GCC, equivalent to the C11 _Thread_local, to declare per-thread variables.)


Here is a verified working example.

Makefile, to build the interposing library and the example program:

# 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 $@

Note that the indentation in Makefiles must use Tabs, so if you copy-paste the above, you will need to fix the indentation, for example by running sed -e 's|^ *||' -i Makefile.

libheld.c, the implementation of the interposing library (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, implementing the interfaces needed to obtain the mutex tracking information (whether mutex tracking is available, how many mutexes the current thread holds, and the addresses of those mutexes):

// 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);
}

Note that because we won't know until runtime whether mutex tracking is available and the interposing library loaded, we need to use <dlfcn.h> to obtain the functions provided by the interposing library itself. (If we compiled held.c into a library, then we could just implement the default functions here, and let the interposing library interpose them too.) This way, it suffices to link the target binary against libld (-ldl) too.

held.h, declaring the interfaces provided by the above 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, showing an example of tracking held mutexes:

// 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.
");
        return;
    } else
    if (err) {
        fprintf(out, "Error in mutex tracking: %s.
", strerror(err));
        return;
    }

    size_t n = held_count();
    if (n > 0) {
        fprintf(out, "%zu %s held by current thread:
", n, (n > 1) ? "mutexes" : "mutex");
        for (size_t i = 0; i < n; i++) {
            fprintf(out, "%p
", held_mutex(i));
        }
    } else {
        fprintf(out, "No mutexes held by current thread.
");
    }
}

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;
}

Compile the library and the example program by running make clean all.

If you run the example, ./example, it just outputs "Mutex tracking is not available."

If you run the example with the interposing library, LD_PRELOAD=./libheld.so ./example, it outputs the (default 5) mutexes the main thread holds.

In case you wonder about the SPDX-License-Identifier comments, they this code is licensed under Creative Commons Zero v1.0 Universal license. It means you can do anything you want with this code, but I provide no guarantees whatsoever.


An even easier external method is to run the unmodified binary under ltrace, i.e. ltrace -fttt -o logfile -e pthread_mutex_lock+pthread_mutex_trylock+pthread_mutex_unlock ./example so that ltrace logs each pthread_mutex_lock, pthread_mutex_trylock, and pthread_mutex_unlock call to the logfile with a timestamp (seconds since Unix epoch).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...