- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/logging.c
r352 r372 31 31 #include <wolfssl/wolfcrypt/logging.h> 32 32 #include <wolfssl/wolfcrypt/error-crypt.h> 33 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 WOLFSSL_API int wolfSSL_Debugging_ON(void); 39 WOLFSSL_API void wolfSSL_Debugging_OFF(void); 40 #ifdef __cplusplus 41 } 33 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) 34 /* avoid adding WANT_READ and WANT_WRITE to error queue */ 35 #include <wolfssl/error-ssl.h> 42 36 #endif 43 37 … … 58 52 }; 59 53 volatile struct wc_error_queue* wc_errors; 54 static struct wc_error_queue* wc_current_node; 60 55 static struct wc_error_queue* wc_last_node; 61 56 /* pointer to last node in queue to make insertion O(1) */ 62 57 #endif 63 58 64 65 66 #if defined(DEBUG_WOLFSSL) 59 #ifdef WOLFSSL_FUNC_TIME 60 /* WARNING: This code is only to be used for debugging performance. 61 * The code is not thread-safe. 62 * Do not use WOLFSSL_FUNC_TIME in production code. 63 */ 64 static double wc_func_start[WC_FUNC_COUNT]; 65 static double wc_func_time[WC_FUNC_COUNT] = { 0, }; 66 static const char* wc_func_name[WC_FUNC_COUNT] = { 67 "SendHelloRequest", 68 "DoHelloRequest", 69 "SendClientHello", 70 "DoClientHello", 71 "SendServerHello", 72 "DoServerHello", 73 "SendEncryptedExtensions", 74 "DoEncryptedExtensions", 75 "SendCertificateRequest", 76 "DoCertificateRequest", 77 "SendCertificate", 78 "DoCertificate", 79 "SendCertificateVerify", 80 "DoCertificateVerify", 81 "SendFinished", 82 "DoFinished", 83 "SendKeyUpdate", 84 "DoKeyUpdate", 85 "SendEarlyData", 86 "DoEarlyData", 87 "SendNewSessionTicket", 88 "DoNewSessionTicket", 89 "SendServerHelloDone", 90 "DoServerHelloDone", 91 "SendTicket", 92 "DoTicket", 93 "SendClientKeyExchange", 94 "DoClientKeyExchange", 95 "SendCertificateStatus", 96 "DoCertificateStatus", 97 "SendServerKeyExchange", 98 "DoServerKeyExchange", 99 "SendEarlyData", 100 "DoEarlyData", 101 }; 102 103 #include <sys/time.h> 104 105 /* WARNING: This function is not portable. */ 106 static WC_INLINE double current_time(int reset) 107 { 108 struct timeval tv; 109 gettimeofday(&tv, 0); 110 (void)reset; 111 112 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000; 113 } 114 #endif /* WOLFSSL_FUNC_TIME */ 115 116 #ifdef DEBUG_WOLFSSL 67 117 68 118 /* Set these to default values initially. */ … … 70 120 static int loggingEnabled = 0; 71 121 122 #if defined(WOLFSSL_APACHE_MYNEWT) 123 #include "log/log.h" 124 static struct log mynewt_log; 125 #endif /* WOLFSSL_APACHE_MYNEWT */ 126 72 127 #endif /* DEBUG_WOLFSSL */ 73 128 74 129 130 /* allow this to be set to NULL, so logs can be redirected to default output */ 75 131 int wolfSSL_SetLoggingCb(wolfSSL_Logging_cb f) 76 132 { 77 133 #ifdef DEBUG_WOLFSSL 78 int res = 0;79 80 if (f)81 134 log_function = f; 82 else 83 res = BAD_FUNC_ARG; 84 85 return res; 135 return 0; 86 136 #else 87 137 (void)f; … … 95 145 #ifdef DEBUG_WOLFSSL 96 146 loggingEnabled = 1; 147 #if defined(WOLFSSL_APACHE_MYNEWT) 148 log_register("wolfcrypt", &mynewt_log, &log_console_handler, NULL, LOG_SYSLEVEL); 149 #endif /* WOLFSSL_APACHE_MYNEWT */ 97 150 return 0; 98 151 #else … … 109 162 } 110 163 164 #ifdef WOLFSSL_FUNC_TIME 165 /* WARNING: This code is only to be used for debugging performance. 166 * The code is not thread-safe. 167 * Do not use WOLFSSL_FUNC_TIME in production code. 168 */ 169 void WOLFSSL_START(int funcNum) 170 { 171 double now = current_time(0) * 1000.0; 172 #ifdef WOLFSSL_FUNC_TIME_LOG 173 fprintf(stderr, "%17.3f: START - %s\n", now, wc_func_name[funcNum]); 174 #endif 175 wc_func_start[funcNum] = now; 176 } 177 178 void WOLFSSL_END(int funcNum) 179 { 180 double now = current_time(0) * 1000.0; 181 wc_func_time[funcNum] += now - wc_func_start[funcNum]; 182 #ifdef WOLFSSL_FUNC_TIME_LOG 183 fprintf(stderr, "%17.3f: END - %s\n", now, wc_func_name[funcNum]); 184 #endif 185 } 186 187 void WOLFSSL_TIME(int count) 188 { 189 int i; 190 double avg, total = 0; 191 192 for (i = 0; i < WC_FUNC_COUNT; i++) { 193 if (wc_func_time[i] > 0) { 194 avg = wc_func_time[i] / count; 195 fprintf(stderr, "%8.3f ms: %s\n", avg, wc_func_name[i]); 196 total += avg; 197 } 198 } 199 fprintf(stderr, "%8.3f ms\n", total); 200 } 201 #endif 111 202 112 203 #ifdef DEBUG_WOLFSSL … … 122 213 int sprintf(char* buf, const char *fmt, ...); 123 214 #elif defined(MICRIUM) 215 #if (BSP_SER_COMM_EN == DEF_ENABLED) 124 216 #include <bsp_ser.h> 217 #endif 218 #elif defined(WOLFSSL_USER_LOG) 219 /* user includes their own headers */ 220 #elif defined(WOLFSSL_ESPIDF) 221 #include "esp_types.h" 222 #include "esp_log.h" 125 223 #else 126 224 #include <stdio.h> /* for default printf stuff */ … … 136 234 log_function(logLevel, logMessage); 137 235 else { 138 if (loggingEnabled) { 139 #if defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) 236 #if defined(WOLFSSL_USER_LOG) 237 WOLFSSL_USER_LOG(logMessage); 238 #elif defined(WOLFSSL_LOG_PRINTF) 239 printf("%s\n", logMessage); 240 241 #elif defined(THREADX) && !defined(THREADX_NO_DC_PRINTF) 140 242 dc_log_printf("%s\n", logMessage); 141 243 #elif defined(MICRIUM) … … 145 247 printf("%s\n", logMessage); 146 248 fflush(stdout) ; 147 #elif defined(WOLFSSL_LOG_PRINTF)148 printf("%s\n", logMessage);149 249 #elif defined(WOLFSSL_UTASKER) 150 250 fnDebugMsg((char*)logMessage); … … 152 252 #elif defined(MQX_USE_IO_OLD) 153 253 fprintf(_mqxio_stderr, "%s\n", logMessage); 254 255 #elif defined(WOLFSSL_APACHE_MYNEWT) 256 LOG_DEBUG(&mynewt_log, LOG_MODULE_DEFAULT, "%s\n", logMessage); 257 #elif defined(WOLFSSL_ESPIDF) 258 extern char* TAG; 259 ESP_LOGI(TAG, "%s", logMessage); 154 260 #else 155 261 fprintf(stderr, "%s\n", logMessage); … … 157 263 } 158 264 } 159 } 160 161 265 266 #ifndef WOLFSSL_DEBUG_ERRORS_ONLY 162 267 void WOLFSSL_MSG(const char* msg) 163 268 { … … 208 313 { 209 314 if (loggingEnabled) { 210 char buffer[ 80];211 sprintf(buffer, "wolfSSL Entering %s", msg);315 char buffer[WOLFSSL_MAX_ERROR_SZ]; 316 XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Entering %s", msg); 212 317 wolfssl_log(ENTER_LOG , buffer); 213 318 } … … 218 323 { 219 324 if (loggingEnabled) { 220 char buffer[80]; 221 sprintf(buffer, "wolfSSL Leaving %s, return %d", msg, ret); 325 char buffer[WOLFSSL_MAX_ERROR_SZ]; 326 XSNPRINTF(buffer, sizeof(buffer), "wolfSSL Leaving %s, return %d", 327 msg, ret); 222 328 wolfssl_log(LEAVE_LOG , buffer); 223 329 } 224 330 } 331 #endif /* !WOLFSSL_DEBUG_ERRORS_ONLY */ 225 332 #endif /* DEBUG_WOLFSSL */ 226 333 … … 230 337 * name where WOLFSSL_ERROR is called at. 231 338 */ 232 #if (defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX)) || defined(WOLFSSL_HAPROXY) 233 #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) 339 #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || \ 340 defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) 341 342 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) 234 343 void WOLFSSL_ERROR_LINE(int error, const char* func, unsigned int line, 235 344 const char* file, void* usrCtx) … … 238 347 #endif 239 348 { 240 #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_NGINX) 241 if ( loggingEnabled &&error != WC_PENDING_E)349 #ifdef WOLFSSL_ASYNC_CRYPT 350 if (error != WC_PENDING_E) 242 351 #endif 243 352 { 244 char buffer[80]; 353 char buffer[WOLFSSL_MAX_ERROR_SZ]; 354 245 355 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) 246 356 (void)usrCtx; /* a user ctx for future flexibility */ … … 249 359 if (wc_LockMutex(&debug_mutex) != 0) { 250 360 WOLFSSL_MSG("Lock debug mutex failed"); 251 sprintf(buffer, "wolfSSL error occurred, error = %d", error); 361 XSNPRINTF(buffer, sizeof(buffer), 362 "wolfSSL error occurred, error = %d", error); 252 363 } 253 364 else { 254 if (error < 0) error = error - (2*error); /*get absolute value*/ 255 sprintf(buffer, "wolfSSL error occurred, error = %d line:%d file:%s", 365 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) 366 /* If running in compatibility mode do not add want read and 367 want right to error queue */ 368 if (error != WANT_READ && error != WANT_WRITE) { 369 #endif 370 if (error < 0) 371 error = error - (2 * error); /* get absolute value */ 372 XSNPRINTF(buffer, sizeof(buffer), 373 "wolfSSL error occurred, error = %d line:%d file:%s", 256 374 error, line, file); 257 375 if (wc_AddErrorNode(error, line, buffer, (char*)file) != 0) { … … 260 378 * to unlock mutex and log what buffer was created. */ 261 379 } 380 #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) 381 } 382 else { 383 XSNPRINTF(buffer, sizeof(buffer), 384 "wolfSSL error occurred, error = %d", error); 385 386 } 387 #endif 262 388 263 389 wc_UnLockMutex(&debug_mutex); 264 390 } 265 391 #else 266 sprintf(buffer, "wolfSSL error occurred, error = %d", error); 392 XSNPRINTF(buffer, sizeof(buffer), 393 "wolfSSL error occurred, error = %d", error); 267 394 #endif 395 268 396 #ifdef DEBUG_WOLFSSL 397 if (loggingEnabled) 269 398 wolfssl_log(ERROR_LOG , buffer); 270 399 #endif 271 400 } 401 } 402 403 void WOLFSSL_ERROR_MSG(const char* msg) 404 { 405 #ifdef DEBUG_WOLFSSL 406 if (loggingEnabled) 407 wolfssl_log(ERROR_LOG , msg); 408 #else 409 (void)msg; 410 #endif 272 411 } 273 412 … … 283 422 } 284 423 wc_errors = NULL; 424 wc_current_node = NULL; 285 425 wc_last_node = NULL; 286 426 … … 304 444 305 445 306 #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || \307 defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_MYSQL_COMPATIBLE)308 446 /* peek at an error node 309 447 * … … 367 505 368 506 507 /* Pulls the current node from error queue and increments current state. 508 * Note: this does not delete nodes because input arguments are pointing to 509 * node buffers. 510 * 511 * file pointer to file that error was in. Can be NULL to return no file. 512 * reason error string giving reason for error. Can be NULL to return no reason. 513 * line return line number of where error happened. 514 * 515 * returns the error value on success and BAD_MUTEX_E or BAD_STATE_E on failure 516 */ 517 int wc_PullErrorNode(const char **file, const char **reason, int *line) 518 { 519 struct wc_error_queue* err; 520 int value; 521 522 if (wc_LockMutex(&debug_mutex) != 0) { 523 WOLFSSL_MSG("Lock debug mutex failed"); 524 return BAD_MUTEX_E; 525 } 526 527 err = wc_current_node; 528 if (err == NULL) { 529 WOLFSSL_MSG("No Errors in queue"); 530 wc_UnLockMutex(&debug_mutex); 531 return BAD_STATE_E; 532 } 533 534 if (file != NULL) { 535 *file = err->file; 536 } 537 538 if (reason != NULL) { 539 *reason = err->error; 540 } 541 542 if (line != NULL) { 543 *line = err->line; 544 } 545 546 value = err->value; 547 wc_current_node = err->next; 548 wc_UnLockMutex(&debug_mutex); 549 550 return value; 551 } 552 553 369 554 /* create new error node and add it to the queue 370 555 * buffers are assumed to be of size WOLFSSL_MAX_ERROR_SZ for this internal … … 416 601 /* check for unexpected case before over writing wc_errors */ 417 602 WOLFSSL_MSG("ERROR in adding new node to logging queue!!\n"); 603 /* In the event both wc_last_node and wc_errors are NULL, err 604 * goes unassigned to external wc_errors, wc_last_node. Free 605 * err in this instance since wc_ClearErrorNodes will not 606 */ 607 XFREE(err, wc_error_heap, DYNAMIC_TYPE_LOG); 418 608 } 419 609 else { 420 610 wc_errors = err; 421 611 wc_last_node = err; 612 wc_current_node = err; 422 613 } 423 614 } … … 426 617 err->prev = wc_last_node; 427 618 wc_last_node = err; 619 620 /* check the case where have read to the end of the queue and the 621 * current node to read needs updated */ 622 if (wc_current_node == NULL) { 623 wc_current_node = err; 624 } 428 625 } 429 626 } … … 465 662 } 466 663 467 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */468 664 469 665 /* Clears out the list of error nodes. … … 471 667 void wc_ClearErrorNodes(void) 472 668 { 669 #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || \ 670 defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) 671 473 672 if (wc_LockMutex(&debug_mutex) != 0) { 474 673 WOLFSSL_MSG("Lock debug mutex failed"); … … 491 690 wc_errors = NULL; 492 691 wc_last_node = NULL; 692 wc_current_node = NULL; 493 693 wc_UnLockMutex(&debug_mutex); 694 #endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX */ 494 695 } 495 696 … … 505 706 } 506 707 708 709 /* frees all nodes in the queue 710 * 711 * id this is the thread id 712 */ 713 int wc_ERR_remove_state(void) 714 { 715 struct wc_error_queue* current; 716 struct wc_error_queue* next; 717 718 if (wc_LockMutex(&debug_mutex) != 0) { 719 WOLFSSL_MSG("Lock debug mutex failed"); 720 return BAD_MUTEX_E; 721 } 722 723 /* free all nodes from error queue */ 724 current = (struct wc_error_queue*)wc_errors; 725 while (current != NULL) { 726 next = current->next; 727 XFREE(current, current->heap, DYNAMIC_TYPE_LOG); 728 current = next; 729 } 730 731 wc_errors = NULL; 732 wc_last_node = NULL; 733 734 wc_UnLockMutex(&debug_mutex); 735 736 return 0; 737 } 738 739 507 740 #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) 508 741 /* empties out the error queue into the file */ 509 void wc_ERR_print_errors_fp( FILE*fp)742 void wc_ERR_print_errors_fp(XFILE fp) 510 743 { 511 744 WOLFSSL_ENTER("wc_ERR_print_errors_fp"); 512 745 513 if (wc_LockMutex(&debug_mutex) != 0) { 746 if (wc_LockMutex(&debug_mutex) != 0) 747 { 514 748 WOLFSSL_MSG("Lock debug mutex failed"); 515 749 } 516 else { 750 else 751 { 517 752 /* free all nodes from error queue and print them to file */ 518 753 { … … 521 756 522 757 current = (struct wc_error_queue*)wc_errors; 523 while (current != NULL) { 758 while (current != NULL) 759 { 524 760 next = current->next; 525 761 fprintf(fp, "%s\n", current->error);
Note:
See TracChangeset
for help on using the changeset viewer.