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 - 2009, 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.
18 #include <FrameworkPei.h>
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>
30 Prints a debug message to the debug output device if the specified error level is enabled.
32 If any bit in ErrorLevel is also set in PcdDebugPrintErrorLevel, then print
33 the message specified by Format and the associated variable argument list to
34 the debug output device.
36 If Format is NULL, then ASSERT().
38 If the length of the message string specificed by Format is larger than the maximum allowable
39 record length, then directly return and not print it.
41 @param ErrorLevel The error level of the debug message.
42 @param Format Format string for the debug message to print.
43 @param ... Variable argument list whose contents are accessed
44 based on the format string specified by Format.
51 IN CONST CHAR8
*Format
,
55 UINT64 Buffer
[(EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof (UINT64
)) + 1];
56 EFI_DEBUG_INFO
*DebugInfo
;
59 BASE_LIST BaseListMarker
;
64 // If Format is NULL, then ASSERT().
66 ASSERT (Format
!= NULL
);
69 // Check driver Debug Level value and global debug level
71 if ((ErrorLevel
& PcdGet32 (PcdDebugPrintErrorLevel
)) == 0) {
76 // Compute the total size of the record.
77 // Note that the passing-in format string and variable parameters will be constructed to
78 // the following layout:
80 // Buffer->|------------------------|
81 // | Padding | 4 bytes
82 // DebugInfo->|------------------------|
83 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
84 // BaseListMarker->|------------------------|
86 // | variable arguments | 12 * sizeof (UINT64)
88 // |------------------------|
90 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
92 TotalSize
= 4 + sizeof (EFI_DEBUG_INFO
) + 12 * sizeof (UINT64
) + AsciiStrSize (Format
);
95 // If the TotalSize is larger than the maximum record size, then return
97 if (TotalSize
> sizeof (Buffer
)) {
102 // Fill in EFI_DEBUG_INFO
104 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is
105 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause
106 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))
107 // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.
109 DebugInfo
= (EFI_DEBUG_INFO
*)(Buffer
) + 1;
110 DebugInfo
->ErrorLevel
= (UINT32
)ErrorLevel
;
111 BaseListMarker
= (BASE_LIST
)(DebugInfo
+ 1);
112 FormatString
= (CHAR8
*)((UINT64
*)(DebugInfo
+ 1) + 12);
115 // Copy the Format string into the record
117 AsciiStrCpy (FormatString
, Format
);
120 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments
121 // of format in DEBUG string, which is followed by the DEBUG format string.
122 // Here we will process the variable arguments and pack them in this area.
124 VA_START (VaListMarker
, Format
);
125 for (; *Format
!= '\0'; Format
++) {
127 // Only format with prefix % is processed.
129 if (*Format
!= '%') {
134 // Parse Flags and Width
136 for (Format
++; TRUE
; Format
++) {
137 if (*Format
== '.' || *Format
== '-' || *Format
== '+' || *Format
== ' ') {
139 // These characters in format field are omitted.
143 if (*Format
>= '0' && *Format
<= '9') {
145 // These characters in format field are omitted.
149 if (*Format
== 'L' || *Format
== 'l') {
151 // 'L" or "l" in format field means the number being printed is a UINT64
156 if (*Format
== '*') {
158 // '*' in format field means the precision of the field is specified by
159 // a UINTN argument in the argument list.
161 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
164 if (*Format
== '\0') {
166 // Make no output if Format string terminates unexpectedly when
167 // looking up for flag, width, precision and type.
172 // When valid argument type detected or format string terminates unexpectedly,
173 // the inner loop is done.
179 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
181 if ((*Format
== 'p') && (sizeof (VOID
*) > 4)) {
184 if (*Format
== 'p' || *Format
== 'X' || *Format
== 'x' || *Format
== 'd') {
186 BASE_ARG (BaseListMarker
, INT64
) = VA_ARG (VaListMarker
, INT64
);
188 BASE_ARG (BaseListMarker
, int) = VA_ARG (VaListMarker
, int);
190 } else if (*Format
== 's' || *Format
== 'S' || *Format
== 'a' || *Format
== 'g' || *Format
== 't') {
191 BASE_ARG (BaseListMarker
, VOID
*) = VA_ARG (VaListMarker
, VOID
*);
192 } else if (*Format
== 'c') {
193 BASE_ARG (BaseListMarker
, UINTN
) = VA_ARG (VaListMarker
, UINTN
);
194 } else if (*Format
== 'r') {
195 BASE_ARG (BaseListMarker
, RETURN_STATUS
) = VA_ARG (VaListMarker
, RETURN_STATUS
);
199 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()
200 // This indicates that the DEBUG() macro is passing in more argument than can be handled by
201 // the EFI_DEBUG_INFO record
203 ASSERT ((CHAR8
*)BaseListMarker
<= FormatString
);
206 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
208 if ((CHAR8
*)BaseListMarker
> FormatString
) {
212 VA_END (VaListMarker
);
215 // Send the DebugInfo record
217 REPORT_STATUS_CODE_EX (
219 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_DC_UNSPECIFIED
),
222 &gEfiStatusCodeDataTypeDebugGuid
,
229 Prints an assert message containing a filename, line number, and description.
230 This may be followed by a breakpoint or a dead loop.
232 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
233 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
234 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
235 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
236 CpuDeadLoop() is called. If neither of these bits are set, then this function
237 returns immediately after the message is printed to the debug output device.
238 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
239 processing another DebugAssert(), then DebugAssert() must return immediately.
241 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
242 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
244 @param FileName Pointer to the name of the source file that generated the assert condition.
245 @param LineNumber The line number in the source file that generated the assert condition
246 @param Description Pointer to the description of the assert condition.
252 IN CONST CHAR8
*FileName
,
254 IN CONST CHAR8
*Description
257 UINT64 Buffer
[EFI_STATUS_CODE_DATA_MAX_SIZE
/ sizeof(UINT64
)];
258 EFI_DEBUG_ASSERT_DATA
*AssertData
;
262 UINTN DescriptionSize
;
265 // Make sure it will all fit in the passed in buffer
267 FileNameSize
= AsciiStrSize (FileName
);
268 DescriptionSize
= AsciiStrSize (Description
);
269 TotalSize
= sizeof (EFI_DEBUG_ASSERT_DATA
) + FileNameSize
+ DescriptionSize
;
270 if (TotalSize
<= sizeof (Buffer
)) {
272 // Fill in EFI_DEBUG_ASSERT_DATA
274 AssertData
= (EFI_DEBUG_ASSERT_DATA
*)Buffer
;
275 AssertData
->LineNumber
= (UINT32
)LineNumber
;
278 // Copy Ascii FileName including NULL.
280 Temp
= AsciiStrCpy ((CHAR8
*)(AssertData
+ 1), FileName
);
283 // Copy Ascii Description
285 AsciiStrCpy (Temp
+ FileNameSize
, Description
);
287 REPORT_STATUS_CODE_EX (
288 (EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
),
289 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
),
299 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
301 if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED
) != 0) {
303 } else if ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED
) != 0) {
310 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
312 This function fills Length bytes of Buffer with the value specified by
313 PcdDebugClearMemoryValue, and returns Buffer.
315 If Buffer is NULL, then ASSERT().
316 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
318 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
319 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
321 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
331 ASSERT (Buffer
!= NULL
);
333 return SetMem (Buffer
, Length
, PcdGet8 (PcdDebugClearMemoryValue
));
338 Returns TRUE if ASSERT() macros are enabled.
340 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
341 PcdDebugProperyMask is set. Otherwise FALSE is returned.
343 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
344 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
353 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED
) != 0);
358 Returns TRUE if DEBUG() macros are enabled.
360 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
361 PcdDebugProperyMask is set. Otherwise FALSE is returned.
363 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
364 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
373 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED
) != 0);
378 Returns TRUE if DEBUG_CODE() macros are enabled.
380 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
381 PcdDebugProperyMask is set. Otherwise FALSE is returned.
383 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
384 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
393 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED
) != 0);
398 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
400 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
401 PcdDebugProperyMask is set. Otherwise FALSE is returned.
403 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
404 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
409 DebugClearMemoryEnabled (
413 return (BOOLEAN
) ((PcdGet8 (PcdDebugPropertyMask
) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED
) != 0);