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