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