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