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
= 7; 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
= (UINT32
*)GetStackFramePointer ();
236 // Generate the ASSERT() message in Ascii format
240 sizeof (Buffer
) / sizeof (CHAR8
),
241 "-> EBP:0x00000000 EIP:0x00000000\n",
242 sizeof (Buffer
) / sizeof (CHAR8
) - 1
244 SerialPortWrite ((UINT8
*)"ASSERT DUMP:\n", 13);
245 while (Frame
!= NULL
) {
246 FillHex ((UINT32
)Frame
, Buffer
+ 9);
247 FillHex (Frame
[1], Buffer
+ 9 + 8 + 8);
248 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
249 if ((Frame
[0] > (UINT32
)Frame
) && (Frame
[0] < (UINT32
)Frame
+ 0x00100000)) {
250 Frame
= (UINT32
*)Frame
[0];
263 Prints an assert message containing a filename, line number, and description.
264 This may be followed by a breakpoint or a dead loop.
266 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
267 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
268 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if
269 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then
270 CpuDeadLoop() is called. If neither of these bits are set, then this function
271 returns immediately after the message is printed to the debug output device.
272 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
273 processing another DebugAssert(), then DebugAssert() must return immediately.
275 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
276 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
278 @param FileName The pointer to the name of the source file that generated the assert condition.
279 @param LineNumber The line number in the source file that generated the assert condition
280 @param Description The pointer to the description of the assert condition.
286 IN CONST CHAR8
*FileName
,
288 IN CONST CHAR8
*Description
291 DebugAssertInternal ();
295 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
297 This function fills Length bytes of Buffer with the value specified by
298 PcdDebugClearMemoryValue, and returns Buffer.
300 If Buffer is NULL, then ASSERT().
301 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
303 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
304 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
306 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
320 Returns TRUE if ASSERT() macros are enabled.
322 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
323 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
325 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.
326 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.
335 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
339 Returns TRUE if DEBUG() macros are enabled.
341 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
342 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
344 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.
345 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.
354 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
358 Returns TRUE if DEBUG_CODE() macros are enabled.
360 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
361 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
363 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.
364 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.
373 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
377 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
379 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
380 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
382 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.
383 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.
388 DebugClearMemoryEnabled (
392 return (BOOLEAN
)((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
396 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
398 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
400 @retval TRUE Current ErrorLevel is supported.
401 @retval FALSE Current ErrorLevel is not supported.
406 DebugPrintLevelEnabled (
407 IN CONST UINTN ErrorLevel
410 return (BOOLEAN
)((ErrorLevel
& PcdGet32 (PcdFixedDebugPrintErrorLevel
)) != 0);