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.
15 #include <FrameworkPei.h>
17 #include <Guid/StatusCodeDataTypeId.h>
18 #include <Guid/StatusCodeDataTypeDebug.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/ReportStatusCodeLib.h>
24 #include <Library/PcdLib.h>
27 Prints a debug message to the debug output device if the specified error level is enabled.
29 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
30 the message specified by Format and the associated variable argument list to
31 the debug output device.
33 If Format is NULL, then ASSERT().
35 @param ErrorLevel The error level of the debug message.
36 @param Format Format string for the debug message to print.
37 @param ... Variable argument list whose contents are accessed
38 based on the format string specified by Format.
45 IN CONST CHAR8
*Format
,
49 UINT64 Buffer
[(EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof (UINT64
)) + 1];
50 EFI_DEBUG_INFO
*DebugInfo
;
53 BASE_LIST BaseListMarker
;
59 // If Format is NULL, then ASSERT().
61 ASSERT (Format
!= NULL
);
64 // Check driver Debug Level value and global debug level
66 if ((ErrorLevel
& PcdGet32 (PcdDebugPrintErrorLevel
)) == 0) {
71 // Compute the total size of the record.
72 // Note that the passing-in format string and variable parameters will be constructed to
73 // the following layout:
75 // Buffer->|------------------------|
77 // DebugInfo->|------------------------|
78 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
79 // BaseListMarker->|------------------------|
81 // | variable arguments | 12 * sizeof (UINT64)
83 // |------------------------|
85 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
87 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
90 // If the TotalSize is larger than the maximum record size, then return
92 if (TotalSize
> sizeof (Buffer
)) {
97 // Fill in EFI_DEBUG_INFO
99 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
100 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
101 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
102 // just makes addess of BaseListMarker, which follows DebugInfo, 64-bit aligned.
104 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
105 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
106 BaseListMarker
= (BASE_LIST
)(DebugInfo
+ 1);
107 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
110 // Copy the Format string into the record
112 AsciiStrCpy (FormatString
, Format
);
115 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
116 // of format in DEBUG string, which is followed by the DEBUG format string.
117 // Here we will process the variable arguments and pack them in this area.
119 VA_START (VaListMarker
, Format
);
120 for (; *Format
!= '\0'; Format
++) {
122 // Only format with prefix % is processed.
124 if (*Format
!= '%') {
129 // Parse Flags and Width
131 for (Done
= FALSE
; !Done
; ) {
150 // These characters in format field are omitted.
156 // 'L" or "l" in format field means the number being printed is a UINT64
162 // '*' in format field means the precision of the field is specified by
163 // a UINTN argument in the argument list.
165 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
169 // Make no output if Format string terminates unexpectedly when
170 // looking up for flag, width, precision and type.
174 // break skipped on purpose.
178 // When valid argument type detected or format string terminates unexpectedly,
179 // the inner loop is done.
187 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
191 if (sizeof (VOID
*) > 4) {
198 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
200 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
208 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
211 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
214 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
219 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
220 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
221 // the EFI_DEBUG_INFO record
223 ASSERT ((CHAR8
*)BaseListMarker
<= FormatString
);
226 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
228 if ((CHAR8
*)BaseListMarker
> FormatString
) {
232 VA_END (VaListMarker
);
235 // Send the DebugInfo record
237 REPORT_STATUS_CODE_EX (
239 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
242 &gEfiStatusCodeDataTypeDebugGuid
,
249 Prints an assert message containing a filename, line number, and description.
250 This may be followed by a breakpoint or a dead loop.
252 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
253 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
254 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
255 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
256 CpuDeadLoop() is called. If neither of these bits are set, then this function
257 returns immediately after the message is printed to the debug output device.
258 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
259 processing another DebugAssert(), then DebugAssert() must return immediately.
261 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
262 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
264 @param FileName Pointer to the name of the source file that generated the assert condition.
265 @param LineNumber The line number in the source file that generated the assert condition
266 @param Description Pointer to the description of the assert condition.
272 IN CONST CHAR8
*FileName
,
274 IN CONST CHAR8
*Description
277 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
278 EFI_DEBUG_ASSERT_DATA
*AssertData
;
282 UINTN DescriptionSize
;
285 // Make sure it will all fit in the passed in buffer
287 FileNameSize
= AsciiStrSize (FileName
);
288 DescriptionSize
= AsciiStrSize (Description
);
289 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
) + FileNameSize
+ DescriptionSize
;
290 if (TotalSize
<= sizeof (Buffer
)) {
292 // Fill in EFI_DEBUG_ASSERT_DATA
294 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
295 AssertData
->LineNumber
= (UINT32
)LineNumber
;
298 // Copy Ascii FileName including NULL.
300 Temp
= AsciiStrCpy ((CHAR8
*)(AssertData
+ 1), FileName
);
303 // Copy Ascii Description
305 AsciiStrCpy (Temp
+ FileNameSize
, Description
);
307 REPORT_STATUS_CODE_EX (
308 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
309 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
319 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
321 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
323 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
330 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
332 This function fills Length bytes of Buffer with the value specified by
333 PcdDebugClearMemoryValue, and returns Buffer.
335 If Buffer is NULL, then ASSERT().
336 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
338 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
339 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
341 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
351 ASSERT (Buffer
!= NULL
);
353 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
358 Returns TRUE if ASSERT() macros are enabled.
360 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
361 PcdDebugProperyMask is set. Otherwise FALSE is returned.
363 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
364 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
373 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
378 Returns TRUE if DEBUG() macros are enabled.
380 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
381 PcdDebugProperyMask is set. Otherwise FALSE is returned.
383 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
384 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
393 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
398 Returns TRUE if DEBUG_CODE() macros are enabled.
400 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
401 PcdDebugProperyMask is set. Otherwise FALSE is returned.
403 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
404 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
413 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
418 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
420 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
421 PcdDebugProperyMask is set. Otherwise FALSE is returned.
423 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
424 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
429 DebugClearMemoryEnabled (
433 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);