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 | */ | |
9d315d6d MJ |
12 | |
13 | /* Has to be included first to override dlfcn.h */ | |
14 | #include <common/compat/dlfcn.h> | |
15 | ||
16 | #include "common/macros.h" | |
600f634a SS |
17 | #include <pthread.h> |
18 | ||
6ba0c2b2 MD |
19 | #define LTTNG_UST_TRACEPOINT_HIDDEN_DEFINITION |
20 | #define LTTNG_UST_TRACEPOINT_PROVIDER_HIDDEN_DEFINITION | |
21 | ||
88c7c4ea | 22 | #define LTTNG_UST_TRACEPOINT_DEFINE |
660323e6 | 23 | #define LTTNG_UST_TRACEPOINT_CREATE_PROBES |
c28c4a88 | 24 | #define LTTNG_UST_TP_IP_PARAM ip |
600f634a SS |
25 | #include "ust_pthread.h" |
26 | ||
16adecf1 | 27 | static __thread int thread_in_trace; |
600f634a SS |
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; | |
cbc06a3b | 49 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex, |
171fcc6f | 50 | LTTNG_UST_CALLER_IP()); |
600f634a | 51 | retval = mutex_lock(mutex); |
cbc06a3b | 52 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex, |
171fcc6f | 53 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
54 | thread_in_trace = 0; |
55 | return retval; | |
56 | } | |
57 | ||
58 | int pthread_mutex_trylock(pthread_mutex_t *mutex) | |
59 | { | |
60 | static int (*mutex_trylock)(pthread_mutex_t *); | |
61 | int retval; | |
62 | ||
63 | if (!mutex_trylock) { | |
64 | mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); | |
65 | if (!mutex_trylock) { | |
66 | if (thread_in_trace) { | |
67 | abort(); | |
68 | } | |
69 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); | |
70 | return EINVAL; | |
71 | } | |
72 | } | |
73 | if (thread_in_trace) { | |
74 | return mutex_trylock(mutex); | |
75 | } | |
76 | ||
77 | thread_in_trace = 1; | |
78 | retval = mutex_trylock(mutex); | |
cbc06a3b | 79 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex, |
171fcc6f | 80 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
81 | thread_in_trace = 0; |
82 | return retval; | |
83 | } | |
84 | ||
85 | int pthread_mutex_unlock(pthread_mutex_t *mutex) | |
86 | { | |
87 | static int (*mutex_unlock)(pthread_mutex_t *); | |
88 | int retval; | |
89 | ||
90 | if (!mutex_unlock) { | |
91 | mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); | |
92 | if (!mutex_unlock) { | |
93 | if (thread_in_trace) { | |
94 | abort(); | |
95 | } | |
96 | fprintf(stderr, "unable to initialize pthread wrapper library.\n"); | |
97 | return EINVAL; | |
98 | } | |
99 | } | |
100 | if (thread_in_trace) { | |
101 | return mutex_unlock(mutex); | |
102 | } | |
103 | ||
104 | thread_in_trace = 1; | |
105 | retval = mutex_unlock(mutex); | |
cbc06a3b | 106 | lttng_ust_tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex, |
171fcc6f | 107 | retval, LTTNG_UST_CALLER_IP()); |
600f634a SS |
108 | thread_in_trace = 0; |
109 | return retval; | |
110 | } |