2 Debug Library based on report status code library.
4 Note that if the debug message length is larger than the maximum allowable
5 record length, then the debug message will be ignored directly.
7 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Guid/StatusCodeDataTypeId.h>
21 #include <Guid/StatusCodeDataTypeDebug.h>
23 #include <Library/DebugLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/ReportStatusCodeLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/DebugPrintErrorLevelLib.h>
31 // VA_LIST can not initialize to NULL for all compiler, so we use this to
32 // indicate a null VA_LIST
37 Prints a debug message to the debug output device if the specified error level is enabled.
39 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
40 GetDebugPrintErrorLevel (), then print the message specified by Format and the
41 associated variable argument list to the debug output device.
43 If Format is NULL, then ASSERT().
45 If the length of the message string specificed by Format is larger than the maximum allowable
46 record length, then directly return and not print it.
48 @param ErrorLevel The error level of the debug message.
49 @param Format Format string for the debug message to print.
50 @param ... Variable argument list whose contents are accessed
51 based on the format string specified by Format.
58 IN CONST CHAR8
*Format
,
64 VA_START (Marker
, Format
);
65 DebugVPrint (ErrorLevel
, Format
, Marker
);
70 Prints a debug message to the debug output device if the specified
71 error level is enabled base on Null-terminated format string and a
72 VA_LIST argument list or a BASE_LIST argument list.
74 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
75 GetDebugPrintErrorLevel (), then print the message specified by Format and
76 the associated variable argument list to the debug output device.
78 Only one list type is used.
79 If BaseListMarker == NULL, then use VaListMarker.
80 Otherwise use BaseListMarker and the VaListMarker should be initilized as
83 If Format is NULL, then ASSERT().
85 @param ErrorLevel The error level of the debug message.
86 @param Format Format string for the debug message to print.
87 @param VaListMarker VA_LIST marker for the variable argument list.
88 @param BaseListMarker BASE_LIST marker for the variable argument list.
94 IN CONST CHAR8
*Format
,
95 IN VA_LIST VaListMarker
,
96 IN BASE_LIST BaseListMarker
99 UINT64 Buffer
[(EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof (UINT64
)) + 1];
100 EFI_DEBUG_INFO
*DebugInfo
;
102 UINTN DestBufferSize
;
103 BASE_LIST BaseListMarkerPointer
;
108 // If Format is NULL, then ASSERT().
110 ASSERT (Format
!= NULL
);
113 // Check driver Debug Level value and global debug level
115 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
120 // Compute the total size of the record.
121 // Note that the passing-in format string and variable parameters will be constructed to
122 // the following layout:
124 // Buffer->|------------------------|
125 // | Padding | 4 bytes
126 // DebugInfo->|------------------------|
127 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
128 // BaseListMarkerPointer->|------------------------|
130 // | variable arguments | 12 * sizeof (UINT64)
132 // |------------------------|
134 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
136 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
139 // If the TotalSize is larger than the maximum record size, then truncate it.
141 if (TotalSize
> sizeof (Buffer
)) {
142 TotalSize
= sizeof (Buffer
);
146 // Fill in EFI_DEBUG_INFO
148 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarkerPointer is
149 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarkerPointer will cause
150 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
151 // just makes address of BaseListMarkerPointer, which follows DebugInfo, 64-bit aligned.
153 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
154 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
155 BaseListMarkerPointer
= (BASE_LIST
)(DebugInfo
+ 1);
156 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
159 // Copy the Format string into the record
161 // According to the content structure of Buffer shown above, the size of
162 // the FormatString buffer is the size of Buffer minus the Padding
163 // (4 bytes), minus the size of EFI_DEBUG_INFO, minus the size of
164 // variable arguments (12 * sizeof (UINT64)).
166 DestBufferSize
= sizeof (Buffer
) - 4 - sizeof (EFI_DEBUG_INFO
) - 12 * sizeof (UINT64
);
167 AsciiStrCpyS (FormatString
, DestBufferSize
/ sizeof (CHAR8
), Format
);
170 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
171 // of format in DEBUG string, which is followed by the DEBUG format string.
172 // Here we will process the variable arguments and pack them in this area.
174 for (; *Format
!= '\0'; Format
++) {
176 // Only format with prefix % is processed.
178 if (*Format
!= '%') {
183 // Parse Flags and Width
185 for (Format
++; TRUE
; Format
++) {
186 if (*Format
== '.' || *Format
== '-' || *Format
== '+' || *Format
== ' ') {
188 // These characters in format field are omitted.
192 if (*Format
>= '0' && *Format
<= '9') {
194 // These characters in format field are omitted.
198 if (*Format
== 'L' || *Format
== 'l') {
200 // 'L" or "l" in format field means the number being printed is a UINT64
205 if (*Format
== '*') {
207 // '*' in format field means the precision of the field is specified by
208 // a UINTN argument in the argument list.
210 if (BaseListMarker
== NULL
) {
211 BASE_ARG (BaseListMarkerPointer
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
213 BASE_ARG (BaseListMarkerPointer
, UINTN
) = BASE_ARG (BaseListMarker
, UINTN
);
217 if (*Format
== '\0') {
219 // Make no output if Format string terminates unexpectedly when
220 // looking up for flag, width, precision and type.
225 // When valid argument type detected or format string terminates unexpectedly,
226 // the inner loop is done.
232 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
234 if ((*Format
== 'p') && (sizeof (VOID
*) > 4)) {
237 if (*Format
== 'p' || *Format
== 'X' || *Format
== 'x' || *Format
== 'd' || *Format
== 'u') {
239 if (BaseListMarker
== NULL
) {
240 BASE_ARG (BaseListMarkerPointer
, INT64
) = VA_ARG (VaListMarker
, INT64
);
242 BASE_ARG (BaseListMarkerPointer
, INT64
) = BASE_ARG (BaseListMarker
, INT64
);
245 if (BaseListMarker
== NULL
) {
246 BASE_ARG (BaseListMarkerPointer
, int) = VA_ARG (VaListMarker
, int);
248 BASE_ARG (BaseListMarkerPointer
, int) = BASE_ARG (BaseListMarker
, int);
251 } else if (*Format
== 's' || *Format
== 'S' || *Format
== 'a' || *Format
== 'g' || *Format
== 't') {
252 if (BaseListMarker
== NULL
) {
253 BASE_ARG (BaseListMarkerPointer
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
255 BASE_ARG (BaseListMarkerPointer
, VOID
*) = BASE_ARG (BaseListMarker
, VOID
*);
257 } else if (*Format
== 'c') {
258 if (BaseListMarker
== NULL
) {
259 BASE_ARG (BaseListMarkerPointer
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
261 BASE_ARG (BaseListMarkerPointer
, UINTN
) = BASE_ARG (BaseListMarker
, UINTN
);
263 } else if (*Format
== 'r') {
264 if (BaseListMarker
== NULL
) {
265 BASE_ARG (BaseListMarkerPointer
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
267 BASE_ARG (BaseListMarkerPointer
, RETURN_STATUS
) = BASE_ARG (BaseListMarker
, RETURN_STATUS
);
272 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
273 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
274 // the EFI_DEBUG_INFO record
276 ASSERT ((CHAR8
*)BaseListMarkerPointer
<= FormatString
);
279 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
281 if ((CHAR8
*)BaseListMarkerPointer
> FormatString
) {
287 // Send the DebugInfo record
289 REPORT_STATUS_CODE_EX (
291 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
294 &gEfiStatusCodeDataTypeDebugGuid
,
301 Prints a debug message to the debug output device if the specified
302 error level is enabled.
304 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
305 GetDebugPrintErrorLevel (), then print the message specified by Format and
306 the associated variable argument list to the debug output device.
308 If Format is NULL, then ASSERT().
310 @param ErrorLevel The error level of the debug message.
311 @param Format Format string for the debug message to print.
312 @param VaListMarker VA_LIST marker for the variable argument list.
319 IN CONST CHAR8
*Format
,
320 IN VA_LIST VaListMarker
323 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
327 Prints a debug message to the debug output device if the specified
328 error level is enabled.
329 This function use BASE_LIST which would provide a more compatible
330 service than VA_LIST.
332 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
333 GetDebugPrintErrorLevel (), then print the message specified by Format and
334 the associated variable argument list to the debug output device.
336 If Format is NULL, then ASSERT().
338 @param ErrorLevel The error level of the debug message.
339 @param Format Format string for the debug message to print.
340 @param BaseListMarker BASE_LIST marker for the variable argument list.
347 IN CONST CHAR8
*Format
,
348 IN BASE_LIST BaseListMarker
351 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
355 Prints an assert message containing a filename, line number, and description.
356 This may be followed by a breakpoint or a dead loop.
358 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
359 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
360 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
361 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
362 CpuDeadLoop() is called. If neither of these bits are set, then this function
363 returns immediately after the message is printed to the debug output device.
364 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
365 processing another DebugAssert(), then DebugAssert() must return immediately.
367 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
368 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
370 @param FileName Pointer to the name of the source file that generated the assert condition.
371 @param LineNumber The line number in the source file that generated the assert condition
372 @param Description Pointer to the description of the assert condition.
378 IN CONST CHAR8
*FileName
,
380 IN CONST CHAR8
*Description
383 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
384 EFI_DEBUG_ASSERT_DATA
*AssertData
;
388 UINTN ModuleNameSize
;
390 UINTN DescriptionSize
;
395 HeaderSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
397 // Compute string size of module name enclosed by []
399 ModuleNameSize
= 2 + AsciiStrSize (gEfiCallerBaseName
);
400 FileNameSize
= AsciiStrSize (FileName
);
401 DescriptionSize
= AsciiStrSize (Description
);
404 // Make sure it will all fit in the passed in buffer.
406 if (HeaderSize
+ ModuleNameSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
408 // remove module name if it's too long to be filled into buffer
411 if (HeaderSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
413 // FileName + Description is too long to be filled into buffer.
415 if (HeaderSize
+ FileNameSize
< sizeof (Buffer
)) {
417 // Description has enough buffer to be truncated.
419 DescriptionSize
= sizeof (Buffer
) - HeaderSize
- FileNameSize
;
422 // FileName is too long to be filled into buffer.
423 // FileName will be truncated. Reserved one byte for Description NULL terminator.
426 FileNameSize
= sizeof (Buffer
) - HeaderSize
- DescriptionSize
;
431 // Fill in EFI_DEBUG_ASSERT_DATA
433 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
434 AssertData
->LineNumber
= (UINT32
)LineNumber
;
435 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
437 Temp
= (CHAR8
*)(AssertData
+ 1);
440 // Copy Ascii [ModuleName].
442 if (ModuleNameSize
!= 0) {
443 CopyMem(Temp
, "[", 1);
444 CopyMem(Temp
+ 1, gEfiCallerBaseName
, ModuleNameSize
- 3);
445 CopyMem(Temp
+ ModuleNameSize
- 2, "] ", 2);
449 // Copy Ascii FileName including NULL terminator.
451 Temp
= CopyMem (Temp
+ ModuleNameSize
, FileName
, FileNameSize
);
452 Temp
[FileNameSize
- 1] = 0;
453 TotalSize
+= (ModuleNameSize
+ FileNameSize
);
456 // Copy Ascii Description include NULL terminator.
458 Temp
= CopyMem (Temp
+ FileNameSize
, Description
, DescriptionSize
);
459 Temp
[DescriptionSize
- 1] = 0;
460 TotalSize
+= DescriptionSize
;
462 REPORT_STATUS_CODE_EX (
463 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
464 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
473 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
475 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
477 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
484 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
486 This function fills Length bytes of Buffer with the value specified by
487 PcdDebugClearMemoryValue, and returns Buffer.
489 If Buffer is NULL, then ASSERT().
490 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
492 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
493 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
495 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
505 ASSERT (Buffer
!= NULL
);
507 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
512 Returns TRUE if ASSERT() macros are enabled.
514 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
515 PcdDebugProperyMask is set. Otherwise FALSE is returned.
517 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
518 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
527 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
532 Returns TRUE if DEBUG() macros are enabled.
534 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
535 PcdDebugProperyMask is set. Otherwise FALSE is returned.
537 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
538 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
547 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
552 Returns TRUE if DEBUG_CODE() macros are enabled.
554 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
555 PcdDebugProperyMask is set. Otherwise FALSE is returned.
557 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
558 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
567 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
572 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
574 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
575 PcdDebugProperyMask is set. Otherwise FALSE is returned.
577 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
578 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
583 DebugClearMemoryEnabled (
587 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
591 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
593 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
595 @retval TRUE Current ErrorLevel is supported.
596 @retval FALSE Current ErrorLevel is not supported.
601 DebugPrintLevelEnabled (
602 IN CONST UINTN ErrorLevel
605 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);