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