1 /* 2 * Copyright The Mbed TLS Contributors 3 * SPDX-License-Identifier: Apache-2.0 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may 6 * not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 * This file is part of mbed TLS (https://tls.mbed.org) 18 */ 19 20 /** 21 * \file mps_trace.h 22 * 23 * \brief Tracing module for MPS 24 */ 25 26 #ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H 27 #define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H 28 29 #include "common.h" 30 #include "mps_common.h" 31 #include "mps_trace.h" 32 33 #if defined(MBEDTLS_PLATFORM_C) 34 #include "mbedtls/platform.h" 35 #else 36 #include <stdio.h> 37 #define mbedtls_printf printf 38 #define mbedtls_vsnprintf vsnprintf 39 #endif /* MBEDTLS_PLATFORM_C */ 40 41 #if defined(MBEDTLS_MPS_ENABLE_TRACE) 42 43 /* 44 * Adapt this to enable/disable tracing output 45 * from the various layers of the MPS. 46 */ 47 48 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1 49 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2 50 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3 51 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4 52 #define MBEDTLS_MPS_TRACE_ENABLE_READER 53 #define MBEDTLS_MPS_TRACE_ENABLE_WRITER 54 55 /* 56 * To use the existing trace module, only change 57 * MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the 58 * rest of this file. 59 */ 60 61 typedef enum 62 { 63 MBEDTLS_MPS_TRACE_TYPE_COMMENT, 64 MBEDTLS_MPS_TRACE_TYPE_CALL, 65 MBEDTLS_MPS_TRACE_TYPE_ERROR, 66 MBEDTLS_MPS_TRACE_TYPE_RETURN 67 } mbedtls_mps_trace_type; 68 69 #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1 70 #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2 71 #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3 72 #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4 73 #define MBEDTLS_MPS_TRACE_BIT_WRITER 5 74 #define MBEDTLS_MPS_TRACE_BIT_READER 6 75 76 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1) 77 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 ) 78 #else 79 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0 80 #endif 81 82 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2) 83 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 ) 84 #else 85 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0 86 #endif 87 88 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3) 89 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 ) 90 #else 91 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0 92 #endif 93 94 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4) 95 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 ) 96 #else 97 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0 98 #endif 99 100 #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER) 101 #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER ) 102 #else 103 #define MBEDTLS_MPS_TRACE_MASK_READER 0 104 #endif 105 106 #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER) 107 #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER ) 108 #else 109 #define MBEDTLS_MPS_TRACE_MASK_WRITER 0 110 #endif 111 112 #define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \ 113 MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \ 114 MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \ 115 MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \ 116 MBEDTLS_MPS_TRACE_MASK_READER | \ 117 MBEDTLS_MPS_TRACE_MASK_WRITER ) 118 119 /* We have to avoid globals because E-ACSL chokes on them... 120 * Wrap everything in stub functions. */ 121 int mbedtls_mps_trace_get_depth( void ); 122 void mbedtls_mps_trace_inc_depth( void ); 123 void mbedtls_mps_trace_dec_depth( void ); 124 125 void mbedtls_mps_trace_color( int id ); 126 void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty ); 127 128 void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... ); 129 130 #define MBEDTLS_MPS_TRACE( type, ... ) \ 131 do { \ 132 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 133 break; \ 134 mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \ 135 mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \ 136 mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \ 137 mbedtls_mps_trace_color( 0 ); \ 138 } while( 0 ) 139 140 #define MBEDTLS_MPS_TRACE_INIT( ... ) \ 141 do { \ 142 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 143 break; \ 144 MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_CALL, __VA_ARGS__ ); \ 145 mbedtls_mps_trace_inc_depth(); \ 146 } while( 0 ) 147 148 #define MBEDTLS_MPS_TRACE_END( val ) \ 149 do { \ 150 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 151 break; \ 152 MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_RETURN, "%d (-%#04x)", \ 153 (int) (val), -((unsigned)(val)) ); \ 154 mbedtls_mps_trace_dec_depth(); \ 155 } while( 0 ) 156 157 #define MBEDTLS_MPS_TRACE_RETURN( val ) \ 158 do { \ 159 /* Breaks tail recursion. */ \ 160 int ret__ = val; \ 161 MBEDTLS_MPS_TRACE_END( ret__ ); \ 162 return( ret__ ); \ 163 } while( 0 ) 164 165 #else /* MBEDTLS_MPS_TRACE */ 166 167 #define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 ) 168 #define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 ) 169 #define MBEDTLS_MPS_TRACE_END do { } while( 0 ) 170 171 #define MBEDTLS_MPS_TRACE_RETURN( val ) return( val ); 172 173 #endif /* MBEDTLS_MPS_TRACE */ 174 175 #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */ 176