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