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