| 1 | /* |
| 2 | * Copyright (C) 2013 Mentor Graphics |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <lttng/ust-dlfcn.h> |
| 21 | #include <pthread.h> |
| 22 | |
| 23 | #define TRACEPOINT_DEFINE |
| 24 | #define TRACEPOINT_CREATE_PROBES |
| 25 | #include "ust_pthread.h" |
| 26 | |
| 27 | static __thread int thread_in_trace; |
| 28 | |
| 29 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 30 | { |
| 31 | static int (*mutex_lock)(pthread_mutex_t *); |
| 32 | int retval; |
| 33 | |
| 34 | if (!mutex_lock) { |
| 35 | mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); |
| 36 | if (!mutex_lock) { |
| 37 | if (thread_in_trace) { |
| 38 | abort(); |
| 39 | } |
| 40 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 41 | return EINVAL; |
| 42 | } |
| 43 | } |
| 44 | if (thread_in_trace) { |
| 45 | return mutex_lock(mutex); |
| 46 | } |
| 47 | |
| 48 | thread_in_trace = 1; |
| 49 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex); |
| 50 | retval = mutex_lock(mutex); |
| 51 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, retval); |
| 52 | thread_in_trace = 0; |
| 53 | return retval; |
| 54 | } |
| 55 | |
| 56 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 57 | { |
| 58 | static int (*mutex_trylock)(pthread_mutex_t *); |
| 59 | int retval; |
| 60 | |
| 61 | if (!mutex_trylock) { |
| 62 | mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); |
| 63 | if (!mutex_trylock) { |
| 64 | if (thread_in_trace) { |
| 65 | abort(); |
| 66 | } |
| 67 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); |
| 68 | return EINVAL; |
| 69 | } |
| 70 | } |
| 71 | if (thread_in_trace) { |
| 72 | return mutex_trylock(mutex); |
| 73 | } |
| 74 | |
| 75 | thread_in_trace = 1; |
| 76 | retval = mutex_trylock(mutex); |
| 77 | tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, retval); |
| 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 | tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, retval); |
| 104 | thread_in_trace = 0; |
| 105 | return retval; |
| 106 | } |