From c73079a78cd0a028375b5d0da4a0d471394278c3 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Fri, 25 Apr 2014 16:34:30 +0200 Subject: [PATCH] Add debug_set_threshold() and thresholding of messages --- ChangeLog | 3 ++- include/polarssl/debug.h | 26 ++++++++++++++++++-------- library/debug.c | 19 ++++++++++++++----- programs/ssl/ssl_client1.c | 14 +++++++++----- programs/ssl/ssl_client2.c | 14 +++++++++----- programs/ssl/ssl_server.c | 14 +++++++++----- programs/ssl/ssl_server2.c | 14 +++++++++----- tests/suites/test_suite_debug.data | 15 +++++++++++++++ tests/suites/test_suite_debug.function | 22 ++++++++++++++++++++++ 9 files changed, 107 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index 53cc59265..7b14ce8c4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,7 +2,8 @@ PolarSSL ChangeLog (Sorted per branch, date) = PolarSSL 1.3 branch Features - * debug_set_log_mode() function added to determine raw or full logging + * debug_set_log_mode() added to determine raw or full logging + * debug_set_threshold() added to ignore messages over threshold level Changes * POLARSSL_CONFIG_OPTIONS has been removed. All values are individually diff --git a/include/polarssl/debug.h b/include/polarssl/debug.h index 96e08f378..599ce43f7 100644 --- a/include/polarssl/debug.h +++ b/include/polarssl/debug.h @@ -52,14 +52,6 @@ /* \} name SECTION: Module settings */ -/** - * \brief Set the log mode for the debug functions globally - * (Default value: POLARSSL_DEBUG_DFL_MODE) - * - * \param log_mode The log mode to use (POLARSSL_DEBUG_LOG_FULL or - * POLARSSL_DEBUG_LOG_RAW) - */ -void debug_set_log_mode( int log_mode ); #define SSL_DEBUG_MSG( level, args ) \ debug_print_msg( ssl, level, __FILE__, __LINE__, debug_fmt args ); @@ -100,6 +92,24 @@ void debug_set_log_mode( int log_mode ); extern "C" { #endif +/** + * \brief Set the log mode for the debug functions globally + * (Default value: POLARSSL_DEBUG_DFL_MODE) + * + * \param log_mode The log mode to use (POLARSSL_DEBUG_LOG_FULL or + * POLARSSL_DEBUG_LOG_RAW) + */ +void debug_set_log_mode( int log_mode ); + +/** + * \brief Set the level threshold to handle globally. Messages that have a + * level over the threshold value are ignored. + * (Default value: 0 (No debug)) + * + * \param threshold maximum level of messages to pass on + */ +void debug_set_threshold( int threshold ); + char *debug_fmt( const char *format, ... ); void debug_print_msg( const ssl_context *ssl, int level, diff --git a/library/debug.c b/library/debug.c index 40a4399f9..60e39ed37 100644 --- a/library/debug.c +++ b/library/debug.c @@ -47,12 +47,18 @@ #endif /* _MSC_VER */ static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE; +static int debug_threshold = 0; void debug_set_log_mode( int log_mode ) { debug_log_mode = log_mode; } +void debug_set_threshold( int threshold ) +{ + debug_threshold = threshold; +} + char *debug_fmt( const char *format, ... ) { va_list argp; @@ -73,7 +79,7 @@ void debug_print_msg( const ssl_context *ssl, int level, char str[512]; int maxlen = sizeof( str ) - 1; - if( ssl->f_dbg == NULL ) + if( ssl->f_dbg == NULL || level > debug_threshold ) return; if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW ) @@ -95,7 +101,7 @@ void debug_print_ret( const ssl_context *ssl, int level, int maxlen = sizeof( str ) - 1; size_t idx = 0; - if( ssl->f_dbg == NULL ) + if( ssl->f_dbg == NULL || level > debug_threshold ) return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) @@ -115,7 +121,7 @@ void debug_print_buf( const ssl_context *ssl, int level, char str[512]; size_t i, maxlen = sizeof( str ) - 1, idx = 0; - if( ssl->f_dbg == NULL ) + if( ssl->f_dbg == NULL || level > debug_threshold ) return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) @@ -169,6 +175,9 @@ void debug_print_ecp( const ssl_context *ssl, int level, char str[512]; int maxlen = sizeof( str ) - 1; + if( ssl->f_dbg == NULL || level > debug_threshold ) + return; + snprintf( str, maxlen, "%s(X)", text ); str[maxlen] = '\0'; debug_print_mpi( ssl, level, file, line, str, &X->X ); @@ -188,7 +197,7 @@ void debug_print_mpi( const ssl_context *ssl, int level, int j, k, maxlen = sizeof( str ) - 1, zeros = 1; size_t i, n, idx = 0; - if( ssl->f_dbg == NULL || X == NULL ) + if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold ) return; for( n = X->n - 1; n > 0; n-- ) @@ -301,7 +310,7 @@ void debug_print_crt( const ssl_context *ssl, int level, char str[1024], prefix[64]; int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0; - if( ssl->f_dbg == NULL || crt == NULL ) + if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold ) return; if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c index 270bb6217..a61ae64d9 100644 --- a/programs/ssl/ssl_client1.c +++ b/programs/ssl/ssl_client1.c @@ -29,6 +29,7 @@ #include #include "polarssl/net.h" +#include "polarssl/debug.h" #include "polarssl/ssl.h" #include "polarssl/entropy.h" #include "polarssl/ctr_drbg.h" @@ -61,11 +62,10 @@ int main( int argc, char *argv[] ) static void my_debug( void *ctx, int level, const char *str ) { - if( level < DEBUG_LEVEL ) - { - fprintf( (FILE *) ctx, "%s", str ); - fflush( (FILE *) ctx ); - } + ((void) level); + + fprintf( (FILE *) ctx, "%s", str ); + fflush( (FILE *) ctx ); } int main( int argc, char *argv[] ) @@ -82,6 +82,10 @@ int main( int argc, char *argv[] ) ((void) argc); ((void) argv); +#if defined(POLARSSL_DEBUG_C) + debug_set_threshold( DEBUG_LEVEL ); +#endif + /* * 0. Initialize the RNG and the session data */ diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index 7c043eb35..012403a40 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -36,6 +36,7 @@ #include "polarssl/certs.h" #include "polarssl/x509.h" #include "polarssl/error.h" +#include "polarssl/debug.h" #if defined(POLARSSL_TIMING_C) #include "polarssl/timing.h" @@ -112,11 +113,10 @@ struct options static void my_debug( void *ctx, int level, const char *str ) { - if( level < opt.debug_level ) - { - fprintf( (FILE *) ctx, "%s", str ); - fflush( (FILE *) ctx ); - } + ((void) level); + + fprintf( (FILE *) ctx, "%s", str ); + fflush( (FILE *) ctx ); } /* @@ -589,6 +589,10 @@ int main( int argc, char *argv[] ) goto usage; } +#if defined(POLARSSL_DEBUG_C) + debug_set_threshold( opt.debug_level ); +#endif + if( opt.force_ciphersuite[0] > 0 ) { const ssl_ciphersuite_t *ciphersuite_info; diff --git a/programs/ssl/ssl_server.c b/programs/ssl/ssl_server.c index 49c3965b1..5b86f9ba1 100644 --- a/programs/ssl/ssl_server.c +++ b/programs/ssl/ssl_server.c @@ -40,6 +40,7 @@ #include "polarssl/ssl.h" #include "polarssl/net.h" #include "polarssl/error.h" +#include "polarssl/debug.h" #if defined(POLARSSL_SSL_CACHE_C) #include "polarssl/ssl_cache.h" @@ -73,11 +74,10 @@ int main( int argc, char *argv[] ) static void my_debug( void *ctx, int level, const char *str ) { - if( level < DEBUG_LEVEL ) - { - fprintf( (FILE *) ctx, "%s", str ); - fflush( (FILE *) ctx ); - } + ((void) level); + + fprintf( (FILE *) ctx, "%s", str ); + fflush( (FILE *) ctx ); } int main( int argc, char *argv[] ) @@ -108,6 +108,10 @@ int main( int argc, char *argv[] ) pk_init( &pkey ); entropy_init( &entropy ); +#if defined(POLARSSL_DEBUG_C) + debug_set_threshold( DEBUG_LEVEL ); +#endif + /* * 1. Load the certificates and private RSA key */ diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index c578c5b0d..44f04a41d 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -51,6 +51,7 @@ #include "polarssl/certs.h" #include "polarssl/x509.h" #include "polarssl/error.h" +#include "polarssl/debug.h" #if defined(POLARSSL_SSL_CACHE_C) #include "polarssl/ssl_cache.h" @@ -137,11 +138,10 @@ struct options static void my_debug( void *ctx, int level, const char *str ) { - if( level < opt.debug_level ) - { - fprintf( (FILE *) ctx, "%s", str ); - fflush( (FILE *) ctx ); - } + ((void) level); + + fprintf( (FILE *) ctx, "%s", str ); + fflush( (FILE *) ctx ); } /* @@ -721,6 +721,10 @@ int main( int argc, char *argv[] ) goto usage; } +#if defined(POLARSSL_DEBUG_C) + debug_set_threshold( opt.debug_level ); +#endif + if( opt.force_ciphersuite[0] > 0 ) { const ssl_ciphersuite_t *ciphersuite_info; diff --git a/tests/suites/test_suite_debug.data b/tests/suites/test_suite_debug.data index d51445d92..9b49f6a48 100644 --- a/tests/suites/test_suite_debug.data +++ b/tests/suites/test_suite_debug.data @@ -1,3 +1,18 @@ +Debug print msg (threshold 1, level 0) +debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n" + +Debug print msg (threshold 1, level 1) +debug_print_msg_threshold:1:1:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n" + +Debug print msg (threshold 1, level 2) +debug_print_msg_threshold:1:2:"MyFile":999:"" + +Debug print msg (threshold 0, level 1) +debug_print_msg_threshold:0:1:"MyFile":999:"" + +Debug print msg (threshold 0, level 5) +debug_print_msg_threshold:0:5:"MyFile":999:"" + Debug print return value #1 debug_print_ret:POLARSSL_DEBUG_LOG_FULL:"MyFile":999:"Test return value":0:"MyFile(0999)\: Test return value() returned 0 (-0x0000)\n" diff --git a/tests/suites/test_suite_debug.function b/tests/suites/test_suite_debug.function index 3a5adaf9a..7717a61a7 100644 --- a/tests/suites/test_suite_debug.function +++ b/tests/suites/test_suite_debug.function @@ -29,6 +29,28 @@ void string_debug(void *data, int level, const char *str) * END_DEPENDENCIES */ +/* BEGIN_CASE */ +void debug_print_msg_threshold( int threshold, int level, char *file, int line, + char *result_str ) +{ + ssl_context ssl; + struct buffer_data buffer; + + memset( &ssl, 0, sizeof( ssl_context ) ); + memset( buffer.buf, 0, 2000 ); + buffer.ptr = buffer.buf; + + debug_set_log_mode( POLARSSL_DEBUG_LOG_FULL ); + debug_set_threshold( threshold ); + ssl_set_dbg(&ssl, string_debug, &buffer); + + debug_print_msg( &ssl, level, file, line, + debug_fmt("Text message, 2 == %d", 2 ) ); + + TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 ); +} +/* END_CASE */ + /* BEGIN_CASE */ void debug_print_ret( int mode, char *file, int line, char *text, int value, char *result_str )