DebugLibPosix.c 8.5 KB

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