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