| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | * |
| 4 | * Copyright (C) 2013 Mentor Graphics |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Do _not_ define _LGPL_SOURCE because we don't want to create a |
| 9 | * circular dependency loop between this malloc wrapper, liburcu and |
| 10 | * libc. |
| 11 | */ |
| 12 | |
| 13 | /* Has to be included first to override dlfcn.h */ |
| 14 | #include <common/compat/dlfcn.h> |
| 15 | |
| 16 | #include "common/macros.h" |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | #define LTTNG_UST_TRACEPOINT_DEFINE |
| 20 | #define TRACEPOINT_CREATE_PROBES |
| 21 | #define TP_IP_PARAM ip |
| 22 | #include "ust_pthread.h" |
| 23 | |
| 24 | static __thread int thread_in_trace; |
| 25 | |
| 26 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 27 | { |
| 28 | static int (*mutex_lock)(pthread_mutex_t *); |
| 29 | int retval; |
| 30 | |
| 31 | if (!mutex_lock) { |
| 32 | mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); |
| 33 | if (!mutex_lock) { |
| 34 | if (thread_in_trace) { |
| 35 | abort(); |
| 36 | } |
| 37 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 38 | return EINVAL; |
| 39 | } |
| 40 | } |
| 41 | if (thread_in_trace) { |
| 42 | return mutex_lock(mutex); |
| 43 | } |
| 44 | |
| 45 | thread_in_trace = 1; |
| 46 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex, |
| 47 | LTTNG_UST_CALLER_IP()); |
| 48 | retval = mutex_lock(mutex); |
| 49 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, |
| 50 | retval, LTTNG_UST_CALLER_IP()); |
| 51 | thread_in_trace = 0; |
| 52 | return retval; |
| 53 | } |
| 54 | |
| 55 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 56 | { |
| 57 | static int (*mutex_trylock)(pthread_mutex_t *); |
| 58 | int retval; |
| 59 | |
| 60 | if (!mutex_trylock) { |
| 61 | mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); |
| 62 | if (!mutex_trylock) { |
| 63 | if (thread_in_trace) { |
| 64 | abort(); |
| 65 | } |
| 66 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 67 | return EINVAL; |
| 68 | } |
| 69 | } |
| 70 | if (thread_in_trace) { |
| 71 | return mutex_trylock(mutex); |
| 72 | } |
| 73 | |
| 74 | thread_in_trace = 1; |
| 75 | retval = mutex_trylock(mutex); |
| 76 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, |
| 77 | retval, LTTNG_UST_CALLER_IP()); |
| 78 | thread_in_trace = 0; |
| 79 | return retval; |
| 80 | } |
| 81 | |
| 82 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 83 | { |
| 84 | static int (*mutex_unlock)(pthread_mutex_t *); |
| 85 | int retval; |
| 86 | |
| 87 | if (!mutex_unlock) { |
| 88 | mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); |
| 89 | if (!mutex_unlock) { |
| 90 | if (thread_in_trace) { |
| 91 | abort(); |
| 92 | } |
| 93 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 94 | return EINVAL; |
| 95 | } |
| 96 | } |
| 97 | if (thread_in_trace) { |
| 98 | return mutex_unlock(mutex); |
| 99 | } |
| 100 | |
| 101 | thread_in_trace = 1; |
| 102 | retval = mutex_unlock(mutex); |
| 103 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, |
| 104 | retval, LTTNG_UST_CALLER_IP()); |
| 105 | thread_in_trace = 0; |
| 106 | return retval; |
| 107 | } |