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