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