suricata
threads-debug.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * \author Pablo Rincon Crespo <pablo.rincon.crespo@gmail.com>
23  *
24  * Threading functions defined as macros: debug variants
25  */
26 
27 #ifndef SURICATA_THREADS_DEBUG_H
28 #define SURICATA_THREADS_DEBUG_H
29 
30 /* mutex */
31 
32 /** When dbg threads is defined, if a mutex fail to lock, it's
33  * initialized, logged, and does a second try; This is to prevent the system to freeze;
34  * It is for Mac OS X users;
35  * If you see a mutex, spinlock or condition not initialized, report it please!
36  */
37 #define SCMutexLock_dbg(mut) ({ \
38  printf("%16s(%s:%d): (thread:%"PRIuMAX") locking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
39  int retl = pthread_mutex_lock(mut); \
40  printf("%16s(%s:%d): (thread:%"PRIuMAX") locked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, retl); \
41  if (retl != 0) { \
42  switch (retl) { \
43  case EINVAL: \
44  printf("The value specified by attr is invalid\n"); \
45  retl = pthread_mutex_init(mut, NULL); \
46  if (retl != 0) \
47  exit(EXIT_FAILURE); \
48  retl = pthread_mutex_lock(mut); \
49  break; \
50  case EDEADLK: \
51  printf("A deadlock would occur if the thread blocked waiting for mutex\n"); \
52  break; \
53  } \
54  } \
55  retl; \
56 })
57 
58 #define SCMutexTrylock_dbg(mut) ({ \
59  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
60  int rett = pthread_mutex_trylock(mut); \
61  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, rett); \
62  if (rett != 0) { \
63  switch (rett) { \
64  case EINVAL: \
65  printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
66  break; \
67  case EBUSY: \
68  printf("Mutex is already locked\n"); \
69  break; \
70  } \
71  } \
72  rett; \
73 })
74 
75 #define SCMutexInit_dbg(mut, mutattr) ({ \
76  int ret; \
77  ret = pthread_mutex_init(mut, mutattr); \
78  if (ret != 0) { \
79  switch (ret) { \
80  case EINVAL: \
81  printf("The value specified by attr is invalid\n"); \
82  printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
83  break; \
84  case EAGAIN: \
85  printf("The system temporarily lacks the resources to create another mutex\n"); \
86  printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
87  break; \
88  case ENOMEM: \
89  printf("The process cannot allocate enough memory to create another mutex\n"); \
90  printf("%16s(%s:%d): (thread:%"PRIuMAX") mutex %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
91  break; \
92  } \
93  } \
94  ret; \
95 })
96 
97 #define SCMutexUnlock_dbg(mut) ({ \
98  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocking mutex %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut); \
99  int retu = pthread_mutex_unlock(mut); \
100  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocked mutex %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, retu); \
101  if (retu != 0) { \
102  switch (retu) { \
103  case EINVAL: \
104  printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
105  break; \
106  case EPERM: \
107  printf("The current thread does not hold a lock on mutex\n"); \
108  break; \
109  } \
110  } \
111  retu; \
112 })
113 
114 #define SCMutex pthread_mutex_t
115 #define SCMutexAttr pthread_mutexattr_t
116 #define SCMutexInit(mut, mutattrs) SCMutexInit_dbg(mut, mutattrs)
117 #define SCMutexLock(mut) SCMutexLock_dbg(mut)
118 #define SCMutexTrylock(mut) SCMutexTrylock_dbg(mut)
119 #define SCMutexIsLocked(mut) (SCMutexTrylock(mut) == EBUSY)
120 #define SCMutexUnlock(mut) SCMutexUnlock_dbg(mut)
121 #define SCMutexDestroy pthread_mutex_destroy
122 #define SCMUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
123 
124 /* conditions */
125 
126 #define SCCondWait_dbg(cond, mut) ({ \
127  int ret = pthread_cond_wait(cond, mut); \
128  switch (ret) { \
129  case EINVAL: \
130  printf("The value specified by attr is invalid (or a SCCondT not initialized!)\n"); \
131  printf("%16s(%s:%d): (thread:%"PRIuMAX") failed SCCondWait %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), mut, ret); \
132  break; \
133  } \
134  ret; \
135 })
136 
137 /* conditions */
138 #define SCCondT pthread_cond_t
139 #define SCCondInit pthread_cond_init
140 #define SCCondSignal pthread_cond_signal
141 #define SCCondDestroy pthread_cond_destroy
142 #define SCCondWait SCCondWait_dbg
143 
144 /* spinlocks */
145 
146 #define SCSpinLock_dbg(spin) ({ \
147  printf("%16s(%s:%d): (thread:%"PRIuMAX") locking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
148  int ret = pthread_spin_lock(spin); \
149  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocked spin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
150  switch (ret) { \
151  case EINVAL: \
152  printf("The value specified by attr is invalid\n"); \
153  break; \
154  case EDEADLK: \
155  printf("A deadlock would occur if the thread blocked waiting for spin\n"); \
156  break; \
157  } \
158  ret; \
159 })
160 
161 #define SCSpinTrylock_dbg(spin) ({ \
162  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
163  int ret = pthread_spin_trylock(spin); \
164  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked spin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
165  switch (ret) { \
166  case EINVAL: \
167  printf("The value specified by attr is invalid\n"); \
168  break; \
169  case EDEADLK: \
170  printf("A deadlock would occur if the thread blocked waiting for spin\n"); \
171  break; \
172  case EBUSY: \
173  printf("A thread currently holds the lock\n"); \
174  break; \
175  } \
176  ret; \
177 })
178 
179 #define SCSpinUnlock_dbg(spin) ({ \
180  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocking spin %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
181  int ret = pthread_spin_unlock(spin); \
182  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlockedspin %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
183  switch (ret) { \
184  case EINVAL: \
185  printf("The value specified by attr is invalid\n"); \
186  break; \
187  case EPERM: \
188  printf("The calling thread does not hold the lock\n"); \
189  break; \
190  } \
191  ret; \
192 })
193 
194 #define SCSpinInit_dbg(spin, spin_attr) ({ \
195  int ret = pthread_spin_init(spin, spin_attr); \
196  printf("%16s(%s:%d): (thread:%"PRIuMAX") spinlock %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
197  switch (ret) { \
198  case EINVAL: \
199  printf("The value specified by attr is invalid\n"); \
200  break; \
201  case EBUSY: \
202  printf("A thread currently holds the lock\n"); \
203  break; \
204  case ENOMEM: \
205  printf("The process cannot allocate enough memory to create another spin\n"); \
206  break; \
207  case EAGAIN: \
208  printf("The system temporarily lacks the resources to create another spin\n"); \
209  break; \
210  } \
211  ret; \
212 })
213 
214 #define SCSpinDestroy_dbg(spin) ({ \
215  printf("%16s(%s:%d): (thread:%"PRIuMAX") condition %p waiting\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin); \
216  int ret = pthread_spin_destroy(spin); \
217  printf("%16s(%s:%d): (thread:%"PRIuMAX") condition %p passed %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), spin, ret); \
218  switch (ret) { \
219  case EINVAL: \
220  printf("The value specified by attr is invalid\n"); \
221  break; \
222  case EBUSY: \
223  printf("A thread currently holds the lock\n"); \
224  break; \
225  case ENOMEM: \
226  printf("The process cannot allocate enough memory to create another spin\n"); \
227  break; \
228  case EAGAIN: \
229  printf("The system temporarily lacks the resources to create another spin\n"); \
230  break; \
231  } \
232  ret; \
233 })
234 
235 #define SCSpinlock pthread_spinlock_t
236 #define SCSpinLock SCSpinLock_dbg
237 #define SCSpinTrylock SCSpinTrylock_dbg
238 #define SCSpinUnlock SCSpinUnlock_dbg
239 #define SCSpinInit SCSpinInit_dbg
240 #define SCSpinDestroy SCSpinDestroy_dbg
241 
242 /* rwlocks */
243 
244 /** When dbg threads is defined, if a rwlock fail to lock, it's
245  * initialized, logged, and does a second try; This is to prevent the system to freeze;
246  * If you see a rwlock, spinlock or condition not initialized, report it please!
247  */
248 #define SCRWLockRDLock_dbg(rwl) ({ \
249  printf("%16s(%s:%d): (thread:%"PRIuMAX") locking rwlock %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl); \
250  int retl = pthread_rwlock_rdlock(rwl); \
251  printf("%16s(%s:%d): (thread:%"PRIuMAX") locked rwlock %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, retl); \
252  if (retl != 0) { \
253  switch (retl) { \
254  case EINVAL: \
255  printf("The value specified by attr is invalid\n"); \
256  retl = pthread_rwlock_init(rwl, NULL); \
257  if (retl != 0) \
258  exit(EXIT_FAILURE); \
259  retl = pthread_rwlock_rdlock(rwl); \
260  break; \
261  case EDEADLK: \
262  printf("A deadlock would occur if the thread blocked waiting for rwlock\n"); \
263  break; \
264  } \
265  } \
266  retl; \
267 })
268 
269 #define SCRWLockWRLock_dbg(rwl) ({ \
270  printf("%16s(%s:%d): (thread:%"PRIuMAX") locking rwlock %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl); \
271  int retl = pthread_rwlock_wrlock(rwl); \
272  printf("%16s(%s:%d): (thread:%"PRIuMAX") locked rwlock %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, retl); \
273  if (retl != 0) { \
274  switch (retl) { \
275  case EINVAL: \
276  printf("The value specified by attr is invalid\n"); \
277  retl = pthread_rwlock_init(rwl, NULL); \
278  if (retl != 0) \
279  exit(EXIT_FAILURE); \
280  retl = pthread_rwlock_wrlock(rwl); \
281  break; \
282  case EDEADLK: \
283  printf("A deadlock would occur if the thread blocked waiting for rwlock\n"); \
284  break; \
285  } \
286  } \
287  retl; \
288 })
289 
290 
291 #define SCRWLockTryWRLock_dbg(rwl) ({ \
292  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking rwlock %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl); \
293  int rett = pthread_rwlock_trywrlock(rwl); \
294  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked rwlock %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, rett); \
295  if (rett != 0) { \
296  switch (rett) { \
297  case EINVAL: \
298  printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
299  break; \
300  case EBUSY: \
301  printf("RWLock is already locked\n"); \
302  break; \
303  } \
304  } \
305  rett; \
306 })
307 
308 #define SCRWLockTryRDLock_dbg(rwl) ({ \
309  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocking rwlock %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl); \
310  int rett = pthread_rwlock_tryrdlock(rwl); \
311  printf("%16s(%s:%d): (thread:%"PRIuMAX") trylocked rwlock %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, rett); \
312  if (rett != 0) { \
313  switch (rett) { \
314  case EINVAL: \
315  printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
316  break; \
317  case EBUSY: \
318  printf("RWLock is already locked\n"); \
319  break; \
320  } \
321  } \
322  rett; \
323 })
324 
325 #define SCRWLockInit_dbg(rwl, rwlattr) ({ \
326  int ret; \
327  ret = pthread_rwlock_init(rwl, rwlattr); \
328  if (ret != 0) { \
329  switch (ret) { \
330  case EINVAL: \
331  printf("The value specified by attr is invalid\n"); \
332  printf("%16s(%s:%d): (thread:%"PRIuMAX") rwlock %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, ret); \
333  break; \
334  case EAGAIN: \
335  printf("The system temporarily lacks the resources to create another rwlock\n"); \
336  printf("%16s(%s:%d): (thread:%"PRIuMAX") rwlock %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, ret); \
337  break; \
338  case ENOMEM: \
339  printf("The process cannot allocate enough memory to create another rwlock\n"); \
340  printf("%16s(%s:%d): (thread:%"PRIuMAX") rwlock %p initialization returned %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, ret); \
341  break; \
342  } \
343  } \
344  ret; \
345 })
346 
347 #define SCRWLockUnlock_dbg(rwl) ({ \
348  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocking rwlock %p\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl); \
349  int retu = pthread_rwlock_unlock(rwl); \
350  printf("%16s(%s:%d): (thread:%"PRIuMAX") unlocked rwlock %p ret %" PRId32 "\n", __FUNCTION__, __FILE__, __LINE__, (uintmax_t)pthread_self(), rwl, retu); \
351  if (retu != 0) { \
352  switch (retu) { \
353  case EINVAL: \
354  printf("%16s(%s:%d): The value specified by attr is invalid\n", __FUNCTION__, __FILE__, __LINE__); \
355  break; \
356  case EPERM: \
357  printf("The current thread does not hold a lock on rwlock\n"); \
358  break; \
359  } \
360  } \
361  retu; \
362 })
363 
364 #define SCRWLock pthread_rwlock_t
365 #define SCRWLockInit(rwl, rwlattrs) SCRWLockInit_dbg(rwl, rwlattrs)
366 #define SCRWLockRDLock(rwl) SCRWLockRDLock_dbg(rwl)
367 #define SCRWLockWRLock(rwl) SCRWLockWRLock_dbg(rwl)
368 #define SCRWLockTryWRLock(rwl) SCRWLockTryWRLock_dbg(rwl)
369 #define SCRWLockTryRDLock(rwl) SCRWLockTryRDLock_dbg(rwl)
370 #define SCRWLockUnlock(rwl) SCRWLockUnlock_dbg(rwl)
371 #define SCRWLockDestroy pthread_rwlock_destroy
372 
373 /* ctrl mutex */
374 #define SCCtrlMutex pthread_mutex_t
375 #define SCCtrlMutexAttr pthread_mutexattr_t
376 #define SCCtrlMutexInit(mut, mutattr ) pthread_mutex_init(mut, mutattr)
377 #define SCCtrlMutexLock(mut) pthread_mutex_lock(mut)
378 #define SCCtrlMutexTrylock(mut) pthread_mutex_trylock(mut)
379 #define SCCtrlMutexUnlock(mut) pthread_mutex_unlock(mut)
380 #define SCCtrlMutexDestroy pthread_mutex_destroy
381 
382 /* ctrl conditions */
383 #define SCCtrlCondT pthread_cond_t
384 #define SCCtrlCondInit pthread_cond_init
385 #define SCCtrlCondSignal pthread_cond_signal
386 #define SCCtrlCondTimedwait pthread_cond_timedwait
387 #define SCCtrlCondWait pthread_cond_wait
388 #define SCCtrlCondDestroy pthread_cond_destroy
389 
390 #endif