IntelFrameworkModulePkg: DebugAssert enhancement
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
CommitLineData
2287f237 1/** @file\r
9ba6cd30 2 Debug Library based on report status code library.\r
2287f237 3\r
a1158366 4 Note that if the debug message length is larger than the maximum allowable\r
5 record length, then the debug message will be ignored directly.\r
6\r
30aba8d3 7 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
180a5a35 8 This program and the accompanying materials\r
2287f237 9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
4569c9e6 18#include <PiPei.h>\r
79bc7a89 19\r
2287f237 20#include <Guid/StatusCodeDataTypeId.h>\r
3a6064fa 21#include <Guid/StatusCodeDataTypeDebug.h>\r
2287f237 22\r
23#include <Library/DebugLib.h>\r
24#include <Library/BaseLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
26#include <Library/ReportStatusCodeLib.h>\r
27#include <Library/PcdLib.h>\r
c77b88d6 28#include <Library/DebugPrintErrorLevelLib.h>\r
2287f237 29\r
2287f237 30/**\r
2287f237 31 Prints a debug message to the debug output device if the specified error level is enabled.\r
32\r
c77b88d6 33 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function \r
34 GetDebugPrintErrorLevel (), then print the message specified by Format and the \r
35 associated variable argument list to the debug output device.\r
2287f237 36\r
37 If Format is NULL, then ASSERT().\r
38\r
a1158366 39 If the length of the message string specificed by Format is larger than the maximum allowable\r
40 record length, then directly return and not print it.\r
41\r
2287f237 42 @param ErrorLevel The error level of the debug message.\r
43 @param Format Format string for the debug message to print.\r
967c09fa 44 @param ... Variable argument list whose contents are accessed \r
45 based on the format string specified by Format.\r
2287f237 46\r
47**/\r
48VOID\r
49EFIAPI\r
50DebugPrint (\r
51 IN UINTN ErrorLevel,\r
52 IN CONST CHAR8 *Format,\r
53 ...\r
54 )\r
55{\r
6916d99c 56 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];\r
2287f237 57 EFI_DEBUG_INFO *DebugInfo;\r
58 UINTN TotalSize;\r
d5cbc27b 59 UINTN DestBufferSize;\r
ca9938b8 60 VA_LIST VaListMarker;\r
61 BASE_LIST BaseListMarker;\r
62 CHAR8 *FormatString;\r
63 BOOLEAN Long;\r
2287f237 64\r
65 //\r
66 // If Format is NULL, then ASSERT().\r
67 //\r
68 ASSERT (Format != NULL);\r
69\r
70 //\r
71 // Check driver Debug Level value and global debug level\r
72 //\r
c77b88d6 73 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
2287f237 74 return;\r
75 }\r
76\r
ca9938b8 77 //\r
0fac539f 78 // Compute the total size of the record.\r
79 // Note that the passing-in format string and variable parameters will be constructed to \r
80 // the following layout:\r
ca9938b8 81 //\r
0fac539f 82 // Buffer->|------------------------|\r
3d747a89 83 // | Padding | 4 bytes\r
0fac539f 84 // DebugInfo->|------------------------|\r
85 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)\r
86 // BaseListMarker->|------------------------|\r
87 // | ... |\r
88 // | variable arguments | 12 * sizeof (UINT64)\r
89 // | ... |\r
90 // |------------------------|\r
91 // | Format String |\r
92 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)\r
93 //\r
94 TotalSize = 4 + sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);\r
ca9938b8 95\r
ca9938b8 96 //\r
97 // If the TotalSize is larger than the maximum record size, then return\r
98 //\r
1ca88083 99 if (TotalSize > sizeof (Buffer)) {\r
2287f237 100 return;\r
101 }\r
102\r
103 //\r
ca9938b8 104 // Fill in EFI_DEBUG_INFO\r
2287f237 105 //\r
9ba6cd30 106 // Here we skip the first 4 bytes of Buffer, because we must ensure BaseListMarker is\r
107 // 64-bit aligned, otherwise retrieving 64-bit parameter from BaseListMarker will cause\r
108 // exception on IPF. Buffer starts at 64-bit aligned address, so skipping 4 types (sizeof(EFI_DEBUG_INFO))\r
3d747a89 109 // just makes address of BaseListMarker, which follows DebugInfo, 64-bit aligned.\r
9ba6cd30 110 //\r
6916d99c 111 DebugInfo = (EFI_DEBUG_INFO *)(Buffer) + 1;\r
2287f237 112 DebugInfo->ErrorLevel = (UINT32)ErrorLevel;\r
ca9938b8 113 BaseListMarker = (BASE_LIST)(DebugInfo + 1);\r
3d7dfb38 114 FormatString = (CHAR8 *)((UINT64 *)(DebugInfo + 1) + 12);\r
ca9938b8 115\r
116 //\r
117 // Copy the Format string into the record\r
118 //\r
d5cbc27b
HW
119 // According to the content structure of Buffer shown above, the size of\r
120 // the FormatString buffer is the size of Buffer minus the Padding\r
121 // (4 bytes), minus the size of EFI_DEBUG_INFO, minus the size of\r
122 // variable arguments (12 * sizeof (UINT64)).\r
123 //\r
124 DestBufferSize = sizeof (Buffer) - 4 - sizeof (EFI_DEBUG_INFO) - 12 * sizeof (UINT64);\r
125 AsciiStrCpyS (FormatString, DestBufferSize / sizeof (CHAR8), Format);\r
2287f237 126\r
127 //\r
9ba6cd30 128 // The first 12 * sizeof (UINT64) bytes following EFI_DEBUG_INFO are for variable arguments\r
129 // of format in DEBUG string, which is followed by the DEBUG format string.\r
130 // Here we will process the variable arguments and pack them in this area.\r
2287f237 131 //\r
ca9938b8 132 VA_START (VaListMarker, Format);\r
133 for (; *Format != '\0'; Format++) {\r
9ba6cd30 134 //\r
135 // Only format with prefix % is processed.\r
136 //\r
ca9938b8 137 if (*Format != '%') {\r
138 continue;\r
139 }\r
140 Long = FALSE;\r
141 //\r
142 // Parse Flags and Width\r
143 //\r
3d747a89 144 for (Format++; TRUE; Format++) {\r
145 if (*Format == '.' || *Format == '-' || *Format == '+' || *Format == ' ') {\r
9ba6cd30 146 //\r
147 // These characters in format field are omitted.\r
148 //\r
3d747a89 149 continue;\r
150 }\r
151 if (*Format >= '0' && *Format <= '9') {\r
152 //\r
153 // These characters in format field are omitted.\r
154 //\r
155 continue;\r
156 }\r
157 if (*Format == 'L' || *Format == 'l') {\r
9ba6cd30 158 //\r
159 // 'L" or "l" in format field means the number being printed is a UINT64\r
160 //\r
ca9938b8 161 Long = TRUE;\r
3d747a89 162 continue;\r
163 }\r
164 if (*Format == '*') {\r
9ba6cd30 165 //\r
166 // '*' in format field means the precision of the field is specified by\r
167 // a UINTN argument in the argument list.\r
168 //\r
ca9938b8 169 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
3d747a89 170 continue;\r
171 }\r
172 if (*Format == '\0') {\r
ca9938b8 173 //\r
174 // Make no output if Format string terminates unexpectedly when\r
175 // looking up for flag, width, precision and type. \r
176 //\r
177 Format--;\r
ca9938b8 178 }\r
3d747a89 179 //\r
180 // When valid argument type detected or format string terminates unexpectedly,\r
181 // the inner loop is done.\r
182 //\r
183 break;\r
184 }\r
185 \r
ca9938b8 186 //\r
9ba6cd30 187 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.\r
ca9938b8 188 //\r
3d747a89 189 if ((*Format == 'p') && (sizeof (VOID *) > 4)) {\r
190 Long = TRUE;\r
191 }\r
94e3eee6 192 if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {\r
ca9938b8 193 if (Long) {\r
194 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);\r
195 } else {\r
196 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);\r
197 }\r
3d747a89 198 } else if (*Format == 's' || *Format == 'S' || *Format == 'a' || *Format == 'g' || *Format == 't') {\r
ca9938b8 199 BASE_ARG (BaseListMarker, VOID *) = VA_ARG (VaListMarker, VOID *);\r
3d747a89 200 } else if (*Format == 'c') {\r
ca9938b8 201 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);\r
3d747a89 202 } else if (*Format == 'r') {\r
ca9938b8 203 BASE_ARG (BaseListMarker, RETURN_STATUS) = VA_ARG (VaListMarker, RETURN_STATUS);\r
ca9938b8 204 }\r
205\r
206 //\r
207 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then ASSERT()\r
208 // This indicates that the DEBUG() macro is passing in more argument than can be handled by \r
209 // the EFI_DEBUG_INFO record\r
210 //\r
3d7dfb38 211 ASSERT ((CHAR8 *)BaseListMarker <= FormatString);\r
ca9938b8 212\r
213 //\r
214 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return\r
215 //\r
3d7dfb38 216 if ((CHAR8 *)BaseListMarker > FormatString) {\r
3bbe68a3 217 VA_END (VaListMarker);\r
ca9938b8 218 return;\r
219 }\r
2287f237 220 }\r
ca9938b8 221 VA_END (VaListMarker);\r
2287f237 222\r
ca9938b8 223 //\r
224 // Send the DebugInfo record\r
225 //\r
2287f237 226 REPORT_STATUS_CODE_EX (\r
227 EFI_DEBUG_CODE,\r
228 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),\r
229 0,\r
230 NULL,\r
231 &gEfiStatusCodeDataTypeDebugGuid,\r
232 DebugInfo,\r
233 TotalSize\r
234 );\r
235}\r
236\r
2287f237 237/**\r
9ba6cd30 238 Prints an assert message containing a filename, line number, and description. \r
2287f237 239 This may be followed by a breakpoint or a dead loop.\r
240\r
241 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9ba6cd30 242 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of \r
243 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if \r
244 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then \r
245 CpuDeadLoop() is called. If neither of these bits are set, then this function \r
2287f237 246 returns immediately after the message is printed to the debug output device.\r
584125bc 247 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
2287f237 248 processing another DebugAssert(), then DebugAssert() must return immediately.\r
249\r
250 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
2287f237 251 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
252\r
253 @param FileName Pointer to the name of the source file that generated the assert condition.\r
254 @param LineNumber The line number in the source file that generated the assert condition\r
255 @param Description Pointer to the description of the assert condition.\r
256\r
257**/\r
258VOID\r
259EFIAPI\r
260DebugAssert (\r
261 IN CONST CHAR8 *FileName,\r
262 IN UINTN LineNumber,\r
263 IN CONST CHAR8 *Description\r
264 )\r
265{\r
266 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];\r
267 EFI_DEBUG_ASSERT_DATA *AssertData;\r
ea1b39e6 268 UINTN HeaderSize;\r
2287f237 269 UINTN TotalSize;\r
270 CHAR8 *Temp;\r
1ffe7cc5 271 UINTN ModuleNameSize;\r
9ba6cd30 272 UINTN FileNameSize;\r
273 UINTN DescriptionSize;\r
2287f237 274\r
275 //\r
ea1b39e6 276 // Get string size\r
2287f237 277 //\r
ea1b39e6 278 HeaderSize = sizeof (EFI_DEBUG_ASSERT_DATA);\r
1ffe7cc5
BA
279 //\r
280 // Compute string size of module name enclosed by []\r
281 //\r
282 ModuleNameSize = 2 + AsciiStrSize (gEfiCallerBaseName);\r
ea1b39e6
LG
283 FileNameSize = AsciiStrSize (FileName);\r
284 DescriptionSize = AsciiStrSize (Description);\r
2287f237 285\r
ea1b39e6
LG
286 //\r
287 // Make sure it will all fit in the passed in buffer.\r
288 //\r
1ffe7cc5 289 if (HeaderSize + ModuleNameSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
2287f237 290 //\r
1ffe7cc5 291 // remove module name if it's too long to be filled into buffer\r
2287f237 292 //\r
1ffe7cc5
BA
293 ModuleNameSize = 0;\r
294 if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {\r
ea1b39e6 295 //\r
1ffe7cc5 296 // FileName + Description is too long to be filled into buffer.\r
ea1b39e6 297 //\r
1ffe7cc5
BA
298 if (HeaderSize + FileNameSize < sizeof (Buffer)) {\r
299 //\r
300 // Description has enough buffer to be truncated.\r
301 //\r
302 DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;\r
303 } else {\r
304 //\r
305 // FileName is too long to be filled into buffer.\r
306 // FileName will be truncated. Reserved one byte for Description NULL terminator.\r
307 //\r
308 DescriptionSize = 1;\r
309 FileNameSize = sizeof (Buffer) - HeaderSize - DescriptionSize;\r
310 }\r
ea1b39e6 311 }\r
2287f237 312 }\r
ea1b39e6
LG
313 //\r
314 // Fill in EFI_DEBUG_ASSERT_DATA\r
315 //\r
316 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;\r
317 AssertData->LineNumber = (UINT32)LineNumber;\r
318 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA);\r
319\r
1ffe7cc5
BA
320 Temp = (CHAR8 *)(AssertData + 1);\r
321\r
322 //\r
323 // Copy Ascii [ModuleName].\r
324 //\r
325 if (ModuleNameSize != 0) {\r
326 CopyMem(Temp, "[", 1);\r
327 CopyMem(Temp + 1, gEfiCallerBaseName, ModuleNameSize - 3);\r
328 CopyMem(Temp + ModuleNameSize - 2, "] ", 2);\r
329 }\r
330\r
ea1b39e6
LG
331 //\r
332 // Copy Ascii FileName including NULL terminator.\r
333 //\r
1ffe7cc5 334 Temp = CopyMem (Temp + ModuleNameSize, FileName, FileNameSize);\r
ea1b39e6 335 Temp[FileNameSize - 1] = 0;\r
1ffe7cc5 336 TotalSize += (ModuleNameSize + FileNameSize);\r
ea1b39e6
LG
337\r
338 //\r
339 // Copy Ascii Description include NULL terminator.\r
340 //\r
856d6438 341 Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);\r
ea1b39e6
LG
342 Temp[DescriptionSize - 1] = 0;\r
343 TotalSize += DescriptionSize;\r
344\r
345 REPORT_STATUS_CODE_EX (\r
346 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),\r
347 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),\r
348 0,\r
349 NULL,\r
350 NULL,\r
351 AssertData,\r
352 TotalSize\r
353 );\r
2287f237 354\r
355 //\r
356 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
357 //\r
9ba6cd30 358 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
2287f237 359 CpuBreakpoint ();\r
9ba6cd30 360 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
2287f237 361 CpuDeadLoop ();\r
362 }\r
363}\r
364\r
365\r
366/**\r
2287f237 367 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
368\r
9ba6cd30 369 This function fills Length bytes of Buffer with the value specified by \r
2287f237 370 PcdDebugClearMemoryValue, and returns Buffer.\r
371\r
372 If Buffer is NULL, then ASSERT().\r
9ba6cd30 373 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
2287f237 374\r
9ba6cd30 375 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
376 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue. \r
2287f237 377\r
9ba6cd30 378 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
2287f237 379\r
380**/\r
381VOID *\r
382EFIAPI\r
383DebugClearMemory (\r
384 OUT VOID *Buffer,\r
385 IN UINTN Length\r
386 )\r
387{\r
2287f237 388 ASSERT (Buffer != NULL);\r
389\r
9ba6cd30 390 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));\r
2287f237 391}\r
392\r
393\r
394/**\r
2287f237 395 Returns TRUE if ASSERT() macros are enabled.\r
396\r
9ba6cd30 397 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of \r
2287f237 398 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
399\r
400 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
401 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
402\r
403**/\r
404BOOLEAN\r
405EFIAPI\r
406DebugAssertEnabled (\r
407 VOID\r
408 )\r
409{\r
9ba6cd30 410 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
2287f237 411}\r
412\r
413\r
9ba6cd30 414/** \r
415 Returns TRUE if DEBUG() macros are enabled.\r
2287f237 416\r
9ba6cd30 417 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of \r
2287f237 418 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
419\r
420 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
421 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
422\r
423**/\r
424BOOLEAN\r
425EFIAPI\r
426DebugPrintEnabled (\r
427 VOID\r
428 )\r
429{\r
9ba6cd30 430 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
2287f237 431}\r
432\r
433\r
9ba6cd30 434/** \r
435 Returns TRUE if DEBUG_CODE() macros are enabled.\r
2287f237 436\r
9ba6cd30 437 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of \r
2287f237 438 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
439\r
440 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
441 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
442\r
443**/\r
444BOOLEAN\r
445EFIAPI\r
446DebugCodeEnabled (\r
447 VOID\r
448 )\r
449{\r
9ba6cd30 450 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
2287f237 451}\r
452\r
453\r
9ba6cd30 454/** \r
455 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
2287f237 456\r
9ba6cd30 457 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of \r
2287f237 458 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
459\r
9ba6cd30 460 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
461 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
2287f237 462\r
463**/\r
464BOOLEAN\r
465EFIAPI\r
466DebugClearMemoryEnabled (\r
467 VOID\r
468 )\r
469{\r
9ba6cd30 470 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
2287f237 471}\r
30aba8d3
LG
472\r
473/**\r
474 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
475\r
476 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
477\r
478 @retval TRUE Current ErrorLevel is supported.\r
479 @retval FALSE Current ErrorLevel is not supported.\r
480\r
481**/\r
482BOOLEAN\r
483EFIAPI\r
484DebugPrintLevelEnabled (\r
485 IN CONST UINTN ErrorLevel\r
486 )\r
487{\r
488 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
489}\r