blob: bdfc597e0cb8aa83ee9bb5202e31cb33559f7253 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file debug.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Simon Butcher7ef5cf32016-02-13 22:20:04 +00004 * \brief Functions for controlling and providing debug output from the library.
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_DEBUG_H
11#define MBEDTLS_DEBUG_H
Paul Bakker5121ce52009-01-03 21:22:43 +000012
Bence Szépkútic662b362021-05-27 11:25:03 +020013#include "mbedtls/build_info.h"
Rich Evans00ab4702015-02-06 13:43:58 +000014
Jaeden Amero6609aef2019-07-04 20:01:14 +010015#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000018
Gilles Peskine449bd832023-01-11 14:50:10 +010019#define MBEDTLS_DEBUG_STRIP_PARENS(...) __VA_ARGS__
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020020
Gilles Peskine449bd832023-01-11 14:50:10 +010021#define MBEDTLS_SSL_DEBUG_MSG(level, args) \
22 mbedtls_debug_print_msg(ssl, level, __FILE__, __LINE__, \
23 MBEDTLS_DEBUG_STRIP_PARENS args)
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Gilles Peskine449bd832023-01-11 14:50:10 +010025#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) \
26 mbedtls_debug_print_ret(ssl, level, __FILE__, __LINE__, text, ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Gilles Peskine449bd832023-01-11 14:50:10 +010028#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) \
29 mbedtls_debug_print_buf(ssl, level, __FILE__, __LINE__, text, buf, len)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker612a2f12020-10-09 09:19:39 +010032#if !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +010033#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) \
34 mbedtls_debug_print_crt(ssl, level, __FILE__, __LINE__, text, crt)
Peter Kolbus9a969b62018-12-11 13:55:56 -060035#else
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
Hanno Becker612a2f12020-10-09 09:19:39 +010037#endif /* MBEDTLS_X509_REMOVE_INFO */
Peter Kolbus9a969b62018-12-11 13:55:56 -060038#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#else /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Gilles Peskine449bd832023-01-11 14:50:10 +010042#define MBEDTLS_SSL_DEBUG_MSG(level, args) do { } while (0)
43#define MBEDTLS_SSL_DEBUG_RET(level, text, ret) do { } while (0)
44#define MBEDTLS_SSL_DEBUG_BUF(level, text, buf, len) do { } while (0)
Gilles Peskine449bd832023-01-11 14:50:10 +010045#define MBEDTLS_SSL_DEBUG_ECP(level, text, X) do { } while (0)
46#define MBEDTLS_SSL_DEBUG_CRT(level, text, crt) do { } while (0)
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#endif /* MBEDTLS_DEBUG_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Elliott4e589702020-12-09 14:38:01 +000050/**
51 * \def MBEDTLS_PRINTF_ATTRIBUTE
52 *
53 * Mark a function as having printf attributes, and thus enable checking
Paul Elliottaa5e1322021-03-05 15:52:25 +000054 * via -wFormat and other flags. This does nothing on builds with compilers
55 * that do not support the format attribute
Paul Elliott4e589702020-12-09 14:38:01 +000056 *
57 * Module: library/debug.c
58 * Caller:
59 *
60 * This module provides debugging functions.
61 */
Paul Elliottaa5e1322021-03-05 15:52:25 +000062#if defined(__has_attribute)
63#if __has_attribute(format)
eugenee42a50d2021-05-12 12:33:36 -040064#if defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1
Paul Elliott4e589702020-12-09 14:38:01 +000065#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
Gilles Peskine449bd832023-01-11 14:50:10 +010066 __attribute__((__format__(gnu_printf, string_index, first_to_check)))
eugenee42a50d2021-05-12 12:33:36 -040067#else /* defined(__MINGW32__) && __USE_MINGW_ANSI_STDIO == 1 */
68#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check) \
69 __attribute__((format(printf, string_index, first_to_check)))
70#endif
Paul Elliottaa5e1322021-03-05 15:52:25 +000071#else /* __has_attribute(format) */
72#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
73#endif /* __has_attribute(format) */
74#else /* defined(__has_attribute) */
Paul Elliottabb3af72020-12-18 16:43:22 +000075#define MBEDTLS_PRINTF_ATTRIBUTE(string_index, first_to_check)
Paul Elliott4e589702020-12-09 14:38:01 +000076#endif
77
Paul Elliottd48d5c62021-01-07 14:47:05 +000078/**
79 * \def MBEDTLS_PRINTF_SIZET
80 *
81 * MBEDTLS_PRINTF_xxx: Due to issues with older window compilers
82 * and MinGW we need to define the printf specifier for size_t
83 * and long long per platform.
84 *
85 * Module: library/debug.c
86 * Caller:
87 *
88 * This module provides debugging functions.
89 */
Bence Szépkúticd1ece72025-03-07 17:22:40 +010090#if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900)
Paul Elliott48438c72021-01-08 17:05:25 +000091 #include <inttypes.h>
92 #define MBEDTLS_PRINTF_SIZET PRIuPTR
93 #define MBEDTLS_PRINTF_LONGLONG "I64d"
Gilles Peskine449bd832023-01-11 14:50:10 +010094#else \
Bence Szépkúti011b6cb2025-03-08 01:02:37 +010095 /* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
Paul Elliottd48d5c62021-01-07 14:47:05 +000096 #define MBEDTLS_PRINTF_SIZET "zu"
97 #define MBEDTLS_PRINTF_LONGLONG "lld"
Gilles Peskine449bd832023-01-11 14:50:10 +010098#endif \
Bence Szépkúti011b6cb2025-03-08 01:02:37 +010099 /* defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER < 1900) */
Paul Elliottd48d5c62021-01-07 14:47:05 +0000100
Jerry Yueba0ab52022-12-15 17:41:41 +0800101#if !defined(MBEDTLS_PRINTF_MS_TIME)
Jerry Yu03511b02023-02-27 10:48:41 +0800102#include <inttypes.h>
103#if !defined(PRId64)
104#define MBEDTLS_PRINTF_MS_TIME MBEDTLS_PRINTF_LONGLONG
105#else
Jerry Yu041c8c12023-02-03 13:15:09 +0800106#define MBEDTLS_PRINTF_MS_TIME PRId64
Jerry Yu03511b02023-02-27 10:48:41 +0800107#endif
Jerry Yueba0ab52022-12-15 17:41:41 +0800108#endif /* MBEDTLS_PRINTF_MS_TIME */
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110#ifdef __cplusplus
111extern "C" {
112#endif
113
Paul Bakkerc73079a2014-04-25 16:34:30 +0200114/**
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000115 * \brief Set the threshold error level to handle globally all debug output.
116 * Debug messages that have a level over the threshold value are
117 * discarded.
118 * (Default value: 0 = No debug )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200119 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800120 * \param threshold threshold level of messages to filter on. Messages at a
Simon Butcher7ef5cf32016-02-13 22:20:04 +0000121 * higher level will be discarded.
122 * - Debug levels
123 * - 0 No debug
124 * - 1 Error
125 * - 2 State change
126 * - 3 Informational
127 * - 4 Verbose
Paul Bakkerc73079a2014-04-25 16:34:30 +0200128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129void mbedtls_debug_set_threshold(int threshold);
Paul Bakkerc73079a2014-04-25 16:34:30 +0200130
Paul Bakker5121ce52009-01-03 21:22:43 +0000131#ifdef __cplusplus
132}
133#endif
134
Valerio Settib4f50762024-01-17 10:24:52 +0100135#endif /* MBEDTLS_DEBUG_H */