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