xref: /openbsd/regress/lib/libpthread/include/test.h (revision 404b540a)
1 /*	$OpenBSD: test.h,v 1.5 2003/09/02 23:52:17 david Exp $	*/
2 
3 #ifndef _h_test_
4 #define _h_test_
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <stdarg.h>
12 
13 int	_thread_sys_write(int, const char*, size_t);
14 __dead void _thread_sys__exit(int) __attribute__((__noreturn__));
15 
16 static __dead void __vpanic(const char *, const char *, const char *,
17 	int, const char *, va_list) __attribute__((__noreturn__));
18 static __dead void __panic(const char *, const char *, const char *,
19 	int, const char *, ...) __attribute__((__noreturn__));
20 
21 #if defined(__OpenBSD__) || defined(__FreeBSD__)
22 #include <pthread.h>
23 #include <pthread_np.h>
24 void	_thread_dump_info(void);
25 #define SET_NAME(x)	pthread_set_name_np(pthread_self(), x)
26 #define DUMP_INFO()	_thread_dump_info()
27 #else
28 #define SET_NAME(x)	/* nada */
29 #define DUMP_INFO()	/* nada */
30 #endif
31 
32 static void
33 __vpanic(type, errstr, filenm, lineno, fmt, ap)
34 	const char *type;
35 	const char *errstr;
36 	const char *filenm;
37 	int lineno;
38 	const char *fmt;
39 	va_list ap;
40 {
41 	char buf[1024];
42 
43 	/* "<type> at <filenm>:<lineno>: <fmt ap...>:<errstr>" */
44 	snprintf(buf, sizeof buf, "%s at %s:%d\n", type, filenm, lineno);
45 	_thread_sys_write(2, buf, strlen(buf));
46 	vsnprintf(buf, sizeof buf, fmt, ap);
47 	if (errstr != NULL) {
48 		strlcat(buf, ": ", sizeof buf);
49 		strlcat(buf, errstr, sizeof buf);
50 	}
51 	strlcat(buf, "\n", sizeof buf);
52 	_thread_sys_write(2, buf, strlen(buf));
53 
54 	DUMP_INFO();
55 	_thread_sys__exit(1);
56 
57 	_thread_sys_write(2, "[locking]\n", 10);
58 	while(1);
59 }
60 
61 static void
62 __panic(type, errstr, filenm, lineno, fmt)
63 	const char *type;
64 	const char *errstr;
65 	const char *filenm;
66 	int lineno;
67 	const char *fmt;
68 {
69 	va_list ap;
70 
71 	va_start(ap, fmt);
72 	__vpanic(type, errstr, filenm, lineno, fmt, ap);
73 	va_end(ap);
74 }
75 
76 #define DIE(e, m, args...) \
77 	__panic("died", strerror(e), __FILE__, __LINE__, m , ## args)
78 
79 #define PANIC(m, args...)  \
80 	__panic("panic", NULL, __FILE__, __LINE__, m, ## args)
81 
82 #define ASSERT(x) do { \
83 	if (!(x)) \
84 		__panic("assert failed", NULL, __FILE__, __LINE__, "%s", #x); \
85 } while(0)
86 
87 #define ASSERTe(x,rhs) do { \
88 	int _x; \
89 	_x = (x); \
90 	if (!(_x rhs)) { \
91 	    if (_x > 0) \
92 		__panic("assert failed", strerror(_x), __FILE__, __LINE__,  \
93 		    "%s %s", #x, #rhs); \
94 	    else \
95 		__panic("assert failed", NULL, __FILE__, __LINE__, \
96 		    "%s [=%d] %s", #x, _x, #rhs); \
97 	} \
98 } while(0)
99 
100 #define _T(x) __builtin_classify_type(x)
101 
102 #define _CHECK(x, rhs, efn) do { \
103 	typeof(x) _x; \
104 	_x = (x); \
105 	if (!(_x rhs)) \
106 		__panic("check failed", efn, __FILE__, __LINE__, \
107 		   ((_T(0) == _T(_x)   )? "failed check %s (=%d) %s " : \
108 		    (_T("") == _T(_x)  )? "failed check %s (=%s) %s " : \
109 		    (_T('x') == _T(_x) )? "failed check %s (=%c) %s " : \
110 		    (_T(0L) == _T(_x)  )? "failed check %s (=%ld) %s " : "?") \
111 		    , #x, _x, #rhs); \
112 } while(0)
113 
114 #define CHECKr(x) _CHECK(x, == 0, strerror(_x))
115 #define CHECKe(x) _CHECK(x, != -1, strerror(errno))
116 #define CHECKn(x) _CHECK(x, != 0, strerror(errno))
117 #define CHECKhn(x) _CHECK(x, != 0, hstrerror(h_errno))
118 
119 #define SUCCEED 	exit(0)
120 
121 #define OK		(0)
122 #define NOTOK		(-1)
123 
124 #endif /* _h_test_ */
125