]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/DebugLib.h
wrap ASSERT_LOCKED macro with MDEPKG_NDEBUG.
[mirror_edk2.git] / MdePkg / Include / Library / DebugLib.h
1 /** @file
2 Provides services to print debug and assert messages to a debug output device.
3
4 The Debug library supports debug print and asserts based on a combination of macros and code.
5 The debug library can be turned on and off so that the debug code does not increase the size of an image.
6
7 Note that a reserved macro named MDEPKG_NDEBUG is introduced for the intention
8 of size reduction when compiler optimization is disabled. If MDEPKG_NDEBUG is
9 defined, then debug and assert related macros wrapped by it are the NULL implementations.
10
11 Copyright (c) 2006 - 2010, Intel Corporation<BR>
12 All rights reserved. This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
16
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19
20 **/
21
22 #ifndef __DEBUG_LIB_H__
23 #define __DEBUG_LIB_H__
24
25 //
26 // Declare bits for PcdDebugPropertyMask
27 //
28 #define DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 0x01
29 #define DEBUG_PROPERTY_DEBUG_PRINT_ENABLED 0x02
30 #define DEBUG_PROPERTY_DEBUG_CODE_ENABLED 0x04
31 #define DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED 0x08
32 #define DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED 0x10
33 #define DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED 0x20
34
35 //
36 // Declare bits for PcdDebugPrintErrorLevel and the ErrorLevel parameter of DebugPrint()
37 //
38 #define DEBUG_INIT 0x00000001 // Initialization
39 #define DEBUG_WARN 0x00000002 // Warnings
40 #define DEBUG_LOAD 0x00000004 // Load events
41 #define DEBUG_FS 0x00000008 // EFI File system
42 #define DEBUG_POOL 0x00000010 // Alloc & Free's
43 #define DEBUG_PAGE 0x00000020 // Alloc & Free's
44 #define DEBUG_INFO 0x00000040 // Verbose
45 #define DEBUG_DISPATCH 0x00000080 // PEI/DXE Dispatchers
46 #define DEBUG_VARIABLE 0x00000100 // Variable
47 #define DEBUG_BM 0x00000400 // Boot Manager
48 #define DEBUG_BLKIO 0x00001000 // BlkIo Driver
49 #define DEBUG_NET 0x00004000 // SNI Driver
50 #define DEBUG_UNDI 0x00010000 // UNDI Driver
51 #define DEBUG_LOADFILE 0x00020000 // UNDI Driver
52 #define DEBUG_EVENT 0x00080000 // Event messages
53 #define DEBUG_ERROR 0x80000000 // Error
54
55 //
56 // Aliases of debug message mask bits
57 //
58 #define EFI_D_INIT DEBUG_INIT
59 #define EFI_D_WARN DEBUG_WARN
60 #define EFI_D_LOAD DEBUG_LOAD
61 #define EFI_D_FS DEBUG_FS
62 #define EFI_D_POOL DEBUG_POOL
63 #define EFI_D_PAGE DEBUG_PAGE
64 #define EFI_D_INFO DEBUG_INFO
65 #define EFI_D_DISPATCH DEBUG_DISPATCH
66 #define EFI_D_VARIABLE DEBUG_VARIABLE
67 #define EFI_D_BM DEBUG_BM
68 #define EFI_D_BLKIO DEBUG_BLKIO
69 #define EFI_D_NET DEBUG_NET
70 #define EFI_D_UNDI DEBUG_UNDI
71 #define EFI_D_LOADFILE DEBUG_LOADFILE
72 #define EFI_D_EVENT DEBUG_EVENT
73 #define EFI_D_ERROR DEBUG_ERROR
74
75 /**
76 Prints a debug message to the debug output device if the specified error level is enabled.
77
78 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
79 the message specified by Format and the associated variable argument list to
80 the debug output device.
81
82 If Format is NULL, then ASSERT().
83
84 @param ErrorLevel The error level of the debug message.
85 @param Format Format string for the debug message to print.
86 @param ... Variable argument list whose contents are accessed
87 based on the format string specified by Format.
88
89 **/
90 VOID
91 EFIAPI
92 DebugPrint (
93 IN UINTN ErrorLevel,
94 IN CONST CHAR8 *Format,
95 ...
96 );
97
98
99 /**
100 Prints an assert message containing a filename, line number, and description.
101 This may be followed by a breakpoint or a dead loop.
102
103 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
104 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
105 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
106 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
107 CpuDeadLoop() is called. If neither of these bits are set, then this function
108 returns immediately after the message is printed to the debug output device.
109 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
110 processing another DebugAssert(), then DebugAssert() must return immediately.
111
112 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
113 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
114
115 @param FileName Pointer to the name of the source file that generated the assert condition.
116 @param LineNumber The line number in the source file that generated the assert condition
117 @param Description Pointer to the description of the assert condition.
118
119 **/
120 VOID
121 EFIAPI
122 DebugAssert (
123 IN CONST CHAR8 *FileName,
124 IN UINTN LineNumber,
125 IN CONST CHAR8 *Description
126 );
127
128
129 /**
130 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
131
132 This function fills Length bytes of Buffer with the value specified by
133 PcdDebugClearMemoryValue, and returns Buffer.
134
135 If Buffer is NULL, then ASSERT().
136 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
137
138 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
139 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
140
141 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
142
143 **/
144 VOID *
145 EFIAPI
146 DebugClearMemory (
147 OUT VOID *Buffer,
148 IN UINTN Length
149 );
150
151
152 /**
153 Returns TRUE if ASSERT() macros are enabled.
154
155 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
156 PcdDebugProperyMask is set. Otherwise FALSE is returned.
157
158 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
159 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
160
161 **/
162 BOOLEAN
163 EFIAPI
164 DebugAssertEnabled (
165 VOID
166 );
167
168
169 /**
170 Returns TRUE if DEBUG() macros are enabled.
171
172 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
173 PcdDebugProperyMask is set. Otherwise FALSE is returned.
174
175 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
176 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
177
178 **/
179 BOOLEAN
180 EFIAPI
181 DebugPrintEnabled (
182 VOID
183 );
184
185
186 /**
187 Returns TRUE if DEBUG_CODE() macros are enabled.
188
189 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
190 PcdDebugProperyMask is set. Otherwise FALSE is returned.
191
192 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
193 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
194
195 **/
196 BOOLEAN
197 EFIAPI
198 DebugCodeEnabled (
199 VOID
200 );
201
202
203 /**
204 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
205
206 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
207 PcdDebugProperyMask is set. Otherwise FALSE is returned.
208
209 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
210 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
211
212 **/
213 BOOLEAN
214 EFIAPI
215 DebugClearMemoryEnabled (
216 VOID
217 );
218
219
220 /**
221 Internal worker macro that calls DebugAssert().
222
223 This macro calls DebugAssert(), passing in the filename, line number, and an
224 expression that evaluated to FALSE.
225
226 @param Expression Boolean expression that evaluated to FALSE
227
228 **/
229 #define _ASSERT(Expression) DebugAssert (__FILE__, __LINE__, #Expression)
230
231
232 /**
233 Internal worker macro that calls DebugPrint().
234
235 This macro calls DebugPrint() passing in the debug error level, a format
236 string, and a variable argument list.
237
238 @param Expression Expression containing an error level, a format string,
239 and a variable argument list based on the format string.
240
241 **/
242 #define _DEBUG(Expression) DebugPrint Expression
243
244
245 /**
246 Macro that calls DebugAssert() if an expression evaluates to FALSE.
247
248 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
249 bit of PcdDebugProperyMask is set, then this macro evaluates the Boolean
250 expression specified by Expression. If Expression evaluates to FALSE, then
251 DebugAssert() is called passing in the source filename, source line number,
252 and Expression.
253
254 @param Expression Boolean expression
255
256 **/
257 #if !defined(MDEPKG_NDEBUG)
258 #define ASSERT(Expression) \
259 do { \
260 if (DebugAssertEnabled ()) { \
261 if (!(Expression)) { \
262 _ASSERT (Expression); \
263 } \
264 } \
265 } while (FALSE)
266 #else
267 #define ASSERT(Expression)
268 #endif
269
270 /**
271 Macro that calls DebugPrint().
272
273 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
274 bit of PcdDebugProperyMask is set, then this macro passes Expression to
275 DebugPrint().
276
277 @param Expression Expression containing an error level, a format string,
278 and a variable argument list based on the format string.
279
280
281 **/
282 #if !defined(MDEPKG_NDEBUG)
283 #define DEBUG(Expression) \
284 do { \
285 if (DebugPrintEnabled ()) { \
286 _DEBUG (Expression); \
287 } \
288 } while (FALSE)
289 #else
290 #define DEBUG(Expression)
291 #endif
292
293 /**
294 Macro that calls DebugAssert() if an EFI_STATUS evaluates to an error code.
295
296 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
297 bit of PcdDebugProperyMask is set, then this macro evaluates the EFI_STATUS
298 value specified by StatusParameter. If StatusParameter is an error code,
299 then DebugAssert() is called passing in the source filename, source line
300 number, and StatusParameter.
301
302 @param StatusParameter EFI_STATUS value to evaluate.
303
304 **/
305 #if !defined(MDEPKG_NDEBUG)
306 #define ASSERT_EFI_ERROR(StatusParameter) \
307 do { \
308 if (DebugAssertEnabled ()) { \
309 if (EFI_ERROR (StatusParameter)) { \
310 DEBUG ((EFI_D_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", StatusParameter)); \
311 _ASSERT (!EFI_ERROR (StatusParameter)); \
312 } \
313 } \
314 } while (FALSE)
315 #else
316 #define ASSERT_EFI_ERROR(StatusParameter)
317 #endif
318
319 /**
320 Macro that calls DebugAssert() if a protocol is already installed in the
321 handle database.
322
323 If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
324 of PcdDebugProperyMask is clear, then return.
325
326 If Handle is NULL, then a check is made to see if the protocol specified by Guid
327 is present on any handle in the handle database. If Handle is not NULL, then
328 a check is made to see if the protocol specified by Guid is present on the
329 handle specified by Handle. If the check finds the protocol, then DebugAssert()
330 is called passing in the source filename, source line number, and Guid.
331
332 If Guid is NULL, then ASSERT().
333
334 @param Handle The handle to check for the protocol. This is an optional
335 parameter that may be NULL. If it is NULL, then the entire
336 handle database is searched.
337
338 @param Guid Pointer to a protocol GUID.
339
340 **/
341 #if !defined(MDEPKG_NDEBUG)
342 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) \
343 do { \
344 if (DebugAssertEnabled ()) { \
345 VOID *Instance; \
346 ASSERT (Guid != NULL); \
347 if (Handle == NULL) { \
348 if (!EFI_ERROR (gBS->LocateProtocol ((EFI_GUID *)Guid, NULL, &Instance))) { \
349 _ASSERT (Guid already installed in database); \
350 } \
351 } else { \
352 if (!EFI_ERROR (gBS->HandleProtocol (Handle, (EFI_GUID *)Guid, &Instance))) { \
353 _ASSERT (Guid already installed on Handle); \
354 } \
355 } \
356 } \
357 } while (FALSE)
358 #else
359 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid)
360 #endif
361
362 /**
363 Macro that marks the beginning of debug source code.
364
365 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
366 then this macro marks the beginning of source code that is included in a module.
367 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
368 are not included in a module.
369
370 **/
371 #define DEBUG_CODE_BEGIN() do { if (DebugCodeEnabled ()) { UINT8 __DebugCodeLocal
372
373
374 /**
375 Macro that marks the end of debug source code.
376
377 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
378 then this macro marks the end of source code that is included in a module.
379 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
380 are not included in a module.
381
382 **/
383 #define DEBUG_CODE_END() __DebugCodeLocal = 0; __DebugCodeLocal++; } } while (FALSE)
384
385
386 /**
387 Macro that declares a section of debug source code.
388
389 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
390 then the source code specified by Expression is included in a module.
391 Otherwise, the source specified by Expression is not included in a module.
392
393 **/
394 #define DEBUG_CODE(Expression) \
395 DEBUG_CODE_BEGIN (); \
396 Expression \
397 DEBUG_CODE_END ()
398
399
400 /**
401 Macro that calls DebugClearMemory() to clear a buffer to a default value.
402
403 If the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set,
404 then this macro calls DebugClearMemory() passing in Address and Length.
405
406 @param Address Pointer to a buffer.
407 @param Length The number of bytes in the buffer to set.
408
409 **/
410 #define DEBUG_CLEAR_MEMORY(Address, Length) \
411 do { \
412 if (DebugClearMemoryEnabled ()) { \
413 DebugClearMemory (Address, Length); \
414 } \
415 } while (FALSE)
416
417
418 /**
419 Macro that calls DebugAssert() if the containing record does not have a
420 matching signature. If the signatures matches, then a pointer to the data
421 structure that contains a specified field of that data structure is returned.
422 This is a light weight method hide information by placing a public data
423 structure inside a larger private data structure and using a pointer to the
424 public data structure to retrieve a pointer to the private data structure.
425
426 If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
427 of PcdDebugProperyMask is clear, then this macro computes the offset, in bytes,
428 of field specified by Field from the beginning of the data structure specified
429 by TYPE. This offset is subtracted from Record, and is used to return a pointer
430 to a data structure of the type specified by TYPE.
431
432 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
433 of PcdDebugProperyMask is set, then this macro computes the offset, in bytes,
434 of field specified by Field from the beginning of the data structure specified
435 by TYPE. This offset is subtracted from Record, and is used to compute a pointer
436 to a data structure of the type specified by TYPE. The Signature field of the
437 data structure specified by TYPE is compared to TestSignature. If the signatures
438 match, then a pointer to the pointer to a data structure of the type specified by
439 TYPE is returned. If the signatures do not match, then DebugAssert() is called
440 with a description of "CR has a bad signature" and Record is returned.
441
442 If the data type specified by TYPE does not contain the field specified by Field,
443 then the module will not compile.
444
445 If TYPE does not contain a field called Signature, then the module will not
446 compile.
447
448 @param Record Pointer to the field specified by Field within a data
449 structure of type TYPE.
450
451 @param TYPE The name of the data structure type to return This
452 data structure must contain the field specified by Field.
453
454 @param Field The name of the field in the data structure specified
455 by TYPE to which Record points.
456
457 @param TestSignature The 32-bit signature value to match.
458
459 **/
460 #if !defined(MDEPKG_NDEBUG)
461 #define CR(Record, TYPE, Field, TestSignature) \
462 (DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
463 (TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
464 BASE_CR (Record, TYPE, Field)
465 #else
466 #define CR(Record, TYPE, Field, TestSignature) \
467 BASE_CR (Record, TYPE, Field)
468 #endif
469
470 #endif