]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/PeiDxeDebugLibReportStatusCode/DebugLib.c
MdeModulePkg: DebugAssert enhancement
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / PeiDxeDebugLibReportStatusCode / DebugLib.c
1 /** @file
2 Debug Library based on report status code library.
3
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.
6
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
12
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.
15
16 **/
17
18 #include <PiPei.h>
19
20 #include <Guid/StatusCodeDataTypeId.h>
21 #include <Guid/StatusCodeDataTypeDebug.h>
22
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>
29
30 /**
31 Prints a debug message to the debug output device if the specified error level is enabled.
32
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.
36
37 If Format is NULL, then ASSERT().
38
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.
41
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.
46
47 **/
48 VOID
49 EFIAPI
50 DebugPrint (
51 IN UINTN ErrorLevel,
52 IN CONST CHAR8 *Format,
53 ...
54 )
55 {
56 UINT64 Buffer[(EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof (UINT64)) + 1];
57 EFI_DEBUG_INFO *DebugInfo;
58 UINTN TotalSize;
59 UINTN DestBufferSize;
60 VA_LIST VaListMarker;
61 BASE_LIST BaseListMarker;
62 CHAR8 *FormatString;
63 BOOLEAN Long;
64
65 //
66 // If Format is NULL, then ASSERT().
67 //
68 ASSERT (Format != NULL);
69
70 //
71 // Check driver Debug Level value and global debug level
72 //
73 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
74 return;
75 }
76
77 //
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:
81 //
82 // Buffer->|------------------------|
83 // | Padding | 4 bytes
84 // DebugInfo->|------------------------|
85 // | EFI_DEBUG_INFO | sizeof(EFI_DEBUG_INFO)
86 // BaseListMarker->|------------------------|
87 // | ... |
88 // | variable arguments | 12 * sizeof (UINT64)
89 // | ... |
90 // |------------------------|
91 // | Format String |
92 // |------------------------|<- (UINT8 *)Buffer + sizeof(Buffer)
93 //
94 TotalSize = 4 + sizeof (EFI_DEBUG_INFO) + 12 * sizeof (UINT64) + AsciiStrSize (Format);
95
96 //
97 // If the TotalSize is larger than the maximum record size, then return
98 //
99 if (TotalSize > sizeof (Buffer)) {
100 return;
101 }
102
103 //
104 // Fill in EFI_DEBUG_INFO
105 //
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.
110 //
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);
115
116 //
117 // Copy the Format string into the record
118 //
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)).
123 //
124 DestBufferSize = sizeof (Buffer) - 4 - sizeof (EFI_DEBUG_INFO) - 12 * sizeof (UINT64);
125 AsciiStrCpyS (FormatString, DestBufferSize / sizeof (CHAR8), Format);
126
127 //
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.
131 //
132 VA_START (VaListMarker, Format);
133 for (; *Format != '\0'; Format++) {
134 //
135 // Only format with prefix % is processed.
136 //
137 if (*Format != '%') {
138 continue;
139 }
140 Long = FALSE;
141 //
142 // Parse Flags and Width
143 //
144 for (Format++; TRUE; Format++) {
145 if (*Format == '.' || *Format == '-' || *Format == '+' || *Format == ' ') {
146 //
147 // These characters in format field are omitted.
148 //
149 continue;
150 }
151 if (*Format >= '0' && *Format <= '9') {
152 //
153 // These characters in format field are omitted.
154 //
155 continue;
156 }
157 if (*Format == 'L' || *Format == 'l') {
158 //
159 // 'L" or "l" in format field means the number being printed is a UINT64
160 //
161 Long = TRUE;
162 continue;
163 }
164 if (*Format == '*') {
165 //
166 // '*' in format field means the precision of the field is specified by
167 // a UINTN argument in the argument list.
168 //
169 BASE_ARG (BaseListMarker, UINTN) = VA_ARG (VaListMarker, UINTN);
170 continue;
171 }
172 if (*Format == '\0') {
173 //
174 // Make no output if Format string terminates unexpectedly when
175 // looking up for flag, width, precision and type.
176 //
177 Format--;
178 }
179 //
180 // When valid argument type detected or format string terminates unexpectedly,
181 // the inner loop is done.
182 //
183 break;
184 }
185
186 //
187 // Pack variable arguments into the storage area following EFI_DEBUG_INFO.
188 //
189 if ((*Format == 'p') && (sizeof (VOID *) > 4)) {
190 Long = TRUE;
191 }
192 if (*Format == 'p' || *Format == 'X' || *Format == 'x' || *Format == 'd' || *Format == 'u') {
193 if (Long) {
194 BASE_ARG (BaseListMarker, INT64) = VA_ARG (VaListMarker, INT64);
195 } else {
196 BASE_ARG (BaseListMarker, int) = VA_ARG (VaListMarker, int);
197 }
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);
204 }
205
206 //
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
210 //
211 ASSERT ((CHAR8 *)BaseListMarker <= FormatString);
212
213 //
214 // If the converted BASE_LIST is larger than the 12 * sizeof (UINT64) allocated bytes, then return
215 //
216 if ((CHAR8 *)BaseListMarker > FormatString) {
217 VA_END (VaListMarker);
218 return;
219 }
220 }
221 VA_END (VaListMarker);
222
223 //
224 // Send the DebugInfo record
225 //
226 REPORT_STATUS_CODE_EX (
227 EFI_DEBUG_CODE,
228 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_DC_UNSPECIFIED),
229 0,
230 NULL,
231 &gEfiStatusCodeDataTypeDebugGuid,
232 DebugInfo,
233 TotalSize
234 );
235 }
236
237 /**
238 Prints an assert message containing a filename, line number, and description.
239 This may be followed by a breakpoint or a dead loop.
240
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.
249
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.
252
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.
256
257 **/
258 VOID
259 EFIAPI
260 DebugAssert (
261 IN CONST CHAR8 *FileName,
262 IN UINTN LineNumber,
263 IN CONST CHAR8 *Description
264 )
265 {
266 UINT64 Buffer[EFI_STATUS_CODE_DATA_MAX_SIZE / sizeof(UINT64)];
267 EFI_DEBUG_ASSERT_DATA *AssertData;
268 UINTN HeaderSize;
269 UINTN TotalSize;
270 CHAR8 *Temp;
271 UINTN FileNameSize;
272 UINTN DescriptionSize;
273
274 //
275 // Get string size
276 //
277 HeaderSize = sizeof (EFI_DEBUG_ASSERT_DATA);
278 FileNameSize = AsciiStrSize (FileName);
279 DescriptionSize = AsciiStrSize (Description);
280
281 //
282 // Make sure it will all fit in the passed in buffer.
283 //
284 if (HeaderSize + FileNameSize + DescriptionSize > sizeof (Buffer)) {
285 //
286 // FileName + Description is too long to be filled into buffer.
287 //
288 if (HeaderSize + FileNameSize < sizeof (Buffer)) {
289 //
290 // Description has enough buffer to be truncated.
291 //
292 DescriptionSize = sizeof (Buffer) - HeaderSize - FileNameSize;
293 } else {
294 //
295 // FileName is too long to be filled into buffer.
296 // FileName will be truncated. Reserved one byte for Description NULL terminator.
297 //
298 DescriptionSize = 1;
299 FileNameSize = sizeof (Buffer) - HeaderSize - DescriptionSize;
300 }
301 }
302
303 //
304 // Fill in EFI_DEBUG_ASSERT_DATA
305 //
306 AssertData = (EFI_DEBUG_ASSERT_DATA *)Buffer;
307 AssertData->LineNumber = (UINT32)LineNumber;
308 TotalSize = sizeof (EFI_DEBUG_ASSERT_DATA);
309
310 //
311 // Copy Ascii FileName including NULL terminator.
312 //
313 Temp = CopyMem (AssertData + 1, FileName, FileNameSize);
314 Temp[FileNameSize - 1] = 0;
315 TotalSize += FileNameSize;
316
317 //
318 // Copy Ascii Description include NULL terminator.
319 //
320 Temp = CopyMem (Temp + FileNameSize, Description, DescriptionSize);
321 Temp[DescriptionSize - 1] = 0;
322 TotalSize += DescriptionSize;
323
324 REPORT_STATUS_CODE_EX (
325 (EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED),
326 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE),
327 0,
328 NULL,
329 NULL,
330 AssertData,
331 TotalSize
332 );
333
334 //
335 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
336 //
337 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
338 CpuBreakpoint ();
339 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
340 CpuDeadLoop ();
341 }
342 }
343
344
345 /**
346 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
347
348 This function fills Length bytes of Buffer with the value specified by
349 PcdDebugClearMemoryValue, and returns Buffer.
350
351 If Buffer is NULL, then ASSERT().
352 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
353
354 @param Buffer Pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
355 @param Length Number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
356
357 @return Buffer Pointer to the target buffer filled with PcdDebugClearMemoryValue.
358
359 **/
360 VOID *
361 EFIAPI
362 DebugClearMemory (
363 OUT VOID *Buffer,
364 IN UINTN Length
365 )
366 {
367 ASSERT (Buffer != NULL);
368
369 return SetMem (Buffer, Length, PcdGet8 (PcdDebugClearMemoryValue));
370 }
371
372
373 /**
374 Returns TRUE if ASSERT() macros are enabled.
375
376 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
377 PcdDebugProperyMask is set. Otherwise FALSE is returned.
378
379 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
380 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
381
382 **/
383 BOOLEAN
384 EFIAPI
385 DebugAssertEnabled (
386 VOID
387 )
388 {
389 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
390 }
391
392
393 /**
394 Returns TRUE if DEBUG() macros are enabled.
395
396 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
397 PcdDebugProperyMask is set. Otherwise FALSE is returned.
398
399 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
400 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
401
402 **/
403 BOOLEAN
404 EFIAPI
405 DebugPrintEnabled (
406 VOID
407 )
408 {
409 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
410 }
411
412
413 /**
414 Returns TRUE if DEBUG_CODE() macros are enabled.
415
416 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
417 PcdDebugProperyMask is set. Otherwise FALSE is returned.
418
419 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
420 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
421
422 **/
423 BOOLEAN
424 EFIAPI
425 DebugCodeEnabled (
426 VOID
427 )
428 {
429 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
430 }
431
432
433 /**
434 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
435
436 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
437 PcdDebugProperyMask is set. Otherwise FALSE is returned.
438
439 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
440 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
441
442 **/
443 BOOLEAN
444 EFIAPI
445 DebugClearMemoryEnabled (
446 VOID
447 )
448 {
449 return (BOOLEAN) ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
450 }
451
452 /**
453 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
454
455 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
456
457 @retval TRUE Current ErrorLevel is supported.
458 @retval FALSE Current ErrorLevel is not supported.
459
460 **/
461 BOOLEAN
462 EFIAPI
463 DebugPrintLevelEnabled (
464 IN CONST UINTN ErrorLevel
465 )
466 {
467 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
468 }