3 Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include <Library/DebugLib.h>
16 #include <Library/BaseLib.h>
17 #include <Library/PrintLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/SerialPortLib.h>
21 #include <Library/DebugDeviceLib.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
29 CONST CHAR8
*mHexTable
= "0123456789ABCDEF";
32 // VA_LIST can not initialize to NULL for all compiler, so we use this to
33 // indicate a null VA_LIST
38 Get stack frame pointer of function call.
40 @return StackFramePointer stack frame pointer of function call.
44 GetStackFramePointer (
50 Prints a debug message to the debug output device if the specified error level is enabled.
52 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
53 GetDebugPrintErrorLevel (), then print the message specified by Format and the
54 associated variable argument list to the debug output device.
56 If Format is NULL, then ASSERT().
58 @param ErrorLevel The error level of the debug message.
59 @param Format Format string for the debug message to print.
60 @param ... Variable argument list whose contents are accessed
61 based on the format string specified by Format.
68 IN CONST CHAR8
*Format
,
74 VA_START (Marker
, Format
);
75 DebugVPrint (ErrorLevel
, Format
, Marker
);
80 Prints a debug message to the debug output device if the specified
81 error level is enabled base on Null-terminated format string and a
82 VA_LIST argument list or a BASE_LIST argument list.
84 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
85 GetDebugPrintErrorLevel (), then print the message specified by Format and
86 the associated variable argument list to the debug output device.
88 If Format is NULL, then ASSERT().
90 @param ErrorLevel The error level of the debug message.
91 @param Format Format string for the debug message to print.
92 @param VaListMarker VA_LIST marker for the variable argument list.
93 @param BaseListMarker BASE_LIST marker for the variable argument list.
99 IN CONST CHAR8
*Format
,
100 IN VA_LIST VaListMarker
,
101 IN BASE_LIST BaseListMarker
104 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
107 // If Format is NULL, then ASSERT().
109 if (!GetDebugPrintDeviceEnable ()) {
114 // Check driver debug mask value and global mask
116 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
121 // If Format is NULL, then ASSERT().
123 ASSERT (Format
!= NULL
);
126 // Convert the DEBUG() message to an ASCII String
128 if (BaseListMarker
== NULL
) {
129 AsciiVSPrint (Buffer
, sizeof (Buffer
), Format
, VaListMarker
);
131 AsciiBSPrint (Buffer
, sizeof (Buffer
), Format
, BaseListMarker
);
135 // Send the print string to a Serial Port
137 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
141 Prints a debug message to the debug output device if the specified
142 error level is enabled.
144 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
145 GetDebugPrintErrorLevel (), then print the message specified by Format and
146 the associated variable argument list to the debug output device.
148 If Format is NULL, then ASSERT().
150 @param ErrorLevel The error level of the debug message.
151 @param Format Format string for the debug message to print.
152 @param VaListMarker VA_LIST marker for the variable argument list.
159 IN CONST CHAR8
*Format
,
160 IN VA_LIST VaListMarker
163 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
167 Prints a debug message to the debug output device if the specified
168 error level is enabled.
169 This function use BASE_LIST which would provide a more compatible
170 service than VA_LIST.
172 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
173 GetDebugPrintErrorLevel (), then print the message specified by Format and
174 the associated variable argument list to the debug output device.
176 If Format is NULL, then ASSERT().
178 @param ErrorLevel The error level of the debug message.
179 @param Format Format string for the debug message to print.
180 @param BaseListMarker BASE_LIST marker for the variable argument list.
187 IN CONST CHAR8
*Format
,
188 IN BASE_LIST BaseListMarker
191 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
195 Convert an UINT32 value into HEX string sepcified by Buffer.
197 @param Value The HEX value to convert to string
198 @param Buffer The pointer to the target buffer to be filled with HEX string
208 for (Idx
= 7; Idx
>= 0; Idx
--) {
209 Buffer
[Idx
] = mHexTable
[Value
& 0x0F];
215 Prints an assert message containing a filename, line number, and description.
216 This may be followed by a breakpoint or a dead loop.
218 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
219 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
220 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
221 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
222 CpuDeadLoop() is called. If neither of these bits are set, then this function
223 returns immediately after the message is printed to the debug output device.
224 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
225 processing another DebugAssert(), then DebugAssert() must return immediately.
227 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
228 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
232 DebugAssertInternal (
236 CHAR8 Buffer
[MAX_DEBUG_MESSAGE_LENGTH
];
239 Frame
= (UINT32
*)GetStackFramePointer ();
242 // Generate the ASSERT() message in Ascii format
246 sizeof(Buffer
) / sizeof(CHAR8
),
247 "-> EBP:0x00000000 EIP:0x00000000\n",
248 sizeof(Buffer
) / sizeof(CHAR8
) - 1
250 SerialPortWrite ((UINT8
*)"ASSERT DUMP:\n", 13);
251 while (Frame
!= NULL
) {
252 FillHex ((UINT32
)Frame
, Buffer
+ 9);
253 FillHex (Frame
[1], Buffer
+ 9 + 8 + 8);
254 SerialPortWrite ((UINT8
*)Buffer
, AsciiStrLen (Buffer
));
255 if ((Frame
[0] > (UINT32
)Frame
) && (Frame
[0] < (UINT32
)Frame
+ 0x00100000)) {
256 Frame
= (UINT32
*)Frame
[0];
269 Prints an assert message containing a filename, line number, and description.
270 This may be followed by a breakpoint or a dead loop.
272 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
273 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
274 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
275 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
276 CpuDeadLoop() is called. If neither of these bits are set, then this function
277 returns immediately after the message is printed to the debug output device.
278 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
279 processing another DebugAssert(), then DebugAssert() must return immediately.
281 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
282 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
284 @param FileName The pointer to the name of the source file that generated the assert condition.
285 @param LineNumber The line number in the source file that generated the assert condition
286 @param Description The pointer to the description of the assert condition.
292 IN CONST CHAR8
*FileName
,
294 IN CONST CHAR8
*Description
297 DebugAssertInternal ();
302 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
304 This function fills Length bytes of Buffer with the value specified by
305 PcdDebugClearMemoryValue, and returns Buffer.
307 If Buffer is NULL, then ASSERT().
308 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
310 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
311 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
313 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
328 Returns TRUE if ASSERT() macros are enabled.
330 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
331 PcdDebugProperyMask is set. Otherwise FALSE is returned.
333 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
334 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
343 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 PcdDebugProperyMask is set. Otherwise FALSE is returned.
353 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
354 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask 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 PcdDebugProperyMask is set. Otherwise FALSE is returned.
372 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
373 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
382 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
387 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
389 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
390 PcdDebugProperyMask is set. Otherwise FALSE is returned.
392 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
393 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
398 DebugClearMemoryEnabled (
402 return (BOOLEAN
) ((PcdGet8(PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
406 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
408 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
410 @retval TRUE Current ErrorLevel is supported.
411 @retval FALSE Current ErrorLevel is not supported.
416 DebugPrintLevelEnabled (
417 IN CONST UINTN ErrorLevel
420 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);