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