10d1ba665SWarner Losh /** @file 20d1ba665SWarner Losh Provides services to print debug and assert messages to a debug output device. 30d1ba665SWarner Losh 40d1ba665SWarner Losh The Debug library supports debug print and asserts based on a combination of macros and code. 50d1ba665SWarner Losh The debug library can be turned on and off so that the debug code does not increase the size of an image. 60d1ba665SWarner Losh 70d1ba665SWarner Losh Note that a reserved macro named MDEPKG_NDEBUG is introduced for the intention 80d1ba665SWarner Losh of size reduction when compiler optimization is disabled. If MDEPKG_NDEBUG is 90d1ba665SWarner Losh defined, then debug and assert related macros wrapped by it are the NULL implementations. 100d1ba665SWarner Losh 11*3245fa21SMitchell Horne Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR> 12*3245fa21SMitchell Horne SPDX-License-Identifier: BSD-2-Clause-Patent 130d1ba665SWarner Losh 140d1ba665SWarner Losh **/ 150d1ba665SWarner Losh 160d1ba665SWarner Losh #ifndef __DEBUG_LIB_H__ 170d1ba665SWarner Losh #define __DEBUG_LIB_H__ 180d1ba665SWarner Losh 190d1ba665SWarner Losh // 200d1ba665SWarner Losh // Declare bits for PcdDebugPropertyMask 210d1ba665SWarner Losh // 220d1ba665SWarner Losh #define DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 0x01 230d1ba665SWarner Losh #define DEBUG_PROPERTY_DEBUG_PRINT_ENABLED 0x02 240d1ba665SWarner Losh #define DEBUG_PROPERTY_DEBUG_CODE_ENABLED 0x04 250d1ba665SWarner Losh #define DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED 0x08 260d1ba665SWarner Losh #define DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED 0x10 270d1ba665SWarner Losh #define DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED 0x20 280d1ba665SWarner Losh 290d1ba665SWarner Losh // 300d1ba665SWarner Losh // Declare bits for PcdDebugPrintErrorLevel and the ErrorLevel parameter of DebugPrint() 310d1ba665SWarner Losh // 320d1ba665SWarner Losh #define DEBUG_INIT 0x00000001 // Initialization 330d1ba665SWarner Losh #define DEBUG_WARN 0x00000002 // Warnings 340d1ba665SWarner Losh #define DEBUG_LOAD 0x00000004 // Load events 350d1ba665SWarner Losh #define DEBUG_FS 0x00000008 // EFI File system 360d1ba665SWarner Losh #define DEBUG_POOL 0x00000010 // Alloc & Free (pool) 370d1ba665SWarner Losh #define DEBUG_PAGE 0x00000020 // Alloc & Free (page) 380d1ba665SWarner Losh #define DEBUG_INFO 0x00000040 // Informational debug messages 390d1ba665SWarner Losh #define DEBUG_DISPATCH 0x00000080 // PEI/DXE/SMM Dispatchers 400d1ba665SWarner Losh #define DEBUG_VARIABLE 0x00000100 // Variable 410d1ba665SWarner Losh #define DEBUG_BM 0x00000400 // Boot Manager 420d1ba665SWarner Losh #define DEBUG_BLKIO 0x00001000 // BlkIo Driver 430d1ba665SWarner Losh #define DEBUG_NET 0x00004000 // Network Io Driver 440d1ba665SWarner Losh #define DEBUG_UNDI 0x00010000 // UNDI Driver 450d1ba665SWarner Losh #define DEBUG_LOADFILE 0x00020000 // LoadFile 460d1ba665SWarner Losh #define DEBUG_EVENT 0x00080000 // Event messages 470d1ba665SWarner Losh #define DEBUG_GCD 0x00100000 // Global Coherency Database changes 480d1ba665SWarner Losh #define DEBUG_CACHE 0x00200000 // Memory range cachability changes 490d1ba665SWarner Losh #define DEBUG_VERBOSE 0x00400000 // Detailed debug messages that may 500d1ba665SWarner Losh // significantly impact boot performance 510d1ba665SWarner Losh #define DEBUG_ERROR 0x80000000 // Error 520d1ba665SWarner Losh 530d1ba665SWarner Losh // 540d1ba665SWarner Losh // Aliases of debug message mask bits 550d1ba665SWarner Losh // 560d1ba665SWarner Losh #define EFI_D_INIT DEBUG_INIT 570d1ba665SWarner Losh #define EFI_D_WARN DEBUG_WARN 580d1ba665SWarner Losh #define EFI_D_LOAD DEBUG_LOAD 590d1ba665SWarner Losh #define EFI_D_FS DEBUG_FS 600d1ba665SWarner Losh #define EFI_D_POOL DEBUG_POOL 610d1ba665SWarner Losh #define EFI_D_PAGE DEBUG_PAGE 620d1ba665SWarner Losh #define EFI_D_INFO DEBUG_INFO 630d1ba665SWarner Losh #define EFI_D_DISPATCH DEBUG_DISPATCH 640d1ba665SWarner Losh #define EFI_D_VARIABLE DEBUG_VARIABLE 650d1ba665SWarner Losh #define EFI_D_BM DEBUG_BM 660d1ba665SWarner Losh #define EFI_D_BLKIO DEBUG_BLKIO 670d1ba665SWarner Losh #define EFI_D_NET DEBUG_NET 680d1ba665SWarner Losh #define EFI_D_UNDI DEBUG_UNDI 690d1ba665SWarner Losh #define EFI_D_LOADFILE DEBUG_LOADFILE 700d1ba665SWarner Losh #define EFI_D_EVENT DEBUG_EVENT 710d1ba665SWarner Losh #define EFI_D_VERBOSE DEBUG_VERBOSE 720d1ba665SWarner Losh #define EFI_D_ERROR DEBUG_ERROR 730d1ba665SWarner Losh 740d1ba665SWarner Losh /** 750d1ba665SWarner Losh Prints a debug message to the debug output device if the specified error level is enabled. 760d1ba665SWarner Losh 770d1ba665SWarner Losh If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function 780d1ba665SWarner Losh GetDebugPrintErrorLevel (), then print the message specified by Format and the 790d1ba665SWarner Losh associated variable argument list to the debug output device. 800d1ba665SWarner Losh 810d1ba665SWarner Losh If Format is NULL, then ASSERT(). 820d1ba665SWarner Losh 830d1ba665SWarner Losh @param ErrorLevel The error level of the debug message. 840d1ba665SWarner Losh @param Format The format string for the debug message to print. 850d1ba665SWarner Losh @param ... The variable argument list whose contents are accessed 860d1ba665SWarner Losh based on the format string specified by Format. 870d1ba665SWarner Losh 880d1ba665SWarner Losh **/ 890d1ba665SWarner Losh VOID 900d1ba665SWarner Losh EFIAPI 910d1ba665SWarner Losh DebugPrint ( 920d1ba665SWarner Losh IN UINTN ErrorLevel, 930d1ba665SWarner Losh IN CONST CHAR8 *Format, 940d1ba665SWarner Losh ... 950d1ba665SWarner Losh ); 960d1ba665SWarner Losh 970d1ba665SWarner Losh 980d1ba665SWarner Losh /** 99*3245fa21SMitchell Horne Prints a debug message to the debug output device if the specified 100*3245fa21SMitchell Horne error level is enabled. 101*3245fa21SMitchell Horne 102*3245fa21SMitchell Horne If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function 103*3245fa21SMitchell Horne GetDebugPrintErrorLevel (), then print the message specified by Format and 104*3245fa21SMitchell Horne the associated variable argument list to the debug output device. 105*3245fa21SMitchell Horne 106*3245fa21SMitchell Horne If Format is NULL, then ASSERT(). 107*3245fa21SMitchell Horne 108*3245fa21SMitchell Horne @param ErrorLevel The error level of the debug message. 109*3245fa21SMitchell Horne @param Format Format string for the debug message to print. 110*3245fa21SMitchell Horne @param VaListMarker VA_LIST marker for the variable argument list. 111*3245fa21SMitchell Horne 112*3245fa21SMitchell Horne **/ 113*3245fa21SMitchell Horne VOID 114*3245fa21SMitchell Horne EFIAPI 115*3245fa21SMitchell Horne DebugVPrint ( 116*3245fa21SMitchell Horne IN UINTN ErrorLevel, 117*3245fa21SMitchell Horne IN CONST CHAR8 *Format, 118*3245fa21SMitchell Horne IN VA_LIST VaListMarker 119*3245fa21SMitchell Horne ); 120*3245fa21SMitchell Horne 121*3245fa21SMitchell Horne 122*3245fa21SMitchell Horne /** 123*3245fa21SMitchell Horne Prints a debug message to the debug output device if the specified 124*3245fa21SMitchell Horne error level is enabled. 125*3245fa21SMitchell Horne This function use BASE_LIST which would provide a more compatible 126*3245fa21SMitchell Horne service than VA_LIST. 127*3245fa21SMitchell Horne 128*3245fa21SMitchell Horne If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function 129*3245fa21SMitchell Horne GetDebugPrintErrorLevel (), then print the message specified by Format and 130*3245fa21SMitchell Horne the associated variable argument list to the debug output device. 131*3245fa21SMitchell Horne 132*3245fa21SMitchell Horne If Format is NULL, then ASSERT(). 133*3245fa21SMitchell Horne 134*3245fa21SMitchell Horne @param ErrorLevel The error level of the debug message. 135*3245fa21SMitchell Horne @param Format Format string for the debug message to print. 136*3245fa21SMitchell Horne @param BaseListMarker BASE_LIST marker for the variable argument list. 137*3245fa21SMitchell Horne 138*3245fa21SMitchell Horne **/ 139*3245fa21SMitchell Horne VOID 140*3245fa21SMitchell Horne EFIAPI 141*3245fa21SMitchell Horne DebugBPrint ( 142*3245fa21SMitchell Horne IN UINTN ErrorLevel, 143*3245fa21SMitchell Horne IN CONST CHAR8 *Format, 144*3245fa21SMitchell Horne IN BASE_LIST BaseListMarker 145*3245fa21SMitchell Horne ); 146*3245fa21SMitchell Horne 147*3245fa21SMitchell Horne 148*3245fa21SMitchell Horne /** 1490d1ba665SWarner Losh Prints an assert message containing a filename, line number, and description. 1500d1ba665SWarner Losh This may be followed by a breakpoint or a dead loop. 1510d1ba665SWarner Losh 1520d1ba665SWarner Losh Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n" 1530d1ba665SWarner Losh to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of 1540d1ba665SWarner Losh PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if 1550d1ba665SWarner Losh DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then 1560d1ba665SWarner Losh CpuDeadLoop() is called. If neither of these bits are set, then this function 1570d1ba665SWarner Losh returns immediately after the message is printed to the debug output device. 1580d1ba665SWarner Losh DebugAssert() must actively prevent recursion. If DebugAssert() is called while 1590d1ba665SWarner Losh processing another DebugAssert(), then DebugAssert() must return immediately. 1600d1ba665SWarner Losh 1610d1ba665SWarner Losh If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed. 1620d1ba665SWarner Losh If Description is NULL, then a <Description> string of "(NULL) Description" is printed. 1630d1ba665SWarner Losh 1640d1ba665SWarner Losh @param FileName The pointer to the name of the source file that generated the assert condition. 1650d1ba665SWarner Losh @param LineNumber The line number in the source file that generated the assert condition 1660d1ba665SWarner Losh @param Description The pointer to the description of the assert condition. 1670d1ba665SWarner Losh 1680d1ba665SWarner Losh **/ 1690d1ba665SWarner Losh VOID 1700d1ba665SWarner Losh EFIAPI 1710d1ba665SWarner Losh DebugAssert ( 1720d1ba665SWarner Losh IN CONST CHAR8 *FileName, 1730d1ba665SWarner Losh IN UINTN LineNumber, 1740d1ba665SWarner Losh IN CONST CHAR8 *Description 1750d1ba665SWarner Losh ); 1760d1ba665SWarner Losh 1770d1ba665SWarner Losh 1780d1ba665SWarner Losh /** 1790d1ba665SWarner Losh Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer. 1800d1ba665SWarner Losh 1810d1ba665SWarner Losh This function fills Length bytes of Buffer with the value specified by 1820d1ba665SWarner Losh PcdDebugClearMemoryValue, and returns Buffer. 1830d1ba665SWarner Losh 1840d1ba665SWarner Losh If Buffer is NULL, then ASSERT(). 1850d1ba665SWarner Losh If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). 1860d1ba665SWarner Losh 1870d1ba665SWarner Losh @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue. 1880d1ba665SWarner Losh @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. 1890d1ba665SWarner Losh 1900d1ba665SWarner Losh @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue. 1910d1ba665SWarner Losh 1920d1ba665SWarner Losh **/ 1930d1ba665SWarner Losh VOID * 1940d1ba665SWarner Losh EFIAPI 1950d1ba665SWarner Losh DebugClearMemory ( 1960d1ba665SWarner Losh OUT VOID *Buffer, 1970d1ba665SWarner Losh IN UINTN Length 1980d1ba665SWarner Losh ); 1990d1ba665SWarner Losh 2000d1ba665SWarner Losh 2010d1ba665SWarner Losh /** 2020d1ba665SWarner Losh Returns TRUE if ASSERT() macros are enabled. 2030d1ba665SWarner Losh 2040d1ba665SWarner Losh This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of 2050d1ba665SWarner Losh PcdDebugProperyMask is set. Otherwise, FALSE is returned. 2060d1ba665SWarner Losh 2070d1ba665SWarner Losh @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set. 2080d1ba665SWarner Losh @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear. 2090d1ba665SWarner Losh 2100d1ba665SWarner Losh **/ 2110d1ba665SWarner Losh BOOLEAN 2120d1ba665SWarner Losh EFIAPI 2130d1ba665SWarner Losh DebugAssertEnabled ( 2140d1ba665SWarner Losh VOID 2150d1ba665SWarner Losh ); 2160d1ba665SWarner Losh 2170d1ba665SWarner Losh 2180d1ba665SWarner Losh /** 2190d1ba665SWarner Losh Returns TRUE if DEBUG() macros are enabled. 2200d1ba665SWarner Losh 2210d1ba665SWarner Losh This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of 2220d1ba665SWarner Losh PcdDebugProperyMask is set. Otherwise, FALSE is returned. 2230d1ba665SWarner Losh 2240d1ba665SWarner Losh @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set. 2250d1ba665SWarner Losh @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear. 2260d1ba665SWarner Losh 2270d1ba665SWarner Losh **/ 2280d1ba665SWarner Losh BOOLEAN 2290d1ba665SWarner Losh EFIAPI 2300d1ba665SWarner Losh DebugPrintEnabled ( 2310d1ba665SWarner Losh VOID 2320d1ba665SWarner Losh ); 2330d1ba665SWarner Losh 2340d1ba665SWarner Losh 2350d1ba665SWarner Losh /** 2360d1ba665SWarner Losh Returns TRUE if DEBUG_CODE() macros are enabled. 2370d1ba665SWarner Losh 2380d1ba665SWarner Losh This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of 2390d1ba665SWarner Losh PcdDebugProperyMask is set. Otherwise, FALSE is returned. 2400d1ba665SWarner Losh 2410d1ba665SWarner Losh @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set. 2420d1ba665SWarner Losh @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear. 2430d1ba665SWarner Losh 2440d1ba665SWarner Losh **/ 2450d1ba665SWarner Losh BOOLEAN 2460d1ba665SWarner Losh EFIAPI 2470d1ba665SWarner Losh DebugCodeEnabled ( 2480d1ba665SWarner Losh VOID 2490d1ba665SWarner Losh ); 2500d1ba665SWarner Losh 2510d1ba665SWarner Losh 2520d1ba665SWarner Losh /** 2530d1ba665SWarner Losh Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled. 2540d1ba665SWarner Losh 2550d1ba665SWarner Losh This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of 2560d1ba665SWarner Losh PcdDebugProperyMask is set. Otherwise, FALSE is returned. 2570d1ba665SWarner Losh 2580d1ba665SWarner Losh @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set. 2590d1ba665SWarner Losh @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear. 2600d1ba665SWarner Losh 2610d1ba665SWarner Losh **/ 2620d1ba665SWarner Losh BOOLEAN 2630d1ba665SWarner Losh EFIAPI 2640d1ba665SWarner Losh DebugClearMemoryEnabled ( 2650d1ba665SWarner Losh VOID 2660d1ba665SWarner Losh ); 2670d1ba665SWarner Losh 2680d1ba665SWarner Losh /** 2690d1ba665SWarner Losh Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel. 2700d1ba665SWarner Losh 2710d1ba665SWarner Losh This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel. 2720d1ba665SWarner Losh 2730d1ba665SWarner Losh @retval TRUE Current ErrorLevel is supported. 2740d1ba665SWarner Losh @retval FALSE Current ErrorLevel is not supported. 2750d1ba665SWarner Losh 2760d1ba665SWarner Losh **/ 2770d1ba665SWarner Losh BOOLEAN 2780d1ba665SWarner Losh EFIAPI 2790d1ba665SWarner Losh DebugPrintLevelEnabled ( 2800d1ba665SWarner Losh IN CONST UINTN ErrorLevel 2810d1ba665SWarner Losh ); 2820d1ba665SWarner Losh 2830d1ba665SWarner Losh /** 2840d1ba665SWarner Losh Internal worker macro that calls DebugAssert(). 2850d1ba665SWarner Losh 2860d1ba665SWarner Losh This macro calls DebugAssert(), passing in the filename, line number, and an 2870d1ba665SWarner Losh expression that evaluated to FALSE. 2880d1ba665SWarner Losh 2890d1ba665SWarner Losh @param Expression Boolean expression that evaluated to FALSE 2900d1ba665SWarner Losh 2910d1ba665SWarner Losh **/ 292*3245fa21SMitchell Horne #if defined(__clang__) && defined(__FILE_NAME__) 293*3245fa21SMitchell Horne #define _ASSERT(Expression) DebugAssert (__FILE_NAME__, __LINE__, #Expression) 294*3245fa21SMitchell Horne #else 2950d1ba665SWarner Losh #define _ASSERT(Expression) DebugAssert (__FILE__, __LINE__, #Expression) 296*3245fa21SMitchell Horne #endif 2970d1ba665SWarner Losh 2980d1ba665SWarner Losh 2990d1ba665SWarner Losh /** 3000d1ba665SWarner Losh Internal worker macro that calls DebugPrint(). 3010d1ba665SWarner Losh 3020d1ba665SWarner Losh This macro calls DebugPrint() passing in the debug error level, a format 3030d1ba665SWarner Losh string, and a variable argument list. 3040d1ba665SWarner Losh __VA_ARGS__ is not supported by EBC compiler, Microsoft Visual Studio .NET 2003 3050d1ba665SWarner Losh and Microsoft Windows Server 2003 Driver Development Kit (Microsoft WINDDK) version 3790.1830. 3060d1ba665SWarner Losh 3070d1ba665SWarner Losh @param Expression Expression containing an error level, a format string, 3080d1ba665SWarner Losh and a variable argument list based on the format string. 3090d1ba665SWarner Losh 3100d1ba665SWarner Losh **/ 3110d1ba665SWarner Losh 3120d1ba665SWarner Losh #if !defined(MDE_CPU_EBC) && (!defined (_MSC_VER) || _MSC_VER > 1400) 3130d1ba665SWarner Losh #define _DEBUG_PRINT(PrintLevel, ...) \ 3140d1ba665SWarner Losh do { \ 3150d1ba665SWarner Losh if (DebugPrintLevelEnabled (PrintLevel)) { \ 3160d1ba665SWarner Losh DebugPrint (PrintLevel, ##__VA_ARGS__); \ 3170d1ba665SWarner Losh } \ 3180d1ba665SWarner Losh } while (FALSE) 3190d1ba665SWarner Losh #define _DEBUG(Expression) _DEBUG_PRINT Expression 3200d1ba665SWarner Losh #else 3210d1ba665SWarner Losh #define _DEBUG(Expression) DebugPrint Expression 3220d1ba665SWarner Losh #endif 3230d1ba665SWarner Losh 3240d1ba665SWarner Losh /** 3250d1ba665SWarner Losh Macro that calls DebugAssert() if an expression evaluates to FALSE. 3260d1ba665SWarner Losh 3270d1ba665SWarner Losh If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 3280d1ba665SWarner Losh bit of PcdDebugProperyMask is set, then this macro evaluates the Boolean 3290d1ba665SWarner Losh expression specified by Expression. If Expression evaluates to FALSE, then 3300d1ba665SWarner Losh DebugAssert() is called passing in the source filename, source line number, 3310d1ba665SWarner Losh and Expression. 3320d1ba665SWarner Losh 3330d1ba665SWarner Losh @param Expression Boolean expression. 3340d1ba665SWarner Losh 3350d1ba665SWarner Losh **/ 3360d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 3370d1ba665SWarner Losh #define ASSERT(Expression) \ 3380d1ba665SWarner Losh do { \ 3390d1ba665SWarner Losh if (DebugAssertEnabled ()) { \ 3400d1ba665SWarner Losh if (!(Expression)) { \ 3410d1ba665SWarner Losh _ASSERT (Expression); \ 3420d1ba665SWarner Losh ANALYZER_UNREACHABLE (); \ 3430d1ba665SWarner Losh } \ 3440d1ba665SWarner Losh } \ 3450d1ba665SWarner Losh } while (FALSE) 3460d1ba665SWarner Losh #else 3470d1ba665SWarner Losh #define ASSERT(Expression) 3480d1ba665SWarner Losh #endif 3490d1ba665SWarner Losh 3500d1ba665SWarner Losh /** 3510d1ba665SWarner Losh Macro that calls DebugPrint(). 3520d1ba665SWarner Losh 3530d1ba665SWarner Losh If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED 3540d1ba665SWarner Losh bit of PcdDebugProperyMask is set, then this macro passes Expression to 3550d1ba665SWarner Losh DebugPrint(). 3560d1ba665SWarner Losh 3570d1ba665SWarner Losh @param Expression Expression containing an error level, a format string, 3580d1ba665SWarner Losh and a variable argument list based on the format string. 3590d1ba665SWarner Losh 3600d1ba665SWarner Losh 3610d1ba665SWarner Losh **/ 3620d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 3630d1ba665SWarner Losh #define DEBUG(Expression) \ 3640d1ba665SWarner Losh do { \ 3650d1ba665SWarner Losh if (DebugPrintEnabled ()) { \ 3660d1ba665SWarner Losh _DEBUG (Expression); \ 3670d1ba665SWarner Losh } \ 3680d1ba665SWarner Losh } while (FALSE) 3690d1ba665SWarner Losh #else 3700d1ba665SWarner Losh #define DEBUG(Expression) 3710d1ba665SWarner Losh #endif 3720d1ba665SWarner Losh 3730d1ba665SWarner Losh /** 3740d1ba665SWarner Losh Macro that calls DebugAssert() if an EFI_STATUS evaluates to an error code. 3750d1ba665SWarner Losh 3760d1ba665SWarner Losh If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 3770d1ba665SWarner Losh bit of PcdDebugProperyMask is set, then this macro evaluates the EFI_STATUS 3780d1ba665SWarner Losh value specified by StatusParameter. If StatusParameter is an error code, 3790d1ba665SWarner Losh then DebugAssert() is called passing in the source filename, source line 3800d1ba665SWarner Losh number, and StatusParameter. 3810d1ba665SWarner Losh 3820d1ba665SWarner Losh @param StatusParameter EFI_STATUS value to evaluate. 3830d1ba665SWarner Losh 3840d1ba665SWarner Losh **/ 3850d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 3860d1ba665SWarner Losh #define ASSERT_EFI_ERROR(StatusParameter) \ 3870d1ba665SWarner Losh do { \ 3880d1ba665SWarner Losh if (DebugAssertEnabled ()) { \ 3890d1ba665SWarner Losh if (EFI_ERROR (StatusParameter)) { \ 3900d1ba665SWarner Losh DEBUG ((EFI_D_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", StatusParameter)); \ 3910d1ba665SWarner Losh _ASSERT (!EFI_ERROR (StatusParameter)); \ 3920d1ba665SWarner Losh } \ 3930d1ba665SWarner Losh } \ 3940d1ba665SWarner Losh } while (FALSE) 3950d1ba665SWarner Losh #else 3960d1ba665SWarner Losh #define ASSERT_EFI_ERROR(StatusParameter) 3970d1ba665SWarner Losh #endif 3980d1ba665SWarner Losh 3990d1ba665SWarner Losh /** 4000d1ba665SWarner Losh Macro that calls DebugAssert() if a RETURN_STATUS evaluates to an error code. 4010d1ba665SWarner Losh 4020d1ba665SWarner Losh If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED 4030d1ba665SWarner Losh bit of PcdDebugProperyMask is set, then this macro evaluates the 4040d1ba665SWarner Losh RETURN_STATUS value specified by StatusParameter. If StatusParameter is an 4050d1ba665SWarner Losh error code, then DebugAssert() is called passing in the source filename, 4060d1ba665SWarner Losh source line number, and StatusParameter. 4070d1ba665SWarner Losh 4080d1ba665SWarner Losh @param StatusParameter RETURN_STATUS value to evaluate. 4090d1ba665SWarner Losh 4100d1ba665SWarner Losh **/ 4110d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 4120d1ba665SWarner Losh #define ASSERT_RETURN_ERROR(StatusParameter) \ 4130d1ba665SWarner Losh do { \ 4140d1ba665SWarner Losh if (DebugAssertEnabled ()) { \ 4150d1ba665SWarner Losh if (RETURN_ERROR (StatusParameter)) { \ 4160d1ba665SWarner Losh DEBUG ((DEBUG_ERROR, "\nASSERT_RETURN_ERROR (Status = %r)\n", \ 4170d1ba665SWarner Losh StatusParameter)); \ 4180d1ba665SWarner Losh _ASSERT (!RETURN_ERROR (StatusParameter)); \ 4190d1ba665SWarner Losh } \ 4200d1ba665SWarner Losh } \ 4210d1ba665SWarner Losh } while (FALSE) 4220d1ba665SWarner Losh #else 4230d1ba665SWarner Losh #define ASSERT_RETURN_ERROR(StatusParameter) 4240d1ba665SWarner Losh #endif 4250d1ba665SWarner Losh 4260d1ba665SWarner Losh /** 4270d1ba665SWarner Losh Macro that calls DebugAssert() if a protocol is already installed in the 4280d1ba665SWarner Losh handle database. 4290d1ba665SWarner Losh 4300d1ba665SWarner Losh If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit 4310d1ba665SWarner Losh of PcdDebugProperyMask is clear, then return. 4320d1ba665SWarner Losh 4330d1ba665SWarner Losh If Handle is NULL, then a check is made to see if the protocol specified by Guid 4340d1ba665SWarner Losh is present on any handle in the handle database. If Handle is not NULL, then 4350d1ba665SWarner Losh a check is made to see if the protocol specified by Guid is present on the 4360d1ba665SWarner Losh handle specified by Handle. If the check finds the protocol, then DebugAssert() 4370d1ba665SWarner Losh is called passing in the source filename, source line number, and Guid. 4380d1ba665SWarner Losh 4390d1ba665SWarner Losh If Guid is NULL, then ASSERT(). 4400d1ba665SWarner Losh 4410d1ba665SWarner Losh @param Handle The handle to check for the protocol. This is an optional 4420d1ba665SWarner Losh parameter that may be NULL. If it is NULL, then the entire 4430d1ba665SWarner Losh handle database is searched. 4440d1ba665SWarner Losh 4450d1ba665SWarner Losh @param Guid The pointer to a protocol GUID. 4460d1ba665SWarner Losh 4470d1ba665SWarner Losh **/ 4480d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 4490d1ba665SWarner Losh #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) \ 4500d1ba665SWarner Losh do { \ 4510d1ba665SWarner Losh if (DebugAssertEnabled ()) { \ 4520d1ba665SWarner Losh VOID *Instance; \ 4530d1ba665SWarner Losh ASSERT (Guid != NULL); \ 4540d1ba665SWarner Losh if (Handle == NULL) { \ 4550d1ba665SWarner Losh if (!EFI_ERROR (gBS->LocateProtocol ((EFI_GUID *)Guid, NULL, &Instance))) { \ 4560d1ba665SWarner Losh _ASSERT (Guid already installed in database); \ 4570d1ba665SWarner Losh } \ 4580d1ba665SWarner Losh } else { \ 4590d1ba665SWarner Losh if (!EFI_ERROR (gBS->HandleProtocol (Handle, (EFI_GUID *)Guid, &Instance))) { \ 4600d1ba665SWarner Losh _ASSERT (Guid already installed on Handle); \ 4610d1ba665SWarner Losh } \ 4620d1ba665SWarner Losh } \ 4630d1ba665SWarner Losh } \ 4640d1ba665SWarner Losh } while (FALSE) 4650d1ba665SWarner Losh #else 4660d1ba665SWarner Losh #define ASSERT_PROTOCOL_ALREADY_INSTALLED(Handle, Guid) 4670d1ba665SWarner Losh #endif 4680d1ba665SWarner Losh 4690d1ba665SWarner Losh /** 4700d1ba665SWarner Losh Macro that marks the beginning of debug source code. 4710d1ba665SWarner Losh 4720d1ba665SWarner Losh If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set, 4730d1ba665SWarner Losh then this macro marks the beginning of source code that is included in a module. 4740d1ba665SWarner Losh Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END() 4750d1ba665SWarner Losh are not included in a module. 4760d1ba665SWarner Losh 4770d1ba665SWarner Losh **/ 4780d1ba665SWarner Losh #define DEBUG_CODE_BEGIN() do { if (DebugCodeEnabled ()) { UINT8 __DebugCodeLocal 4790d1ba665SWarner Losh 4800d1ba665SWarner Losh 4810d1ba665SWarner Losh /** 4820d1ba665SWarner Losh The macro that marks the end of debug source code. 4830d1ba665SWarner Losh 4840d1ba665SWarner Losh If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set, 4850d1ba665SWarner Losh then this macro marks the end of source code that is included in a module. 4860d1ba665SWarner Losh Otherwise, the source lines between DEBUG_CODE_BEGIN() and DEBUG_CODE_END() 4870d1ba665SWarner Losh are not included in a module. 4880d1ba665SWarner Losh 4890d1ba665SWarner Losh **/ 4900d1ba665SWarner Losh #define DEBUG_CODE_END() __DebugCodeLocal = 0; __DebugCodeLocal++; } } while (FALSE) 4910d1ba665SWarner Losh 4920d1ba665SWarner Losh 4930d1ba665SWarner Losh /** 4940d1ba665SWarner Losh The macro that declares a section of debug source code. 4950d1ba665SWarner Losh 4960d1ba665SWarner Losh If the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set, 4970d1ba665SWarner Losh then the source code specified by Expression is included in a module. 4980d1ba665SWarner Losh Otherwise, the source specified by Expression is not included in a module. 4990d1ba665SWarner Losh 5000d1ba665SWarner Losh **/ 5010d1ba665SWarner Losh #define DEBUG_CODE(Expression) \ 5020d1ba665SWarner Losh DEBUG_CODE_BEGIN (); \ 5030d1ba665SWarner Losh Expression \ 5040d1ba665SWarner Losh DEBUG_CODE_END () 5050d1ba665SWarner Losh 5060d1ba665SWarner Losh 5070d1ba665SWarner Losh /** 5080d1ba665SWarner Losh The macro that calls DebugClearMemory() to clear a buffer to a default value. 5090d1ba665SWarner Losh 5100d1ba665SWarner Losh If the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set, 5110d1ba665SWarner Losh then this macro calls DebugClearMemory() passing in Address and Length. 5120d1ba665SWarner Losh 5130d1ba665SWarner Losh @param Address The pointer to a buffer. 5140d1ba665SWarner Losh @param Length The number of bytes in the buffer to set. 5150d1ba665SWarner Losh 5160d1ba665SWarner Losh **/ 5170d1ba665SWarner Losh #define DEBUG_CLEAR_MEMORY(Address, Length) \ 5180d1ba665SWarner Losh do { \ 5190d1ba665SWarner Losh if (DebugClearMemoryEnabled ()) { \ 5200d1ba665SWarner Losh DebugClearMemory (Address, Length); \ 5210d1ba665SWarner Losh } \ 5220d1ba665SWarner Losh } while (FALSE) 5230d1ba665SWarner Losh 5240d1ba665SWarner Losh 5250d1ba665SWarner Losh /** 5260d1ba665SWarner Losh Macro that calls DebugAssert() if the containing record does not have a 5270d1ba665SWarner Losh matching signature. If the signatures matches, then a pointer to the data 5280d1ba665SWarner Losh structure that contains a specified field of that data structure is returned. 5290d1ba665SWarner Losh This is a lightweight method hide information by placing a public data 5300d1ba665SWarner Losh structure inside a larger private data structure and using a pointer to the 5310d1ba665SWarner Losh public data structure to retrieve a pointer to the private data structure. 5320d1ba665SWarner Losh 5330d1ba665SWarner Losh If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit 5340d1ba665SWarner Losh of PcdDebugProperyMask is clear, then this macro computes the offset, in bytes, 5350d1ba665SWarner Losh of the field specified by Field from the beginning of the data structure specified 5360d1ba665SWarner Losh by TYPE. This offset is subtracted from Record, and is used to return a pointer 5370d1ba665SWarner Losh to a data structure of the type specified by TYPE. 5380d1ba665SWarner Losh 5390d1ba665SWarner Losh If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit 5400d1ba665SWarner Losh of PcdDebugProperyMask is set, then this macro computes the offset, in bytes, 5410d1ba665SWarner Losh of field specified by Field from the beginning of the data structure specified 5420d1ba665SWarner Losh by TYPE. This offset is subtracted from Record, and is used to compute a pointer 5430d1ba665SWarner Losh to a data structure of the type specified by TYPE. The Signature field of the 5440d1ba665SWarner Losh data structure specified by TYPE is compared to TestSignature. If the signatures 5450d1ba665SWarner Losh match, then a pointer to the pointer to a data structure of the type specified by 5460d1ba665SWarner Losh TYPE is returned. If the signatures do not match, then DebugAssert() is called 5470d1ba665SWarner Losh with a description of "CR has a bad signature" and Record is returned. 5480d1ba665SWarner Losh 5490d1ba665SWarner Losh If the data type specified by TYPE does not contain the field specified by Field, 5500d1ba665SWarner Losh then the module will not compile. 5510d1ba665SWarner Losh 5520d1ba665SWarner Losh If TYPE does not contain a field called Signature, then the module will not 5530d1ba665SWarner Losh compile. 5540d1ba665SWarner Losh 5550d1ba665SWarner Losh @param Record The pointer to the field specified by Field within a data 5560d1ba665SWarner Losh structure of type TYPE. 5570d1ba665SWarner Losh 5580d1ba665SWarner Losh @param TYPE The name of the data structure type to return This 5590d1ba665SWarner Losh data structure must contain the field specified by Field. 5600d1ba665SWarner Losh 5610d1ba665SWarner Losh @param Field The name of the field in the data structure specified 5620d1ba665SWarner Losh by TYPE to which Record points. 5630d1ba665SWarner Losh 5640d1ba665SWarner Losh @param TestSignature The 32-bit signature value to match. 5650d1ba665SWarner Losh 5660d1ba665SWarner Losh **/ 5670d1ba665SWarner Losh #if !defined(MDEPKG_NDEBUG) 5680d1ba665SWarner Losh #define CR(Record, TYPE, Field, TestSignature) \ 5690d1ba665SWarner Losh (DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \ 5700d1ba665SWarner Losh (TYPE *) (_ASSERT (CR has Bad Signature), Record) : \ 5710d1ba665SWarner Losh BASE_CR (Record, TYPE, Field) 5720d1ba665SWarner Losh #else 5730d1ba665SWarner Losh #define CR(Record, TYPE, Field, TestSignature) \ 5740d1ba665SWarner Losh BASE_CR (Record, TYPE, Field) 5750d1ba665SWarner Losh #endif 5760d1ba665SWarner Losh 5770d1ba665SWarner Losh #endif 578