]> git.proxmox.com Git - mirror_edk2.git/blob - UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.c
0daea00728430bdb75826914bdf406103963fc8e
[mirror_edk2.git] / UnitTestFrameworkPkg / Library / Posix / DebugLibPosix / DebugLibPosix.c
1 /** @file
2 Instance of Debug Library based on POSIX APIs
3
4 Uses Print Library to produce formatted output strings sent to printf().
5
6 Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 **/
9
10 #include <stdio.h>
11
12 #include <Base.h>
13 #include <Library/DebugLib.h>
14 #include <Library/BaseLib.h>
15 #include <Library/PrintLib.h>
16 #include <Library/BaseMemoryLib.h>
17
18 ///
19 /// Define the maximum debug and assert message length that this library supports
20 ///
21 #define MAX_DEBUG_MESSAGE_LENGTH 0x100
22
23 /**
24 Prints a debug message to the debug output device if the specified error level is enabled.
25
26 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
27 GetDebugPrintErrorLevel (), then print the message specified by Format and the
28 associated variable argument list to the debug output device.
29
30 If Format is NULL, then ASSERT().
31
32 @param ErrorLevel The error level of the debug message.
33 @param Format The format string for the debug message to print.
34 @param ... The variable argument list whose contents are accessed
35 based on the format string specified by Format.
36
37 **/
38 VOID
39 EFIAPI
40 DebugPrint (
41 IN UINTN ErrorLevel,
42 IN CONST CHAR8 *Format,
43 ...
44 )
45 {
46 VA_LIST Marker;
47
48 VA_START (Marker, Format);
49 DebugVPrint (ErrorLevel, Format, Marker);
50 VA_END (Marker);
51 }
52
53 /**
54 Prints a debug message to the debug output device if the specified
55 error level is enabled.
56
57 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
58 GetDebugPrintErrorLevel (), then print the message specified by Format and
59 the associated variable argument list to the debug output device.
60
61 If Format is NULL, then ASSERT().
62
63 @param ErrorLevel The error level of the debug message.
64 @param Format Format string for the debug message to print.
65 @param VaListMarker VA_LIST marker for the variable argument list.
66
67 **/
68 VOID
69 EFIAPI
70 DebugVPrint (
71 IN UINTN ErrorLevel,
72 IN CONST CHAR8 *Format,
73 IN VA_LIST VaListMarker
74 )
75 {
76 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
77
78 AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);
79 printf ("%s", Buffer);
80 }
81
82 /**
83 Prints a debug message to the debug output device if the specified
84 error level is enabled.
85 This function use BASE_LIST which would provide a more compatible
86 service than VA_LIST.
87
88 If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
89 GetDebugPrintErrorLevel (), then print the message specified by Format and
90 the associated variable argument list to the debug output device.
91
92 If Format is NULL, then ASSERT().
93
94 @param ErrorLevel The error level of the debug message.
95 @param Format Format string for the debug message to print.
96 @param BaseListMarker BASE_LIST marker for the variable argument list.
97
98 **/
99 VOID
100 EFIAPI
101 DebugBPrint (
102 IN UINTN ErrorLevel,
103 IN CONST CHAR8 *Format,
104 IN BASE_LIST BaseListMarker
105 )
106 {
107 CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];
108
109 AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
110 printf ("%s", Buffer);
111 }
112
113 /**
114 Prints an assert message containing a filename, line number, and description.
115 This may be followed by a breakpoint or a dead loop.
116
117 Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"
118 to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of
119 PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if
120 DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then
121 CpuDeadLoop() is called. If neither of these bits are set, then this function
122 returns immediately after the message is printed to the debug output device.
123 DebugAssert() must actively prevent recursion. If DebugAssert() is called while
124 processing another DebugAssert(), then DebugAssert() must return immediately.
125
126 If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.
127 If Description is NULL, then a <Description> string of "(NULL) Description" is printed.
128
129 @param FileName The pointer to the name of the source file that generated the assert condition.
130 @param LineNumber The line number in the source file that generated the assert condition
131 @param Description The pointer to the description of the assert condition.
132
133 **/
134 VOID
135 EFIAPI
136 DebugAssert (
137 IN CONST CHAR8 *FileName,
138 IN UINTN LineNumber,
139 IN CONST CHAR8 *Description
140 )
141 {
142 printf ("ASSERT: %s(%d): %s\n", FileName, (INT32)(UINT32)LineNumber, Description);
143
144 //
145 // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings
146 //
147 if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) {
148 CpuBreakpoint ();
149 } else if ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) {
150 CpuDeadLoop ();
151 }
152 }
153
154 /**
155 Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.
156
157 This function fills Length bytes of Buffer with the value specified by
158 PcdDebugClearMemoryValue, and returns Buffer.
159
160 If Buffer is NULL, then ASSERT().
161 If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
162
163 @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.
164 @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.
165
166 @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.
167
168 **/
169 VOID *
170 EFIAPI
171 DebugClearMemory (
172 OUT VOID *Buffer,
173 IN UINTN Length
174 )
175 {
176 //
177 // If Buffer is NULL, then ASSERT().
178 //
179 ASSERT (Buffer != NULL);
180
181 //
182 // SetMem() checks for the the ASSERT() condition on Length and returns Buffer
183 //
184 return SetMem (Buffer, Length, PcdGet8(PcdDebugClearMemoryValue));
185 }
186
187 /**
188 Returns TRUE if ASSERT() macros are enabled.
189
190 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of
191 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
192
193 @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.
194 @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.
195
196 **/
197 BOOLEAN
198 EFIAPI
199 DebugAssertEnabled (
200 VOID
201 )
202 {
203 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);
204 }
205
206 /**
207 Returns TRUE if DEBUG() macros are enabled.
208
209 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of
210 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
211
212 @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.
213 @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.
214
215 **/
216 BOOLEAN
217 EFIAPI
218 DebugPrintEnabled (
219 VOID
220 )
221 {
222 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);
223 }
224
225 /**
226 Returns TRUE if DEBUG_CODE() macros are enabled.
227
228 This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of
229 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
230
231 @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.
232 @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.
233
234 **/
235 BOOLEAN
236 EFIAPI
237 DebugCodeEnabled (
238 VOID
239 )
240 {
241 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);
242 }
243
244 /**
245 Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.
246
247 This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of
248 PcdDebugPropertyMask is set. Otherwise FALSE is returned.
249
250 @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.
251 @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.
252
253 **/
254 BOOLEAN
255 EFIAPI
256 DebugClearMemoryEnabled (
257 VOID
258 )
259 {
260 return (BOOLEAN) ((PcdGet8(PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);
261 }
262
263 /**
264 Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.
265
266 This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.
267
268 @retval TRUE Current ErrorLevel is supported.
269 @retval FALSE Current ErrorLevel is not supported.
270
271 **/
272 BOOLEAN
273 EFIAPI
274 DebugPrintLevelEnabled (
275 IN CONST UINTN ErrorLevel
276 )
277 {
278 return (BOOLEAN) ((ErrorLevel & PcdGet32(PcdFixedDebugPrintErrorLevel)) != 0);
279 }