• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file debug.h
3  *
4  * \brief Functions for controlling and providing debug output from the library.
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_DEBUG_H
11 #define MBEDTLS_DEBUG_H
12 
13 #include "mbedtls/build_info.h"
14 
15 #include "mbedtls/ssl.h"
16 
17 #if defined(MBEDTLS_ECP_C)
18 #include "mbedtls/ecp.h"
19 #endif
20 
21 #if defined(MBEDTLS_DEBUG_C)
22 
23 #define MBEDTLS_DEBUG_STRIP_PARENS(...)   __VA_ARGS__
24 
25 #define MBEDTLS_SSL_DEBUG_MSG(level, args)                    \
26     mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__,    \
27                             MBEDTLS_DEBUG_STRIP_PARENS args)
28 
29 #define MBEDTLS_SSL_DEBUG_RET(level, text, ret)                \
30     mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
31 
32 #define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len)           \
33     mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
34 
35 #if defined(MBEDTLS_BIGNUM_C)
36 #define MBEDTLS_SSL_DEBUG_MPI(level, text, X)                  \
37     mbedtls_debug_print_mpi(ssl, level, __FILE__, __LINE__, text, X)
38 #endif
39 
40 #if defined(MBEDTLS_ECP_C)
41 #define MBEDTLS_SSL_DEBUG_ECP(level, text, X)                  \
42     mbedtls_debug_print_ecp(ssl, level, __FILE__, __LINE__, text, X)
43 #endif
44 
45 #if defined(MBEDTLS_X509_CRT_PARSE_C)
46 #if !defined(MBEDTLS_X509_REMOVE_INFO)
47 #define MBEDTLS_SSL_DEBUG_CRT(level, text, crt)                \
48     mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
49 #else
50 #define MBEDTLS_SSL_DEBUG_CRT(level, text, crt)       do { } while (0)
51 #endif /* MBEDTLS_X509_REMOVE_INFO */
52 #endif /* MBEDTLS_X509_CRT_PARSE_C */
53 
54 #if defined(MBEDTLS_ECDH_C)
55 #define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr)               \
56     mbedtls_debug_printf_ecdh(ssl, level, __FILE__, __LINE__, ecdh, attr)
57 #endif
58 
59 #else /* MBEDTLS_DEBUG_C */
60 
61 #define MBEDTLS_SSL_DEBUG_MSG(level, args)            do { } while (0)
62 #define MBEDTLS_SSL_DEBUG_RET(level, text, ret)       do { } while (0)
63 #define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len)  do { } while (0)
64 #define MBEDTLS_SSL_DEBUG_MPI(level, text, X)         do { } while (0)
65 #define MBEDTLS_SSL_DEBUG_ECP(level, text, X)         do { } while (0)
66 #define MBEDTLS_SSL_DEBUG_CRT(level, text, crt)       do { } while (0)
67 #define MBEDTLS_SSL_DEBUG_ECDH(level, ecdh, attr)     do { } while (0)
68 
69 #endif /* MBEDTLS_DEBUG_C */
70 
71 /**
72  * \def MBEDTLS_PRINTF_ATTRIBUTE
73  *
74  * Mark a function as having printf attributes, and thus enable checking
75  * via -wFormat and other flags. This does nothing on builds with compilers
76  * that do not support the format attribute
77  *
78  * Module:  library/debug.c
79  * Caller:
80  *
81  * This module provides debugging functions.
82  */
83 #if defined(__has_attribute)
84 #if __has_attribute(format)
85 #if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
86 #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)    \
87     __attribute__((__format__(gnu_printf, string_index, first_to_check)))
88 #else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
89 #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)    \
90     __attribute__((format(printf, string_index, first_to_check)))
91 #endif
92 #else /* __has_attribute(format) */
93 #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
94 #endif /* __has_attribute(format) */
95 #else /* defined(__has_attribute) */
96 #define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
97 #endif
98 
99 /**
100  * \def MBEDTLS_PRINTF_SIZET
101  *
102  * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
103  * and MinGW we need to define the printf specifier for size_t
104  * and long long per platform.
105  *
106  * Module:  library/debug.c
107  * Caller:
108  *
109  * This module provides debugging functions.
110  */
111 #if (defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800)
112    #include <inttypes.h>
113    #define MBEDTLS_PRINTF_SIZET     PRIuPTR
114    #define MBEDTLS_PRINTF_LONGLONG  "I64d"
115 #else \
116     /* (defined(__MINGW32__)  && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
117    #define MBEDTLS_PRINTF_SIZET     "zu"
118    #define MBEDTLS_PRINTF_LONGLONG  "lld"
119 #endif \
120     /* (defined(__MINGW32__)  && __USE_MINGW_ANSI_STDIO == 0) || (defined(_MSC_VER) && _MSC_VER < 1800) */
121 
122 #if !defined(MBEDTLS_PRINTF_MS_TIME)
123 #include <inttypes.h>
124 #if !defined(PRId64)
125 #define MBEDTLS_PRINTF_MS_TIME MBEDTLS_PRINTF_LONGLONG
126 #else
127 #define MBEDTLS_PRINTF_MS_TIME PRId64
128 #endif
129 #endif /* MBEDTLS_PRINTF_MS_TIME */
130 
131 #ifdef __cplusplus
132 extern "C" {
133 #endif
134 
135 /**
136  * \brief   Set the threshold error level to handle globally all debug output.
137  *          Debug messages that have a level over the threshold value are
138  *          discarded.
139  *          (Default value: 0 = No debug )
140  *
141  * \param threshold     threshold level of messages to filter on. Messages at a
142  *                      higher level will be discarded.
143  *                          - Debug levels
144  *                              - 0 No debug
145  *                              - 1 Error
146  *                              - 2 State change
147  *                              - 3 Informational
148  *                              - 4 Verbose
149  */
150 void mbedtls_debug_set_threshold(int threshold);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif /* MBEDTLS_DEBUG_H */
157