]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Include/Library/DebugLib.h
MdePkg/DebugLib.h: Add new APIs for DebugLib
[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 - 2019, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials are licensed and made available under
13 the terms and conditions of the BSD License that accompanies this distribution.
14 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 (pool)
43 #define DEBUG_PAGE 0x00000020 // Alloc & Free (page)
44 #define DEBUG_INFO 0x00000040 // Informational debug messages
45 #define DEBUG_DISPATCH 0x00000080 // PEI/DXE/SMM 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 // Network Io Driver
50 #define DEBUG_UNDI 0x00010000 // UNDI Driver
51 #define DEBUG_LOADFILE 0x00020000 // LoadFile
52 #define DEBUG_EVENT 0x00080000 // Event messages
53 #define DEBUG_GCD 0x00100000 // Global Coherency Database changes
54 #define DEBUG_CACHE 0x00200000 // Memory range cachability changes
55 #define DEBUG_VERBOSE 0x00400000 // Detailed debug messages that may
56 // significantly impact boot performance
57 #define DEBUG_ERROR 0x80000000 // Error
58
59 //
60 // Aliases of debug message mask bits
61 //
62 #define EFI_D_INIT DEBUG_INIT
63 #define EFI_D_WARN DEBUG_WARN
64 #define EFI_D_LOAD DEBUG_LOAD
65 #define EFI_D_FS DEBUG_FS
66 #define EFI_D_POOL DEBUG_POOL
67 #define EFI_D_PAGE DEBUG_PAGE
68 #define EFI_D_INFO DEBUG_INFO
69 #define EFI_D_DISPATCH DEBUG_DISPATCH
70 #define EFI_D_VARIABLE DEBUG_VARIABLE
71 #define EFI_D_BM DEBUG_BM
72 #define EFI_D_BLKIO DEBUG_BLKIO
73 #define EFI_D_NET DEBUG_NET
74 #define EFI_D_UNDI DEBUG_UNDI
75 #define EFI_D_LOADFILE DEBUG_LOADFILE
76 #define EFI_D_EVENT DEBUG_EVENT
77 #define EFI_D_VERBOSE DEBUG_VERBOSE
78 #define EFI_D_ERROR DEBUG_ERROR
79
80 /**
81 Prints a debug message to the debug output device if the specified error level is enabled.
82
83 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
84 GetDebugPrintErrorLevel (), then print the message specified by Format and the
85 associated variable argument list to the debug output device.
86
87 If Format is NULL, then ASSERT().
88
89 @param ErrorLevel The error level of the debug message.
90 @param Format The format string for the debug message to print.
91 @param ... The variable argument list whose contents are accessed
92 based on the format string specified by Format.
93
94 **/
95 VOID
96 EFIAPI
97 DebugPrint (
98 IN UINTN ErrorLevel,
99 IN CONST CHAR8 *Format,
100 ...
101 );
102
103
104 /**
105 Prints a debug message to the debug output device if the specified
106 error level is enabled.
107
108 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
109 GetDebugPrintErrorLevel (), then print the message specified by Format and
110 the associated variable argument list to the debug output device.
111
112 If Format is NULL, then ASSERT().
113
114 @param ErrorLevel The error level of the debug message.
115 @param Format Format string for the debug message to print.
116 @param VaListMarker VA_LIST marker for the variable argument list.
117
118 **/
119 VOID
120 EFIAPI
121 DebugVPrint (
122 IN UINTN ErrorLevel,
123 IN CONST CHAR8 *Format,
124 IN VA_LIST VaListMarker
125 );
126
127
128 /**
129 Prints a debug message to the debug output device if the specified
130 error level is enabled.
131 This function use BASE_LIST which would provide a more compatible
132 service than VA_LIST.
133
134 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
135 GetDebugPrintErrorLevel (), then print the message specified by Format and
136 the associated variable argument list to the debug output device.
137
138 If Format is NULL, then ASSERT().
139
140 @param ErrorLevel The error level of the debug message.
141 @param Format Format string for the debug message to print.
142 @param BaseListMarker BASE_LIST marker for the variable argument list.
143
144 **/
145 VOID
146 EFIAPI
147 DebugBPrint (
148 IN UINTN ErrorLevel,
149 IN CONST CHAR8 *Format,
150 IN BASE_LIST BaseListMarker
151 );
152
153
154 /**
155 Prints an assert message containing a filename, line number, and description.
156 This may be followed by a breakpoint or a dead loop.
157
158 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
159 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
160 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
161 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
162 CpuDeadLoop() is called. If neither of these bits are set, then this function
163 returns immediately after the message is printed to the debug output device.
164 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
165 processing another DebugAssert(), then DebugAssert() must return immediately.
166
167 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
168 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
169
170 @param FileName The pointer to the name of the source file that generated the assert condition.
171 @param LineNumber The line number in the source file that generated the assert condition
172 @param Description The pointer to the description of the assert condition.
173
174 **/
175 VOID
176 EFIAPI
177 DebugAssert (
178 IN CONST CHAR8 *FileName,
179 IN UINTN LineNumber,
180 IN CONST CHAR8 *Description
181 );
182
183
184 /**
185 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
186
187 This function fills Length bytes of Buffer with the value specified by
188 PcdDebugClearMemoryValue, and returns Buffer.
189
190 If Buffer is NULL, then ASSERT().
191 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
192
193 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
194 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
195
196 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
197
198 **/
199 VOID *
200 EFIAPI
201 DebugClearMemory (
202 OUT VOID *Buffer,
203 IN UINTN Length
204 );
205
206
207 /**
208 Returns TRUE if ASSERT() macros are enabled.
209
210 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
211 PcdDebugProperyMask is set. Otherwise, FALSE is returned.
212
213 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
214 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
215
216 **/
217 BOOLEAN
218 EFIAPI
219 DebugAssertEnabled (
220 VOID
221 );
222
223
224 /**
225 Returns TRUE if DEBUG() macros are enabled.
226
227 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
228 PcdDebugProperyMask is set. Otherwise, FALSE is returned.
229
230 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
231 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
232
233 **/
234 BOOLEAN
235 EFIAPI
236 DebugPrintEnabled (
237 VOID
238 );
239
240
241 /**
242 Returns TRUE if DEBUG_CODE() macros are enabled.
243
244 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
245 PcdDebugProperyMask is set. Otherwise, FALSE is returned.
246
247 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
248 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
249
250 **/
251 BOOLEAN
252 EFIAPI
253 DebugCodeEnabled (
254 VOID
255 );
256
257
258 /**
259 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
260
261 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
262 PcdDebugProperyMask is set. Otherwise, FALSE is returned.
263
264 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
265 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
266
267 **/
268 BOOLEAN
269 EFIAPI
270 DebugClearMemoryEnabled (
271 VOID
272 );
273
274 /**
275 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
276
277 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
278
279 @retval TRUE Current ErrorLevel is supported.
280 @retval FALSE Current ErrorLevel is not supported.
281
282 **/
283 BOOLEAN
284 EFIAPI
285 DebugPrintLevelEnabled (
286 IN CONST UINTN ErrorLevel
287 );
288
289 /**
290 Internal worker macro that calls DebugAssert().
291
292 This macro calls DebugAssert(), passing in the filename, line number, and an
293 expression that evaluated to FALSE.
294
295 @param Expression Boolean expression that evaluated to FALSE
296
297 **/
298 #define _ASSERT(Expression) DebugAssert (__FILE__, __LINE__, #Expression)
299
300
301 /**
302 Internal worker macro that calls DebugPrint().
303
304 This macro calls DebugPrint() passing in the debug error level, a format
305 string, and a variable argument list.
306 __VA_ARGS__ is not supported by EBC compiler, Microsoft Visual Studio .NET 2003
307 and Microsoft Windows Server 2003 Driver Development Kit (Microsoft WINDDK) version 3790.1830.
308
309 @param Expression Expression containing an error level, a format string,
310 and a variable argument list based on the format string.
311
312 **/
313
314 #if !defined(MDE_CPU_EBC) && (!defined (_MSC_VER) || _MSC_VER > 1400)
315 #define _DEBUG_PRINT(PrintLevel, ...) \
316 do { \
317 if (DebugPrintLevelEnabled (PrintLevel)) { \
318 DebugPrint (PrintLevel, ##__VA_ARGS__); \
319 } \
320 } while (FALSE)
321 #define _DEBUG(Expression) _DEBUG_PRINT Expression
322 #else
323 #define _DEBUG(Expression) DebugPrint Expression
324 #endif
325
326 /**
327 Macro that calls DebugAssert() if an expression evaluates to FALSE.
328
329 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
330 bit of PcdDebugProperyMask is set, then this macro evaluates the Boolean
331 expression specified by Expression. If Expression evaluates to FALSE, then
332 DebugAssert() is called passing in the source filename, source line number,
333 and Expression.
334
335 @param Expression Boolean expression.
336
337 **/
338 #if !defined(MDEPKG_NDEBUG)
339 #define ASSERT(Expression) \
340 do { \
341 if (DebugAssertEnabled ()) { \
342 if (!(Expression)) { \
343 _ASSERT (Expression); \
344 ANALYZER_UNREACHABLE (); \
345 } \
346 } \
347 } while (FALSE)
348 #else
349 #define ASSERT(Expression)
350 #endif
351
352 /**
353 Macro that calls DebugPrint().
354
355 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
356 bit of PcdDebugProperyMask is set, then this macro passes Expression to
357 DebugPrint().
358
359 @param Expression Expression containing an error level, a format string,
360 and a variable argument list based on the format string.
361
362
363 **/
364 #if !defined(MDEPKG_NDEBUG)
365 #define DEBUG(Expression) \
366 do { \
367 if (DebugPrintEnabled ()) { \
368 _DEBUG (Expression); \
369 } \
370 } while (FALSE)
371 #else
372 #define DEBUG(Expression)
373 #endif
374
375 /**
376 Macro that calls DebugAssert() if an EFI_STATUS evaluates to an error code.
377
378 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
379 bit of PcdDebugProperyMask is set, then this macro evaluates the EFI_STATUS
380 value specified by StatusParameter. If StatusParameter is an error code,
381 then DebugAssert() is called passing in the source filename, source line
382 number, and StatusParameter.
383
384 @param StatusParameter EFI_STATUS value to evaluate.
385
386 **/
387 #if !defined(MDEPKG_NDEBUG)
388 #define ASSERT_EFI_ERROR(StatusParameter) \
389 do { \
390 if (DebugAssertEnabled ()) { \
391 if (EFI_ERROR (StatusParameter)) { \
392 DEBUG ((EFI_D_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", StatusParameter)); \
393 _ASSERT (!EFI_ERROR (StatusParameter)); \
394 } \
395 } \
396 } while (FALSE)
397 #else
398 #define ASSERT_EFI_ERROR(StatusParameter)
399 #endif
400
401 /**
402 Macro that calls DebugAssert() if a RETURN_STATUS evaluates to an error code.
403
404 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
405 bit of PcdDebugProperyMask is set, then this macro evaluates the
406 RETURN_STATUS value specified by StatusParameter. If StatusParameter is an
407 error code, then DebugAssert() is called passing in the source filename,
408 source line number, and StatusParameter.
409
410 @param StatusParameter RETURN_STATUS value to evaluate.
411
412 **/
413 #if !defined(MDEPKG_NDEBUG)
414 #define ASSERT_RETURN_ERROR(StatusParameter) \
415 do { \
416 if (DebugAssertEnabled ()) { \
417 if (RETURN_ERROR (StatusParameter)) { \
418 DEBUG ((DEBUG_ERROR, "\nASSERT_RETURN_ERROR (Status = %r)\n", \
419 StatusParameter)); \
420 _ASSERT (!RETURN_ERROR (StatusParameter)); \
421 } \
422 } \
423 } while (FALSE)
424 #else
425 #define ASSERT_RETURN_ERROR(StatusParameter)
426 #endif
427
428 /**
429 Macro that calls DebugAssert() if a protocol is already installed in the
430 handle database.
431
432 If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
433 of PcdDebugProperyMask is clear, then return.
434
435 If Handle is NULL, then a check is made to see if the protocol specified by Guid
436 is present on any handle in the handle database. If Handle is not NULL, then
437 a check is made to see if the protocol specified by Guid is present on the
438 handle specified by Handle. If the check finds the protocol, then DebugAssert()
439 is called passing in the source filename, source line number, and Guid.
440
441 If Guid is NULL, then ASSERT().
442
443 @param Handle The handle to check for the protocol. This is an optional
444 parameter that may be NULL. If it is NULL, then the entire
445 handle database is searched.
446
447 @param Guid The pointer to a protocol GUID.
448
449 **/
450 #if !defined(MDEPKG_NDEBUG)
451 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) \
452 do { \
453 if (DebugAssertEnabled ()) { \
454 VOID *Instance; \
455 ASSERT (Guid != NULL); \
456 if (Handle == NULL) { \
457 if (!EFI_ERROR (gBS->LocateProtocol ((EFI_GUID *)Guid, NULL, &Instance))) { \
458 _ASSERT (Guid already installed in database); \
459 } \
460 } else { \
461 if (!EFI_ERROR (gBS->HandleProtocol (Handle, (EFI_GUID *)Guid, &Instance))) { \
462 _ASSERT (Guid already installed on Handle); \
463 } \
464 } \
465 } \
466 } while (FALSE)
467 #else
468 #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid)
469 #endif
470
471 /**
472 Macro that marks the beginning of debug source code.
473
474 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
475 then this macro marks the beginning of source code that is included in a module.
476 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
477 are not included in a module.
478
479 **/
480 #define DEBUG_CODE_BEGIN() do { if (DebugCodeEnabled ()) { UINT8 __DebugCodeLocal
481
482
483 /**
484 The macro that marks the end of debug source code.
485
486 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
487 then this macro marks the end of source code that is included in a module.
488 Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END()
489 are not included in a module.
490
491 **/
492 #define DEBUG_CODE_END() __DebugCodeLocal = 0; __DebugCodeLocal++; } } while (FALSE)
493
494
495 /**
496 The macro that declares a section of debug source code.
497
498 If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set,
499 then the source code specified by Expression is included in a module.
500 Otherwise, the source specified by Expression is not included in a module.
501
502 **/
503 #define DEBUG_CODE(Expression) \
504 DEBUG_CODE_BEGIN (); \
505 Expression \
506 DEBUG_CODE_END ()
507
508
509 /**
510 The macro that calls DebugClearMemory() to clear a buffer to a default value.
511
512 If the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set,
513 then this macro calls DebugClearMemory() passing in Address and Length.
514
515 @param Address The pointer to a buffer.
516 @param Length The number of bytes in the buffer to set.
517
518 **/
519 #define DEBUG_CLEAR_MEMORY(Address, Length) \
520 do { \
521 if (DebugClearMemoryEnabled ()) { \
522 DebugClearMemory (Address, Length); \
523 } \
524 } while (FALSE)
525
526
527 /**
528 Macro that calls DebugAssert() if the containing record does not have a
529 matching signature. If the signatures matches, then a pointer to the data
530 structure that contains a specified field of that data structure is returned.
531 This is a lightweight method hide information by placing a public data
532 structure inside a larger private data structure and using a pointer to the
533 public data structure to retrieve a pointer to the private data structure.
534
535 If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
536 of PcdDebugProperyMask is clear, then this macro computes the offset, in bytes,
537 of the field specified by Field from the beginning of the data structure specified
538 by TYPE. This offset is subtracted from Record, and is used to return a pointer
539 to a data structure of the type specified by TYPE.
540
541 If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
542 of PcdDebugProperyMask is set, then this macro computes the offset, in bytes,
543 of field specified by Field from the beginning of the data structure specified
544 by TYPE. This offset is subtracted from Record, and is used to compute a pointer
545 to a data structure of the type specified by TYPE. The Signature field of the
546 data structure specified by TYPE is compared to TestSignature. If the signatures
547 match, then a pointer to the pointer to a data structure of the type specified by
548 TYPE is returned. If the signatures do not match, then DebugAssert() is called
549 with a description of "CR has a bad signature" and Record is returned.
550
551 If the data type specified by TYPE does not contain the field specified by Field,
552 then the module will not compile.
553
554 If TYPE does not contain a field called Signature, then the module will not
555 compile.
556
557 @param Record The pointer to the field specified by Field within a data
558 structure of type TYPE.
559
560 @param TYPE The name of the data structure type to return This
561 data structure must contain the field specified by Field.
562
563 @param Field The name of the field in the data structure specified
564 by TYPE to which Record points.
565
566 @param TestSignature The 32-bit signature value to match.
567
568 **/
569 #if !defined(MDEPKG_NDEBUG)
570 #define CR(Record, TYPE, Field, TestSignature) \
571 (DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
572 (TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
573 BASE_CR (Record, TYPE, Field)
574 #else
575 #define CR(Record, TYPE, Field, TestSignature) \
576 BASE_CR (Record, TYPE, Field)
577 #endif
578
579 #endif