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