DebugLib.c 12 KB

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