logging.h (12734B)
1 /* logging.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 /*! 23 \file wolfssl/wolfcrypt/logging.h 24 */ 25 26 27 /* submitted by eof */ 28 29 30 #ifndef WOLFSSL_LOGGING_H 31 #define WOLFSSL_LOGGING_H 32 33 #include <wolfssl/wolfcrypt/types.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 40 enum wc_LogLevels { 41 ERROR_LOG = 0, 42 INFO_LOG, 43 ENTER_LOG, 44 LEAVE_LOG, 45 OTHER_LOG 46 }; 47 48 #ifdef WOLFSSL_FUNC_TIME 49 /* WARNING: This code is only to be used for debugging performance. 50 * The code is not thread-safe. 51 * Do not use WOLFSSL_FUNC_TIME in production code. 52 */ 53 enum wc_FuncNum { 54 WC_FUNC_HELLO_REQUEST_SEND = 0, 55 WC_FUNC_HELLO_REQUEST_DO, 56 WC_FUNC_CLIENT_HELLO_SEND, 57 WC_FUNC_CLIENT_HELLO_DO, 58 WC_FUNC_SERVER_HELLO_SEND, 59 WC_FUNC_SERVER_HELLO_DO, 60 WC_FUNC_ENCRYPTED_EXTENSIONS_SEND, 61 WC_FUNC_ENCRYPTED_EXTENSIONS_DO, 62 WC_FUNC_CERTIFICATE_REQUEST_SEND, 63 WC_FUNC_CERTIFICATE_REQUEST_DO, 64 WC_FUNC_CERTIFICATE_SEND, 65 WC_FUNC_CERTIFICATE_DO, 66 WC_FUNC_CERTIFICATE_VERIFY_SEND, 67 WC_FUNC_CERTIFICATE_VERIFY_DO, 68 WC_FUNC_FINISHED_SEND, 69 WC_FUNC_FINISHED_DO, 70 WC_FUNC_KEY_UPDATE_SEND, 71 WC_FUNC_KEY_UPDATE_DO, 72 WC_FUNC_EARLY_DATA_SEND, 73 WC_FUNC_EARLY_DATA_DO, 74 WC_FUNC_NEW_SESSION_TICKET_SEND, 75 WC_FUNC_NEW_SESSION_TICKET_DO, 76 WC_FUNC_SERVER_HELLO_DONE_SEND, 77 WC_FUNC_SERVER_HELLO_DONE_DO, 78 WC_FUNC_TICKET_SEND, 79 WC_FUNC_TICKET_DO, 80 WC_FUNC_CLIENT_KEY_EXCHANGE_SEND, 81 WC_FUNC_CLIENT_KEY_EXCHANGE_DO, 82 WC_FUNC_CERTIFICATE_STATUS_SEND, 83 WC_FUNC_CERTIFICATE_STATUS_DO, 84 WC_FUNC_SERVER_KEY_EXCHANGE_SEND, 85 WC_FUNC_SERVER_KEY_EXCHANGE_DO, 86 WC_FUNC_END_OF_EARLY_DATA_SEND, 87 WC_FUNC_END_OF_EARLY_DATA_DO, 88 WC_FUNC_COUNT 89 }; 90 #endif 91 92 typedef void (*wolfSSL_Logging_cb)(const int logLevel, 93 const char *const logMessage); 94 95 WOLFSSL_API int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb log_function); 96 WOLFSSL_API wolfSSL_Logging_cb wolfSSL_GetLoggingCb(void); 97 98 /* turn logging on, only if compiled in */ 99 WOLFSSL_API int wolfSSL_Debugging_ON(void); 100 /* turn logging off */ 101 WOLFSSL_API void wolfSSL_Debugging_OFF(void); 102 103 WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix); 104 105 #ifdef HAVE_WC_INTROSPECTION 106 WOLFSSL_API const char *wolfSSL_configure_args(void); 107 WOLFSSL_API const char *wolfSSL_global_cflags(void); 108 #endif 109 110 111 #if (defined(OPENSSL_EXTRA) && !defined(_WIN32) && \ 112 !defined(NO_ERROR_QUEUE)) || defined(DEBUG_WOLFSSL_VERBOSE) \ 113 || defined(HAVE_MEMCACHED) 114 #define WOLFSSL_HAVE_ERROR_QUEUE 115 #endif 116 117 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) || defined(HAVE_MEMCACHED) 118 WOLFSSL_LOCAL int wc_LoggingInit(void); 119 WOLFSSL_LOCAL int wc_LoggingCleanup(void); 120 WOLFSSL_LOCAL int wc_AddErrorNode(int error, int line, char* buf, 121 char* file); 122 WOLFSSL_LOCAL int wc_PeekErrorNode(int idx, const char **file, 123 const char **reason, int *line); 124 WOLFSSL_LOCAL void wc_RemoveErrorNode(int idx); 125 WOLFSSL_LOCAL void wc_ClearErrorNodes(void); 126 WOLFSSL_LOCAL int wc_PullErrorNode(const char **file, const char **reason, 127 int *line); 128 WOLFSSL_API int wc_SetLoggingHeap(void* h); 129 WOLFSSL_API int wc_ERR_remove_state(void); 130 WOLFSSL_LOCAL unsigned long wc_PeekErrorNodeLineData( 131 const char **file, int *line, const char **data, int *flags, 132 int (*ignore_err)(int err)); 133 WOLFSSL_LOCAL int wc_GetErrorNodeErr(void); 134 #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) 135 WOLFSSL_API void wc_ERR_print_errors_fp(XFILE fp); 136 WOLFSSL_API void wc_ERR_print_errors_cb(int (*cb)(const char *str, 137 size_t len, void *u), void *u); 138 #endif 139 #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE || HAVE_MEMCACHED */ 140 141 #ifdef WOLFSSL_FUNC_TIME 142 /* WARNING: This code is only to be used for debugging performance. 143 * The code is not thread-safe. 144 * Do not use WOLFSSL_FUNC_TIME in production code. 145 */ 146 WOLFSSL_API void WOLFSSL_START(int funcNum); 147 WOLFSSL_API void WOLFSSL_END(int funcNum); 148 WOLFSSL_API void WOLFSSL_TIME(int count); 149 #else 150 #define WOLFSSL_START(n) WC_DO_NOTHING 151 #define WOLFSSL_END(n) WC_DO_NOTHING 152 #define WOLFSSL_TIME(n) WC_DO_NOTHING 153 #endif 154 155 #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_DEBUG_ERRORS_ONLY) 156 #if defined(_WIN32) 157 #if defined(INTIME_RTOS) 158 #define __func__ NULL 159 #else 160 #define __func__ __FUNCTION__ 161 #endif 162 #endif 163 164 /* a is prepended to m and b is appended, creating a log msg a + m + b */ 165 #define WOLFSSL_LOG_CAT(a, m, b) #a " " m " " #b 166 167 WOLFSSL_API void WOLFSSL_ENTER(const char* msg); 168 WOLFSSL_API void WOLFSSL_LEAVE(const char* msg, int ret); 169 #define WOLFSSL_STUB(m) \ 170 WOLFSSL_MSG(WOLFSSL_LOG_CAT(wolfSSL Stub, m, not implemented)) 171 WOLFSSL_API int WOLFSSL_IS_DEBUG_ON(void); 172 #if defined(XVSNPRINTF) && !defined(NO_WOLFSSL_MSG_EX) 173 WOLFSSL_API void WOLFSSL_MSG_EX(const char* fmt, ...); 174 #define HAVE_WOLFSSL_MSG_EX 175 #else 176 #ifdef WOLF_NO_VARIADIC_MACROS 177 #define WOLFSSL_MSG_EX() WC_DO_NOTHING 178 #else 179 #define WOLFSSL_MSG_EX(...) WC_DO_NOTHING 180 #endif 181 #endif 182 WOLFSSL_API void WOLFSSL_MSG(const char* msg); 183 #ifdef WOLFSSL_DEBUG_CODEPOINTS 184 WOLFSSL_API void WOLFSSL_MSG2( 185 const char *file, int line, const char* msg); 186 WOLFSSL_API void WOLFSSL_ENTER2( 187 const char *file, int line, const char* msg); 188 WOLFSSL_API void WOLFSSL_LEAVE2( 189 const char *file, int line, const char* msg, int ret); 190 #define WOLFSSL_MSG(msg) WOLFSSL_MSG2(__FILE__, __LINE__, msg) 191 #define WOLFSSL_ENTER(msg) WOLFSSL_ENTER2(__FILE__, __LINE__, msg) 192 #define WOLFSSL_LEAVE(msg, ret) WOLFSSL_LEAVE2(__FILE__, __LINE__, msg, ret) 193 #ifdef XVSNPRINTF 194 WOLFSSL_API void WOLFSSL_MSG_EX2( 195 const char *file, int line, const char* fmt, ...); 196 #define WOLFSSL_MSG_EX(fmt, args...) \ 197 WOLFSSL_MSG_EX2(__FILE__, __LINE__, fmt, ## args) 198 #else 199 #ifdef WOLF_NO_VARIADIC_MACROS 200 #define WOLFSSL_MSG_EX2() WC_DO_NOTHING 201 #else 202 #define WOLFSSL_MSG_EX2(...) WC_DO_NOTHING 203 #endif 204 #endif 205 #endif 206 WOLFSSL_API void WOLFSSL_BUFFER(const byte* buffer, word32 length); 207 208 #else 209 210 #define WOLFSSL_ENTER(m) WC_DO_NOTHING 211 #define WOLFSSL_LEAVE(m, r) WC_DO_NOTHING 212 #define WOLFSSL_STUB(m) WC_DO_NOTHING 213 #define WOLFSSL_IS_DEBUG_ON() 0 214 215 #ifdef WOLF_NO_VARIADIC_MACROS 216 /* note, modern preprocessors will generate errors with this definition. 217 * "error: macro "WOLFSSL_MSG_EX" passed 2 arguments, but takes just 0" 218 */ 219 #define WOLFSSL_MSG_EX() WC_DO_NOTHING 220 #else 221 #define WOLFSSL_MSG_EX(...) WC_DO_NOTHING 222 #endif 223 #define WOLFSSL_MSG(m) WC_DO_NOTHING 224 #define WOLFSSL_BUFFER(b, l) WC_DO_NOTHING 225 226 #endif /* DEBUG_WOLFSSL && !WOLFSSL_DEBUG_ERRORS_ONLY */ 227 228 #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\ 229 defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) 230 231 #ifdef WOLFSSL_HAVE_ERROR_QUEUE 232 WOLFSSL_API void WOLFSSL_ERROR_LINE(int err, const char* func, unsigned int line, 233 const char* file, void* ctx); 234 #ifdef WOLF_C89 235 #define WOLFSSL_ERROR(x) \ 236 WOLFSSL_ERROR_LINE((x), __FILE__, __LINE__, __FILE__, NULL) 237 #else 238 #define WOLFSSL_ERROR(x) \ 239 WOLFSSL_ERROR_LINE((x), __func__, __LINE__, __FILE__, NULL) 240 #endif 241 #else 242 WOLFSSL_API void WOLFSSL_ERROR(int err); 243 #endif /* WOLFSSL_HAVE_ERROR_QUEUE */ 244 245 WOLFSSL_API void WOLFSSL_ERROR_MSG(const char* msg); 246 #else 247 #define WOLFSSL_ERROR(e) (void)(e) 248 #define WOLFSSL_ERROR_MSG(m) (void)(m) 249 #endif /* DEBUG_WOLFSSL | OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY || 250 OPENSSL_EXTRA */ 251 252 #ifdef WOLFSSL_VERBOSE_ERRORS 253 #define WOLFSSL_ERROR_VERBOSE(e) WOLFSSL_ERROR(e) 254 #else 255 #define WOLFSSL_ERROR_VERBOSE(e) (void)(e) 256 #endif /* WOLFSSL_VERBOSE_ERRORS */ 257 258 #ifdef HAVE_STACK_SIZE_VERBOSE 259 extern WOLFSSL_API THREAD_LS_T unsigned char *StackSizeCheck_myStack; 260 extern WOLFSSL_API THREAD_LS_T size_t StackSizeCheck_stackSize; 261 extern WOLFSSL_API THREAD_LS_T size_t StackSizeCheck_stackSizeHWM; 262 extern WOLFSSL_API THREAD_LS_T size_t *StackSizeCheck_stackSizeHWM_ptr; 263 extern WOLFSSL_API THREAD_LS_T void *StackSizeCheck_stackOffsetPointer; 264 #endif 265 266 /* Port-specific includes and printf methods: */ 267 268 #if defined(ARDUINO) 269 /* implemented in Arduino wolfssl.h */ 270 extern WOLFSSL_API int wolfSSL_Arduino_Serial_Print(const char* const s); 271 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 272 /* see wc_port.h for fio.h and nio.h includes */ 273 #elif defined(WOLFSSL_SGX) 274 /* Declare sprintf for ocall */ 275 int sprintf(char* buf, const char *fmt, ...); 276 #elif defined(WOLFSSL_DEOS) 277 #elif defined(MICRIUM) 278 #if (BSP_SER_COMM_EN == DEF_ENABLED) 279 #include <bsp_ser.h> 280 #endif 281 #elif defined(WOLFSSL_USER_LOG) 282 /* user includes their own headers */ 283 #elif defined(WOLFSSL_ESPIDF) 284 #include "esp_types.h" 285 #include "esp_log.h" 286 #elif defined(WOLFSSL_TELIT_M2MB) 287 #include <stdio.h> 288 #include "m2m_log.h" 289 #elif defined(WOLFSSL_ANDROID_DEBUG) 290 #include <android/log.h> 291 #elif defined(WOLFSSL_XILINX) 292 #include "xil_printf.h" 293 #elif defined(WOLFSSL_LINUXKM) 294 /* the requisite linux/kernel.h is included in linuxkm_wc_port.h, with 295 * incompatible warnings masked out. 296 */ 297 #elif defined(FUSION_RTOS) 298 #include <fclstdio.h> 299 #define fprintf FCL_FPRINTF 300 #else 301 #include <stdio.h> /* for default printf stuff */ 302 #endif 303 304 #if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) 305 int dc_log_printf(char*, ...); 306 #endif 307 308 #ifdef WOLFSSL_DEBUG_PRINTF_FN 309 /* user-supplied definition */ 310 #elif defined(ARDUINO) 311 /* ARDUINO only has print and sprintf, no printf. */ 312 #elif defined(WOLFSSL_LOG_PRINTF) || defined(WOLFSSL_DEOS) 313 #define WOLFSSL_DEBUG_PRINTF_FN printf 314 #elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) 315 #define WOLFSSL_DEBUG_PRINTF_FN dc_log_printf 316 #elif defined(MICRIUM) 317 #define WOLFSSL_DEBUG_PRINTF_FN BSP_Ser_Printf 318 #elif defined(WOLFSSL_MDK_ARM) 319 #define WOLFSSL_DEBUG_PRINTF_FN printf 320 #elif defined(WOLFSSL_UTASKER) 321 /* WOLFSSL_UTASKER only has fnDebugMsg and related primitives, no printf. */ 322 #elif defined(MQX_USE_IO_OLD) 323 #define WOLFSSL_DEBUG_PRINTF_FN fprintf 324 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS _mqxio_stderr, 325 #elif defined(WOLFSSL_APACHE_MYNEWT) 326 #define WOLFSSL_DEBUG_PRINTF_FN LOG_DEBUG 327 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS &mynewt_log, LOG_MODULE_DEFAULT, 328 #elif defined(WOLFSSL_ESPIDF) 329 #define WOLFSSL_DEBUG_PRINTF_FN ESP_LOGI 330 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS "wolfssl", 331 #elif defined(WOLFSSL_ZEPHYR) 332 #define WOLFSSL_DEBUG_PRINTF_FN printk 333 #elif defined(WOLFSSL_TELIT_M2MB) 334 #define WOLFSSL_DEBUG_PRINTF_FN M2M_LOG_INFO 335 #elif defined(WOLFSSL_ANDROID_DEBUG) 336 #define WOLFSSL_DEBUG_PRINTF_FN __android_log_print 337 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS ANDROID_LOG_VERBOSE, "[wolfSSL]" 338 #elif defined(WOLFSSL_XILINX) 339 #define WOLFSSL_DEBUG_PRINTF_FN xil_printf 340 #elif defined(WOLFSSL_LINUXKM) 341 #define WOLFSSL_DEBUG_PRINTF_FN printk 342 #elif defined(WOLFSSL_RENESAS_RA6M4) 343 #define WOLFSSL_DEBUG_PRINTF_FN myprintf 344 #else 345 #define WOLFSSL_DEBUG_PRINTF_FN fprintf 346 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS stderr, 347 #endif 348 349 #ifndef WOLFSSL_DEBUG_PRINTF_FIRST_ARGS 350 #define WOLFSSL_DEBUG_PRINTF_FIRST_ARGS 351 #endif 352 353 #if defined(WOLFSSL_DEBUG_PRINTF_FN) && !defined(WOLFSSL_DEBUG_PRINTF) && \ 354 !defined(WOLF_NO_VARIADIC_MACROS) 355 #define WOLFSSL_DEBUG_PRINTF(...) \ 356 WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS __VA_ARGS__) 357 #endif 358 359 #ifdef __cplusplus 360 } 361 #endif 362 #endif /* WOLFSSL_LOGGING_H */ 363