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