logging.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* logging.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/logging.h
  23. */
  24. /* submitted by eof */
  25. #ifndef WOLFSSL_LOGGING_H
  26. #define WOLFSSL_LOGGING_H
  27. #include <wolfssl/wolfcrypt/types.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. enum wc_LogLevels {
  32. ERROR_LOG = 0,
  33. INFO_LOG,
  34. ENTER_LOG,
  35. LEAVE_LOG,
  36. OTHER_LOG
  37. };
  38. #ifdef WOLFSSL_FUNC_TIME
  39. /* WARNING: This code is only to be used for debugging performance.
  40. * The code is not thread-safe.
  41. * Do not use WOLFSSL_FUNC_TIME in production code.
  42. */
  43. enum wc_FuncNum {
  44. WC_FUNC_HELLO_REQUEST_SEND = 0,
  45. WC_FUNC_HELLO_REQUEST_DO,
  46. WC_FUNC_CLIENT_HELLO_SEND,
  47. WC_FUNC_CLIENT_HELLO_DO,
  48. WC_FUNC_SERVER_HELLO_SEND,
  49. WC_FUNC_SERVER_HELLO_DO,
  50. WC_FUNC_ENCRYPTED_EXTENSIONS_SEND,
  51. WC_FUNC_ENCRYPTED_EXTENSIONS_DO,
  52. WC_FUNC_CERTIFICATE_REQUEST_SEND,
  53. WC_FUNC_CERTIFICATE_REQUEST_DO,
  54. WC_FUNC_CERTIFICATE_SEND,
  55. WC_FUNC_CERTIFICATE_DO,
  56. WC_FUNC_CERTIFICATE_VERIFY_SEND,
  57. WC_FUNC_CERTIFICATE_VERIFY_DO,
  58. WC_FUNC_FINISHED_SEND,
  59. WC_FUNC_FINISHED_DO,
  60. WC_FUNC_KEY_UPDATE_SEND,
  61. WC_FUNC_KEY_UPDATE_DO,
  62. WC_FUNC_EARLY_DATA_SEND,
  63. WC_FUNC_EARLY_DATA_DO,
  64. WC_FUNC_NEW_SESSION_TICKET_SEND,
  65. WC_FUNC_NEW_SESSION_TICKET_DO,
  66. WC_FUNC_SERVER_HELLO_DONE_SEND,
  67. WC_FUNC_SERVER_HELLO_DONE_DO,
  68. WC_FUNC_TICKET_SEND,
  69. WC_FUNC_TICKET_DO,
  70. WC_FUNC_CLIENT_KEY_EXCHANGE_SEND,
  71. WC_FUNC_CLIENT_KEY_EXCHANGE_DO,
  72. WC_FUNC_CERTIFICATE_STATUS_SEND,
  73. WC_FUNC_CERTIFICATE_STATUS_DO,
  74. WC_FUNC_SERVER_KEY_EXCHANGE_SEND,
  75. WC_FUNC_SERVER_KEY_EXCHANGE_DO,
  76. WC_FUNC_END_OF_EARLY_DATA_SEND,
  77. WC_FUNC_END_OF_EARLY_DATA_DO,
  78. WC_FUNC_COUNT
  79. };
  80. #endif
  81. typedef void (*wolfSSL_Logging_cb)(const int logLevel,
  82. const char *const logMessage);
  83. WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function);
  84. WOLFSSL_API wolfSSL_Logging_cb wolfSSL_GetLoggingCb(void);
  85. /* turn logging on, only if compiled in */
  86. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  87. /* turn logging off */
  88. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  89. WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix);
  90. #ifdef HAVE_WC_INTROSPECTION
  91. WOLFSSL_API const char *wolfSSL_configure_args(void);
  92. WOLFSSL_API const char *wolfSSL_global_cflags(void);
  93. #endif
  94. #if (defined(OPENSSL_EXTRA) && !defined(_WIN32) && \
  95. !defined(NO_ERROR_QUEUE)) || defined(DEBUG_WOLFSSL_VERBOSE)
  96. #define WOLFSSL_HAVE_ERROR_QUEUE
  97. #endif
  98. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  99. WOLFSSL_LOCAL int wc_LoggingInit(void);
  100. WOLFSSL_LOCAL int wc_LoggingCleanup(void);
  101. WOLFSSL_LOCAL int wc_AddErrorNode(int error, int line, char* buf,
  102. char* file);
  103. WOLFSSL_LOCAL int wc_PeekErrorNode(int idx, const char **file,
  104. const char **reason, int *line);
  105. WOLFSSL_LOCAL void wc_RemoveErrorNode(int idx);
  106. WOLFSSL_LOCAL void wc_ClearErrorNodes(void);
  107. WOLFSSL_LOCAL int wc_PullErrorNode(const char **file, const char **reason,
  108. int *line);
  109. WOLFSSL_API int wc_SetLoggingHeap(void* h);
  110. WOLFSSL_API int wc_ERR_remove_state(void);
  111. WOLFSSL_LOCAL unsigned long wc_PeekErrorNodeLineData(
  112. const char **file, int *line, const char **data, int *flags,
  113. int (*ignore_err)(int err));
  114. WOLFSSL_LOCAL unsigned long wc_GetErrorNodeErr(void);
  115. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  116. WOLFSSL_API void wc_ERR_print_errors_fp(XFILE fp);
  117. WOLFSSL_API void wc_ERR_print_errors_cb(int (*cb)(const char *str,
  118. size_t len, void *u), void *u);
  119. #endif
  120. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  121. #ifdef WOLFSSL_FUNC_TIME
  122. /* WARNING: This code is only to be used for debugging performance.
  123. * The code is not thread-safe.
  124. * Do not use WOLFSSL_FUNC_TIME in production code.
  125. */
  126. WOLFSSL_API void WOLFSSL_START(int funcNum);
  127. WOLFSSL_API void WOLFSSL_END(int funcNum);
  128. WOLFSSL_API void WOLFSSL_TIME(int count);
  129. #else
  130. #define WOLFSSL_START(n) WC_DO_NOTHING
  131. #define WOLFSSL_END(n) WC_DO_NOTHING
  132. #define WOLFSSL_TIME(n) WC_DO_NOTHING
  133. #endif
  134. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_DEBUG_ERRORS_ONLY)
  135. #if defined(_WIN32)
  136. #if defined(INTIME_RTOS)
  137. #define __func__ NULL
  138. #else
  139. #define __func__ __FUNCTION__
  140. #endif
  141. #endif
  142. /* a is prepended to m and b is appended, creating a log msg a + m + b */
  143. #define WOLFSSL_LOG_CAT(a, m, b) #a " " m " " #b
  144. WOLFSSL_API void WOLFSSL_ENTER(const char* msg);
  145. WOLFSSL_API void WOLFSSL_LEAVE(const char* msg, int ret);
  146. #define WOLFSSL_STUB(m) \
  147. WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented))
  148. WOLFSSL_API int WOLFSSL_IS_DEBUG_ON(void);
  149. #if defined(XVSNPRINTF)
  150. WOLFSSL_API void WOLFSSL_MSG_EX(const char* fmt, ...);
  151. #define HAVE_WOLFSSL_MSG_EX
  152. #else
  153. #define WOLFSSL_MSG_EX(...) WC_DO_NOTHING
  154. #endif
  155. WOLFSSL_API void WOLFSSL_MSG(const char* msg);
  156. WOLFSSL_API void WOLFSSL_BUFFER(const byte* buffer, word32 length);
  157. #else
  158. #define WOLFSSL_ENTER(m) WC_DO_NOTHING
  159. #define WOLFSSL_LEAVE(m, r) WC_DO_NOTHING
  160. #define WOLFSSL_STUB(m) WC_DO_NOTHING
  161. #define WOLFSSL_IS_DEBUG_ON() 0
  162. #define WOLFSSL_MSG_EX(...) WC_DO_NOTHING
  163. #define WOLFSSL_MSG(m) WC_DO_NOTHING
  164. #define WOLFSSL_BUFFER(b, l) WC_DO_NOTHING
  165. #endif /* DEBUG_WOLFSSL && !WOLFSSL_DEBUG_ERRORS_ONLY */
  166. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  167. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  168. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  169. WOLFSSL_API void WOLFSSL_ERROR_LINE(int err, const char* func, unsigned int line,
  170. const char* file, void* ctx);
  171. #define WOLFSSL_ERROR(x) \
  172. WOLFSSL_ERROR_LINE((x), __func__, __LINE__, __FILE__, NULL)
  173. #else
  174. WOLFSSL_API void WOLFSSL_ERROR(int err);
  175. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  176. WOLFSSL_API void WOLFSSL_ERROR_MSG(const char* msg);
  177. #else
  178. #define WOLFSSL_ERROR(e) (void)(e)
  179. #define WOLFSSL_ERROR_MSG(m) (void)(m)
  180. #endif /* DEBUG_WOLFSSL | OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  181. OPENSSL_EXTRA */
  182. #ifdef WOLFSSL_VERBOSE_ERRORS
  183. #define WOLFSSL_ERROR_VERBOSE(e) WOLFSSL_ERROR(e)
  184. #else
  185. #define WOLFSSL_ERROR_VERBOSE(e) (void)(e)
  186. #endif /* WOLFSSL_VERBOSE_ERRORS */
  187. #ifdef HAVE_STACK_SIZE_VERBOSE
  188. extern WOLFSSL_API THREAD_LS_T unsigned char *StackSizeCheck_myStack;
  189. extern WOLFSSL_API THREAD_LS_T size_t StackSizeCheck_stackSize;
  190. extern WOLFSSL_API THREAD_LS_T size_t StackSizeCheck_stackSizeHWM;
  191. extern WOLFSSL_API THREAD_LS_T size_t *StackSizeCheck_stackSizeHWM_ptr;
  192. extern WOLFSSL_API THREAD_LS_T void *StackSizeCheck_stackOffsetPointer;
  193. #endif
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif /* WOLFSSL_LOGGING_H */