| 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 | #include <ust-dlfcn.h> |
| 13 | #include <ust-helper.h> |
| 14 | #include <pthread.h> |
| 15 | |
| 16 | #define TRACEPOINT_DEFINE |
| 17 | #define TRACEPOINT_CREATE_PROBES |
| 18 | #define TP_IP_PARAM ip |
| 19 | #include "ust_pthread.h" |
| 20 | |
| 21 | static __thread int thread_in_trace; |
| 22 | |
| 23 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 24 | { |
| 25 | static int (*mutex_lock)(pthread_mutex_t *); |
| 26 | int retval; |
| 27 | |
| 28 | if (!mutex_lock) { |
| 29 | mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); |
| 30 | if (!mutex_lock) { |
| 31 | if (thread_in_trace) { |
| 32 | abort(); |
| 33 | } |
| 34 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 35 | return EINVAL; |
| 36 | } |
| 37 | } |
| 38 | if (thread_in_trace) { |
| 39 | return mutex_lock(mutex); |
| 40 | } |
| 41 | |
| 42 | thread_in_trace = 1; |
| 43 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex, |
| 44 | LTTNG_UST_CALLER_IP()); |
| 45 | retval = mutex_lock(mutex); |
| 46 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, |
| 47 | retval, LTTNG_UST_CALLER_IP()); |
| 48 | thread_in_trace = 0; |
| 49 | return retval; |
| 50 | } |
| 51 | |
| 52 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 53 | { |
| 54 | static int (*mutex_trylock)(pthread_mutex_t *); |
| 55 | int retval; |
| 56 | |
| 57 | if (!mutex_trylock) { |
| 58 | mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); |
| 59 | if (!mutex_trylock) { |
| 60 | if (thread_in_trace) { |
| 61 | abort(); |
| 62 | } |
| 63 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 64 | return EINVAL; |
| 65 | } |
| 66 | } |
| 67 | if (thread_in_trace) { |
| 68 | return mutex_trylock(mutex); |
| 69 | } |
| 70 | |
| 71 | thread_in_trace = 1; |
| 72 | retval = mutex_trylock(mutex); |
| 73 | tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, |
| 74 | retval, LTTNG_UST_CALLER_IP()); |
| 75 | thread_in_trace = 0; |
| 76 | return retval; |
| 77 | } |
| 78 | |
| 79 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 80 | { |
| 81 | static int (*mutex_unlock)(pthread_mutex_t *); |
| 82 | int retval; |
| 83 | |
| 84 | if (!mutex_unlock) { |
| 85 | mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); |
| 86 | if (!mutex_unlock) { |
| 87 | if (thread_in_trace) { |
| 88 | abort(); |
| 89 | } |
| 90 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 91 | return EINVAL; |
| 92 | } |
| 93 | } |
| 94 | if (thread_in_trace) { |
| 95 | return mutex_unlock(mutex); |
| 96 | } |
| 97 | |
| 98 | thread_in_trace = 1; |
| 99 | retval = mutex_unlock(mutex); |
| 100 | tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, |
| 101 | retval, LTTNG_UST_CALLER_IP()); |
| 102 | thread_in_trace = 0; |
| 103 | return retval; |
| 104 | } |