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 - 2015, 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
;
61 BASE_LIST BaseListMarker
;
66 // If Format is NULL, then ASSERT().
68 ASSERT (Format
!= NULL
);
71 // Check driver Debug Level value and global debug level
73 if ((ErrorLevel
& GetDebugPrintErrorLevel ()) == 0) {
78 // Compute the total size of the record.
79 // Note that the passing-in format string and variable parameters will be constructed to
80 // the following layout:
82 // Buffer->|------------------------|
83 // | Padding | 4 bytes
84 // DebugInfo->|------------------------|
85 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
86 // BaseListMarker->|------------------------|
88 // | variable arguments | 12 * sizeof (UINT64)
90 // |------------------------|
92 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
94 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
97 // If the TotalSize is larger than the maximum record size, then return
99 if (TotalSize
> sizeof (Buffer
)) {
104 // Fill in EFI_DEBUG_INFO
106 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
107 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
108 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
109 // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.
111 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
112 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
113 BaseListMarker
= (BASE_LIST
)(DebugInfo
+ 1);
114 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
117 // Copy the Format string into the record
119 // According to the content structure of Buffer shown above, the size of
120 // the FormatString buffer is the size of Buffer minus the Padding
121 // (4 bytes), minus the size of EFI_DEBUG_INFO, minus the size of
122 // variable arguments (12 * sizeof (UINT64)).
124 DestBufferSize
= sizeof (Buffer
) - 4 - sizeof (EFI_DEBUG_INFO
) - 12 * sizeof (UINT64
);
125 AsciiStrCpyS (FormatString
, DestBufferSize
/ sizeof (CHAR8
), Format
);
128 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
129 // of format in DEBUG string, which is followed by the DEBUG format string.
130 // Here we will process the variable arguments and pack them in this area.
132 VA_START (VaListMarker
, Format
);
133 for (; *Format
!= '\0'; Format
++) {
135 // Only format with prefix % is processed.
137 if (*Format
!= '%') {
142 // Parse Flags and Width
144 for (Format
++; TRUE
; Format
++) {
145 if (*Format
== '.' || *Format
== '-' || *Format
== '+' || *Format
== ' ') {
147 // These characters in format field are omitted.
151 if (*Format
>= '0' && *Format
<= '9') {
153 // These characters in format field are omitted.
157 if (*Format
== 'L' || *Format
== 'l') {
159 // 'L" or "l" in format field means the number being printed is a UINT64
164 if (*Format
== '*') {
166 // '*' in format field means the precision of the field is specified by
167 // a UINTN argument in the argument list.
169 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
172 if (*Format
== '\0') {
174 // Make no output if Format string terminates unexpectedly when
175 // looking up for flag, width, precision and type.
180 // When valid argument type detected or format string terminates unexpectedly,
181 // the inner loop is done.
187 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
189 if ((*Format
== 'p') && (sizeof (VOID
*) > 4)) {
192 if (*Format
== 'p' || *Format
== 'X' || *Format
== 'x' || *Format
== 'd' || *Format
== 'u') {
194 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
196 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
198 } else if (*Format
== 's' || *Format
== 'S' || *Format
== 'a' || *Format
== 'g' || *Format
== 't') {
199 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
200 } else if (*Format
== 'c') {
201 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
202 } else if (*Format
== 'r') {
203 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
207 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
208 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
209 // the EFI_DEBUG_INFO record
211 ASSERT ((CHAR8
*)BaseListMarker
<= FormatString
);
214 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
216 if ((CHAR8
*)BaseListMarker
> FormatString
) {
217 VA_END (VaListMarker
);
221 VA_END (VaListMarker
);
224 // Send the DebugInfo record
226 REPORT_STATUS_CODE_EX (
228 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
231 &gEfiStatusCodeDataTypeDebugGuid
,
238 Prints an assert message containing a filename, line number, and description.
239 This may be followed by a breakpoint or a dead loop.
241 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
242 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
243 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
244 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
245 CpuDeadLoop() is called. If neither of these bits are set, then this function
246 returns immediately after the message is printed to the debug output device.
247 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
248 processing another DebugAssert(), then DebugAssert() must return immediately.
250 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
251 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
253 @param FileName Pointer to the name of the source file that generated the assert condition.
254 @param LineNumber The line number in the source file that generated the assert condition
255 @param Description Pointer to the description of the assert condition.
261 IN CONST CHAR8
*FileName
,
263 IN CONST CHAR8
*Description
266 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
267 EFI_DEBUG_ASSERT_DATA
*AssertData
;
271 UINTN ModuleNameSize
;
273 UINTN DescriptionSize
;
278 HeaderSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
280 // Compute string size of module name enclosed by []
282 ModuleNameSize
= 2 + AsciiStrSize (gEfiCallerBaseName
);
283 FileNameSize
= AsciiStrSize (FileName
);
284 DescriptionSize
= AsciiStrSize (Description
);
287 // Make sure it will all fit in the passed in buffer.
289 if (HeaderSize
+ ModuleNameSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
291 // remove module name if it's too long to be filled into buffer
294 if (HeaderSize
+ FileNameSize
+ DescriptionSize
> sizeof (Buffer
)) {
296 // FileName + Description is too long to be filled into buffer.
298 if (HeaderSize
+ FileNameSize
< sizeof (Buffer
)) {
300 // Description has enough buffer to be truncated.
302 DescriptionSize
= sizeof (Buffer
) - HeaderSize
- FileNameSize
;
305 // FileName is too long to be filled into buffer.
306 // FileName will be truncated. Reserved one byte for Description NULL terminator.
309 FileNameSize
= sizeof (Buffer
) - HeaderSize
- DescriptionSize
;
314 // Fill in EFI_DEBUG_ASSERT_DATA
316 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
317 AssertData
->LineNumber
= (UINT32
)LineNumber
;
318 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
);
320 Temp
= (CHAR8
*)(AssertData
+ 1);
323 // Copy Ascii [ModuleName].
325 if (ModuleNameSize
!= 0) {
326 CopyMem(Temp
, "[", 1);
327 CopyMem(Temp
+ 1, gEfiCallerBaseName
, ModuleNameSize
- 3);
328 CopyMem(Temp
+ ModuleNameSize
- 2, "] ", 2);
332 // Copy Ascii FileName including NULL terminator.
334 Temp
= CopyMem (Temp
+ ModuleNameSize
, FileName
, FileNameSize
);
335 Temp
[FileNameSize
- 1] = 0;
336 TotalSize
+= (ModuleNameSize
+ FileNameSize
);
339 // Copy Ascii Description include NULL terminator.
341 Temp
= CopyMem (Temp
+ FileNameSize
, Description
, DescriptionSize
);
342 Temp
[DescriptionSize
- 1] = 0;
343 TotalSize
+= DescriptionSize
;
345 REPORT_STATUS_CODE_EX (
346 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
347 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
356 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
358 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
360 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
367 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
369 This function fills Length bytes of Buffer with the value specified by
370 PcdDebugClearMemoryValue, and returns Buffer.
372 If Buffer is NULL, then ASSERT().
373 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
375 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
376 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
378 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
388 ASSERT (Buffer
!= NULL
);
390 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
395 Returns TRUE if ASSERT() macros are enabled.
397 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
398 PcdDebugProperyMask is set. Otherwise FALSE is returned.
400 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
401 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
410 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
415 Returns TRUE if DEBUG() macros are enabled.
417 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
418 PcdDebugProperyMask is set. Otherwise FALSE is returned.
420 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
421 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
430 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
435 Returns TRUE if DEBUG_CODE() macros are enabled.
437 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
438 PcdDebugProperyMask is set. Otherwise FALSE is returned.
440 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
441 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
450 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
455 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
457 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
458 PcdDebugProperyMask is set. Otherwise FALSE is returned.
460 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
461 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
466 DebugClearMemoryEnabled (
470 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);
474 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
476 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
478 @retval TRUE Current ErrorLevel is supported.
479 @retval FALSE Current ErrorLevel is not supported.
484 DebugPrintLevelEnabled (
485 IN CONST UINTN ErrorLevel
488 return (BOOLEAN
) ((ErrorLevel
& PcdGet32(PcdFixedDebugPrintErrorLevel
)) != 0);