adecac6f467cc114564d566f3aed0430bd3a298d
[reactos.git] / sdk / include / reactos / debug.h
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: include/reactos/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 #pragma once
16
17 #include <builddir.h>
18
19 /* Define DbgPrint/DbgPrintEx/RtlAssert unless the NDK is used */
20 #if !defined(_RTLFUNCS_H) && !defined(_NTDDK_)
21
22 /* Make sure we have basic types (some people include us *before* SDK)... */
23 #if !defined(_NTDEF_) && !defined(_NTDEF_H) && !defined(_WINDEF_) && !defined(_WINDEF_H)
24 #error Please include SDK first.
25 #endif
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 ULONG
32 __cdecl
33 DbgPrint(
34 _In_z_ _Printf_format_string_ PCSTR Format,
35 ...
36 );
37
38 NTSYSAPI
39 ULONG
40 __cdecl
41 DbgPrintEx(
42 _In_ ULONG ComponentId,
43 _In_ ULONG Level,
44 _In_z_ _Printf_format_string_ PCSTR Format,
45 ...
46 );
47
48 __analysis_noreturn
49 NTSYSAPI
50 VOID
51 NTAPI
52 RtlAssert(
53 _In_ PVOID FailedAssertion,
54 _In_ PVOID FileName,
55 _In_ ULONG LineNumber,
56 _In_opt_z_ PCHAR Message
57 );
58
59 #ifdef __cplusplus
60 } /* extern "C" */
61 #endif
62
63 #endif /* !defined(_RTLFUNCS_H) && !defined(_NTDDK_) */
64
65 #ifndef assert
66 #if DBG && !defined(NASSERT)
67 #define assert(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__RELFILE__, __LINE__, ""); }
68 #else
69 #define assert(x) ((VOID) 0)
70 #endif
71 #endif
72
73 #ifndef ASSERT
74 #if DBG && !defined(NASSERT)
75 #define ASSERT(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__RELFILE__, __LINE__, ""); }
76 #else
77 #define ASSERT(x) ((VOID) 0)
78 #endif
79 #endif
80
81 #ifndef ASSERTMSG
82 #if DBG && !defined(NASSERT)
83 #define ASSERTMSG(m, x) if (!(x)) { RtlAssert((PVOID)#x, __RELFILE__, __LINE__, m); }
84 #else
85 #define ASSERTMSG(m, x) ((VOID) 0)
86 #endif
87 #endif
88
89 /* For internal purposes only */
90 #define __NOTICE(level, fmt, ...) DbgPrint(#level ": %s at %s:%d " fmt, __FUNCTION__, __RELFILE__, __LINE__, ##__VA_ARGS__)
91
92 /* Print stuff only on Debug Builds*/
93 #if DBG
94
95 /* These are always printed */
96 #define DPRINT1(fmt, ...) do { \
97 if (DbgPrint("(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)) \
98 DbgPrint("(%s:%d) DbgPrint() failed!\n", __RELFILE__, __LINE__); \
99 } while (0)
100
101 /* These are printed only if NDEBUG is NOT defined */
102 #ifndef NDEBUG
103
104 #define DPRINT(fmt, ...) do { \
105 if (DbgPrint("(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)) \
106 DbgPrint("(%s:%d) DbgPrint() failed!\n", __RELFILE__, __LINE__); \
107 } while (0)
108
109 #else
110
111 #if defined(_MSC_VER)
112 #define DPRINT __noop
113 #else
114 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
115 #endif
116
117 #endif
118
119 #define UNIMPLEMENTED __NOTICE(WARNING, "is UNIMPLEMENTED!\n")
120 #define UNIMPLEMENTED_ONCE do { static int bWarnedOnce = 0; if (!bWarnedOnce) { bWarnedOnce++; UNIMPLEMENTED; } } while (0)
121
122 #define ERR_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
123 #define WARN_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
124 #define TRACE_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
125 #define INFO_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
126
127 #define ERR__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
128 #define WARN__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
129 #define TRACE__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
130 #define INFO__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __RELFILE__, __LINE__, ##__VA_ARGS__)
131
132 #else /* not DBG */
133
134 /* On non-debug builds, we never show these */
135 #define UNIMPLEMENTED
136 #define UNIMPLEMENTED_ONCE
137 #if defined(_MSC_VER)
138 #define DPRINT1 __noop
139 #define DPRINT __noop
140
141 #define ERR_(ch, ...) __noop
142 #define WARN_(ch, ...) __noop
143 #define TRACE_(ch, ...) __noop
144 #define INFO_(ch, ...) __noop
145
146 #define ERR__(ch, ...) __noop
147 #define WARN__(ch, ...) __noop
148 #define TRACE__(ch, ...) __noop
149 #define INFO__(ch, ...) __noop
150 #else
151 #define DPRINT1(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
152 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
153
154 #define ERR_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
155 #define WARN_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
156 #define TRACE_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
157 #define INFO_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
158
159 #define ERR__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
160 #define WARN__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
161 #define TRACE__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
162 #define INFO__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
163 #endif /* _MSC_VER */
164
165 #endif /* not DBG */
166
167 /******************************************************************************/
168 /*
169 * Declare a target-dependent process termination procedure.
170 */
171 #ifndef _NTDDK_ /* User-Mode */
172 #ifndef NTOS_MODE_USER /* Should be Win32 */
173 #ifndef _WIN32
174 #error "Unsupported target."
175 #else
176 #define TerminateCurrentProcess(Status) TerminateProcess(GetCurrentProcess(), (Status))
177 #endif
178 #else /* Native */
179 #ifndef _PSFUNCS_H
180 NTSYSCALLAPI
181 NTSTATUS
182 NTAPI
183 NtTerminateProcess(
184 IN HANDLE ProcessHandle,
185 IN NTSTATUS ExitStatus
186 );
187 #endif
188 #ifndef NtCurrentProcess
189 #define NtCurrentProcess() ((HANDLE)(LONG_PTR)-1)
190 #endif
191 #define TerminateCurrentProcess(Status) NtTerminateProcess(NtCurrentProcess(), (Status))
192 #endif
193 #else /* Kernel-Mode */
194 #include <bugcodes.h>
195 #define TerminateCurrentProcess(Status) KeBugCheckEx(CRITICAL_SERVICE_FAILED, (Status), 0, 0, 0)
196 #endif
197
198
199 /* For internal purposes only */
200 #define __ERROR_DBGBREAK(...) \
201 do { \
202 DbgPrint("" __VA_ARGS__); \
203 DbgBreakPoint(); \
204 } while (0)
205
206 /* For internal purposes only */
207 #define __ERROR_FATAL(Status, ...) \
208 do { \
209 DbgPrint("" __VA_ARGS__); \
210 DbgBreakPoint(); \
211 TerminateCurrentProcess(Status); \
212 } while (0)
213
214 /*
215 * These macros are designed to display an optional printf-like
216 * user-defined message and to break into the debugger.
217 * After that they allow to continue the program execution.
218 */
219 #define ERROR_DBGBREAK(...) \
220 do { \
221 __NOTICE(ERROR, "\n"); \
222 __ERROR_DBGBREAK(__VA_ARGS__); \
223 } while (0)
224
225 #define UNIMPLEMENTED_DBGBREAK(...) \
226 do { \
227 __NOTICE(ERROR, "is UNIMPLEMENTED!\n"); \
228 __ERROR_DBGBREAK(__VA_ARGS__); \
229 } while (0)
230
231 /*
232 * These macros are designed to display an optional printf-like
233 * user-defined message and to break into the debugger.
234 * After that they halt the execution of the current thread.
235 */
236 #define ERROR_FATAL(...) \
237 do { \
238 __NOTICE(UNRECOVERABLE ERROR, "\n"); \
239 __ERROR_FATAL(STATUS_ASSERTION_FAILURE, __VA_ARGS__); \
240 } while (0)
241
242 #define UNIMPLEMENTED_FATAL(...) \
243 do { \
244 __NOTICE(UNRECOVERABLE ERROR, "is UNIMPLEMENTED!\n"); \
245 __ERROR_FATAL(STATUS_NOT_IMPLEMENTED, __VA_ARGS__); \
246 } while (0)
247 /******************************************************************************/
248
249 #define ASSERT_IRQL_LESS_OR_EQUAL(x) ASSERT(KeGetCurrentIrql()<=(x))
250 #define ASSERT_IRQL_EQUAL(x) ASSERT(KeGetCurrentIrql()==(x))
251 #define ASSERT_IRQL_LESS(x) ASSERT(KeGetCurrentIrql()<(x))
252
253 #define __STRING2__(x) #x
254 #define __STRING__(x) __STRING2__(x)
255 #define __STRLINE__ __STRING__(__LINE__)
256
257 #if !defined(_MSC_VER) && !defined(__pragma)
258 #define __pragma(x) _Pragma(#x)
259 #endif
260
261 #define _WARN(msg) __pragma(message("WARNING! Line " __STRLINE__ ": " msg))
262
263 /* EOF */