blob: ceddffb56963de2a442ca1e79e1b068ba4b49f7c [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 Beckerc518c3b2021-01-28 07:08:08 +000022#include "mps_common.h"
Hanno Beckerb9100162021-01-12 09:46:03 +000023
Hanno Becker984fbde2021-01-28 09:02:18 +000024#if defined(MBEDTLS_MPS_ENABLE_TRACE)
Hanno Beckerb9100162021-01-12 09:46:03 +000025
Hanno Beckerc518c3b2021-01-28 07:08:08 +000026#include "mps_trace.h"
Hanno Beckerb9100162021-01-12 09:46:03 +000027#include <stdarg.h>
28
29static int trace_depth_ = 0;
30
31#define color_default "\x1B[0m"
32#define color_red "\x1B[1;31m"
33#define color_green "\x1B[1;32m"
34#define color_yellow "\x1B[1;33m"
35#define color_blue "\x1B[1;34m"
36#define color_magenta "\x1B[1;35m"
37#define color_cyan "\x1B[1;36m"
38#define color_white "\x1B[1;37m"
39
40static char const * colors[] =
41{
42 color_default,
43 color_green,
44 color_yellow,
45 color_magenta,
46 color_cyan,
47 color_blue,
48 color_white
49};
50
51#define MPS_TRACE_BUF_SIZE 100
52
Hanno Becker984fbde2021-01-28 09:02:18 +000053void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... )
Hanno Beckerb9100162021-01-12 09:46:03 +000054{
55 int ret;
56 char str[MPS_TRACE_BUF_SIZE];
57 va_list argp;
58 va_start( argp, format );
59 ret = mbedtls_vsnprintf( str, MPS_TRACE_BUF_SIZE, format, argp );
60 va_end( argp );
61
62 if( ret >= 0 && ret < MPS_TRACE_BUF_SIZE )
63 {
64 str[ret] = '\0';
65 mbedtls_printf( "[%d|L%d]: %s\n", id, line, str );
66 }
67}
68
Hanno Becker984fbde2021-01-28 09:02:18 +000069int mbedtls_mps_trace_get_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000070{
71 return trace_depth_;
72}
Hanno Becker984fbde2021-01-28 09:02:18 +000073void mbedtls_mps_trace_dec_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000074{
75 trace_depth_--;
76}
Hanno Becker984fbde2021-01-28 09:02:18 +000077void mbedtls_mps_trace_inc_depth()
Hanno Beckerb9100162021-01-12 09:46:03 +000078{
79 trace_depth_++;
80}
81
Hanno Becker984fbde2021-01-28 09:02:18 +000082void mbedtls_mps_trace_color( int id )
Hanno Beckerb9100162021-01-12 09:46:03 +000083{
84 if( id > (int) ( sizeof( colors ) / sizeof( *colors ) ) )
85 return;
86 printf( "%s", colors[ id ] );
87}
88
Hanno Becker984fbde2021-01-28 09:02:18 +000089void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty )
Hanno Beckerb9100162021-01-12 09:46:03 +000090{
91 if( level > 0 )
92 {
93 while( --level )
94 printf( "| " );
95
96 printf( "| " );
97 }
98
99 switch( ty )
100 {
Hanno Becker984fbde2021-01-28 09:02:18 +0000101 case mbedtls_mps_trace_comment:
Hanno Beckerb9100162021-01-12 09:46:03 +0000102 mbedtls_printf( "@ " );
103 break;
104
Hanno Becker984fbde2021-01-28 09:02:18 +0000105 case mbedtls_mps_trace_call:
Hanno Beckerb9100162021-01-12 09:46:03 +0000106 mbedtls_printf( "+--> " );
107 break;
108
Hanno Becker984fbde2021-01-28 09:02:18 +0000109 case mbedtls_mps_trace_error:
Hanno Beckerb9100162021-01-12 09:46:03 +0000110 mbedtls_printf( "E " );
111 break;
112
Hanno Becker984fbde2021-01-28 09:02:18 +0000113 case mbedtls_mps_trace_return:
Hanno Beckerb9100162021-01-12 09:46:03 +0000114 mbedtls_printf( "< " );
115 break;
116
117 default:
118 break;
119 }
120}
121
Hanno Becker984fbde2021-01-28 09:02:18 +0000122#endif /* MBEDTLS_MPS_ENABLE_TRACE */