2 Base Debug library instance base on Serial Port library.
3 It uses PrintLib to send debug messages to serial port device.
5 NOTE: If the Serial Port library enables hardware flow control, then a call
6 to DebugPrint() or DebugAssert() may hang if writes to the serial port are
7 being blocked. This may occur if a key(s) are pressed in a terminal emulator
8 used to monitor the DEBUG() and ASSERT() messages.
10 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Library/DebugLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/PrintLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/DebugPrintErrorLevelLib.h>
25 // Define the maximum debug and assert message length that this library supports
27 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
30 // VA_LIST can not initialize to NULL for all compiler, so we use this to
31 // indicate a null VA_LIST
36 The constructor function initialize the Serial Port Library
38 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
43 BaseDebugLibSerialPortConstructor (
47 return SerialPortInitialize ();
51 Prints a debug message to the debug output device if the specified error level is enabled.
53 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
54 GetDebugPrintErrorLevel (), then print the message specified by Format and the
55 associated variable argument list to the debug output device.
57 If Format is NULL, then ASSERT().
59 @param ErrorLevel The error level of the debug message.
60 @param Format Format string for the debug message to print.
61 @param ... Variable argument list whose contents are accessed
62 based on the format string specified by Format.
69 IN CONST CHAR8
*Format
,
75 VA_START (Marker
, Format
);
76 DebugVPrint (ErrorLevel
, Format
, Marker
);
81 Prints a debug message to the debug output device if the specified
82 error level is enabled base on Null-terminated format string and a
83 VA_LIST argument list or a BASE_LIST argument list.
85 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
86 GetDebugPrintErrorLevel (), then print the message specified by Format and
87 the associated variable argument list to the debug output device.
89 If Format is NULL, then ASSERT().
91 @param ErrorLevel The error level of the debug message.
92 @param Format Format string for the debug message to print.
93 @param VaListMarker VA_LIST marker for the variable argument list.
94 @param BaseListMarker BASE_LIST marker for the variable argument list.
100 IN CONST CHAR8
*Format
,
101 IN VA_LIST VaListMarker
,
102 IN BASE_LIST BaseListMarker
105 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
108 // If Format is NULL, then ASSERT().
110 ASSERT (Format
!= NULL
);
113 // Check driver debug mask value and global mask
115 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
120 // Convert the DEBUG() message to an ASCII String
122 if (BaseListMarker
== NULL
) {
123 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
125 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
129 // Send the print string to a Serial Port
131 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
135 Prints a debug message to the debug output device if the specified
136 error level is enabled.
138 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
139 GetDebugPrintErrorLevel (), then print the message specified by Format and
140 the associated variable argument list to the debug output device.
142 If Format is NULL, then ASSERT().
144 @param ErrorLevel The error level of the debug message.
145 @param Format Format string for the debug message to print.
146 @param VaListMarker VA_LIST marker for the variable argument list.
153 IN CONST CHAR8
*Format
,
154 IN VA_LIST VaListMarker
157 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
161 Prints a debug message to the debug output device if the specified
162 error level is enabled.
163 This function use BASE_LIST which would provide a more compatible
164 service than VA_LIST.
166 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
167 GetDebugPrintErrorLevel (), then print the message specified by Format and
168 the associated variable argument list to the debug output device.
170 If Format is NULL, then ASSERT().
172 @param ErrorLevel The error level of the debug message.
173 @param Format Format string for the debug message to print.
174 @param BaseListMarker BASE_LIST marker for the variable argument list.
181 IN CONST CHAR8
*Format
,
182 IN BASE_LIST BaseListMarker
185 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
189 Prints an assert message containing a filename, line number, and description.
190 This may be followed by a breakpoint or a dead loop.
192 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
193 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
194 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
195 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
196 CpuDeadLoop() is called. If neither of these bits are set, then this function
197 returns immediately after the message is printed to the debug output device.
198 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
199 processing another DebugAssert(), then DebugAssert() must return immediately.
201 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
202 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
204 @param FileName The pointer to the name of the source file that generated the assert condition.
205 @param LineNumber The line number in the source file that generated the assert condition
206 @param Description The pointer to the description of the assert condition.
212 IN CONST CHAR8
*FileName
,
214 IN CONST CHAR8
*Description
217 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
220 // Generate the ASSERT() message in Ascii format
222 AsciiSPrint (Buffer
, sizeof (Buffer
), "ASSERT [%a] %a(%d): %a\n", gEfiCallerBaseName
, FileName
, LineNumber
, Description
);
225 // Send the print string to the Console Output device
227 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
230 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
232 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
234 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
240 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
242 This function fills Length bytes of Buffer with the value specified by
243 PcdDebugClearMemoryValue, and returns Buffer.
245 If Buffer is NULL, then ASSERT().
246 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
248 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
249 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
251 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
262 // If Buffer is NULL, then ASSERT().
264 ASSERT (Buffer
!= NULL
);
267 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
269 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
273 Returns TRUE if ASSERT() macros are enabled.
275 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
276 PcdDebugProperyMask is set. Otherwise FALSE is returned.
278 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
279 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
288 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
292 Returns TRUE if DEBUG() macros are enabled.
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
307 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
311 Returns TRUE if DEBUG_CODE() macros are enabled.
313 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
314 PcdDebugProperyMask is set. Otherwise FALSE is returned.
316 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
317 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
326 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
330 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
332 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
333 PcdDebugProperyMask is set. Otherwise FALSE is returned.
335 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
336 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
341 DebugClearMemoryEnabled (
345 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
349 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
351 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
353 @retval TRUE Current ErrorLevel is supported.
354 @retval FALSE Current ErrorLevel is not supported.
359 DebugPrintLevelEnabled (
360 IN CONST UINTN ErrorLevel
363 return (BOOLEAN
)((ErrorLevel
& PcdGet32 (PcdFixedDebugPrintErrorLevel
)) != 0);