Commit | Line | Data |
---|---|---|
600f634a | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
600f634a | 3 | * |
c0c0989a | 4 | * Copyright (C) 2013 Mentor Graphics |
600f634a SS |
5 | */ |
6 | ||
d7e89462 MD |
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 | */ | |
ae4b659d | 12 | #include <ust-dlfcn.h> |
864a1eda | 13 | #include <ust-helper.h> |
600f634a SS |
14 | #include <pthread.h> |
15 | ||
16 | #define TRACEPOINT_DEFINE | |
17 | #define TRACEPOINT_CREATE_PROBES | |
35176231 | 18 | #define TP_IP_PARAM ip |
600f634a SS |
19 | #include "ust_pthread.h" |
20 | ||
16adecf1 | 21 | static __thread int thread_in_trace; |
600f634a SS |
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; | |
35176231 | 43 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex, |
171fcc6f | 44 | LTTNG_UST_CALLER_IP()); |
600f634a | 45 | retval = mutex_lock(mutex); |
35176231 | 46 | tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, |
171fcc6f | 47 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
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); | |
35176231 | 73 | tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, |
171fcc6f | 74 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
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); | |
35176231 | 100 | tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, |
171fcc6f | 101 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
102 | thread_in_trace = 0; |
103 | return retval; | |
104 | } |