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