Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 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 | |
Hanno Becker | 43c8f8c | 2021-03-05 05:16:45 +0000 | [diff] [blame] | 22 | #include "common.h" |
| 23 | |
| 24 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 25 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 26 | # include "mps_common.h" |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 27 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # if defined(MBEDTLS_MPS_ENABLE_TRACE) |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 29 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 30 | # include "mps_trace.h" |
| 31 | # include <stdarg.h> |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 32 | |
Hanno Becker | 2332f8f | 2021-02-22 16:58:16 +0000 | [diff] [blame] | 33 | static int trace_depth = 0; |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 34 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 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" |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 43 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 44 | static char const *colors[] = { color_default, color_green, color_yellow, |
| 45 | color_magenta, color_cyan, color_blue, |
| 46 | color_white }; |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | # define MPS_TRACE_BUF_SIZE 100 |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 49 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 50 | void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...) |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 51 | { |
| 52 | int ret; |
| 53 | char str[MPS_TRACE_BUF_SIZE]; |
| 54 | va_list argp; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 55 | va_start(argp, format); |
| 56 | ret = mbedtls_vsnprintf(str, MPS_TRACE_BUF_SIZE, format, argp); |
| 57 | va_end(argp); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 58 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | if (ret >= 0 && ret < MPS_TRACE_BUF_SIZE) { |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 60 | str[ret] = '\0'; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 61 | mbedtls_printf("[%d|L%d]: %s\n", id, line, str); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 65 | int mbedtls_mps_trace_get_depth() |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 66 | { |
Hanno Becker | 2332f8f | 2021-02-22 16:58:16 +0000 | [diff] [blame] | 67 | return trace_depth; |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 68 | } |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 69 | void mbedtls_mps_trace_dec_depth() |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 70 | { |
Hanno Becker | 2332f8f | 2021-02-22 16:58:16 +0000 | [diff] [blame] | 71 | trace_depth--; |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 72 | } |
Hanno Becker | 984fbde | 2021-01-28 09:02:18 +0000 | [diff] [blame] | 73 | void mbedtls_mps_trace_inc_depth() |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 74 | { |
Hanno Becker | 2332f8f | 2021-02-22 16:58:16 +0000 | [diff] [blame] | 75 | trace_depth++; |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 78 | void mbedtls_mps_trace_color(int id) |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 79 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 80 | if (id > (int)(sizeof(colors) / sizeof(*colors))) |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 81 | return; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 82 | printf("%s", colors[id]); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 85 | void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty) |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 86 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 87 | if (level > 0) { |
| 88 | while (--level) |
| 89 | printf("| "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 90 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 91 | printf("| "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 94 | switch (ty) { |
Dave Rodgman | b746825 | 2021-04-07 12:44:02 +0100 | [diff] [blame] | 95 | case MBEDTLS_MPS_TRACE_TYPE_COMMENT: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 96 | mbedtls_printf("@ "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 97 | break; |
| 98 | |
Dave Rodgman | b746825 | 2021-04-07 12:44:02 +0100 | [diff] [blame] | 99 | case MBEDTLS_MPS_TRACE_TYPE_CALL: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 100 | mbedtls_printf("+--> "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 101 | break; |
| 102 | |
Dave Rodgman | b746825 | 2021-04-07 12:44:02 +0100 | [diff] [blame] | 103 | case MBEDTLS_MPS_TRACE_TYPE_ERROR: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | mbedtls_printf("E "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 105 | break; |
| 106 | |
Dave Rodgman | b746825 | 2021-04-07 12:44:02 +0100 | [diff] [blame] | 107 | case MBEDTLS_MPS_TRACE_TYPE_RETURN: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | mbedtls_printf("< "); |
Hanno Becker | b910016 | 2021-01-12 09:46:03 +0000 | [diff] [blame] | 109 | break; |
| 110 | |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 116 | # endif /* MBEDTLS_MPS_ENABLE_TRACE */ |
Hanno Becker | 43c8f8c | 2021-03-05 05:16:45 +0000 | [diff] [blame] | 117 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |