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