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