blob: e8b4e1b9e9d491a5fe61d1c0e806c10bf85f3e4f [file] [log] [blame]
Hanno Beckerb9100162021-01-12 09:46:03 +00001/*
2 * Message Processing Stack, Trace module
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of Mbed TLS (https://tls.mbed.org)
20 */
21
Hanno Becker43c8f8c2021-03-05 05:16:45 +000022#include "common.h"
23
Ronald Cron6f135e12021-12-08 16:57:54 +010024#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Becker43c8f8c2021-03-05 05:16:45 +000025
Hanno Beckerc518c3b2021-01-28 07:08:08 +000026#include "mps_common.h"
Hanno Beckerb9100162021-01-12 09:46:03 +000027
Hanno Becker984fbde2021-01-28 09:02:18 +000028#if defined(MBEDTLS_MPS_ENABLE_TRACE)
Hanno Beckerb9100162021-01-12 09:46:03 +000029
Hanno Beckerc518c3b2021-01-28 07:08:08 +000030#include "mps_trace.h"
Hanno Beckerb9100162021-01-12 09:46:03 +000031#include <stdarg.h>
32
Hanno Becker2332f8f2021-02-22 16:58:16 +000033static int trace_depth = 0;
Hanno Beckerb9100162021-01-12 09:46:03 +000034
35#define color_default "\x1B[0m"
36#define color_red "\x1B[1;31m"
37#define color_green "\x1B[1;32m"
38#define color_yellow "\x1B[1;33m"
39#define color_blue "\x1B[1;34m"
40#define color_magenta "\x1B[1;35m"
41#define color_cyan "\x1B[1;36m"
42#define color_white "\x1B[1;37m"
43
44static char const * colors[] =
45{
46 color_default,
47 color_green,
48 color_yellow,
49 color_magenta,
50 color_cyan,
51 color_blue,
52 color_white
53};
54
55#define MPS_TRACE_BUF_SIZE 100
56
Hanno Becker984fbde2021-01-28 09:02:18 +000057void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
Hanno Beckerb9100162021-01-12 09:46:03 +000058{
59 int ret;
60 char str[MPS_TRACE_BUF_SIZE];
61 va_list argp;
62 va_start( argp, format );
63 ret = mbedtls_vsnprintf( str, MPS_TRACE_BUF_SIZE, format, argp );
64 va_end( argp );
65
66 if( ret >= 0 && ret < MPS_TRACE_BUF_SIZE )
67 {
68 str[ret] = '\0';
69 mbedtls_printf( "[%d|L%d]: %s\n", id, line, str );
70 }
71}
72
Hanno Becker984fbde2021-01-28 09:02:18 +000073int mbedtls_mps_trace_get_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000074{
Hanno Becker2332f8f2021-02-22 16:58:16 +000075 return trace_depth;
Hanno Beckerb9100162021-01-12 09:46:03 +000076}
Hanno Becker984fbde2021-01-28 09:02:18 +000077void mbedtls_mps_trace_dec_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000078{
Hanno Becker2332f8f2021-02-22 16:58:16 +000079 trace_depth--;
Hanno Beckerb9100162021-01-12 09:46:03 +000080}
Hanno Becker984fbde2021-01-28 09:02:18 +000081void mbedtls_mps_trace_inc_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000082{
Hanno Becker2332f8f2021-02-22 16:58:16 +000083 trace_depth++;
Hanno Beckerb9100162021-01-12 09:46:03 +000084}
85
Hanno Becker984fbde2021-01-28 09:02:18 +000086void mbedtls_mps_trace_color( int id )
Hanno Beckerb9100162021-01-12 09:46:03 +000087{
88 if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
89 return;
90 printf( "%s", colors[ id ] );
91}
92
Hanno Becker984fbde2021-01-28 09:02:18 +000093void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
Hanno Beckerb9100162021-01-12 09:46:03 +000094{
95 if( level > 0 )
96 {
97 while( --level )
98 printf( "| " );
99
100 printf( "| " );
101 }
102
103 switch( ty )
104 {
Dave Rodgmanb7468252021-04-07 12:44:02 +0100105 case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
Hanno Beckerb9100162021-01-12 09:46:03 +0000106 mbedtls_printf( "@ " );
107 break;
108
Dave Rodgmanb7468252021-04-07 12:44:02 +0100109 case MBEDTLS_MPS_TRACE_TYPE_CALL:
Hanno Beckerb9100162021-01-12 09:46:03 +0000110 mbedtls_printf( "+--> " );
111 break;
112
Dave Rodgmanb7468252021-04-07 12:44:02 +0100113 case MBEDTLS_MPS_TRACE_TYPE_ERROR:
Hanno Beckerb9100162021-01-12 09:46:03 +0000114 mbedtls_printf( "E " );
115 break;
116
Dave Rodgmanb7468252021-04-07 12:44:02 +0100117 case MBEDTLS_MPS_TRACE_TYPE_RETURN:
Hanno Beckerb9100162021-01-12 09:46:03 +0000118 mbedtls_printf( "< " );
119 break;
120
121 default:
122 break;
123 }
124}
125
Hanno Becker984fbde2021-01-28 09:02:18 +0000126#endif /* MBEDTLS_MPS_ENABLE_TRACE */
Ronald Cron6f135e12021-12-08 16:57:54 +0100127#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */