xref: /optee_os/lib/libutils/ext/include/trace.h (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef TRACE_H
29 #define TRACE_H
30 
31 #include <stdbool.h>
32 #include <stddef.h>
33 #include <compiler.h>
34 #include <trace_levels.h>
35 
36 #define MAX_PRINT_SIZE      256
37 #define MAX_FUNC_PRINT_SIZE 32
38 
39 #ifndef TRACE_LEVEL
40 #define TRACE_LEVEL TRACE_MAX
41 #endif
42 
43 /*
44  * Symbols provided by the entity that uses this API.
45  */
46 extern int trace_level;
47 extern const char trace_ext_prefix[];
48 void trace_ext_puts(const char *str);
49 int trace_ext_get_thread_id(void);
50 void trace_set_level(int level);
51 int trace_get_level(void);
52 
53 /* Internal functions used by the macros below */
54 void trace_printf(const char *func, int line, int level, bool level_ok,
55 		  const char *fmt, ...) __printf(5, 6);
56 
57 #define trace_printf_helper(level, level_ok, ...) \
58 	trace_printf(__func__, __LINE__, (level), (level_ok), \
59 		     __VA_ARGS__)
60 
61 /* Formatted trace tagged with level independent */
62 #if (TRACE_LEVEL <= 0)
63 #define MSG(...)   (void)0
64 #else
65 #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
66 #endif
67 
68 /* Formatted trace tagged with TRACE_ERROR level */
69 #if (TRACE_LEVEL < TRACE_ERROR)
70 #define EMSG(...)   (void)0
71 #else
72 #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
73 #endif
74 
75 /* Formatted trace tagged with TRACE_INFO level */
76 #if (TRACE_LEVEL < TRACE_INFO)
77 #define IMSG(...)   (void)0
78 #else
79 #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
80 #endif
81 
82 /* Formatted trace tagged with TRACE_DEBUG level */
83 #if (TRACE_LEVEL < TRACE_DEBUG)
84 #define DMSG(...)   (void)0
85 #else
86 #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
87 #endif
88 
89 /* Formatted trace tagged with TRACE_FLOW level */
90 #if (TRACE_LEVEL < TRACE_FLOW)
91 #define FMSG(...)   (void)0
92 #else
93 #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
94 #endif
95 
96 /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
97 #define INMSG(...)     FMSG("> " __VA_ARGS__)
98 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
99 #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
100 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
101  * an error message if r != 0 */
102 #define OUTRMSG(r)			\
103 	do {				\
104 		OUTMSG("r=[%x]", r);	\
105 		return r;		\
106 	} while (0)
107 
108 void dhex_dump(const char *function, int line, int level,
109 	       const void *buf, int len);
110 #if (TRACE_LEVEL < TRACE_DEBUG)
111 #define DHEXDUMP(buf, len) (void)0
112 #else
113 #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
114 				     buf, len)
115 #endif
116 
117 
118 /* Trace api without trace formatting */
119 
120 #define trace_printf_helper_raw(level, level_ok, ...) \
121 	trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__)
122 
123 /* No formatted trace tagged with level independent */
124 #if (TRACE_LEVEL <= 0)
125 #define MSG_RAW(...)   (void)0
126 #else
127 #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
128 #endif
129 
130 /* No formatted trace tagged with TRACE_ERROR level */
131 #if (TRACE_LEVEL < TRACE_ERROR)
132 #define EMSG_RAW(...)   (void)0
133 #else
134 #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
135 #endif
136 
137 /* No formatted trace tagged with TRACE_INFO level */
138 #if (TRACE_LEVEL < TRACE_INFO)
139 #define IMSG_RAW(...)   (void)0
140 #else
141 #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
142 #endif
143 
144 /* No formatted trace tagged with TRACE_DEBUG level */
145 #if (TRACE_LEVEL < TRACE_DEBUG)
146 #define DMSG_RAW(...)   (void)0
147 #else
148 #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
149 #endif
150 
151 /* No formatted trace tagged with TRACE_FLOW level */
152 #if (TRACE_LEVEL < TRACE_FLOW)
153 #define FMSG_RAW(...)   (void)0
154 #else
155 #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
156 #endif
157 
158 #if (TRACE_LEVEL <= 0)
159 #define SMSG(...)   (void)0
160 #else
161 /*
162  * Synchronised flushed trace, an Always message straight to HW trace IP.
163  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
164  * in another context.
165  */
166 #define SMSG(...)   \
167 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__)
168 
169 #endif /* TRACE_LEVEL */
170 
171 #if defined(__KERNEL__) && defined(CFG_UNWIND)
172 #include <kernel/unwind.h>
173 #define _PRINT_STACK
174 #endif
175 
176 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_ERROR)
177 #define EPRINT_STACK() print_kernel_stack(TRACE_ERROR)
178 #else
179 #define EPRINT_STACK() (void)0
180 #endif
181 
182 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_INFO)
183 #define IPRINT_STACK() print_kernel_stack(TRACE_INFO)
184 #else
185 #define IPRINT_STACK() (void)0
186 #endif
187 
188 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_DEBUG)
189 #define DPRINT_STACK() print_kernel_stack(TRACE_DEBUG)
190 #else
191 #define DPRINT_STACK() (void)0
192 #endif
193 
194 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_FLOW)
195 #define FPRINT_STACK() print_kernel_stack(TRACE_FLOW)
196 #else
197 #define FPRINT_STACK() (void)0
198 #endif
199 
200 #if defined(__KERNEL__) && defined(CFG_UNWIND)
201 #undef _PRINT_STACK
202 #endif
203 
204 #endif /* TRACE_H */
205