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