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 #include "mbedtls/platform.h" 34 35 #if defined(MBEDTLS_MPS_ENABLE_TRACE) 36 37 /* 38 * Adapt this to enable/disable tracing output 39 * from the various layers of the MPS. 40 */ 41 42 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1 43 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2 44 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3 45 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4 46 #define MBEDTLS_MPS_TRACE_ENABLE_READER 47 #define MBEDTLS_MPS_TRACE_ENABLE_WRITER 48 49 /* 50 * To use the existing trace module, only change 51 * MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the 52 * rest of this file. 53 */ 54 55 typedef enum 56 { 57 MBEDTLS_MPS_TRACE_TYPE_COMMENT, 58 MBEDTLS_MPS_TRACE_TYPE_CALL, 59 MBEDTLS_MPS_TRACE_TYPE_ERROR, 60 MBEDTLS_MPS_TRACE_TYPE_RETURN 61 } mbedtls_mps_trace_type; 62 63 #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1 64 #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2 65 #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3 66 #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4 67 #define MBEDTLS_MPS_TRACE_BIT_WRITER 5 68 #define MBEDTLS_MPS_TRACE_BIT_READER 6 69 70 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1) 71 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 ) 72 #else 73 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0 74 #endif 75 76 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2) 77 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 ) 78 #else 79 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0 80 #endif 81 82 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3) 83 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 ) 84 #else 85 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0 86 #endif 87 88 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4) 89 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 ) 90 #else 91 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0 92 #endif 93 94 #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER) 95 #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER ) 96 #else 97 #define MBEDTLS_MPS_TRACE_MASK_READER 0 98 #endif 99 100 #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER) 101 #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER ) 102 #else 103 #define MBEDTLS_MPS_TRACE_MASK_WRITER 0 104 #endif 105 106 #define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \ 107 MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \ 108 MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \ 109 MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \ 110 MBEDTLS_MPS_TRACE_MASK_READER | \ 111 MBEDTLS_MPS_TRACE_MASK_WRITER ) 112 113 /* We have to avoid globals because E-ACSL chokes on them... 114 * Wrap everything in stub functions. */ 115 int mbedtls_mps_trace_get_depth( void ); 116 void mbedtls_mps_trace_inc_depth( void ); 117 void mbedtls_mps_trace_dec_depth( void ); 118 119 void mbedtls_mps_trace_color( int id ); 120 void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty ); 121 122 void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... ); 123 124 #define MBEDTLS_MPS_TRACE( type, ... ) \ 125 do { \ 126 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 127 break; \ 128 mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \ 129 mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \ 130 mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \ 131 mbedtls_mps_trace_color( 0 ); \ 132 } while( 0 ) 133 134 #define MBEDTLS_MPS_TRACE_INIT( ... ) \ 135 do { \ 136 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 137 break; \ 138 MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_CALL, __VA_ARGS__ ); \ 139 mbedtls_mps_trace_inc_depth(); \ 140 } while( 0 ) 141 142 #define MBEDTLS_MPS_TRACE_END( val ) \ 143 do { \ 144 if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \ 145 break; \ 146 MBEDTLS_MPS_TRACE( MBEDTLS_MPS_TRACE_TYPE_RETURN, "%d (-%#04x)", \ 147 (int) (val), -((unsigned)(val)) ); \ 148 mbedtls_mps_trace_dec_depth(); \ 149 } while( 0 ) 150 151 #define MBEDTLS_MPS_TRACE_RETURN( val ) \ 152 do { \ 153 /* Breaks tail recursion. */ \ 154 int ret__ = val; \ 155 MBEDTLS_MPS_TRACE_END( ret__ ); \ 156 return( ret__ ); \ 157 } while( 0 ) 158 159 #else /* MBEDTLS_MPS_TRACE */ 160 161 #define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 ) 162 #define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 ) 163 #define MBEDTLS_MPS_TRACE_END do { } while( 0 ) 164 165 #define MBEDTLS_MPS_TRACE_RETURN( val ) return( val ); 166 167 #endif /* MBEDTLS_MPS_TRACE */ 168 169 #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */ 170