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