]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiDebugLibConOut/DebugLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / UefiDebugLibConOut / DebugLib.c
CommitLineData
e386b444 1/** @file\r
eceb3a4c 2 UEFI Debug Library that sends messages to the Console Output Device in the EFI System Table.\r
e386b444 3\r
ec81dba5 4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9095d37b
LG
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
e386b444 9\r
9095d37b
LG
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
e386b444 12\r
13**/\r
14\r
c7d265a9 15#include <Uefi.h>\r
c892d846 16\r
c7d265a9 17#include <Library/DebugLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/PrintLib.h>\r
20#include <Library/PcdLib.h>\r
21#include <Library/BaseLib.h>\r
22#include <Library/BaseMemoryLib.h>\r
2891fc8b 23#include <Library/DebugPrintErrorLevelLib.h>\r
c7d265a9 24\r
e386b444 25//\r
9095d37b 26// Define the maximum debug and assert message length that this library supports\r
e386b444 27//\r
28#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
29\r
ec81dba5
BB
30//\r
31// VA_LIST can not initialize to NULL for all compiler, so we use this to\r
32// indicate a null VA_LIST\r
33//\r
34VA_LIST mVaListNull;\r
35\r
e386b444 36/**\r
e386b444 37 Prints a debug message to the debug output device if the specified error level is enabled.\r
38\r
9095d37b
LG
39 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
40 GetDebugPrintErrorLevel (), then print the message specified by Format and the\r
2891fc8b 41 associated variable argument list to the debug output device.\r
e386b444 42\r
43 If Format is NULL, then ASSERT().\r
44\r
45 @param ErrorLevel The error level of the debug message.\r
46 @param Format Format string for the debug message to print.\r
9095d37b 47 @param ... A variable argument list whose contents are accessed\r
285010e7 48 based on the format string specified by Format.\r
e386b444 49\r
50**/\r
51VOID\r
52EFIAPI\r
53DebugPrint (\r
54 IN UINTN ErrorLevel,\r
55 IN CONST CHAR8 *Format,\r
56 ...\r
57 )\r
58{\r
e386b444 59 VA_LIST Marker;\r
60\r
ec81dba5
BB
61 VA_START (Marker, Format);\r
62 DebugVPrint (ErrorLevel, Format, Marker);\r
63 VA_END (Marker);\r
64}\r
65\r
66\r
67/**\r
68 Prints a debug message to the debug output device if the specified\r
69 error level is enabled base on Null-terminated format string and a\r
70 VA_LIST argument list or a BASE_LIST argument list.\r
71\r
72 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
73 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
74 the associated variable argument list to the debug output device.\r
75\r
76 If Format is NULL, then ASSERT().\r
77\r
78 @param ErrorLevel The error level of the debug message.\r
79 @param Format Format string for the debug message to print.\r
80 @param VaListMarker VA_LIST marker for the variable argument list.\r
81 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
82\r
83**/\r
84VOID\r
85DebugPrintMarker (\r
86 IN UINTN ErrorLevel,\r
87 IN CONST CHAR8 *Format,\r
88 IN VA_LIST VaListMarker,\r
89 IN BASE_LIST BaseListMarker\r
90 )\r
91{\r
92 CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
93\r
e386b444 94 //\r
95 // If Format is NULL, then ASSERT().\r
96 //\r
97 ASSERT (Format != NULL);\r
98\r
99 //\r
100 // Check driver debug mask value and global mask\r
101 //\r
2891fc8b 102 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r
e386b444 103 return;\r
104 }\r
105\r
106 //\r
107 // Convert the DEBUG() message to a Unicode String\r
108 //\r
ec81dba5
BB
109 if (BaseListMarker == NULL) {\r
110 UnicodeVSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, VaListMarker);\r
111 } else {\r
112 UnicodeBSPrintAsciiFormat (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Format, BaseListMarker);\r
113 }\r
e386b444 114\r
457cfbae 115\r
e386b444 116 //\r
117 // Send the print string to the Console Output device\r
118 //\r
119 if ((gST != NULL) && (gST->ConOut != NULL)) {\r
120 gST->ConOut->OutputString (gST->ConOut, Buffer);\r
121 }\r
122}\r
123\r
124\r
ec81dba5
BB
125/**\r
126 Prints a debug message to the debug output device if the specified\r
127 error level is enabled.\r
128\r
129 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
130 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
131 the associated variable argument list to the debug output device.\r
132\r
133 If Format is NULL, then ASSERT().\r
134\r
135 @param ErrorLevel The error level of the debug message.\r
136 @param Format Format string for the debug message to print.\r
137 @param VaListMarker VA_LIST marker for the variable argument list.\r
138\r
139**/\r
140VOID\r
141EFIAPI\r
142DebugVPrint (\r
143 IN UINTN ErrorLevel,\r
144 IN CONST CHAR8 *Format,\r
145 IN VA_LIST VaListMarker\r
146 )\r
147{\r
148 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r
149}\r
150\r
151\r
152/**\r
153 Prints a debug message to the debug output device if the specified\r
154 error level is enabled.\r
155 This function use BASE_LIST which would provide a more compatible\r
156 service than VA_LIST.\r
157\r
158 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r
159 GetDebugPrintErrorLevel (), then print the message specified by Format and\r
160 the associated variable argument list to the debug output device.\r
161\r
162 If Format is NULL, then ASSERT().\r
163\r
164 @param ErrorLevel The error level of the debug message.\r
165 @param Format Format string for the debug message to print.\r
166 @param BaseListMarker BASE_LIST marker for the variable argument list.\r
167\r
168**/\r
169VOID\r
170EFIAPI\r
171DebugBPrint (\r
172 IN UINTN ErrorLevel,\r
173 IN CONST CHAR8 *Format,\r
174 IN BASE_LIST BaseListMarker\r
175 )\r
176{\r
177 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r
178}\r
179\r
180\r
e386b444 181/**\r
9095d37b 182 Prints an assert message containing a filename, line number, and description.\r
e386b444 183 This may be followed by a breakpoint or a dead loop.\r
184\r
3e5c3238 185 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
9095d37b
LG
186 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r
187 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if\r
188 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then\r
189 CpuDeadLoop() is called. If neither of these bits are set, then this function\r
e386b444 190 returns immediately after the message is printed to the debug output device.\r
3e5c3238 191 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
e386b444 192 processing another DebugAssert(), then DebugAssert() must return immediately.\r
193\r
194 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r
e386b444 195 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
196\r
9095d37b 197 @param FileName The pointer to the name of the source file that generated\r
58380e9c 198 the assert condition.\r
9095d37b 199 @param LineNumber The line number in the source file that generated the\r
58380e9c 200 assert condition\r
2fc59a00 201 @param Description The pointer to the description of the assert condition.\r
e386b444 202\r
203**/\r
204VOID\r
205EFIAPI\r
206DebugAssert (\r
207 IN CONST CHAR8 *FileName,\r
208 IN UINTN LineNumber,\r
209 IN CONST CHAR8 *Description\r
210 )\r
211{\r
212 CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
213\r
214 //\r
215 // Generate the ASSERT() message in Unicode format\r
216 //\r
7c6d32a5 217 UnicodeSPrintAsciiFormat (\r
9095d37b
LG
218 Buffer,\r
219 sizeof (Buffer),\r
aa0a5149
BA
220 "ASSERT [%a] %a(%d): %a\n",\r
221 gEfiCallerBaseName,\r
9095d37b
LG
222 FileName,\r
223 LineNumber,\r
7c6d32a5 224 Description\r
225 );\r
9095d37b 226\r
e386b444 227 //\r
228 // Send the print string to the Console Output device\r
229 //\r
230 if ((gST != NULL) && (gST->ConOut != NULL)) {\r
231 gST->ConOut->OutputString (gST->ConOut, Buffer);\r
232 }\r
233\r
234 //\r
235 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings\r
236 //\r
237 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {\r
238 CpuBreakpoint ();\r
239 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {\r
240 CpuDeadLoop ();\r
241 }\r
242}\r
243\r
244\r
245/**\r
e386b444 246 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
247\r
9095d37b 248 This function fills Length bytes of Buffer with the value specified by\r
e386b444 249 PcdDebugClearMemoryValue, and returns Buffer.\r
250\r
251 If Buffer is NULL, then ASSERT().\r
9095d37b 252 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
e386b444 253\r
2fc59a00 254 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
9095d37b 255 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
e386b444 256\r
2fc59a00 257 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
e386b444 258\r
259**/\r
260VOID *\r
261EFIAPI\r
262DebugClearMemory (\r
263 OUT VOID *Buffer,\r
264 IN UINTN Length\r
265 )\r
266{\r
267 //\r
268 // If Buffer is NULL, then ASSERT().\r
269 //\r
270 ASSERT (Buffer != NULL);\r
271\r
272 //\r
273 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer\r
274 //\r
275 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));\r
276}\r
277\r
278\r
279/**\r
e386b444 280 Returns TRUE if ASSERT() macros are enabled.\r
281\r
9095d37b 282 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
e386b444 283 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
284\r
285 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.\r
286 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.\r
287\r
288**/\r
289BOOLEAN\r
290EFIAPI\r
291DebugAssertEnabled (\r
292 VOID\r
293 )\r
294{\r
295 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r
296}\r
297\r
298\r
9095d37b 299/**\r
3e5c3238 300 Returns TRUE if DEBUG() macros are enabled.\r
e386b444 301\r
9095d37b 302 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
e386b444 303 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
304\r
305 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.\r
306 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.\r
307\r
308**/\r
309BOOLEAN\r
310EFIAPI\r
311DebugPrintEnabled (\r
312 VOID\r
313 )\r
314{\r
315 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r
316}\r
317\r
318\r
9095d37b 319/**\r
3e5c3238 320 Returns TRUE if DEBUG_CODE() macros are enabled.\r
e386b444 321\r
9095d37b 322 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
e386b444 323 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
324\r
325 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.\r
326 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.\r
327\r
328**/\r
329BOOLEAN\r
330EFIAPI\r
331DebugCodeEnabled (\r
332 VOID\r
333 )\r
334{\r
335 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r
336}\r
337\r
338\r
9095d37b 339/**\r
3e5c3238 340 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
e386b444 341\r
9095d37b 342 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
e386b444 343 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
344\r
eceb3a4c
LG
345 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.\r
346 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.\r
e386b444 347\r
348**/\r
349BOOLEAN\r
350EFIAPI\r
351DebugClearMemoryEnabled (\r
352 VOID\r
353 )\r
354{\r
355 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r
356}\r
dfbe4d27
LG
357\r
358/**\r
359 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
360\r
361 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r
362\r
363 @retval TRUE Current ErrorLevel is supported.\r
364 @retval FALSE Current ErrorLevel is not supported.\r
365\r
366**/\r
367BOOLEAN\r
368EFIAPI\r
369DebugPrintLevelEnabled (\r
370 IN CONST UINTN ErrorLevel\r
371 )\r
372{\r
373 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);\r
374}\r