3 Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include <Library/DebugLib.h>
10 #include <Library/BaseLib.h>
11 #include <Library/PrintLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/SerialPortLib.h>
15 #include <Library/DebugDeviceLib.h>
16 #include <Library/DebugPrintErrorLevelLib.h>
19 // Define the maximum debug and assert message length that this library supports
21 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
23 CONST CHAR8
*mHexTable
= "0123456789ABCDEF";
26 // VA_LIST can not initialize to NULL for all compiler, so we use this to
27 // indicate a null VA_LIST
32 Get stack frame pointer of function call.
34 @return StackFramePointer stack frame pointer of function call.
38 GetStackFramePointer (
43 Prints a debug message to the debug output device if the specified error level is enabled.
45 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
46 GetDebugPrintErrorLevel (), then print the message specified by Format and the
47 associated variable argument list to the debug output device.
49 If Format is NULL, then ASSERT().
51 @param ErrorLevel The error level of the debug message.
52 @param Format Format string for the debug message to print.
53 @param ... Variable argument list whose contents are accessed
54 based on the format string specified by Format.
61 IN CONST CHAR8
*Format
,
67 VA_START (Marker
, Format
);
68 DebugVPrint (ErrorLevel
, Format
, Marker
);
73 Prints a debug message to the debug output device if the specified
74 error level is enabled base on Null-terminated format string and a
75 VA_LIST argument list or a BASE_LIST argument list.
77 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
78 GetDebugPrintErrorLevel (), then print the message specified by Format and
79 the associated variable argument list to the debug output device.
81 If Format is NULL, then ASSERT().
83 @param ErrorLevel The error level of the debug message.
84 @param Format Format string for the debug message to print.
85 @param VaListMarker VA_LIST marker for the variable argument list.
86 @param BaseListMarker BASE_LIST marker for the variable argument list.
92 IN CONST CHAR8
*Format
,
93 IN VA_LIST VaListMarker
,
94 IN BASE_LIST BaseListMarker
97 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
100 // If Format is NULL, then ASSERT().
102 if (!GetDebugPrintDeviceEnable ()) {
107 // Check driver debug mask value and global mask
109 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
114 // If Format is NULL, then ASSERT().
116 ASSERT (Format
!= NULL
);
119 // Convert the DEBUG() message to an ASCII String
121 if (BaseListMarker
== NULL
) {
122 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
124 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
128 // Send the print string to a Serial Port
130 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
134 Prints a debug message to the debug output device if the specified
135 error level is enabled.
137 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
138 GetDebugPrintErrorLevel (), then print the message specified by Format and
139 the associated variable argument list to the debug output device.
141 If Format is NULL, then ASSERT().
143 @param ErrorLevel The error level of the debug message.
144 @param Format Format string for the debug message to print.
145 @param VaListMarker VA_LIST marker for the variable argument list.
152 IN CONST CHAR8
*Format
,
153 IN VA_LIST VaListMarker
156 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
160 Prints a debug message to the debug output device if the specified
161 error level is enabled.
162 This function use BASE_LIST which would provide a more compatible
163 service than VA_LIST.
165 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
166 GetDebugPrintErrorLevel (), then print the message specified by Format and
167 the associated variable argument list to the debug output device.
169 If Format is NULL, then ASSERT().
171 @param ErrorLevel The error level of the debug message.
172 @param Format Format string for the debug message to print.
173 @param BaseListMarker BASE_LIST marker for the variable argument list.
180 IN CONST CHAR8
*Format
,
181 IN BASE_LIST BaseListMarker
184 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
188 Convert an UINT32 value into HEX string specified by Buffer.
190 @param Value The HEX value to convert to string
191 @param Buffer The pointer to the target buffer to be filled with HEX string
202 for (Idx
= (sizeof (UINTN
) * 2) - 1; Idx
>= 0; Idx
--) {
203 Buffer
[Idx
] = mHexTable
[Value
& 0x0F];
209 Prints an assert message containing a filename, line number, and description.
210 This may be followed by a breakpoint or a dead loop.
212 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
213 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
214 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then
216 CpuDeadLoop() is called. If neither of these bits are set, then this function
217 returns immediately after the message is printed to the debug output device.
218 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
219 processing another DebugAssert(), then DebugAssert() must return immediately.
221 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
222 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
226 DebugAssertInternal (
230 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
233 Frame
= (UINTN
*)GetStackFramePointer ();
236 // Generate the ASSERT() message in Ascii format
238 if (sizeof (UINTN
) == sizeof (UINT32
)) {
241 sizeof (Buffer
) / sizeof (CHAR8
),
242 "-> EBP:0x00000000 EIP:0x00000000\n",
243 sizeof (Buffer
) / sizeof (CHAR8
) - 1
248 sizeof (Buffer
) / sizeof (CHAR8
),
249 "-> RBP:0x0000000000000000 RIP:0x0000000000000000\n",
250 sizeof (Buffer
) / sizeof (CHAR8
) - 1
253 SerialPortWrite ((UINT8
*)"ASSERT DUMP:\n", 13);
254 while (Frame
!= NULL
) {
255 FillHex ((UINTN
)Frame
, Buffer
+ 9);
256 FillHex (Frame
[1], Buffer
+ 9 + (sizeof (UINTN
) * 2) + 8);
257 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
258 if ((Frame
[0] > (UINTN
)Frame
) && (Frame
[0] < (UINTN
)Frame
+ 0x00100000)) {
259 Frame
= (UINTN
*)Frame
[0];
272 Prints an assert message containing a filename, line number, and description.
273 This may be followed by a breakpoint or a dead loop.
275 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
276 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
277 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if
278 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then
279 CpuDeadLoop() is called. If neither of these bits are set, then this function
280 returns immediately after the message is printed to the debug output device.
281 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
282 processing another DebugAssert(), then DebugAssert() must return immediately.
284 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
285 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
287 @param FileName The pointer to the name of the source file that generated the assert condition.
288 @param LineNumber The line number in the source file that generated the assert condition
289 @param Description The pointer to the description of the assert condition.
295 IN CONST CHAR8
*FileName
,
297 IN CONST CHAR8
*Description
300 DebugAssertInternal ();
304 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
306 This function fills Length bytes of Buffer with the value specified by
307 PcdDebugClearMemoryValue, and returns Buffer.
309 If Buffer is NULL, then ASSERT().
310 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
312 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
313 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
315 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
329 Returns TRUE if ASSERT() macros are enabled.
331 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
332 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
334 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.
335 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.
344 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
348 Returns TRUE if DEBUG() macros are enabled.
350 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
351 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
353 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.
354 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.
363 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
367 Returns TRUE if DEBUG_CODE() macros are enabled.
369 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
370 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
372 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.
373 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.
382 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
386 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
388 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
389 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
391 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.
392 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.
397 DebugClearMemoryEnabled (
401 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
405 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
407 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
409 @retval TRUE Current ErrorLevel is supported.
410 @retval FALSE Current ErrorLevel is not supported.
415 DebugPrintLevelEnabled (
416 IN CONST UINTN ErrorLevel
419 return (BOOLEAN
)((ErrorLevel
& PcdGet32 (PcdFixedDebugPrintErrorLevel
)) != 0);