]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseDebugLibSerialPort / DebugLib.c
1 /** @file
2 Base Debug library instance base on Serial Port library.
3 It uses PrintLib to send debug messages to serial port device.
4
5 NOTE: If the Serial Port library enables hardware flow control, then a call
6 to DebugPrint() or DebugAssert() may hang if writes to the serial port are
7 being blocked. This may occur if a key(s) are pressed in a terminal emulator
8 used to monitor the DEBUG() and ASSERT() messages.
9
10 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
11 SPDX-License-Identifier: BSD-2-Clause-Patent
12
13 **/
14
15 #include <Base.h>
16 #include <Library/DebugLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/PrintLib.h>
19 #include <Library/PcdLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/SerialPortLib.h>
22 #include <Library/DebugPrintErrorLevelLib.h>
23
24 //
25 // Define the maximum debug and assert message length that this library supports
26 //
27 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
28
29 //
30 // VA_LIST can not initialize to NULL for all compiler, so we use this to
31 // indicate a null VA_LIST
32 //
33 VA_LIST mVaListNull;
34
35 /**
36 The constructor function initialize the Serial Port Library
37
38 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
39
40 **/
41 RETURN_STATUS
42 EFIAPI
43 BaseDebugLibSerialPortConstructor (
44 VOID
45 )
46 {
47 return SerialPortInitialize ();
48 }
49
50 /**
51 Prints a debug message to the debug output device if the specified error level is enabled.
52
53 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
54 GetDebugPrintErrorLevel (), then print the message specified by Format and the
55 associated variable argument list to the debug output device.
56
57 If Format is NULL, then ASSERT().
58
59 @param ErrorLevel The error level of the debug message.
60 @param Format Format string for the debug message to print.
61 @param ... Variable argument list whose contents are accessed
62 based on the format string specified by Format.
63
64 **/
65 VOID
66 EFIAPI
67 DebugPrint (
68 IN UINTN ErrorLevel,
69 IN CONST CHAR8 *Format,
70 ...
71 )
72 {
73 VA_LIST Marker;
74
75 VA_START (Marker, Format);
76 DebugVPrint (ErrorLevel, Format, Marker);
77 VA_END (Marker);
78 }
79
80 /**
81 Prints a debug message to the debug output device if the specified
82 error level is enabled base on Null-terminated format string and a
83 VA_LIST argument list or a BASE_LIST argument list.
84
85 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
86 GetDebugPrintErrorLevel (), then print the message specified by Format and
87 the associated variable argument list to the debug output device.
88
89 If Format is NULL, then ASSERT().
90
91 @param ErrorLevel The error level of the debug message.
92 @param Format Format string for the debug message to print.
93 @param VaListMarker VA_LIST marker for the variable argument list.
94 @param BaseListMarker BASE_LIST marker for the variable argument list.
95
96 **/
97 VOID
98 DebugPrintMarker (
99 IN UINTN ErrorLevel,
100 IN CONST CHAR8 *Format,
101 IN VA_LIST VaListMarker,
102 IN BASE_LIST BaseListMarker
103 )
104 {
105 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
106
107 //
108 // If Format is NULL, then ASSERT().
109 //
110 ASSERT (Format != NULL);
111
112 //
113 // Check driver debug mask value and global mask
114 //
115 if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {
116 return;
117 }
118
119 //
120 // Convert the DEBUG() message to an ASCII String
121 //
122 if (BaseListMarker == NULL) {
123 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
124 } else {
125 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
126 }
127
128 //
129 // Send the print string to a Serial Port
130 //
131 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
132 }
133
134 /**
135 Prints a debug message to the debug output device if the specified
136 error level is enabled.
137
138 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
139 GetDebugPrintErrorLevel (), then print the message specified by Format and
140 the associated variable argument list to the debug output device.
141
142 If Format is NULL, then ASSERT().
143
144 @param ErrorLevel The error level of the debug message.
145 @param Format Format string for the debug message to print.
146 @param VaListMarker VA_LIST marker for the variable argument list.
147
148 **/
149 VOID
150 EFIAPI
151 DebugVPrint (
152 IN UINTN ErrorLevel,
153 IN CONST CHAR8 *Format,
154 IN VA_LIST VaListMarker
155 )
156 {
157 DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);
158 }
159
160 /**
161 Prints a debug message to the debug output device if the specified
162 error level is enabled.
163 This function use BASE_LIST which would provide a more compatible
164 service than VA_LIST.
165
166 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
167 GetDebugPrintErrorLevel (), then print the message specified by Format and
168 the associated variable argument list to the debug output device.
169
170 If Format is NULL, then ASSERT().
171
172 @param ErrorLevel The error level of the debug message.
173 @param Format Format string for the debug message to print.
174 @param BaseListMarker BASE_LIST marker for the variable argument list.
175
176 **/
177 VOID
178 EFIAPI
179 DebugBPrint (
180 IN UINTN ErrorLevel,
181 IN CONST CHAR8 *Format,
182 IN BASE_LIST BaseListMarker
183 )
184 {
185 DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);
186 }
187
188 /**
189 Prints an assert message containing a filename, line number, and description.
190 This may be followed by a breakpoint or a dead loop.
191
192 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
193 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
194 PcdDebugProperyMask is set then CpuBreakpoint() is called. Otherwise, if
195 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugProperyMask is set then
196 CpuDeadLoop() is called. If neither of these bits are set, then this function
197 returns immediately after the message is printed to the debug output device.
198 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
199 processing another DebugAssert(), then DebugAssert() must return immediately.
200
201 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
202 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
203
204 @param FileName The pointer to the name of the source file that generated the assert condition.
205 @param LineNumber The line number in the source file that generated the assert condition
206 @param Description The pointer to the description of the assert condition.
207
208 **/
209 VOID
210 EFIAPI
211 DebugAssert (
212 IN CONST CHAR8 *FileName,
213 IN UINTN LineNumber,
214 IN CONST CHAR8 *Description
215 )
216 {
217 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
218
219 //
220 // Generate the ASSERT() message in Ascii format
221 //
222 AsciiSPrint (Buffer, sizeof (Buffer), "ASSERT [%a] %a(%d): %a\n", gEfiCallerBaseName, FileName, LineNumber, Description);
223
224 //
225 // Send the print string to the Console Output device
226 //
227 SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));
228
229 //
230 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
231 //
232 if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
233 CpuBreakpoint ();
234 } else if ((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
235 CpuDeadLoop ();
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 Returns TRUE if ASSERT() macros are enabled.
274
275 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
276 PcdDebugProperyMask is set. Otherwise FALSE is returned.
277
278 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is set.
279 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugProperyMask is clear.
280
281 **/
282 BOOLEAN
283 EFIAPI
284 DebugAssertEnabled (
285 VOID
286 )
287 {
288 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
289 }
290
291 /**
292 Returns TRUE if DEBUG() macros are enabled.
293
294 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
295 PcdDebugProperyMask is set. Otherwise FALSE is returned.
296
297 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is set.
298 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugProperyMask is clear.
299
300 **/
301 BOOLEAN
302 EFIAPI
303 DebugPrintEnabled (
304 VOID
305 )
306 {
307 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
308 }
309
310 /**
311 Returns TRUE if DEBUG_CODE() macros are enabled.
312
313 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
314 PcdDebugProperyMask is set. Otherwise FALSE is returned.
315
316 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is set.
317 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugProperyMask is clear.
318
319 **/
320 BOOLEAN
321 EFIAPI
322 DebugCodeEnabled (
323 VOID
324 )
325 {
326 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
327 }
328
329 /**
330 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
331
332 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
333 PcdDebugProperyMask is set. Otherwise FALSE is returned.
334
335 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is set.
336 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugProperyMask is clear.
337
338 **/
339 BOOLEAN
340 EFIAPI
341 DebugClearMemoryEnabled (
342 VOID
343 )
344 {
345 return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
346 }
347
348 /**
349 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
350
351 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
352
353 @retval TRUE Current ErrorLevel is supported.
354 @retval FALSE Current ErrorLevel is not supported.
355
356 **/
357 BOOLEAN
358 EFIAPI
359 DebugPrintLevelEnabled (
360 IN CONST UINTN ErrorLevel
361 )
362 {
363 return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);
364 }