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 - 2018, 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 Prints a debug message to the debug output device if the specified error level is enabled.
33 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
34 GetDebugPrintErrorLevel (), then print the message specified by Format and the
35 associated variable argument list to the debug output device.
37 If Format is NULL, then ASSERT().
39 If the length of the message string specificed by Format is larger than the maximum allowable
40 record length, then directly return and not print it.
42 @param ErrorLevel The error level of the debug message.
43 @param Format Format string for the debug message to print.
44 @param ... Variable argument list whose contents are accessed
45 based on the format string specified by Format.
52 IN CONST CHAR8
*Format
,
56 UINT64 Buffer
[(EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof (UINT64
)) + 1];
57 EFI_DEBUG_INFO
*DebugInfo
;
60 BASE_LIST BaseListMarker
;
65 // If Format is NULL, then ASSERT().
67 ASSERT (Format
!= NULL
);
70 // Check driver Debug Level value and global debug level
72 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
77 // Compute the total size of the record.
78 // Note that the passing-in format string and variable parameters will be constructed to
79 // the following layout:
81 // Buffer->|------------------------|
82 // | Padding | 4 bytes
83 // DebugInfo->|------------------------|
84 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
85 // BaseListMarker->|------------------------|
87 // | variable arguments | 12 * sizeof (UINT64)
89 // |------------------------|
91 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
93 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
96 // If the TotalSize is larger than the maximum record size, then return
98 if (TotalSize
> sizeof (Buffer
)) {
99 TotalSize
= sizeof (Buffer
);
103 // Fill in EFI_DEBUG_INFO
105 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
106 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
107 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
108 // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.
110 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
111 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
112 BaseListMarker
= (BASE_LIST
)(DebugInfo
+ 1);
113 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
116 // Copy the Format string into the record. It will be truncated if it's too long.
119 FormatString
, sizeof(Buffer
) - (4 + sizeof(EFI_DEBUG_INFO
) + 12 * sizeof(UINT64
)),
120 Format
, sizeof(Buffer
) - (4 + sizeof(EFI_DEBUG_INFO
) + 12 * sizeof(UINT64
)) - 1
124 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
125 // of format in DEBUG string, which is followed by the DEBUG format string.
126 // Here we will process the variable arguments and pack them in this area.
128 VA_START (VaListMarker
, Format
);
131 // Use the actual format string.
133 Format
= FormatString
;
134 for (; *Format
!= '\0'; Format
++) {
136 // Only format with prefix % is processed.
138 if (*Format
!= '%') {
143 // Parse Flags and Width
145 for (Format
++; TRUE
; Format
++) {
146 if (*Format
== '.' || *Format
== '-' || *Format
== '+' || *Format
== ' ') {
148 // These characters in format field are omitted.
152 if (*Format
>= '0' && *Format
<= '9') {
154 // These characters in format field are omitted.
158 if (*Format
== 'L' || *Format
== 'l') {
160 // 'L" or "l" in format field means the number being printed is a UINT64
165 if (*Format
== '*') {
167 // '*' in format field means the precision of the field is specified by
168 // a UINTN argument in the argument list.
170 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
173 if (*Format
== '\0') {
175 // Make no output if Format string terminates unexpectedly when
176 // looking up for flag, width, precision and type.
181 // When valid argument type detected or format string terminates unexpectedly,
182 // the inner loop is done.
188 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
190 if ((*Format
== 'p') && (sizeof (VOID
*) > 4)) {
193 if (*Format
== 'p' || *Format
== 'X' || *Format
== 'x' || *Format
== 'd' || *Format
== 'u') {
195 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
197 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
199 } else if (*Format
== 's' || *Format
== 'S' || *Format
== 'a' || *Format
== 'g' || *Format
== 't') {
200 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
201 } else if (*Format
== 'c') {
202 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
203 } else if (*Format
== 'r') {
204 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
208 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
209 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
210 // the EFI_DEBUG_INFO record
212 ASSERT ((CHAR8
*)BaseListMarker
<= FormatString
);
215 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
217 if ((CHAR8
*)BaseListMarker
> FormatString
) {
218 VA_END (VaListMarker
);
222 VA_END (VaListMarker
);
225 // Send the DebugInfo record
227 REPORT_STATUS_CODE_EX (
229 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
232 &gEfiStatusCodeDataTypeDebugGuid
,
239 Prints an assert message containing a filename, line number, and description.
240 This may be followed by a breakpoint or a dead loop.
242 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
243 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
244 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
245 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
246 CpuDeadLoop() is called. If neither of these bits are set, then this function
247 returns immediately after the message is printed to the debug output device.
248 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
249 processing another DebugAssert(), then DebugAssert() must return immediately.
251 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
252 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
254 @param FileName Pointer to the name of the source file that generated the assert condition.
255 @param LineNumber The line number in the source file that generated the assert condition
256 @param Description Pointer to the description of the assert condition.
262 IN CONST CHAR8
*FileName
,
264 IN CONST CHAR8
*Description
267 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
268 EFI_DEBUG_ASSERT_DATA
*AssertData
;
272 UINTN ModuleNameSize
;
274 UINTN DescriptionSize
;
279 HeaderSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
281 // Compute string size of module name enclosed by []
283 ModuleNameSize
= 2 + AsciiStrSize (gEfiCallerBaseName
);
284 FileNameSize
= AsciiStrSize (FileName
);
285 DescriptionSize
= AsciiStrSize (Description
);
288 // Make sure it will all fit in the passed in buffer.
290 if (HeaderSize
+ ModuleNameSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
292 // remove module name if it's too long to be filled into buffer
295 if (HeaderSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
297 // FileName + Description is too long to be filled into buffer.
299 if (HeaderSize
+ FileNameSize
< sizeof (Buffer
)) {
301 // Description has enough buffer to be truncated.
303 DescriptionSize
= sizeof (Buffer
) - HeaderSize
- FileNameSize
;
306 // FileName is too long to be filled into buffer.
307 // FileName will be truncated. Reserved one byte for Description NULL terminator.
310 FileNameSize
= sizeof (Buffer
) - HeaderSize
- DescriptionSize
;
315 // Fill in EFI_DEBUG_ASSERT_DATA
317 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
318 AssertData
->LineNumber
= (UINT32
)LineNumber
;
319 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
321 Temp
= (CHAR8
*)(AssertData
+ 1);
324 // Copy Ascii [ModuleName].
326 if (ModuleNameSize
!= 0) {
327 CopyMem(Temp
, "[", 1);
328 CopyMem(Temp
+ 1, gEfiCallerBaseName
, ModuleNameSize
- 3);
329 CopyMem(Temp
+ ModuleNameSize
- 2, "] ", 2);
333 // Copy Ascii FileName including NULL terminator.
335 Temp
= CopyMem (Temp
+ ModuleNameSize
, FileName
, FileNameSize
);
336 Temp
[FileNameSize
- 1] = 0;
337 TotalSize
+= (ModuleNameSize
+ FileNameSize
);
340 // Copy Ascii Description include NULL terminator.
342 Temp
= CopyMem (Temp
+ FileNameSize
, Description
, DescriptionSize
);
343 Temp
[DescriptionSize
- 1] = 0;
344 TotalSize
+= DescriptionSize
;
346 REPORT_STATUS_CODE_EX (
347 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
348 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
357 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
359 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
361 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
368 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
370 This function fills Length bytes of Buffer with the value specified by
371 PcdDebugClearMemoryValue, and returns Buffer.
373 If Buffer is NULL, then ASSERT().
374 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
376 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
377 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
379 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
389 ASSERT (Buffer
!= NULL
);
391 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
396 Returns TRUE if ASSERT() macros are enabled.
398 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
399 PcdDebugProperyMask is set. Otherwise FALSE is returned.
401 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
402 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
411 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
416 Returns TRUE if DEBUG() macros are enabled.
418 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
419 PcdDebugProperyMask is set. Otherwise FALSE is returned.
421 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
422 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
431 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
436 Returns TRUE if DEBUG_CODE() macros are enabled.
438 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
439 PcdDebugProperyMask is set. Otherwise FALSE is returned.
441 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
442 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
451 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
456 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
458 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
459 PcdDebugProperyMask is set. Otherwise FALSE is returned.
461 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
462 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
467 DebugClearMemoryEnabled (
471 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
475 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
477 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
479 @retval TRUE Current ErrorLevel is supported.
480 @retval FALSE Current ErrorLevel is not supported.
485 DebugPrintLevelEnabled (
486 IN CONST UINTN ErrorLevel
489 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);