1 /*
2  *  Copyright The Mbed TLS Contributors
3  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4  */
5 
6 /**
7  * \file mps_trace.h
8  *
9  * \brief Tracing module for MPS
10  */
11 
12 #ifndef MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
13 #define MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H
14 
15 #include "common.h"
16 #include "mps_common.h"
17 #include "mps_trace.h"
18 
19 #include "mbedtls/platform.h"
20 
21 #if defined(MBEDTLS_MPS_ENABLE_TRACE)
22 
23 /*
24  * Adapt this to enable/disable tracing output
25  * from the various layers of the MPS.
26  */
27 
28 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_1
29 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_2
30 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_3
31 #define MBEDTLS_MPS_TRACE_ENABLE_LAYER_4
32 #define MBEDTLS_MPS_TRACE_ENABLE_READER
33 #define MBEDTLS_MPS_TRACE_ENABLE_WRITER
34 
35 /*
36  * To use the existing trace module, only change
37  * MBEDTLS_MPS_TRACE_ENABLE_XXX above, but don't modify the
38  * rest of this file.
39  */
40 
41 typedef enum {
42     MBEDTLS_MPS_TRACE_TYPE_COMMENT,
43     MBEDTLS_MPS_TRACE_TYPE_CALL,
44     MBEDTLS_MPS_TRACE_TYPE_ERROR,
45     MBEDTLS_MPS_TRACE_TYPE_RETURN
46 } mbedtls_mps_trace_type;
47 
48 #define MBEDTLS_MPS_TRACE_BIT_LAYER_1 1
49 #define MBEDTLS_MPS_TRACE_BIT_LAYER_2 2
50 #define MBEDTLS_MPS_TRACE_BIT_LAYER_3 3
51 #define MBEDTLS_MPS_TRACE_BIT_LAYER_4 4
52 #define MBEDTLS_MPS_TRACE_BIT_WRITER  5
53 #define MBEDTLS_MPS_TRACE_BIT_READER  6
54 
55 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_1)
56 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_1)
57 #else
58 #define MBEDTLS_MPS_TRACE_MASK_LAYER_1 0
59 #endif
60 
61 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_2)
62 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_2)
63 #else
64 #define MBEDTLS_MPS_TRACE_MASK_LAYER_2 0
65 #endif
66 
67 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_3)
68 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_3)
69 #else
70 #define MBEDTLS_MPS_TRACE_MASK_LAYER_3 0
71 #endif
72 
73 #if defined(MBEDTLS_MPS_TRACE_ENABLE_LAYER_4)
74 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 (1u << MBEDTLS_MPS_TRACE_BIT_LAYER_4)
75 #else
76 #define MBEDTLS_MPS_TRACE_MASK_LAYER_4 0
77 #endif
78 
79 #if defined(MBEDTLS_MPS_TRACE_ENABLE_READER)
80 #define MBEDTLS_MPS_TRACE_MASK_READER (1u << MBEDTLS_MPS_TRACE_BIT_READER)
81 #else
82 #define MBEDTLS_MPS_TRACE_MASK_READER 0
83 #endif
84 
85 #if defined(MBEDTLS_MPS_TRACE_ENABLE_WRITER)
86 #define MBEDTLS_MPS_TRACE_MASK_WRITER (1u << MBEDTLS_MPS_TRACE_BIT_WRITER)
87 #else
88 #define MBEDTLS_MPS_TRACE_MASK_WRITER 0
89 #endif
90 
91 #define MBEDTLS_MPS_TRACE_MASK (MBEDTLS_MPS_TRACE_MASK_LAYER_1 |       \
92                                 MBEDTLS_MPS_TRACE_MASK_LAYER_2 |       \
93                                 MBEDTLS_MPS_TRACE_MASK_LAYER_3 |       \
94                                 MBEDTLS_MPS_TRACE_MASK_LAYER_4 |       \
95                                 MBEDTLS_MPS_TRACE_MASK_READER  |       \
96                                 MBEDTLS_MPS_TRACE_MASK_WRITER)
97 
98 /* We have to avoid globals because E-ACSL chokes on them...
99  * Wrap everything in stub functions. */
100 int  mbedtls_mps_trace_get_depth(void);
101 void mbedtls_mps_trace_inc_depth(void);
102 void mbedtls_mps_trace_dec_depth(void);
103 
104 void mbedtls_mps_trace_color(int id);
105 void mbedtls_mps_trace_indent(int level, mbedtls_mps_trace_type ty);
106 
107 void mbedtls_mps_trace_print_msg(int id, int line, const char *format, ...);
108 
109 #define MBEDTLS_MPS_TRACE(type, ...)                                              \
110     do {                                                                            \
111         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id)))         \
112         break;                                                                  \
113         mbedtls_mps_trace_indent(mbedtls_mps_trace_get_depth(), type);            \
114         mbedtls_mps_trace_color(mbedtls_mps_trace_id);                            \
115         mbedtls_mps_trace_print_msg(mbedtls_mps_trace_id, __LINE__, __VA_ARGS__); \
116         mbedtls_mps_trace_color(0);                                               \
117     } while (0)
118 
119 #define MBEDTLS_MPS_TRACE_INIT(...)                                         \
120     do {                                                                      \
121         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id)))   \
122         break;                                                            \
123         MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_CALL, __VA_ARGS__);        \
124         mbedtls_mps_trace_inc_depth();                                        \
125     } while (0)
126 
127 #define MBEDTLS_MPS_TRACE_END(val)                                        \
128     do {                                                                    \
129         if (!(MBEDTLS_MPS_TRACE_MASK & (1u << mbedtls_mps_trace_id))) \
130         break;                                                          \
131         MBEDTLS_MPS_TRACE(MBEDTLS_MPS_TRACE_TYPE_RETURN, "%d (-%#04x)",    \
132                           (int) (val), -((unsigned) (val)));                           \
133         mbedtls_mps_trace_dec_depth();                                      \
134     } while (0)
135 
136 #define MBEDTLS_MPS_TRACE_RETURN(val)         \
137     do {                                        \
138         /* Breaks tail recursion. */            \
139         int ret__ = val;                        \
140         MBEDTLS_MPS_TRACE_END(ret__);         \
141         return ret__;                        \
142     } while (0)
143 
144 #else /* MBEDTLS_MPS_TRACE */
145 
146 #define MBEDTLS_MPS_TRACE(type, ...) do { } while (0)
147 #define MBEDTLS_MPS_TRACE_INIT(...)  do { } while (0)
148 #define MBEDTLS_MPS_TRACE_END          do { } while (0)
149 
150 #define MBEDTLS_MPS_TRACE_RETURN(val) return val;
151 
152 #endif /* MBEDTLS_MPS_TRACE */
153 
154 #endif /* MBEDTLS_MPS_MBEDTLS_MPS_TRACE_H */
155