2021-01-12 10:46:03 +01:00
|
|
|
/*
|
|
|
|
* Message Processing Stack, Trace module
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2021-01-12 10:46:03 +01:00
|
|
|
*/
|
|
|
|
|
2021-03-05 06:16:45 +01:00
|
|
|
#include "common.h"
|
|
|
|
|
2021-12-08 16:57:54 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-03-05 06:16:45 +01:00
|
|
|
|
2021-01-28 08:08:08 +01:00
|
|
|
#include "mps_common.h"
|
2021-01-12 10:46:03 +01:00
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_ENABLE_TRACE)
|
2021-01-12 10:46:03 +01:00
|
|
|
|
2021-01-28 08:08:08 +01:00
|
|
|
#include "mps_trace.h"
|
2021-01-12 10:46:03 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2021-02-22 17:58:16 +01:00
|
|
|
static int trace_depth = 0;
|
2021-01-12 10:46:03 +01:00
|
|
|
|
|
|
|
#define color_default "\x1B[0m"
|
|
|
|
#define color_red "\x1B[1;31m"
|
|
|
|
#define color_green "\x1B[1;32m"
|
|
|
|
#define color_yellow "\x1B[1;33m"
|
|
|
|
#define color_blue "\x1B[1;34m"
|
|
|
|
#define color_magenta "\x1B[1;35m"
|
|
|
|
#define color_cyan "\x1B[1;36m"
|
|
|
|
#define color_white "\x1B[1;37m"
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static char const *colors[] =
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
|
|
|
color_default,
|
|
|
|
color_green,
|
|
|
|
color_yellow,
|
|
|
|
color_magenta,
|
|
|
|
color_cyan,
|
|
|
|
color_blue,
|
|
|
|
color_white
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MPS_TRACE_BUF_SIZE 100
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...)
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char str[MPS_TRACE_BUF_SIZE];
|
|
|
|
va_list argp;
|
2023-01-11 14:50:10 +01:00
|
|
|
va_start(argp, format);
|
|
|
|
ret = mbedtls_vsnprintf(str, MPS_TRACE_BUF_SIZE, format, argp);
|
|
|
|
va_end(argp);
|
2021-01-12 10:46:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret >= 0 && ret < MPS_TRACE_BUF_SIZE) {
|
2021-01-12 10:46:03 +01:00
|
|
|
str[ret] = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("[%d|L%d]: %s\n", id, line, str);
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
int mbedtls_mps_trace_get_depth()
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
2021-02-22 17:58:16 +01:00
|
|
|
return trace_depth;
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
2021-01-28 10:02:18 +01:00
|
|
|
void mbedtls_mps_trace_dec_depth()
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
2021-02-22 17:58:16 +01:00
|
|
|
trace_depth--;
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
2021-01-28 10:02:18 +01:00
|
|
|
void mbedtls_mps_trace_inc_depth()
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
2021-02-22 17:58:16 +01:00
|
|
|
trace_depth++;
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_trace_color(int id)
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (id > (int) (sizeof(colors) / sizeof(*colors))) {
|
2021-01-12 10:46:03 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
printf("%s", colors[id]);
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty)
|
2021-01-12 10:46:03 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (level > 0) {
|
|
|
|
while (--level) {
|
|
|
|
printf("| ");
|
|
|
|
}
|
2021-01-12 10:46:03 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
printf("| ");
|
2021-01-12 10:46:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ty) {
|
2021-04-07 13:44:02 +02:00
|
|
|
case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("@ ");
|
2021-01-12 10:46:03 +01:00
|
|
|
break;
|
|
|
|
|
2021-04-07 13:44:02 +02:00
|
|
|
case MBEDTLS_MPS_TRACE_TYPE_CALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("+--> ");
|
2021-01-12 10:46:03 +01:00
|
|
|
break;
|
|
|
|
|
2021-04-07 13:44:02 +02:00
|
|
|
case MBEDTLS_MPS_TRACE_TYPE_ERROR:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("E ");
|
2021-01-12 10:46:03 +01:00
|
|
|
break;
|
|
|
|
|
2021-04-07 13:44:02 +02:00
|
|
|
case MBEDTLS_MPS_TRACE_TYPE_RETURN:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_printf("< ");
|
2021-01-12 10:46:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#endif /* MBEDTLS_MPS_ENABLE_TRACE */
|
2021-12-08 16:57:54 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|