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