1*25efa36bSclaudio /* $OpenBSD: test.h,v 1.7 2024/08/29 15:18:17 claudio Exp $ */ 2b2ea75c1Sfgsch 3b2ea75c1Sfgsch #ifndef _h_test_ 4b2ea75c1Sfgsch #define _h_test_ 5b2ea75c1Sfgsch 6b2ea75c1Sfgsch #include <stdio.h> 71821443cSdavid #include <stdlib.h> 8b2ea75c1Sfgsch #include <signal.h> 9b2ea75c1Sfgsch #include <errno.h> 10b2ea75c1Sfgsch #include <string.h> 11b2ea75c1Sfgsch #include <stdarg.h> 122558b9a9Sguenther #include <unistd.h> 13b2ea75c1Sfgsch 142558b9a9Sguenther static void __vpanic(const char *, const char *, const char *, 15f3c3a9c6Smillert int, const char *, va_list) __attribute__((__noreturn__)); 162558b9a9Sguenther static void __panic(const char *, const char *, const char *, 17*25efa36bSclaudio int, const char *, ...) __attribute__((__noreturn__)) 18*25efa36bSclaudio __attribute__((__format__ (printf, 5, 6))); 19b2ea75c1Sfgsch 20b2ea75c1Sfgsch #if defined(__OpenBSD__) || defined(__FreeBSD__) 21b2ea75c1Sfgsch #include <pthread.h> 22b2ea75c1Sfgsch #include <pthread_np.h> 23c72b5b24Smillert void _thread_dump_info(void); 242558b9a9Sguenther int _thread_sys_write(int, const char*, size_t); 252558b9a9Sguenther void _thread_sys__exit(int) __attribute__((__noreturn__)); 26b2ea75c1Sfgsch #define SET_NAME(x) pthread_set_name_np(pthread_self(), x) 27b2ea75c1Sfgsch #define DUMP_INFO() _thread_dump_info() 28b2ea75c1Sfgsch #else 292558b9a9Sguenther #define _thread_sys_write(fd,buf,len) write(fd,buf,len) 302558b9a9Sguenther #define _thread_sys__exit(ret) _exit(ret) 312558b9a9Sguenther #define strlcat(dst,src,siz) strcat(dst,src) 32b2ea75c1Sfgsch #define SET_NAME(x) /* nada */ 33b2ea75c1Sfgsch #define DUMP_INFO() /* nada */ 34b2ea75c1Sfgsch #endif 35b2ea75c1Sfgsch 36b2ea75c1Sfgsch static void 37*25efa36bSclaudio __vpanic(const char *type, const char *errstr, const char *filenm, int lineno, 38*25efa36bSclaudio const char *fmt, va_list ap) 39b2ea75c1Sfgsch { 40b2ea75c1Sfgsch char buf[1024]; 41b2ea75c1Sfgsch 42b2ea75c1Sfgsch /* "<type> at <filenm>:<lineno>: <fmt ap...>:<errstr>" */ 43b2ea75c1Sfgsch snprintf(buf, sizeof buf, "%s at %s:%d\n", type, filenm, lineno); 44b2ea75c1Sfgsch _thread_sys_write(2, buf, strlen(buf)); 45b2ea75c1Sfgsch vsnprintf(buf, sizeof buf, fmt, ap); 46b2ea75c1Sfgsch if (errstr != NULL) { 47b2ea75c1Sfgsch strlcat(buf, ": ", sizeof buf); 48b2ea75c1Sfgsch strlcat(buf, errstr, sizeof buf); 49b2ea75c1Sfgsch } 50b2ea75c1Sfgsch strlcat(buf, "\n", sizeof buf); 51b2ea75c1Sfgsch _thread_sys_write(2, buf, strlen(buf)); 52b2ea75c1Sfgsch 53b2ea75c1Sfgsch DUMP_INFO(); 54b2ea75c1Sfgsch _thread_sys__exit(1); 55b2ea75c1Sfgsch 56b2ea75c1Sfgsch _thread_sys_write(2, "[locking]\n", 10); 57b2ea75c1Sfgsch while(1); 58b2ea75c1Sfgsch } 59b2ea75c1Sfgsch 60b2ea75c1Sfgsch static void 61*25efa36bSclaudio __panic(const char *type, const char *errstr, const char *filenm, int lineno, 62*25efa36bSclaudio const char *fmt, ...) 63b2ea75c1Sfgsch { 64b2ea75c1Sfgsch va_list ap; 65b2ea75c1Sfgsch 66b2ea75c1Sfgsch va_start(ap, fmt); 67b2ea75c1Sfgsch __vpanic(type, errstr, filenm, lineno, fmt, ap); 68b2ea75c1Sfgsch va_end(ap); 69b2ea75c1Sfgsch } 70b2ea75c1Sfgsch 71b2ea75c1Sfgsch #define DIE(e, m, args...) \ 72b2ea75c1Sfgsch __panic("died", strerror(e), __FILE__, __LINE__, m , ## args) 73b2ea75c1Sfgsch 74b2ea75c1Sfgsch #define PANIC(m, args...) \ 75b2ea75c1Sfgsch __panic("panic", NULL, __FILE__, __LINE__, m, ## args) 76b2ea75c1Sfgsch 77b2ea75c1Sfgsch #define ASSERT(x) do { \ 78b2ea75c1Sfgsch if (!(x)) \ 79b2ea75c1Sfgsch __panic("assert failed", NULL, __FILE__, __LINE__, "%s", #x); \ 80b2ea75c1Sfgsch } while(0) 81b2ea75c1Sfgsch 82b2ea75c1Sfgsch #define ASSERTe(x,rhs) do { \ 83b2ea75c1Sfgsch int _x; \ 84b2ea75c1Sfgsch _x = (x); \ 85b2ea75c1Sfgsch if (!(_x rhs)) { \ 86b2ea75c1Sfgsch if (_x > 0) \ 87b2ea75c1Sfgsch __panic("assert failed", strerror(_x), __FILE__, __LINE__, \ 88b2ea75c1Sfgsch "%s %s", #x, #rhs); \ 89b2ea75c1Sfgsch else \ 90b2ea75c1Sfgsch __panic("assert failed", NULL, __FILE__, __LINE__, \ 91b2ea75c1Sfgsch "%s [=%d] %s", #x, _x, #rhs); \ 92b2ea75c1Sfgsch } \ 93b2ea75c1Sfgsch } while(0) 94b2ea75c1Sfgsch 95b2ea75c1Sfgsch #define _T(x) __builtin_classify_type(x) 96b2ea75c1Sfgsch 97b2ea75c1Sfgsch #define _CHECK(x, rhs, efn) do { \ 98b2ea75c1Sfgsch typeof(x) _x; \ 99b2ea75c1Sfgsch _x = (x); \ 100b2ea75c1Sfgsch if (!(_x rhs)) \ 101b2ea75c1Sfgsch __panic("check failed", efn, __FILE__, __LINE__, \ 102b2ea75c1Sfgsch ((_T(0) == _T(_x) )? "failed check %s (=%d) %s " : \ 103b2ea75c1Sfgsch (_T("") == _T(_x) )? "failed check %s (=%s) %s " : \ 104b2ea75c1Sfgsch (_T('x') == _T(_x) )? "failed check %s (=%c) %s " : \ 105b2ea75c1Sfgsch (_T(0L) == _T(_x) )? "failed check %s (=%ld) %s " : "?") \ 106b2ea75c1Sfgsch , #x, _x, #rhs); \ 107b2ea75c1Sfgsch } while(0) 108b2ea75c1Sfgsch 109b2ea75c1Sfgsch #define CHECKr(x) _CHECK(x, == 0, strerror(_x)) 110b2ea75c1Sfgsch #define CHECKe(x) _CHECK(x, != -1, strerror(errno)) 111b2ea75c1Sfgsch #define CHECKn(x) _CHECK(x, != 0, strerror(errno)) 112b2ea75c1Sfgsch #define CHECKhn(x) _CHECK(x, != 0, hstrerror(h_errno)) 113b2ea75c1Sfgsch 114b2ea75c1Sfgsch #define SUCCEED exit(0) 115b2ea75c1Sfgsch 116b2ea75c1Sfgsch #define OK (0) 117b2ea75c1Sfgsch #define NOTOK (-1) 118b2ea75c1Sfgsch 1194d546e0cStodd #endif /* _h_test_ */ 120