Skip to content

Commit ba743cc

Browse files
committed
wolfcrypt/src/logging.c, wolfssl/wolfcrypt/error-crypt.h, and wolfssl/wolfcrypt/logging.h: implement WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE, wc_debug_trace_error_codes_enabled(), and wc_debug_trace_error_codes_set(), to allow runtime control of error tracing.
1 parent 84a4abf commit ba743cc

3 files changed

Lines changed: 68 additions & 17 deletions

File tree

wolfcrypt/src/logging.c

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,12 +1822,38 @@ void WOLFSSL_ERROR_MSG(const char* msg)
18221822

18231823
#endif /* DEBUG_WOLFSSL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
18241824

1825+
#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES
1826+
1827+
#ifndef WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE
1828+
#define WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE 1
1829+
#endif
1830+
1831+
#ifdef WOLFSSL_ATOMIC_OPS
1832+
static wolfSSL_Atomic_Int wc_debug_trace_error_codes_state =
1833+
WOLFSSL_ATOMIC_INITIALIZER(WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE);
1834+
#else
1835+
static int wc_debug_trace_error_codes_state =
1836+
WOLFSSL_DEBUG_TRACE_ERROR_CODES_INIT_STATE;
1837+
#endif
1838+
1839+
int wc_debug_trace_error_codes_enabled(void) {
1840+
return WOLFSSL_ATOMIC_LOAD(wc_debug_trace_error_codes_state);
1841+
}
1842+
1843+
int wc_debug_trace_error_codes_set(int state) {
1844+
return wolfSSL_Atomic_Int_Exchange(&wc_debug_trace_error_codes_state,
1845+
state);
1846+
}
1847+
1848+
#endif /* WOLFSSL_DEBUG_TRACE_ERROR_CODES */
1849+
18251850
#ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
18261851

18271852
#ifdef WOLFSSL_LINUXKM
18281853

1829-
void wc_backtrace_render(void) {
1854+
int wc_backtrace_render(void) {
18301855
dump_stack();
1856+
return 0;
18311857
}
18321858

18331859
#else /* !WOLFSSL_LINUXKM */
@@ -1900,22 +1926,24 @@ static int backtrace_init(struct backtrace_state **backtrace_state) {
19001926
return 0;
19011927
}
19021928

1903-
void wc_backtrace_render(void) {
1929+
int wc_backtrace_render(void) {
19041930
static wolfSSL_Mutex backtrace_mutex
19051931
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(backtrace_mutex);
19061932
static struct backtrace_state *backtrace_state = NULL;
19071933
int depth = 0;
1934+
int ret;
19081935

19091936
#ifndef WOLFSSL_MUTEX_INITIALIZER
19101937
static wolfSSL_Atomic_Int init_count = 0;
19111938
if (init_count != 1) {
19121939
int cur_init_count = wolfSSL_Atomic_Int_FetchSub(&init_count, 1);
19131940
if (cur_init_count != 0) {
19141941
(void)wolfSSL_Atomic_Int_FetchAdd(&init_count, 1);
1915-
return;
1942+
return DEADLOCK_AVERTED_E;
19161943
}
1917-
if (wc_InitMutex(&backtrace_mutex) != 0)
1918-
return;
1944+
ret = wc_InitMutex(&backtrace_mutex);
1945+
if (ret != 0)
1946+
return ret;
19191947
/* set init_count to 1, race-free: (-1) - (0-2) = 1 */
19201948
(void)wolfSSL_Atomic_Int_FetchSub(&init_count, cur_init_count - 2);
19211949
}
@@ -1925,13 +1953,14 @@ void wc_backtrace_render(void) {
19251953
* BACKTRACE_SUPPORTS_THREADS == 1, so we serialize the render op. this
19261954
* helpfully mutexes the initialization too.
19271955
*/
1928-
if (wc_LockMutex(&backtrace_mutex) != 0)
1929-
return;
1956+
ret = wc_LockMutex(&backtrace_mutex);
1957+
if (ret != 0)
1958+
return ret;
19301959

19311960
if (backtrace_state == NULL) {
19321961
if (backtrace_init(&backtrace_state) < 0) {
19331962
wc_UnLockMutex(&backtrace_mutex);
1934-
return;
1963+
return BAD_STATE_E;
19351964
}
19361965
}
19371966

@@ -1943,6 +1972,8 @@ void wc_backtrace_render(void) {
19431972
(void *)&depth);
19441973

19451974
wc_UnLockMutex(&backtrace_mutex);
1975+
1976+
return 0;
19461977
}
19471978
#endif /* !WOLFSSL_LINUXKM */
19481979

wolfssl/wolfcrypt/error-crypt.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,23 +344,38 @@ WOLFSSL_ABI WOLFSSL_API const char* wc_GetErrorString(int error);
344344
#if defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES) && \
345345
(defined(BUILDING_WOLFSSL) || \
346346
defined(WOLFSSL_DEBUG_TRACE_ERROR_CODES_ALWAYS))
347-
WOLFSSL_API extern void wc_backtrace_render(void);
347+
WOLFSSL_API extern int wc_backtrace_render(void);
348348
#define WC_NO_ERR_TRACE(label) (CONST_NUM_ERR_ ## label)
349349
#ifndef WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE
350350
#ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
351351
#define WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE wc_backtrace_render()
352352
#else
353-
#define WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE (void)0
353+
#define WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE 0
354354
#endif
355355
#endif
356356
#ifndef WC_ERR_TRACE
357-
#define WC_ERR_TRACE(label) \
358-
( WOLFSSL_DEBUG_PRINTF_FN(WOLFSSL_DEBUG_PRINTF_FIRST_ARGS \
359-
"ERR TRACE: %s L %d %s (%d)\n", \
360-
__FILE__, __LINE__, #label, label), \
361-
WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE, \
362-
label \
363-
)
357+
#ifdef __GNUC__
358+
#define WC_ERR_TRACE(label) \
359+
({ if (wc_debug_trace_error_codes_enabled()) { \
360+
(void)WOLFSSL_DEBUG_PRINTF_FN( \
361+
WOLFSSL_DEBUG_PRINTF_FIRST_ARGS \
362+
"ERR TRACE: %s L %d %s (%d)\n", \
363+
__FILE__, __LINE__, #label, label); \
364+
(void)WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE; } \
365+
(label); \
366+
})
367+
#else /* ! __GNUC__ */
368+
#define WC_ERR_TRACE(label) \
369+
((void)(wc_debug_trace_error_codes_enabled() && \
370+
WOLFSSL_DEBUG_PRINTF_FN( \
371+
WOLFSSL_DEBUG_PRINTF_FIRST_ARGS \
372+
"ERR TRACE: %s L %d %s (%d)\n", \
373+
__FILE__, __LINE__, #label, label)), \
374+
(void)(wc_debug_trace_error_codes_enabled() && \
375+
WOLFSSL_DEBUG_BACKTRACE_RENDER_CLAUSE), \
376+
(label) \
377+
)
378+
#endif /* ! __GNUC__ */
364379
#endif
365380
#include <wolfssl/debug-trace-error-codes.h>
366381
#else

wolfssl/wolfcrypt/logging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,11 @@ WOLFSSL_API void wolfSSL_SetLoggingPrefix(const char* prefix);
579579
#error "Failed: Cannot WOLFSSL_DEBUG_CERTS with WOLFSSL_DEBUG_ERRORS_ONLY"
580580
#endif
581581

582+
#ifdef WOLFSSL_DEBUG_TRACE_ERROR_CODES
583+
WOLFSSL_API int wc_debug_trace_error_codes_enabled(void);
584+
WOLFSSL_API int wc_debug_trace_error_codes_set(int state);
585+
#endif
586+
582587
#ifdef __cplusplus
583588
}
584589
#endif

0 commit comments

Comments
 (0)