]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | \r | |
3 | Copyright (c) 2014 - 2019, Intel Corporation. All rights reserved.<BR>\r | |
4 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
5 | \r | |
6 | **/\r | |
7 | \r | |
8 | #include <Base.h>\r | |
9 | #include <Library/DebugLib.h>\r | |
10 | #include <Library/BaseLib.h>\r | |
11 | #include <Library/PrintLib.h>\r | |
12 | #include <Library/PcdLib.h>\r | |
13 | #include <Library/BaseMemoryLib.h>\r | |
14 | #include <Library/SerialPortLib.h>\r | |
15 | #include <Library/DebugDeviceLib.h>\r | |
16 | #include <Library/DebugPrintErrorLevelLib.h>\r | |
17 | \r | |
18 | //\r | |
19 | // Define the maximum debug and assert message length that this library supports\r | |
20 | //\r | |
21 | #define MAX_DEBUG_MESSAGE_LENGTH 0x100\r | |
22 | \r | |
23 | CONST CHAR8 *mHexTable = "0123456789ABCDEF";\r | |
24 | \r | |
25 | //\r | |
26 | // VA_LIST can not initialize to NULL for all compiler, so we use this to\r | |
27 | // indicate a null VA_LIST\r | |
28 | //\r | |
29 | VA_LIST mVaListNull;\r | |
30 | \r | |
31 | /**\r | |
32 | Get stack frame pointer of function call.\r | |
33 | \r | |
34 | @return StackFramePointer stack frame pointer of function call.\r | |
35 | **/\r | |
36 | UINTN *\r | |
37 | EFIAPI\r | |
38 | GetStackFramePointer (\r | |
39 | VOID\r | |
40 | );\r | |
41 | \r | |
42 | /**\r | |
43 | Prints a debug message to the debug output device if the specified error level is enabled.\r | |
44 | \r | |
45 | If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r | |
46 | GetDebugPrintErrorLevel (), then print the message specified by Format and the\r | |
47 | associated variable argument list to the debug output device.\r | |
48 | \r | |
49 | If Format is NULL, then ASSERT().\r | |
50 | \r | |
51 | @param ErrorLevel The error level of the debug message.\r | |
52 | @param Format Format string for the debug message to print.\r | |
53 | @param ... Variable argument list whose contents are accessed\r | |
54 | based on the format string specified by Format.\r | |
55 | \r | |
56 | **/\r | |
57 | VOID\r | |
58 | EFIAPI\r | |
59 | DebugPrint (\r | |
60 | IN UINTN ErrorLevel,\r | |
61 | IN CONST CHAR8 *Format,\r | |
62 | ...\r | |
63 | )\r | |
64 | {\r | |
65 | VA_LIST Marker;\r | |
66 | \r | |
67 | VA_START (Marker, Format);\r | |
68 | DebugVPrint (ErrorLevel, Format, Marker);\r | |
69 | VA_END (Marker);\r | |
70 | }\r | |
71 | \r | |
72 | /**\r | |
73 | Prints a debug message to the debug output device if the specified\r | |
74 | error level is enabled base on Null-terminated format string and a\r | |
75 | VA_LIST argument list or a BASE_LIST argument list.\r | |
76 | \r | |
77 | If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r | |
78 | GetDebugPrintErrorLevel (), then print the message specified by Format and\r | |
79 | the associated variable argument list to the debug output device.\r | |
80 | \r | |
81 | If Format is NULL, then ASSERT().\r | |
82 | \r | |
83 | @param ErrorLevel The error level of the debug message.\r | |
84 | @param Format Format string for the debug message to print.\r | |
85 | @param VaListMarker VA_LIST marker for the variable argument list.\r | |
86 | @param BaseListMarker BASE_LIST marker for the variable argument list.\r | |
87 | \r | |
88 | **/\r | |
89 | VOID\r | |
90 | DebugPrintMarker (\r | |
91 | IN UINTN ErrorLevel,\r | |
92 | IN CONST CHAR8 *Format,\r | |
93 | IN VA_LIST VaListMarker,\r | |
94 | IN BASE_LIST BaseListMarker\r | |
95 | )\r | |
96 | {\r | |
97 | CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r | |
98 | \r | |
99 | //\r | |
100 | // If Format is NULL, then ASSERT().\r | |
101 | //\r | |
102 | if (!GetDebugPrintDeviceEnable ()) {\r | |
103 | return;\r | |
104 | }\r | |
105 | \r | |
106 | //\r | |
107 | // Check driver debug mask value and global mask\r | |
108 | //\r | |
109 | if ((ErrorLevel & GetDebugPrintErrorLevel ()) == 0) {\r | |
110 | return;\r | |
111 | }\r | |
112 | \r | |
113 | //\r | |
114 | // If Format is NULL, then ASSERT().\r | |
115 | //\r | |
116 | ASSERT (Format != NULL);\r | |
117 | \r | |
118 | //\r | |
119 | // Convert the DEBUG() message to an ASCII String\r | |
120 | //\r | |
121 | if (BaseListMarker == NULL) {\r | |
122 | AsciiVSPrint (Buffer, sizeof (Buffer), Format, VaListMarker);\r | |
123 | } else {\r | |
124 | AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);\r | |
125 | }\r | |
126 | \r | |
127 | //\r | |
128 | // Send the print string to a Serial Port\r | |
129 | //\r | |
130 | SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r | |
131 | }\r | |
132 | \r | |
133 | /**\r | |
134 | Prints a debug message to the debug output device if the specified\r | |
135 | error level is enabled.\r | |
136 | \r | |
137 | If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r | |
138 | GetDebugPrintErrorLevel (), then print the message specified by Format and\r | |
139 | the associated variable argument list to the debug output device.\r | |
140 | \r | |
141 | If Format is NULL, then ASSERT().\r | |
142 | \r | |
143 | @param ErrorLevel The error level of the debug message.\r | |
144 | @param Format Format string for the debug message to print.\r | |
145 | @param VaListMarker VA_LIST marker for the variable argument list.\r | |
146 | \r | |
147 | **/\r | |
148 | VOID\r | |
149 | EFIAPI\r | |
150 | DebugVPrint (\r | |
151 | IN UINTN ErrorLevel,\r | |
152 | IN CONST CHAR8 *Format,\r | |
153 | IN VA_LIST VaListMarker\r | |
154 | )\r | |
155 | {\r | |
156 | DebugPrintMarker (ErrorLevel, Format, VaListMarker, NULL);\r | |
157 | }\r | |
158 | \r | |
159 | /**\r | |
160 | Prints a debug message to the debug output device if the specified\r | |
161 | error level is enabled.\r | |
162 | This function use BASE_LIST which would provide a more compatible\r | |
163 | service than VA_LIST.\r | |
164 | \r | |
165 | If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function\r | |
166 | GetDebugPrintErrorLevel (), then print the message specified by Format and\r | |
167 | the associated variable argument list to the debug output device.\r | |
168 | \r | |
169 | If Format is NULL, then ASSERT().\r | |
170 | \r | |
171 | @param ErrorLevel The error level of the debug message.\r | |
172 | @param Format Format string for the debug message to print.\r | |
173 | @param BaseListMarker BASE_LIST marker for the variable argument list.\r | |
174 | \r | |
175 | **/\r | |
176 | VOID\r | |
177 | EFIAPI\r | |
178 | DebugBPrint (\r | |
179 | IN UINTN ErrorLevel,\r | |
180 | IN CONST CHAR8 *Format,\r | |
181 | IN BASE_LIST BaseListMarker\r | |
182 | )\r | |
183 | {\r | |
184 | DebugPrintMarker (ErrorLevel, Format, mVaListNull, BaseListMarker);\r | |
185 | }\r | |
186 | \r | |
187 | /**\r | |
188 | Convert an UINT32 value into HEX string specified by Buffer.\r | |
189 | \r | |
190 | @param Value The HEX value to convert to string\r | |
191 | @param Buffer The pointer to the target buffer to be filled with HEX string\r | |
192 | \r | |
193 | **/\r | |
194 | VOID\r | |
195 | FillHex (\r | |
196 | UINTN Value,\r | |
197 | CHAR8 *Buffer\r | |
198 | )\r | |
199 | {\r | |
200 | INTN Idx;\r | |
201 | \r | |
202 | for (Idx = (sizeof (UINTN) * 2) - 1; Idx >= 0; Idx--) {\r | |
203 | Buffer[Idx] = mHexTable[Value & 0x0F];\r | |
204 | Value >>= 4;\r | |
205 | }\r | |
206 | }\r | |
207 | \r | |
208 | /**\r | |
209 | Prints an assert message containing a filename, line number, and description.\r | |
210 | This may be followed by a breakpoint or a dead loop.\r | |
211 | \r | |
212 | Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r | |
213 | to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r | |
214 | PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r | |
215 | DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r | |
216 | CpuDeadLoop() is called. If neither of these bits are set, then this function\r | |
217 | returns immediately after the message is printed to the debug output device.\r | |
218 | DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r | |
219 | processing another DebugAssert(), then DebugAssert() must return immediately.\r | |
220 | \r | |
221 | If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r | |
222 | If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r | |
223 | \r | |
224 | **/\r | |
225 | VOID\r | |
226 | DebugAssertInternal (\r | |
227 | VOID\r | |
228 | )\r | |
229 | {\r | |
230 | CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r | |
231 | UINTN *Frame;\r | |
232 | \r | |
233 | Frame = (UINTN *)GetStackFramePointer ();\r | |
234 | \r | |
235 | //\r | |
236 | // Generate the ASSERT() message in Ascii format\r | |
237 | //\r | |
238 | if (sizeof (UINTN) == sizeof (UINT32)) {\r | |
239 | AsciiStrnCpyS (\r | |
240 | Buffer,\r | |
241 | sizeof (Buffer) / sizeof (CHAR8),\r | |
242 | "-> EBP:0x00000000 EIP:0x00000000\n",\r | |
243 | sizeof (Buffer) / sizeof (CHAR8) - 1\r | |
244 | );\r | |
245 | } else {\r | |
246 | AsciiStrnCpyS (\r | |
247 | Buffer,\r | |
248 | sizeof (Buffer) / sizeof (CHAR8),\r | |
249 | "-> RBP:0x0000000000000000 RIP:0x0000000000000000\n",\r | |
250 | sizeof (Buffer) / sizeof (CHAR8) - 1\r | |
251 | );\r | |
252 | }\r | |
253 | \r | |
254 | SerialPortWrite ((UINT8 *)"ASSERT DUMP:\n", 13);\r | |
255 | while (Frame != NULL) {\r | |
256 | FillHex ((UINTN)Frame, Buffer + 9);\r | |
257 | FillHex (Frame[1], Buffer + 9 + (sizeof (UINTN) * 2) + 8);\r | |
258 | SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r | |
259 | if ((Frame[0] > (UINTN)Frame) && (Frame[0] < (UINTN)Frame + 0x00100000)) {\r | |
260 | Frame = (UINTN *)Frame[0];\r | |
261 | } else {\r | |
262 | Frame = NULL;\r | |
263 | }\r | |
264 | }\r | |
265 | \r | |
266 | //\r | |
267 | // Dead loop\r | |
268 | //\r | |
269 | CpuDeadLoop ();\r | |
270 | }\r | |
271 | \r | |
272 | /**\r | |
273 | Prints an assert message containing a filename, line number, and description.\r | |
274 | This may be followed by a breakpoint or a dead loop.\r | |
275 | \r | |
276 | Print a message of the form "ASSERT <FileName>(<LineNumber>): <Description>\n"\r | |
277 | to the debug output device. If DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED bit of\r | |
278 | PcdDebugPropertyMask is set then CpuBreakpoint() is called. Otherwise, if\r | |
279 | DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED bit of PcdDebugPropertyMask is set then\r | |
280 | CpuDeadLoop() is called. If neither of these bits are set, then this function\r | |
281 | returns immediately after the message is printed to the debug output device.\r | |
282 | DebugAssert() must actively prevent recursion. If DebugAssert() is called while\r | |
283 | processing another DebugAssert(), then DebugAssert() must return immediately.\r | |
284 | \r | |
285 | If FileName is NULL, then a <FileName> string of "(NULL) Filename" is printed.\r | |
286 | If Description is NULL, then a <Description> string of "(NULL) Description" is printed.\r | |
287 | \r | |
288 | @param FileName The pointer to the name of the source file that generated the assert condition.\r | |
289 | @param LineNumber The line number in the source file that generated the assert condition\r | |
290 | @param Description The pointer to the description of the assert condition.\r | |
291 | \r | |
292 | **/\r | |
293 | VOID\r | |
294 | EFIAPI\r | |
295 | DebugAssert (\r | |
296 | IN CONST CHAR8 *FileName,\r | |
297 | IN UINTN LineNumber,\r | |
298 | IN CONST CHAR8 *Description\r | |
299 | )\r | |
300 | {\r | |
301 | DebugAssertInternal ();\r | |
302 | }\r | |
303 | \r | |
304 | /**\r | |
305 | Fills a target buffer with PcdDebugClearMemoryValue, and returns the target buffer.\r | |
306 | \r | |
307 | This function fills Length bytes of Buffer with the value specified by\r | |
308 | PcdDebugClearMemoryValue, and returns Buffer.\r | |
309 | \r | |
310 | If Buffer is NULL, then ASSERT().\r | |
311 | If Length is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r | |
312 | \r | |
313 | @param Buffer The pointer to the target buffer to be filled with PcdDebugClearMemoryValue.\r | |
314 | @param Length The number of bytes in Buffer to fill with zeros PcdDebugClearMemoryValue.\r | |
315 | \r | |
316 | @return Buffer The pointer to the target buffer filled with PcdDebugClearMemoryValue.\r | |
317 | \r | |
318 | **/\r | |
319 | VOID *\r | |
320 | EFIAPI\r | |
321 | DebugClearMemory (\r | |
322 | OUT VOID *Buffer,\r | |
323 | IN UINTN Length\r | |
324 | )\r | |
325 | {\r | |
326 | return Buffer;\r | |
327 | }\r | |
328 | \r | |
329 | /**\r | |
330 | Returns TRUE if ASSERT() macros are enabled.\r | |
331 | \r | |
332 | This function returns TRUE if the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of\r | |
333 | PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r | |
334 | \r | |
335 | @retval TRUE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is set.\r | |
336 | @retval FALSE The DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit of PcdDebugPropertyMask is clear.\r | |
337 | \r | |
338 | **/\r | |
339 | BOOLEAN\r | |
340 | EFIAPI\r | |
341 | DebugAssertEnabled (\r | |
342 | VOID\r | |
343 | )\r | |
344 | {\r | |
345 | return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED) != 0);\r | |
346 | }\r | |
347 | \r | |
348 | /**\r | |
349 | Returns TRUE if DEBUG() macros are enabled.\r | |
350 | \r | |
351 | This function returns TRUE if the DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of\r | |
352 | PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r | |
353 | \r | |
354 | @retval TRUE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is set.\r | |
355 | @retval FALSE The DEBUG_PROPERTY_DEBUG_PRINT_ENABLED bit of PcdDebugPropertyMask is clear.\r | |
356 | \r | |
357 | **/\r | |
358 | BOOLEAN\r | |
359 | EFIAPI\r | |
360 | DebugPrintEnabled (\r | |
361 | VOID\r | |
362 | )\r | |
363 | {\r | |
364 | return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_PRINT_ENABLED) != 0);\r | |
365 | }\r | |
366 | \r | |
367 | /**\r | |
368 | Returns TRUE if DEBUG_CODE() macros are enabled.\r | |
369 | \r | |
370 | This function returns TRUE if the DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of\r | |
371 | PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r | |
372 | \r | |
373 | @retval TRUE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is set.\r | |
374 | @retval FALSE The DEBUG_PROPERTY_DEBUG_CODE_ENABLED bit of PcdDebugPropertyMask is clear.\r | |
375 | \r | |
376 | **/\r | |
377 | BOOLEAN\r | |
378 | EFIAPI\r | |
379 | DebugCodeEnabled (\r | |
380 | VOID\r | |
381 | )\r | |
382 | {\r | |
383 | return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_DEBUG_CODE_ENABLED) != 0);\r | |
384 | }\r | |
385 | \r | |
386 | /**\r | |
387 | Returns TRUE if DEBUG_CLEAR_MEMORY() macro is enabled.\r | |
388 | \r | |
389 | This function returns TRUE if the DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of\r | |
390 | PcdDebugPropertyMask is set. Otherwise FALSE is returned.\r | |
391 | \r | |
392 | @retval TRUE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is set.\r | |
393 | @retval FALSE The DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED bit of PcdDebugPropertyMask is clear.\r | |
394 | \r | |
395 | **/\r | |
396 | BOOLEAN\r | |
397 | EFIAPI\r | |
398 | DebugClearMemoryEnabled (\r | |
399 | VOID\r | |
400 | )\r | |
401 | {\r | |
402 | return (BOOLEAN)((PcdGet8 (PcdDebugPropertyMask) & DEBUG_PROPERTY_CLEAR_MEMORY_ENABLED) != 0);\r | |
403 | }\r | |
404 | \r | |
405 | /**\r | |
406 | Returns TRUE if any one of the bit is set both in ErrorLevel and PcdFixedDebugPrintErrorLevel.\r | |
407 | \r | |
408 | This function compares the bit mask of ErrorLevel and PcdFixedDebugPrintErrorLevel.\r | |
409 | \r | |
410 | @retval TRUE Current ErrorLevel is supported.\r | |
411 | @retval FALSE Current ErrorLevel is not supported.\r | |
412 | \r | |
413 | **/\r | |
414 | BOOLEAN\r | |
415 | EFIAPI\r | |
416 | DebugPrintLevelEnabled (\r | |
417 | IN CONST UINTN ErrorLevel\r | |
418 | )\r | |
419 | {\r | |
420 | return (BOOLEAN)((ErrorLevel & PcdGet32 (PcdFixedDebugPrintErrorLevel)) != 0);\r | |
421 | }\r |