a8923711c21cd87aaa2fca8cfc5ee885a2266577
[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 #define __BCRYPT_H__ /* Hack for NDK - umtypes.h */
56 typedef _Return_type_success_(return >= 0) LONG NTSTATUS, *PNTSTATUS;
57 #endif
58 __analysis_noreturn
59 NTSYSAPI
60 VOID
61 NTAPI
62 RtlRaiseStatus(
63 _In_ NTSTATUS Status
64 );
65
66 #endif /* !defined(_RTLFUNCS_H) && !defined(_NTDDK_) */
67
68
69 /* Fix usage of RtlRaiseStatus */
70 #if !defined(_RTLFUNCS_H) && defined(_NTDDK_)
71 #define RaiseStatus ExRaiseStatus
72 #else
73 #define RaiseStatus RtlRaiseStatus
74 #endif /* !defined(_RTLFUNCS_H) && defined(_NTDDK_) */
75
76
77 #ifndef assert
78 #ifndef NASSERT
79 #define assert(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__FILE__, __LINE__, ""); }
80 #else
81 #define assert(x)
82 #endif
83 #endif
84
85 #ifndef ASSERT
86 #ifndef NASSERT
87 #define ASSERT(x) if (!(x)) { RtlAssert((PVOID)#x, (PVOID)__FILE__, __LINE__, ""); }
88 #else
89 #define ASSERT(x)
90 #endif
91 #endif
92
93 #ifndef ASSERTMSG
94 #ifndef NASSERT
95 #define ASSERTMSG(x,m) if (!(x)) { RtlAssert((PVOID)#x, __FILE__, __LINE__, m); }
96 #else
97 #define ASSERTMSG(x)
98 #endif
99 #endif
100
101 /* For internal purposes only */
102 #define __NOTICE(level, fmt, ...) DbgPrint(#level ": %s at %s:%d " fmt, __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
103
104 /* Print stuff only on Debug Builds*/
105 #define DPFLTR_DEFAULT_ID -1
106 #if DBG
107
108 /* These are always printed */
109 #define DPRINT1(fmt, ...) do { \
110 if (DbgPrint("(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)) \
111 DbgPrint("(%s:%d) DbgPrint() failed!\n", __FILE__, __LINE__); \
112 } while (0)
113
114 /* These are printed only if NDEBUG is NOT defined */
115 #ifndef NDEBUG
116
117 #define DPRINT(fmt, ...) do { \
118 if (DbgPrint("(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)) \
119 DbgPrint("(%s:%d) DbgPrint() failed!\n", __FILE__, __LINE__); \
120 } while (0)
121
122 #else
123
124 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
125
126 #endif
127
128 #define UNIMPLEMENTED __NOTICE(WARNING, "is UNIMPLEMENTED!\n");
129
130 #define ERR_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
131 #define WARN_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
132 #define TRACE_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
133 #define INFO_(ch, fmt, ...) DbgPrintEx(DPFLTR_##ch##_ID, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
134
135 #define ERR__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_ERROR_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
136 #define WARN__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_WARNING_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
137 #define TRACE__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_TRACE_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
138 #define INFO__(ch, fmt, ...) DbgPrintEx(ch, DPFLTR_INFO_LEVEL, "(%s:%d) " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
139
140 #else /* not DBG */
141
142 /* On non-debug builds, we never show these */
143 #define DPRINT1(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
144 #define DPRINT(...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
145
146 #define UNIMPLEMENTED
147
148 #define ERR_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
149 #define WARN_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
150 #define TRACE_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
151 #define INFO_(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
152
153 #define ERR__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
154 #define WARN__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
155 #define TRACE__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
156 #define INFO__(ch, ...) do { if(0) { DbgPrint(__VA_ARGS__); } } while(0)
157
158 #endif /* not DBG */
159
160 /******************************************************************************/
161 /* For internal purposes only */
162 #define __ERROR_DBGBREAK(...) \
163 do { \
164 DbgPrint("" __VA_ARGS__); \
165 DbgBreakPoint(); \
166 } while (0)
167
168 /* For internal purposes only */
169 #define __ERROR_FATAL(Status, ...) \
170 do { \
171 DbgPrint("" __VA_ARGS__); \
172 RaiseStatus((Status)); \
173 } while (0)
174
175 /*
176 * These macros are designed to display an optional printf-like
177 * user-defined message and to break into the debugger.
178 * After that they allow to continue the program execution.
179 */
180 #define ERROR_DBGBREAK(...) \
181 do { \
182 __NOTICE(ERROR, "\n"); \
183 __ERROR_DBGBREAK(__VA_ARGS__); \
184 } while (0)
185
186 #define UNIMPLEMENTED_DBGBREAK(...) \
187 do { \
188 __NOTICE(ERROR, "is UNIMPLEMENTED!\n"); \
189 __ERROR_DBGBREAK(__VA_ARGS__); \
190 } while (0)
191
192 /*
193 * These macros are designed to display an optional printf-like
194 * user-defined message and to break into the debugger.
195 * After that they halt the execution of the current thread.
196 */
197 #define ERROR_FATAL(...) \
198 do { \
199 __NOTICE(UNRECOVERABLE ERROR, "\n"); \
200 __ERROR_FATAL(STATUS_ASSERTION_FAILURE, __VA_ARGS__); \
201 } while (0)
202
203 #define UNIMPLEMENTED_FATAL(...) \
204 do { \
205 __NOTICE(UNRECOVERABLE ERROR, "is UNIMPLEMENTED!\n"); \
206 __ERROR_FATAL(STATUS_NOT_IMPLEMENTED, __VA_ARGS__); \
207 } while (0)
208 /******************************************************************************/
209
210 #define ASSERT_IRQL_LESS_OR_EQUAL(x) ASSERT(KeGetCurrentIrql()<=(x))
211 #define ASSERT_IRQL_EQUAL(x) ASSERT(KeGetCurrentIrql()==(x))
212 #define ASSERT_IRQL_LESS(x) ASSERT(KeGetCurrentIrql()<(x))
213
214 #endif /* __INTERNAL_DEBUG */