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