xref: /minix/minix/lib/libvassert/vassert.h (revision e3b78ef1)
1 #ifndef _VASSERT_H_
2 #define _VASSERT_H_
3 
4 #ifdef __cplusplus
5 extern "C"
6 {
7 #endif /*__cplusplus*/
8 
9 #define ALIGNED(n) __attribute__((__aligned__(n)))
10 #define VASSERT_TRIGGER_OFFSET 1221
11 #define VASSERT_PAGE_SIZE      4096
12 
13 /* Need to align at 4K. */
14 /* Ensure the inReplay flag is on its own page. */
15 #pragma pack(1)
16 typedef struct VAssert_StateWrapper {
17    char space1[VASSERT_TRIGGER_OFFSET];
18    volatile char inReplay;
19    char space[VASSERT_PAGE_SIZE - VASSERT_TRIGGER_OFFSET - sizeof(char)];
20 } VAssert_StateWrapper;
21 #pragma pack()
22 
23 extern VAssert_StateWrapper vassert_state;
24 
25 /*
26  * User-selectable standard functions.
27  * XXX: Document these, in coordination with the SDK docs.
28  */
29 
30 #if defined(__KERNEL__)
31 #  define KERNEL_VASSERT
32 #endif
33 
34 #ifdef KERNEL_VASSERT
35 
36 #  ifndef VASSERT_CUSTOM_ASSERT
37 #     define VASSERT_CUSTOM_ASSERT(expr)
38 #  endif
39 
40 #  ifndef VASSERT_CUSTOM_ABORT
41 #     include <linux/kernel.h>
42 #     define VASSERT_CUSTOM_ABORT() ((void)0) // printk(KERN_ALERT"VAssert abort at %s: %d", __FILE__, __LINE__)
43 #  endif
44 
45 #  ifndef VASSERT_CUSTOM_LOG
46 #     include <linux/kernel.h>
47 #     define VASSERT_CUSTOM_LOG printk
48 #  endif
49 
50 #else
51 #  ifndef VASSERT_CUSTOM_ASSERT
52 #     include <assert.h>
53 #     define VASSERT_CUSTOM_ASSERT assert
54 #  endif
55 
56 #  ifndef VASSERT_CUSTOM_ABORT
57 #     include <stdlib.h>
58 #     define VASSERT_CUSTOM_ABORT abort
59 #  endif
60 
61 #  ifndef VASSERT_CUSTOM_LOG
62 #     include <stdio.h>
63 #     define VASSERT_CUSTOM_LOG printf
64 #  endif
65 #endif
66 
67 /* Results: 0 if successful, -1 if not. */
68 // XXX need to automatic de-register trigger page when the program quits
69 extern char VAssert_Init(void);
70 extern char VAssert_Uninit(void);
71 
72 /*
73  * These functions should not be called directly; they need to be wrapped.
74  */
75 extern void VAssert_LogMain(const char *format, ...)
76 	__attribute__((__format__(__printf__, 1, 2)));
77 extern void VAssert_GoLiveMain(void);
78 extern void VAssert_ReturnToReplayMain(void);
79 extern char VAssert_Trace(size_t max_size);
80 
81 #ifdef VASSERT_ALWAYS_EXECUTE
82 
83 #define VAssert_Assert(expr)              \
84 do {                                      \
85    VASSERT_CUSTOM_ASSERT(expr);           \
86 } while (0)
87 
88 #define VAssert_Log(args)                 \
89 do {                                      \
90    VASSERT_CUSTOM_LOG args;               \
91 } while (0)
92 
93 #define VAssert_BeginBlock
94 #define VAssert_EndBlock
95 
96 #else /* VASSERT_ALWAYS_EXECUTE */
97 
98 #define VAssert_Assert(expr)              \
99 do {                                      \
100    if (vassert_state.inReplay) {          \
101       if (!(expr)) {                      \
102          VAssert_GoLiveMain();            \
103          VASSERT_CUSTOM_ABORT();          \
104       } else {                            \
105          VAssert_ReturnToReplayMain();    \
106       }                                   \
107    }                                      \
108 } while (0)
109 
110 #define VAssert_Log(args)                 \
111 do {                                      \
112    if (vassert_state.inReplay) {          \
113       VAssert_LogMain args;               \
114       VAssert_ReturnToReplayMain();       \
115    }                                      \
116 } while (0)
117 
118 #define VAssert_BeginBlock if (vassert_state.inReplay)
119 #define VAssert_EndBlock VAssert_ReturnToReplayMain()
120 
121 #endif /* VASSERT_ALWAYS_EXECUTE */
122 
123 /*
124  * Record/Replay functionality
125  */
126 /*
127  * Results: True if successful, false if not.
128  * Input: True to start recording, false to stop.
129  */
130 extern char VAssert_SetRecordingMain(char start);
131 
132 #define VAssert_StartRecording() VAssert_SetRecordingMain(1)
133 #define VAssert_StopRecording() VAssert_SetRecordingMain(0)
134 
135 #ifdef __cplusplus
136 }
137 #endif /*__cplusplus*/
138 
139 #endif /*_VASSERT_H_*/
140