Avoid static buffer in debug module

Caused issues in threading situations
diff --git a/include/mbedtls/debug.h b/include/mbedtls/debug.h
index 6f52a9c..99ea111 100644
--- a/include/mbedtls/debug.h
+++ b/include/mbedtls/debug.h
@@ -57,7 +57,7 @@
 
 
 #define MBEDTLS_SSL_DEBUG_MSG( level, args )                    \
-    mbedtls_debug_print_msg( ssl, level, __FILE__, __LINE__, mbedtls_debug_fmt args )
+    mbedtls_debug_print_msg_free( ssl, level, __FILE__, __LINE__, mbedtls_debug_fmt args )
 
 #define MBEDTLS_SSL_DEBUG_RET( level, text, ret )                \
     mbedtls_debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret )
@@ -118,6 +118,9 @@
 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line, const char *text );
 
+void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level,
+                      const char *file, int line, char *text );
+
 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line,
                       const char *text, int ret );
diff --git a/library/debug.c b/library/debug.c
index 0aeb0e4..195e0ed 100644
--- a/library/debug.c
+++ b/library/debug.c
@@ -37,9 +37,14 @@
 #if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
-#define mbedtls_snprintf snprintf
+#include <stdlib.h>
+#define mbedtls_calloc      calloc
+#define mbedtls_free        free
+#define mbedtls_snprintf    snprintf
 #endif
 
+#define DEBUG_BUF_SIZE      512
+
 static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
 static int debug_threshold = 0;
 
@@ -56,23 +61,35 @@
 char *mbedtls_debug_fmt( const char *format, ... )
 {
     va_list argp;
-    static char str[512];
+    char *str = mbedtls_calloc( DEBUG_BUF_SIZE, 1 );
+
+    if( str == NULL )
+        return( NULL );
 
     va_start( argp, format );
 #if defined(_WIN32)
-    _vsnprintf_s( str, sizeof( str ), _TRUNCATE, format, argp );
+    _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
 #else
-    vsnprintf( str, sizeof( str ), format, argp );
+    vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
 #endif
     va_end( argp );
 
     return( str );
 }
 
+void mbedtls_debug_print_msg_free( const mbedtls_ssl_context *ssl, int level,
+                      const char *file, int line, char *text )
+{
+    if( text != NULL )
+        mbedtls_debug_print_msg( ssl, level, file, line, text );
+
+    mbedtls_free( text );
+}
+
 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line, const char *text )
 {
-    char str[512];
+    char str[DEBUG_BUF_SIZE];
 
     if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
         return;
@@ -91,7 +108,7 @@
                       const char *file, int line,
                       const char *text, int ret )
 {
-    char str[512];
+    char str[DEBUG_BUF_SIZE];
     size_t idx = 0;
 
     if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
@@ -118,7 +135,7 @@
                       const char *file, int line, const char *text,
                       const unsigned char *buf, size_t len )
 {
-    char str[512];
+    char str[DEBUG_BUF_SIZE];
     char txt[17];
     size_t i, idx = 0;
 
@@ -179,7 +196,7 @@
                       const char *file, int line,
                       const char *text, const mbedtls_ecp_point *X )
 {
-    char str[512];
+    char str[DEBUG_BUF_SIZE];
 
     if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
         return;
@@ -197,7 +214,7 @@
                       const char *file, int line,
                       const char *text, const mbedtls_mpi *X )
 {
-    char str[512];
+    char str[DEBUG_BUF_SIZE];
     int j, k, zeros = 1;
     size_t i, n, idx = 0;
 
diff --git a/tests/suites/test_suite_debug.function b/tests/suites/test_suite_debug.function
index 5c8993f..6adcc10 100644
--- a/tests/suites/test_suite_debug.function
+++ b/tests/suites/test_suite_debug.function
@@ -48,7 +48,7 @@
     mbedtls_debug_set_threshold( threshold );
     mbedtls_ssl_conf_dbg( &conf, string_debug, &buffer);
 
-    mbedtls_debug_print_msg( &ssl, level, file, line,
+    mbedtls_debug_print_msg_free( &ssl, level, file, line,
                      mbedtls_debug_fmt("Text message, 2 == %d", 2 ) );
 
     TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );