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     MBEDTLS_MPS_TRACE_TYPE_COMMENT,
57     MBEDTLS_MPS_TRACE_TYPE_CALL,
58     MBEDTLS_MPS_TRACE_TYPE_ERROR,
59     MBEDTLS_MPS_TRACE_TYPE_RETURN
60 } mbedtls_mps_trace_type;
61 
62 #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
63 #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
64 #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
65 #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
66 #define MBEDTLS_MPS_TRACE_BIT_WRITER  5
67 #define MBEDTLS_MPS_TRACE_BIT_READER  6
68 
69 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
70 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1)
71 #else
72 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
73 #endif
74 
75 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
76 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2)
77 #else
78 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
79 #endif
80 
81 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
82 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3)
83 #else
84 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
85 #endif
86 
87 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
88 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4)
89 #else
90 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
91 #endif
92 
93 #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
94 #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER)
95 #else
96 #define MBEDTLS_MPS_TRACE_MASK_READER 0
97 #endif
98 
99 #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
100 #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER)
101 #else
102 #define MBEDTLS_MPS_TRACE_MASK_WRITER 0
103 #endif
104 
105 #define MBEDTLS_MPS_TRACE_MASK (MBEDTLS_MPS_TRACE_MASK_LAYER_1 |       \
106                                 MBEDTLS_MPS_TRACE_MASK_LAYER_2 |       \
107                                 MBEDTLS_MPS_TRACE_MASK_LAYER_3 |       \
108                                 MBEDTLS_MPS_TRACE_MASK_LAYER_4 |       \
109                                 MBEDTLS_MPS_TRACE_MASK_READER  |       \
110                                 MBEDTLS_MPS_TRACE_MASK_WRITER)
111 
112 /* We have to avoid globals because E-ACSL chokes on them...
113  * Wrap everything in stub functions. */
114 int  mbedtls_mps_trace_get_depth(void);
115 void mbedtls_mps_trace_inc_depth(void);
116 void mbedtls_mps_trace_dec_depth(void);
117 
118 void mbedtls_mps_trace_color(int id);
119 void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty);
120 
121 void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...);
122 
123 #define MBEDTLS_MPS_TRACE(type, ...)                                              \
124     do {                                                                            \
125         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id)))         \
126         break;                                                                  \
127         mbedtls_mps_trace_indent(mbedtls_mps_trace_get_depth(), type);            \
128         mbedtls_mps_trace_color(mbedtls_mps_trace_id);                            \
129         mbedtls_mps_trace_print_msg(mbedtls_mps_trace_id, __LINE__, __VA_ARGS__); \
130         mbedtls_mps_trace_color(0);                                               \
131     } while (0)
132 
133 #define MBEDTLS_MPS_TRACE_INIT(...)                                         \
134     do {                                                                      \
135         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id)))   \
136         break;                                                            \
137         MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_CALL, __VA_ARGS__);        \
138         mbedtls_mps_trace_inc_depth();                                        \
139     } while (0)
140 
141 #define MBEDTLS_MPS_TRACE_END(val)                                        \
142     do {                                                                    \
143         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id))) \
144         break;                                                          \
145         MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_RETURN, "%d (-%#04x)",    \
146                           (int) (val), -((unsigned) (val)));                           \
147         mbedtls_mps_trace_dec_depth();                                      \
148     } while (0)
149 
150 #define MBEDTLS_MPS_TRACE_RETURN(val)         \
151     do {                                        \
152         /* Breaks tail recursion. */            \
153         int ret__ = val;                        \
154         MBEDTLS_MPS_TRACE_END(ret__);         \
155         return ret__;                        \
156     } while (0)
157 
158 #else /* MBEDTLS_MPS_TRACE */
159 
160 #define MBEDTLS_MPS_TRACE(type, ...) do { } while (0)
161 #define MBEDTLS_MPS_TRACE_INIT(...)  do { } while (0)
162 #define MBEDTLS_MPS_TRACE_END          do { } while (0)
163 
164 #define MBEDTLS_MPS_TRACE_RETURN(val) return val;
165 
166 #endif /* MBEDTLS_MPS_TRACE */
167 
168 #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */
169