1 /*
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
22 #include "common.h"
23
24 #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
25
26 #include "mps_common.h"
27
28 #if defined(MBEDTLS_MPS_ENABLE_TRACE)
29
30 #include "mps_trace.h"
31 #include <stdarg.h>
32
33 static int trace_depth = 0;
34
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
44 static 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
mbedtls_mps_trace_print_msg(int id,int line,const char * format,...)57 void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
58 {
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
mbedtls_mps_trace_get_depth()73 int mbedtls_mps_trace_get_depth()
74 {
75 return trace_depth;
76 }
mbedtls_mps_trace_dec_depth()77 void mbedtls_mps_trace_dec_depth()
78 {
79 trace_depth--;
80 }
mbedtls_mps_trace_inc_depth()81 void mbedtls_mps_trace_inc_depth()
82 {
83 trace_depth++;
84 }
85
mbedtls_mps_trace_color(int id)86 void mbedtls_mps_trace_color( int id )
87 {
88 if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
89 return;
90 printf( "%s", colors[ id ] );
91 }
92
mbedtls_mps_trace_indent(int level,mbedtls_mps_trace_type ty)93 void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
94 {
95 if( level > 0 )
96 {
97 while( --level )
98 printf( "| " );
99
100 printf( "| " );
101 }
102
103 switch( ty )
104 {
105 case MBEDTLS_MPS_TRACE_TYPE_COMMENT:
106 mbedtls_printf( "@ " );
107 break;
108
109 case MBEDTLS_MPS_TRACE_TYPE_CALL:
110 mbedtls_printf( "+--> " );
111 break;
112
113 case MBEDTLS_MPS_TRACE_TYPE_ERROR:
114 mbedtls_printf( "E " );
115 break;
116
117 case MBEDTLS_MPS_TRACE_TYPE_RETURN:
118 mbedtls_printf( "< " );
119 break;
120
121 default:
122 break;
123 }
124 }
125
126 #endif /* MBEDTLS_MPS_ENABLE_TRACE */
127 #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
128