DebugLib.c 11 KB

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