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 (
44 Prints a debug message to the debug output device if the specified error level is enabled.
46 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
47 GetDebugPrintErrorLevel (), then print the message specified by Format and the
48 associated variable argument list to the debug output device.
50 If Format is NULL, then ASSERT().
52 @param ErrorLevel The error level of the debug message.
53 @param Format Format string for the debug message to print.
54 @param ... Variable argument list whose contents are accessed
55 based on the format string specified by Format.
62 IN CONST CHAR8
*Format
,
68 VA_START (Marker
, Format
);
69 DebugVPrint (ErrorLevel
, Format
, Marker
);
74 Prints a debug message to the debug output device if the specified
75 error level is enabled base on Null-terminated format string and a
76 VA_LIST argument list or a BASE_LIST argument list.
78 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
79 GetDebugPrintErrorLevel (), then print the message specified by Format and
80 the associated variable argument list to the debug output device.
82 If Format is NULL, then ASSERT().
84 @param ErrorLevel The error level of the debug message.
85 @param Format Format string for the debug message to print.
86 @param VaListMarker VA_LIST marker for the variable argument list.
87 @param BaseListMarker BASE_LIST marker for the variable argument list.
93 IN CONST CHAR8
*Format
,
94 IN VA_LIST VaListMarker
,
95 IN BASE_LIST BaseListMarker
98 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
101 // If Format is NULL, then ASSERT().
103 if (!GetDebugPrintDeviceEnable ()) {
108 // Check driver debug mask value and global mask
110 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
115 // If Format is NULL, then ASSERT().
117 ASSERT (Format
!= NULL
);
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 Convert an UINT32 value into HEX string sepcified by Buffer.
191 @param Value The HEX value to convert to string
192 @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 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
215 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
269 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask 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 ();
296 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
298 This function fills Length bytes of Buffer with the value specified by
299 PcdDebugClearMemoryValue, and returns Buffer.
301 If Buffer is NULL, then ASSERT().
302 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
304 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
305 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
307 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
322 Returns TRUE if ASSERT() macros are enabled.
324 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
325 PcdDebugProperyMask is set. Otherwise FALSE is returned.
327 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
328 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
337 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
342 Returns TRUE if DEBUG() macros are enabled.
344 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
345 PcdDebugProperyMask is set. Otherwise FALSE is returned.
347 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
348 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
357 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
361 Returns TRUE if DEBUG_CODE() macros are enabled.
363 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
364 PcdDebugProperyMask is set. Otherwise FALSE is returned.
366 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
367 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
376 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
381 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
383 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
384 PcdDebugProperyMask is set. Otherwise FALSE is returned.
386 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
387 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
392 DebugClearMemoryEnabled (
396 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
400 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
402 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
404 @retval TRUE Current ErrorLevel is supported.
405 @retval FALSE Current ErrorLevel is not supported.
410 DebugPrintLevelEnabled (
411 IN CONST UINTN ErrorLevel
414 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);