2 Debug Library based on report status code library.
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <FrameworkPei.h>
18 #include <Guid/StatusCodeDataTypeId.h>
19 #include <Guid/StatusCodeDataTypeDebug.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/ReportStatusCodeLib.h>
25 #include <Library/PcdLib.h>
28 Prints a debug message to the debug output device if the specified error level is enabled.
30 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
31 the message specified by Format and the associated variable argument list to
32 the debug output device.
34 If Format is NULL, then ASSERT().
36 @param ErrorLevel The error level of the debug message.
37 @param Format Format string for the debug message to print.
38 @param ... Variable argument list whose contents are accessed
39 based on the format string specified by Format.
46 IN CONST CHAR8
*Format
,
50 UINT64 Buffer
[(EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof (UINT64
)) + 1];
51 EFI_DEBUG_INFO
*DebugInfo
;
54 BASE_LIST BaseListMarker
;
60 // If Format is NULL, then ASSERT().
62 ASSERT (Format
!= NULL
);
65 // Check driver Debug Level value and global debug level
67 if ((ErrorLevel
& PcdGet32 (PcdDebugPrintErrorLevel
)) == 0) {
72 // Compute the total size of the record
74 TotalSize
= sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
77 // If the TotalSize is larger than the maximum record size, then return
79 if (TotalSize
> sizeof (Buffer
)) {
84 // Fill in EFI_DEBUG_INFO
86 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
87 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
88 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
89 // just makes addess of BaseListMarker, which follows DebugInfo, 64-bit aligned.
91 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
92 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
93 BaseListMarker
= (BASE_LIST
)(DebugInfo
+ 1);
94 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
97 // Copy the Format string into the record
99 AsciiStrCpy (FormatString
, Format
);
102 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
103 // of format in DEBUG string, which is followed by the DEBUG format string.
104 // Here we will process the variable arguments and pack them in this area.
106 VA_START (VaListMarker
, Format
);
107 for (; *Format
!= '\0'; Format
++) {
109 // Only format with prefix % is processed.
111 if (*Format
!= '%') {
116 // Parse Flags and Width
118 for (Done
= FALSE
; !Done
; ) {
137 // These characters in format field are omitted.
143 // 'L" or "l" in format field means the number being printed is a UINT64
149 // '*' in format field means the precision of the field is specified by
150 // a UINTN argument in the argument list.
152 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
156 // Make no output if Format string terminates unexpectedly when
157 // looking up for flag, width, precision and type.
161 // break skipped on purpose.
165 // When valid argument type detected or format string terminates unexpectedly,
166 // the inner loop is done.
174 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
178 if (sizeof (VOID
*) > 4) {
185 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
187 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
195 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
198 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
201 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
206 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
207 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
208 // the EFI_DEBUG_INFO record
210 ASSERT ((CHAR8
*)BaseListMarker
<= FormatString
);
213 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
215 if ((CHAR8
*)BaseListMarker
> FormatString
) {
219 VA_END (VaListMarker
);
222 // Send the DebugInfo record
224 REPORT_STATUS_CODE_EX (
226 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
229 &gEfiStatusCodeDataTypeDebugGuid
,
236 Prints an assert message containing a filename, line number, and description.
237 This may be followed by a breakpoint or a dead loop.
239 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
240 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
241 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
242 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
243 CpuDeadLoop() is called. If neither of these bits are set, then this function
244 returns immediately after the message is printed to the debug output device.
245 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
246 processing another DebugAssert(), then DebugAssert() must return immediately.
248 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
249 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
251 @param FileName Pointer to the name of the source file that generated the assert condition.
252 @param LineNumber The line number in the source file that generated the assert condition
253 @param Description Pointer to the description of the assert condition.
259 IN CONST CHAR8
*FileName
,
261 IN CONST CHAR8
*Description
264 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
265 EFI_DEBUG_ASSERT_DATA
*AssertData
;
269 UINTN DescriptionSize
;
272 // Make sure it will all fit in the passed in buffer
274 FileNameSize
= AsciiStrSize (FileName
);
275 DescriptionSize
= AsciiStrSize (Description
);
276 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
) + FileNameSize
+ DescriptionSize
;
277 if (TotalSize
<= sizeof (Buffer
)) {
279 // Fill in EFI_DEBUG_ASSERT_DATA
281 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
282 AssertData
->LineNumber
= (UINT32
)LineNumber
;
285 // Copy Ascii FileName including NULL.
287 Temp
= AsciiStrCpy ((CHAR8
*)(AssertData
+ 1), FileName
);
290 // Copy Ascii Description
292 AsciiStrCpy (Temp
+ FileNameSize
, Description
);
294 REPORT_STATUS_CODE_EX (
295 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
296 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
306 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
308 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
310 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
317 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
319 This function fills Length bytes of Buffer with the value specified by
320 PcdDebugClearMemoryValue, and returns Buffer.
322 If Buffer is NULL, then ASSERT().
323 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
325 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
326 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
328 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
338 ASSERT (Buffer
!= NULL
);
340 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
345 Returns TRUE if ASSERT() macros are enabled.
347 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
348 PcdDebugProperyMask is set. Otherwise FALSE is returned.
350 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
351 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
360 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
365 Returns TRUE if DEBUG() macros are enabled.
367 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
368 PcdDebugProperyMask is set. Otherwise FALSE is returned.
370 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
371 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
380 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
385 Returns TRUE if DEBUG_CODE() macros are enabled.
387 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
388 PcdDebugProperyMask is set. Otherwise FALSE is returned.
390 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
391 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
400 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
405 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
407 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
408 PcdDebugProperyMask is set. Otherwise FALSE is returned.
410 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
411 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
416 DebugClearMemoryEnabled (
420 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);