]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/UefiDebugLibStdErr/DebugLib.c
MdePkg/UefiDebugLibStdErr: Pass the correct buffer size
[mirror_edk2.git] / MdePkg / Library / UefiDebugLibStdErr / DebugLib.c
... / ...
CommitLineData
1/** @file\r
2 UEFI Debug Lib that sends messages to the Standard Error Device in the EFI System Table.\r
3\r
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
5 SPDX-License-Identifier: BSD-2-Clause-Patent\r
6\r
7**/\r
8\r
9\r
10#include <Uefi.h>\r
11\r
12#include <Library/DebugLib.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
17#include <Library/DebugPrintErrorLevelLib.h>\r
18\r
19//\r
20// Define the maximum debug and assert message length that this library supports\r
21//\r
22#define MAX_DEBUG_MESSAGE_LENGTH 0x100\r
23\r
24\r
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
31extern BOOLEAN mPostEBS;\r
32extern EFI_SYSTEM_TABLE *mDebugST;\r
33\r
34/**\r
35 Prints a debug message to the debug output device if the specified error level is enabled.\r
36\r
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
39 associated variable argument list to the debug output device.\r
40\r
41 If Format is NULL, then ASSERT().\r
42\r
43 @param ErrorLevel The error level of the debug message.\r
44 @param Format The format string for the debug message to print.\r
45 @param ... The variable argument list whose contents are accessed\r
46 based on the format string specified by Format.\r
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
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
89{\r
90 CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r
91\r
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
109 UnicodeVSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, VaListMarker);\r
110 } else {\r
111 UnicodeBSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, BaseListMarker);\r
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
120 }\r
121}\r
122\r
123\r
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
180/**\r
181 Prints an assert message containing a filename, line number, and description.\r
182 This may be followed by a breakpoint or a dead loop.\r
183\r
184 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r
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
189 returns immediately after the message is printed to the debug output device.\r
190 DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r
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
194 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r
195\r
196 @param FileName The pointer to the name of the source file that generated\r
197 the assert condition.\r
198 @param LineNumber The line number in the source file that generated the\r
199 assert condition\r
200 @param Description The pointer to the description of the assert condition.\r
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
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
242 }\r
243}\r
244\r
245\r
246/**\r
247 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r
248\r
249 This function fills Length bytes of Buffer with the value specified by\r
250 PcdDebugClearMemoryValue, and returns Buffer.\r
251\r
252 If Buffer is NULL, then ASSERT().\r
253 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
254\r
255 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r
256 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r
257\r
258 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r
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
281 Returns TRUE if ASSERT() macros are enabled.\r
282\r
283 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r
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
300/**\r
301 Returns TRUE if DEBUG() macros are enabled.\r
302\r
303 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r
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
320/**\r
321 Returns TRUE if DEBUG_CODE() macros are enabled.\r
322\r
323 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r
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
340/**\r
341 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r
342\r
343 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r
344 PcdDebugProperyMask is set. Otherwise FALSE is returned.\r
345\r
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
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
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