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 BASE_LIST BaseListMarkerPointer
;
107 // If Format is NULL, then ASSERT().
109 ASSERT (Format
!= NULL
);
112 // Check driver Debug Level value and global debug level
114 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
119 // Compute the total size of the record.
120 // Note that the passing-in format string and variable parameters will be constructed to
121 // the following layout:
123 // Buffer->|------------------------|
124 // | Padding | 4 bytes
125 // DebugInfo->|------------------------|
126 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
127 // BaseListMarkerPointer->|------------------------|
129 // | variable arguments | 12 * sizeof (UINT64)
131 // |------------------------|
133 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
135 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
138 // If the TotalSize is larger than the maximum record size, then truncate it.
140 if (TotalSize
> sizeof (Buffer
)) {
141 TotalSize
= sizeof (Buffer
);
145 // Fill in EFI_DEBUG_INFO
147 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarkerPointer is
148 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarkerPointer will cause
149 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
150 // just makes address of BaseListMarkerPointer, which follows DebugInfo, 64-bit aligned.
152 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
153 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
154 BaseListMarkerPointer
= (BASE_LIST
)(DebugInfo
+ 1);
155 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
158 // Copy the Format string into the record. It will be truncated if it's too long.
161 FormatString
, sizeof(Buffer
) - (4 + sizeof(EFI_DEBUG_INFO
) + 12 * sizeof(UINT64
)),
162 Format
, sizeof(Buffer
) - (4 + sizeof(EFI_DEBUG_INFO
) + 12 * sizeof(UINT64
)) - 1
166 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
167 // of format in DEBUG string, which is followed by the DEBUG format string.
168 // Here we will process the variable arguments and pack them in this area.
172 // Use the actual format string.
174 Format
= FormatString
;
175 for (; *Format
!= '\0'; Format
++) {
177 // Only format with prefix % is processed.
179 if (*Format
!= '%') {
184 // Parse Flags and Width
186 for (Format
++; TRUE
; Format
++) {
187 if (*Format
== '.' || *Format
== '-' || *Format
== '+' || *Format
== ' ') {
189 // These characters in format field are omitted.
193 if (*Format
>= '0' && *Format
<= '9') {
195 // These characters in format field are omitted.
199 if (*Format
== 'L' || *Format
== 'l') {
201 // 'L" or "l" in format field means the number being printed is a UINT64
206 if (*Format
== '*') {
208 // '*' in format field means the precision of the field is specified by
209 // a UINTN argument in the argument list.
211 if (BaseListMarker
== NULL
) {
212 BASE_ARG (BaseListMarkerPointer
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
214 BASE_ARG (BaseListMarkerPointer
, UINTN
) = BASE_ARG (BaseListMarker
, UINTN
);
218 if (*Format
== '\0') {
220 // Make no output if Format string terminates unexpectedly when
221 // looking up for flag, width, precision and type.
226 // When valid argument type detected or format string terminates unexpectedly,
227 // the inner loop is done.
233 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
235 if ((*Format
== 'p') && (sizeof (VOID
*) > 4)) {
238 if (*Format
== 'p' || *Format
== 'X' || *Format
== 'x' || *Format
== 'd' || *Format
== 'u') {
240 if (BaseListMarker
== NULL
) {
241 BASE_ARG (BaseListMarkerPointer
, INT64
) = VA_ARG (VaListMarker
, INT64
);
243 BASE_ARG (BaseListMarkerPointer
, INT64
) = BASE_ARG (BaseListMarker
, INT64
);
246 if (BaseListMarker
== NULL
) {
247 BASE_ARG (BaseListMarkerPointer
, int) = VA_ARG (VaListMarker
, int);
249 BASE_ARG (BaseListMarkerPointer
, int) = BASE_ARG (BaseListMarker
, int);
252 } else if (*Format
== 's' || *Format
== 'S' || *Format
== 'a' || *Format
== 'g' || *Format
== 't') {
253 if (BaseListMarker
== NULL
) {
254 BASE_ARG (BaseListMarkerPointer
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
256 BASE_ARG (BaseListMarkerPointer
, VOID
*) = BASE_ARG (BaseListMarker
, VOID
*);
258 } else if (*Format
== 'c') {
259 if (BaseListMarker
== NULL
) {
260 BASE_ARG (BaseListMarkerPointer
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
262 BASE_ARG (BaseListMarkerPointer
, UINTN
) = BASE_ARG (BaseListMarker
, UINTN
);
264 } else if (*Format
== 'r') {
265 if (BaseListMarker
== NULL
) {
266 BASE_ARG (BaseListMarkerPointer
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
268 BASE_ARG (BaseListMarkerPointer
, RETURN_STATUS
) = BASE_ARG (BaseListMarker
, RETURN_STATUS
);
273 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
274 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
275 // the EFI_DEBUG_INFO record
277 ASSERT ((CHAR8
*)BaseListMarkerPointer
<= FormatString
);
280 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
282 if ((CHAR8
*)BaseListMarkerPointer
> FormatString
) {
288 // Send the DebugInfo record
290 REPORT_STATUS_CODE_EX (
292 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
295 &gEfiStatusCodeDataTypeDebugGuid
,
302 Prints a debug message to the debug output device if the specified
303 error level is enabled.
305 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
306 GetDebugPrintErrorLevel (), then print the message specified by Format and
307 the associated variable argument list to the debug output device.
309 If Format is NULL, then ASSERT().
311 @param ErrorLevel The error level of the debug message.
312 @param Format Format string for the debug message to print.
313 @param VaListMarker VA_LIST marker for the variable argument list.
320 IN CONST CHAR8
*Format
,
321 IN VA_LIST VaListMarker
324 DebugPrintMarker (ErrorLevel
, Format
, VaListMarker
, NULL
);
328 Prints a debug message to the debug output device if the specified
329 error level is enabled.
330 This function use BASE_LIST which would provide a more compatible
331 service than VA_LIST.
333 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
334 GetDebugPrintErrorLevel (), then print the message specified by Format and
335 the associated variable argument list to the debug output device.
337 If Format is NULL, then ASSERT().
339 @param ErrorLevel The error level of the debug message.
340 @param Format Format string for the debug message to print.
341 @param BaseListMarker BASE_LIST marker for the variable argument list.
348 IN CONST CHAR8
*Format
,
349 IN BASE_LIST BaseListMarker
352 DebugPrintMarker (ErrorLevel
, Format
, mVaListNull
, BaseListMarker
);
356 Prints an assert message containing a filename, line number, and description.
357 This may be followed by a breakpoint or a dead loop.
359 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
360 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
361 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
362 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
363 CpuDeadLoop() is called. If neither of these bits are set, then this function
364 returns immediately after the message is printed to the debug output device.
365 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
366 processing another DebugAssert(), then DebugAssert() must return immediately.
368 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
369 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
371 @param FileName Pointer to the name of the source file that generated the assert condition.
372 @param LineNumber The line number in the source file that generated the assert condition
373 @param Description Pointer to the description of the assert condition.
379 IN CONST CHAR8
*FileName
,
381 IN CONST CHAR8
*Description
384 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
385 EFI_DEBUG_ASSERT_DATA
*AssertData
;
389 UINTN ModuleNameSize
;
391 UINTN DescriptionSize
;
396 HeaderSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
398 // Compute string size of module name enclosed by []
400 ModuleNameSize
= 2 + AsciiStrSize (gEfiCallerBaseName
);
401 FileNameSize
= AsciiStrSize (FileName
);
402 DescriptionSize
= AsciiStrSize (Description
);
405 // Make sure it will all fit in the passed in buffer.
407 if (HeaderSize
+ ModuleNameSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
409 // remove module name if it's too long to be filled into buffer
412 if (HeaderSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
414 // FileName + Description is too long to be filled into buffer.
416 if (HeaderSize
+ FileNameSize
< sizeof (Buffer
)) {
418 // Description has enough buffer to be truncated.
420 DescriptionSize
= sizeof (Buffer
) - HeaderSize
- FileNameSize
;
423 // FileName is too long to be filled into buffer.
424 // FileName will be truncated. Reserved one byte for Description NULL terminator.
427 FileNameSize
= sizeof (Buffer
) - HeaderSize
- DescriptionSize
;
432 // Fill in EFI_DEBUG_ASSERT_DATA
434 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
435 AssertData
->LineNumber
= (UINT32
)LineNumber
;
436 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
438 Temp
= (CHAR8
*)(AssertData
+ 1);
441 // Copy Ascii [ModuleName].
443 if (ModuleNameSize
!= 0) {
444 CopyMem(Temp
, "[", 1);
445 CopyMem(Temp
+ 1, gEfiCallerBaseName
, ModuleNameSize
- 3);
446 CopyMem(Temp
+ ModuleNameSize
- 2, "] ", 2);
450 // Copy Ascii FileName including NULL terminator.
452 Temp
= CopyMem (Temp
+ ModuleNameSize
, FileName
, FileNameSize
);
453 Temp
[FileNameSize
- 1] = 0;
454 TotalSize
+= (ModuleNameSize
+ FileNameSize
);
457 // Copy Ascii Description include NULL terminator.
459 Temp
= CopyMem (Temp
+ FileNameSize
, Description
, DescriptionSize
);
460 Temp
[DescriptionSize
- 1] = 0;
461 TotalSize
+= DescriptionSize
;
463 REPORT_STATUS_CODE_EX (
464 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
465 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
474 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
476 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
478 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
485 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
487 This function fills Length bytes of Buffer with the value specified by
488 PcdDebugClearMemoryValue, and returns Buffer.
490 If Buffer is NULL, then ASSERT().
491 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
493 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
494 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
496 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
506 ASSERT (Buffer
!= NULL
);
508 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
513 Returns TRUE if ASSERT() macros are enabled.
515 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
516 PcdDebugProperyMask is set. Otherwise FALSE is returned.
518 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
519 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
528 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
533 Returns TRUE if DEBUG() macros are enabled.
535 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
536 PcdDebugProperyMask is set. Otherwise FALSE is returned.
538 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
539 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
548 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
553 Returns TRUE if DEBUG_CODE() macros are enabled.
555 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
556 PcdDebugProperyMask is set. Otherwise FALSE is returned.
558 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
559 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
568 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
573 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
575 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
576 PcdDebugProperyMask is set. Otherwise FALSE is returned.
578 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
579 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
584 DebugClearMemoryEnabled (
588 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
592 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
594 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
596 @retval TRUE Current ErrorLevel is supported.
597 @retval FALSE Current ErrorLevel is not supported.
602 DebugPrintLevelEnabled (
603 IN CONST UINTN ErrorLevel
606 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);