[REACTOS]
[reactos.git] / reactos / include / reactos / debug.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: include/internal/debug.h
5 * PURPOSE: Useful debugging macros
6 * PROGRAMMERS: David Welch (welch@mcmail.com)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /*
11 * NOTE: Define NDEBUG before including this header
12 * to disable debugging macros.
13 */
14
15 #ifndef __INTERNAL_DEBUG
16 #define __INTERNAL_DEBUG
17
18 /* Define DbgPrint/DbgPrintEx/RtlAssert/RtlRaiseStatus unless the NDK is used */
19 #if !defined(_RTLFUNCS_H) && !defined(_NTDDK_)
20
21 /* Make sure we have basic types (some people include us *before* SDK)... */
22 #if !defined(_NTDEF_) && !defined(_NTDEF_H) && !defined(_WINDEF_) && !defined(_WINDEF_H)
23 #error Please include SDK first.
24 #endif
25
26 ULONG
27 __cdecl
28 DbgPrint(
29 _In_z_ _Printf_format_string_ PCSTR Format,
30 ...
31 );
32
33 NTSYSAPI
34 ULONG
35 __cdecl
36 DbgPrintEx(
37 _In_ ULONG ComponentId,
38 _In_ ULONG Level,
39 _In_z_ _Printf_format_string_ PCCH Format,
40 ...
41 );
42
43 __analysis_noreturn
44 NTSYSAPI
45 VOID
46 NTAPI
47 RtlAssert(
48 PVOID FailedAssertion,
49 PVOID FileName,
50 ULONG LineNumber,
51 PCHAR Message
52 );
53
54 #ifndef _NTDEF_ /* Guard against redefinition from ntdef.h */
55 typedef _Return_type_success_(return >= 0) LONG NTSTATUS;
56 #endif
57 __analysis_noreturn
58 NTSYSAPI
59 VOID
60 NTAPI
61 RtlRaiseStatus(
62 _In_ NTSTATUS Status
63 );
64
65 #endif /* !defined(_RTLFUNCS_H) && !defined(_NTDDK_) */
66
67
68 /* Fix usage of RtlRaiseStatus */
69 #if !defined(_RTLFUNCS_H) && defined(_NTDDK_)
70 #define RaiseStatus ExRaiseStatus
71 #else
72 #define RaiseStatus RtlRaiseStatus
73 #endif /* !defined(_RTLFUNCS_H) && defined(_NTDDK_) */
74
75
76 #ifndef assert
77 #ifndef NASSERT
78 #define assert(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__FILE__, __LINE__, ""); }
79 #else
80 #define assert(x)
81 #endif
82 #endif
83
84 #ifndef ASSERT
85 #ifndef NASSERT
86 #define ASSERT(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__FILE__, __LINE__, ""); }
87 #else
88 #define ASSERT(x)
89 #endif
90 #endif
91
92 #ifndef ASSERTMSG
93 #ifndef NASSERT
94 #define ASSERTMSG(x,m) if (!(x)) { RtlAssert((PVOID)#x, __FILE__, __LINE__, m); }
95 #else
96 #define ASSERTMSG(x)
97 #endif
98 #endif
99
100 /* For internal purposes only */
101 #define __NOTICE(level, fmt, ...) DbgPrint(#level ": %s at %s:%d " fmt, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
102
103 /* Print stuff only on Debug Builds*/
104 #define DPFLTR_DEFAULT_ID -1
105 #if DBG
106
107 /* These are always printed */
108 #define DPRINT1(fmt, ...) do { \
109 if (DbgPrint("(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)) \
110 DbgPrint("(%s:%d) DbgPrint() failed!\n", __FILE__, __LINE__); \
111 } while (0)
112
113 /* These are printed only if NDEBUG is NOT defined */
114 #ifndef NDEBUG
115
116 #define DPRINT(fmt, ...) do { \
117 if (DbgPrint("(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)) \
118 DbgPrint("(%s:%d) DbgPrint() failed!\n", __FILE__, __LINE__); \
119 } while (0)
120
121 #else
122
123 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
124
125 #endif
126
127 #define UNIMPLEMENTED __NOTICE(WARNING, "is UNIMPLEMENTED!\n");
128
129 #define ERR_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
130 #define WARN_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
131 #define TRACE_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
132 #define INFO_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
133
134 #define ERR__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
135 #define WARN__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
136 #define TRACE__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
137 #define INFO__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
138
139 #else /* not DBG */
140
141 /* On non-debug builds, we never show these */
142 #define DPRINT1(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
143 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
144
145 #define UNIMPLEMENTED
146
147 #define ERR_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
148 #define WARN_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
149 #define TRACE_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
150 #define INFO_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
151
152 #define ERR__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
153 #define WARN__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
154 #define TRACE__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
155 #define INFO__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
156
157 #endif /* not DBG */
158
159 /******************************************************************************/
160 /* For internal purposes only */
161 #define __ERROR_DBGBREAK(...) \
162 do { \
163 DbgPrint("" __VA_ARGS__); \
164 DbgBreakPoint(); \
165 } while (0)
166
167 /* For internal purposes only */
168 #define __ERROR_FATAL(Status, ...) \
169 do { \
170 DbgPrint("" __VA_ARGS__); \
171 RaiseStatus((Status)); \
172 } while (0)
173
174 /*
175 * These macros are designed to display an optional printf-like
176 * user-defined message and to break into the debugger.
177 * After that they allow to continue the program execution.
178 */
179 #define ERROR_DBGBREAK(...) \
180 do { \
181 __NOTICE(ERROR, "\n"); \
182 __ERROR_DBGBREAK(__VA_ARGS__); \
183 } while (0)
184
185 #define UNIMPLEMENTED_DBGBREAK(...) \
186 do { \
187 __NOTICE(ERROR, "is UNIMPLEMENTED!\n"); \
188 __ERROR_DBGBREAK(__VA_ARGS__); \
189 } while (0)
190
191 /*
192 * These macros are designed to display an optional printf-like
193 * user-defined message and to break into the debugger.
194 * After that they halt the execution of the current thread.
195 */
196 #define ERROR_FATAL(...) \
197 do { \
198 __NOTICE(UNRECOVERABLE ERROR, "\n"); \
199 __ERROR_FATAL(STATUS_ASSERTION_FAILURE, __VA_ARGS__); \
200 } while (0)
201
202 #define UNIMPLEMENTED_FATAL(...) \
203 do { \
204 __NOTICE(UNRECOVERABLE ERROR, "is UNIMPLEMENTED!\n"); \
205 __ERROR_FATAL(STATUS_NOT_IMPLEMENTED, __VA_ARGS__); \
206 } while (0)
207 /******************************************************************************/
208
209 #define ASSERT_IRQL_LESS_OR_EQUAL(x) ASSERT(KeGetCurrentIrql()<=(x))
210 #define ASSERT_IRQL_EQUAL(x) ASSERT(KeGetCurrentIrql()==(x))
211 #define ASSERT_IRQL_LESS(x) ASSERT(KeGetCurrentIrql()<(x))
212
213 #endif /* __INTERNAL_DEBUG */