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