summaryrefslogtreecommitdiff
path: root/minix/lib/libvassert/vassert.h
blob: 0eca9533d392d77dfd7eb315883e5d9896287287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef _VASSERT_H_
#define _VASSERT_H_

#ifdef __cplusplus
extern "C"
{
#endif /*__cplusplus*/

#define ALIGNED(n) __attribute__((__aligned__(n)))
#define VASSERT_TRIGGER_OFFSET 1221
#define VASSERT_PAGE_SIZE      4096

/* Need to align at 4K. */
/* Ensure the inReplay flag is on its own page. */
#pragma pack(1)
typedef struct VAssert_StateWrapper {
   char space1[VASSERT_TRIGGER_OFFSET];
   volatile char inReplay;
   char space[VASSERT_PAGE_SIZE - VASSERT_TRIGGER_OFFSET - sizeof(char)];
} VAssert_StateWrapper;
#pragma pack()

extern VAssert_StateWrapper vassert_state;

/*
 * User-selectable standard functions.
 * XXX: Document these, in coordination with the SDK docs.
 */

#if defined(__KERNEL__)
#  define KERNEL_VASSERT
#endif

#ifdef KERNEL_VASSERT

#  ifndef VASSERT_CUSTOM_ASSERT
#     define VASSERT_CUSTOM_ASSERT(expr)
#  endif

#  ifndef VASSERT_CUSTOM_ABORT
#     include <linux/kernel.h>
#     define VASSERT_CUSTOM_ABORT() ((void)0) // printk(KERN_ALERT"VAssert abort at %s: %d", __FILE__, __LINE__)
#  endif

#  ifndef VASSERT_CUSTOM_LOG
#     include <linux/kernel.h>
#     define VASSERT_CUSTOM_LOG printk
#  endif

#else
#  ifndef VASSERT_CUSTOM_ASSERT
#     include <assert.h>
#     define VASSERT_CUSTOM_ASSERT assert
#  endif

#  ifndef VASSERT_CUSTOM_ABORT
#     include <stdlib.h>
#     define VASSERT_CUSTOM_ABORT abort
#  endif

#  ifndef VASSERT_CUSTOM_LOG
#     include <stdio.h>
#     define VASSERT_CUSTOM_LOG printf
#  endif
#endif

/* Results: 0 if successful, -1 if not. */
// XXX need to automatic de-register trigger page when the program quits
extern char VAssert_Init(void);
extern char VAssert_Uninit(void);

/*
 * These functions should not be called directly; they need to be wrapped.
 */
extern void VAssert_LogMain(const char *format, ...)
	__attribute__((__format__(__printf__, 1, 2)));
extern void VAssert_GoLiveMain(void);
extern void VAssert_ReturnToReplayMain(void);
extern char VAssert_Trace(size_t max_size);

#ifdef VASSERT_ALWAYS_EXECUTE

#define VAssert_Assert(expr)              \
do {                                      \
   VASSERT_CUSTOM_ASSERT(expr);           \
} while (0)

#define VAssert_Log(args)                 \
do {                                      \
   VASSERT_CUSTOM_LOG args;               \
} while (0)

#define VAssert_BeginBlock
#define VAssert_EndBlock

#else /* VASSERT_ALWAYS_EXECUTE */

#define VAssert_Assert(expr)              \
do {                                      \
   if (vassert_state.inReplay) {          \
      if (!(expr)) {                      \
         VAssert_GoLiveMain();            \
         VASSERT_CUSTOM_ABORT();          \
      } else {                            \
         VAssert_ReturnToReplayMain();    \
      }                                   \
   }                                      \
} while (0)

#define VAssert_Log(args)                 \
do {                                      \
   if (vassert_state.inReplay) {          \
      VAssert_LogMain args;               \
      VAssert_ReturnToReplayMain();       \
   }                                      \
} while (0)

#define VAssert_BeginBlock if (vassert_state.inReplay)
#define VAssert_EndBlock VAssert_ReturnToReplayMain()

#endif /* VASSERT_ALWAYS_EXECUTE */

/* 
 * Record/Replay functionality
 */
/*
 * Results: True if successful, false if not.
 * Input: True to start recording, false to stop.
 */
extern char VAssert_SetRecordingMain(char start);

#define VAssert_StartRecording() VAssert_SetRecordingMain(1)
#define VAssert_StopRecording() VAssert_SetRecordingMain(0)

#ifdef __cplusplus
}
#endif /*__cplusplus*/

#endif /*_VASSERT_H_*/