debug.h 5.0 KB

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