blob: b1da7ede2d914d81ad2e56ee2f9c59b52cc2f4d9 [file] [log] [blame]
Hanno Beckerc809ff62021-01-12 06:54:04 +00001/*
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 trace.h
22 *
23 * \brief Tracing module for MPS
24 */
25
26#ifndef MBEDTLS_MPS_TRACE_H
27#define MBEDTLS_MPS_TRACE_H
28
29#include "common.h"
30
Hanno Beckerb9100162021-01-12 09:46:03 +000031#include "../common.h"
32
33#include "trace.h"
34#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#include <stdio.h>
38#define mbedtls_printf printf
39#define mbedtls_vsnprintf vsnprintf
40#endif /* MBEDTLS_PLATFORM_C */
41
Hanno Beckerc809ff62021-01-12 06:54:04 +000042#if defined(MBEDTLS_MPS_TRACE)
43
Hanno Beckerb9100162021-01-12 09:46:03 +000044/*
45 * Adapt this to enable/disable tracing output
46 * from the various layers of the MPS.
47 */
48
49#define TRACE_ENABLE_LAYER_1
50#define TRACE_ENABLE_LAYER_2
51#define TRACE_ENABLE_LAYER_3
52#define TRACE_ENABLE_LAYER_4
53#define TRACE_ENABLE_READER
54#define TRACE_ENABLE_WRITER
55
56/*
57 * To use the existing trace module, only change
58 * TRACE_ENABLE_XXX above, but don't modify the
59 * rest of this file.
60 */
61
62typedef enum
63{
64 trace_comment,
65 trace_call,
66 trace_error,
67 trace_return
68} trace_type;
69
70#define TRACE_BIT_LAYER_1 1
71#define TRACE_BIT_LAYER_2 2
72#define TRACE_BIT_LAYER_3 3
73#define TRACE_BIT_LAYER_4 4
74#define TRACE_BIT_WRITER 5
75#define TRACE_BIT_READER 6
76
77#if defined(TRACE_ENABLE_LAYER_1)
78#define TRACE_MASK_LAYER_1 (1u << TRACE_BIT_LAYER_1 )
79#else
80#define TRACE_MASK_LAYER_1 0
81#endif
82
83#if defined(TRACE_ENABLE_LAYER_2)
84#define TRACE_MASK_LAYER_2 (1u << TRACE_BIT_LAYER_2 )
85#else
86#define TRACE_MASK_LAYER_2 0
87#endif
88
89#if defined(TRACE_ENABLE_LAYER_3)
90#define TRACE_MASK_LAYER_3 (1u << TRACE_BIT_LAYER_3 )
91#else
92#define TRACE_MASK_LAYER_3 0
93#endif
94
95#if defined(TRACE_ENABLE_LAYER_4)
96#define TRACE_MASK_LAYER_4 (1u << TRACE_BIT_LAYER_4 )
97#else
98#define TRACE_MASK_LAYER_4 0
99#endif
100
101#if defined(TRACE_ENABLE_READER)
102#define TRACE_MASK_READER (1u << TRACE_BIT_READER )
103#else
104#define TRACE_MASK_READER 0
105#endif
106
107#if defined(TRACE_ENABLE_WRITER)
108#define TRACE_MASK_WRITER (1u << TRACE_BIT_WRITER )
109#else
110#define TRACE_MASK_WRITER 0
111#endif
112
113#define TRACE_MASK ( TRACE_MASK_LAYER_1 | \
114 TRACE_MASK_LAYER_2 | \
115 TRACE_MASK_LAYER_3 | \
116 TRACE_MASK_LAYER_4 | \
117 TRACE_MASK_READER | \
118 TRACE_MASK_WRITER )
119
120/* We have to avoid globals because E-ACSL chokes on them...
121 * Wrap everything in stub functions. */
122int trace_get_depth( void );
123void trace_inc_depth( void );
124void trace_dec_depth( void );
125
126void trace_color( int id );
127void trace_indent( int level, trace_type ty );
128
129void trace_print_msg( int id, int line, const char *format, ... );
130
131#define TRACE( type, ... ) \
132 do { \
133 if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
134 break; \
135 trace_indent( trace_get_depth(), type ); \
136 trace_color( trace_id ); \
137 trace_print_msg( trace_id, __LINE__, __VA_ARGS__ ); \
138 trace_color( 0 ); \
139 } while( 0 )
140
141#define TRACE_INIT( ... ) \
142 do { \
143 if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
144 break; \
145 TRACE( trace_call, __VA_ARGS__ ); \
146 trace_inc_depth(); \
147 } while( 0 )
148
149#define TRACE_END( val ) \
150 do { \
151 if( ! ( TRACE_MASK & ( 1u << trace_id ) ) ) \
152 break; \
153 TRACE( trace_return, "%d (-%#04x)", \
154 (int) (val), -((unsigned)(val)) ); \
155 trace_dec_depth(); \
156 } while( 0 )
157
158#define RETURN( val ) \
159 do { \
160 /* Breaks tail recursion. */ \
161 int ret__ = val; \
162 TRACE_END( ret__ ); \
163 return( ret__ ); \
164 } while( 0 )
Hanno Beckerc809ff62021-01-12 06:54:04 +0000165
166#else /* MBEDTLS_MPS_TRACE */
167
Hanno Beckerb9100162021-01-12 09:46:03 +0000168#define TRACE( type, ... ) do { } while( 0 )
169#define TRACE_INIT( ... ) do { } while( 0 )
170#define TRACE_END do { } while( 0 )
Hanno Beckerc809ff62021-01-12 06:54:04 +0000171
172#define RETURN( val ) return( val );
173
174#endif /* MBEDTLS_MPS_TRACE */
175
176#endif /* MBEDTLS_MPS_TRACE_H */