2021-01-12 07:54:04 +01:00
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-03-05 06:09:37 +01:00
|
|
|
* \file mps_trace.h
|
2021-01-12 07:54:04 +01:00
|
|
|
*
|
|
|
|
* \brief Tracing module for MPS
|
|
|
|
*/
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
|
|
|
|
#define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
|
2021-01-12 07:54:04 +01:00
|
|
|
|
|
|
|
#include "common.h"
|
2021-01-28 08:08:08 +01:00
|
|
|
#include "mps_common.h"
|
|
|
|
#include "mps_trace.h"
|
2021-01-12 07:54:04 +01:00
|
|
|
|
2021-01-12 10:46:03 +01:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
#define mbedtls_printf printf
|
|
|
|
#define mbedtls_vsnprintf vsnprintf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_ENABLE_TRACE)
|
2021-01-12 07:54:04 +01:00
|
|
|
|
2021-01-12 10:46:03 +01:00
|
|
|
/*
|
|
|
|
* Adapt this to enable/disable tracing output
|
|
|
|
* from the various layers of the MPS.
|
|
|
|
*/
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1
|
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2
|
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3
|
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4
|
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_READER
|
|
|
|
#define MBEDTLS_MPS_TRACE_ENABLE_WRITER
|
2021-01-12 10:46:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To use the existing trace module, only change
|
2021-01-28 10:02:18 +01:00
|
|
|
* MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the
|
2021-01-12 10:46:03 +01:00
|
|
|
* rest of this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2021-01-28 10:02:18 +01:00
|
|
|
mbedtls_mps_trace_comment,
|
|
|
|
mbedtls_mps_trace_call,
|
|
|
|
mbedtls_mps_trace_error,
|
|
|
|
mbedtls_mps_trace_return
|
|
|
|
} mbedtls_mps_trace_type;
|
|
|
|
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_WRITER 5
|
|
|
|
#define MBEDTLS_MPS_TRACE_BIT_READER 6
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1 )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2 )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3 )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4 )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_READER 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
|
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER )
|
2021-01-12 10:46:03 +01:00
|
|
|
#else
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK_WRITER 0
|
2021-01-12 10:46:03 +01:00
|
|
|
#endif
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_MASK ( MBEDTLS_MPS_TRACE_MASK_LAYER_1 | \
|
|
|
|
MBEDTLS_MPS_TRACE_MASK_LAYER_2 | \
|
|
|
|
MBEDTLS_MPS_TRACE_MASK_LAYER_3 | \
|
|
|
|
MBEDTLS_MPS_TRACE_MASK_LAYER_4 | \
|
|
|
|
MBEDTLS_MPS_TRACE_MASK_READER | \
|
|
|
|
MBEDTLS_MPS_TRACE_MASK_WRITER )
|
2021-01-12 10:46:03 +01:00
|
|
|
|
|
|
|
/* We have to avoid globals because E-ACSL chokes on them...
|
|
|
|
* Wrap everything in stub functions. */
|
2021-01-28 10:02:18 +01:00
|
|
|
int mbedtls_mps_trace_get_depth( void );
|
|
|
|
void mbedtls_mps_trace_inc_depth( void );
|
|
|
|
void mbedtls_mps_trace_dec_depth( void );
|
|
|
|
|
|
|
|
void mbedtls_mps_trace_color( int id );
|
|
|
|
void mbedtls_mps_trace_indent( int level, mbedtls_mps_trace_type ty );
|
|
|
|
|
|
|
|
void mbedtls_mps_trace_print_msg( int id, int line, const char *format, ... );
|
|
|
|
|
|
|
|
#define MBEDTLS_MPS_TRACE( type, ... ) \
|
|
|
|
do { \
|
|
|
|
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
|
|
|
break; \
|
|
|
|
mbedtls_mps_trace_indent( mbedtls_mps_trace_get_depth(), type ); \
|
|
|
|
mbedtls_mps_trace_color( mbedtls_mps_trace_id ); \
|
|
|
|
mbedtls_mps_trace_print_msg( mbedtls_mps_trace_id, __LINE__, __VA_ARGS__ ); \
|
|
|
|
mbedtls_mps_trace_color( 0 ); \
|
2021-01-12 10:46:03 +01:00
|
|
|
} while( 0 )
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_INIT( ... ) \
|
|
|
|
do { \
|
|
|
|
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
|
|
|
break; \
|
|
|
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_call, __VA_ARGS__ ); \
|
|
|
|
mbedtls_mps_trace_inc_depth(); \
|
2021-01-12 10:46:03 +01:00
|
|
|
} while( 0 )
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_END( val ) \
|
|
|
|
do { \
|
|
|
|
if( ! ( MBEDTLS_MPS_TRACE_MASK & ( 1u << mbedtls_mps_trace_id ) ) ) \
|
|
|
|
break; \
|
|
|
|
MBEDTLS_MPS_TRACE( mbedtls_mps_trace_return, "%d (-%#04x)", \
|
|
|
|
(int) (val), -((unsigned)(val)) ); \
|
|
|
|
mbedtls_mps_trace_dec_depth(); \
|
2021-01-12 10:46:03 +01:00
|
|
|
} while( 0 )
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_RETURN( val ) \
|
2021-01-12 10:46:03 +01:00
|
|
|
do { \
|
|
|
|
/* Breaks tail recursion. */ \
|
|
|
|
int ret__ = val; \
|
2021-01-28 10:02:18 +01:00
|
|
|
MBEDTLS_MPS_TRACE_END( ret__ ); \
|
2021-01-12 10:46:03 +01:00
|
|
|
return( ret__ ); \
|
|
|
|
} while( 0 )
|
2021-01-12 07:54:04 +01:00
|
|
|
|
|
|
|
#else /* MBEDTLS_MPS_TRACE */
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE( type, ... ) do { } while( 0 )
|
|
|
|
#define MBEDTLS_MPS_TRACE_INIT( ... ) do { } while( 0 )
|
|
|
|
#define MBEDTLS_MPS_TRACE_END do { } while( 0 )
|
2021-01-12 07:54:04 +01:00
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#define MBEDTLS_MPS_TRACE_RETURN( val ) return( val );
|
2021-01-12 07:54:04 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_MPS_TRACE */
|
|
|
|
|
2021-01-28 10:02:18 +01:00
|
|
|
#endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */
|