xref: /optee_os/lib/libutils/ext/include/trace.h (revision e5dc28ccb831dc8101907e187d284758ad837a6b)
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 void trace_set_level(int level);
49 int trace_get_level(void);
50 
51 /* Internal functions used by the macros below */
52 void trace_printf(const char *func, int line, int level, bool level_ok,
53 		  bool sync, const char *fmt, ...) __printf(6, 7);
54 
55 #define trace_printf_helper(level, level_ok, ...) \
56 	trace_printf(__func__, __LINE__, (level), (level_ok), \
57 		     false, __VA_ARGS__)
58 
59 /* Formatted trace tagged with level independent */
60 #if (CFG_TRACE_LEVEL <= 0)
61 #define MSG(...)   (void)0
62 #else
63 #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
64 #endif
65 
66 /* Formatted trace tagged with TRACE_ERROR level */
67 #if (CFG_TRACE_LEVEL < TRACE_ERROR)
68 #define EMSG(...)   (void)0
69 #else
70 #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
71 #endif
72 
73 /* Formatted trace tagged with TRACE_INFO level */
74 #if (CFG_TRACE_LEVEL < TRACE_INFO)
75 #define IMSG(...)   (void)0
76 #else
77 #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
78 #endif
79 
80 /* Formatted trace tagged with TRACE_DEBUG level */
81 #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
82 #define DMSG(...)   (void)0
83 #else
84 #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
85 #endif
86 
87 /* Formatted trace tagged with TRACE_FLOW level */
88 #if (CFG_TRACE_LEVEL < TRACE_FLOW)
89 #define FMSG(...)   (void)0
90 #else
91 #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
92 #endif
93 
94 /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
95 #define INMSG(...)     FMSG("> " __VA_ARGS__)
96 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
97 #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
98 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
99  * an error message if r != 0 */
100 #define OUTRMSG(r)			\
101 	do {				\
102 		OUTMSG("r=[%x]", r);	\
103 		return r;		\
104 	} while (0)
105 
106 void dhex_dump(const char *function, int line, int level,
107 	       const void *buf, int len);
108 #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
109 #define DHEXDUMP(buf, len) (void)0
110 #else
111 #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
112 				     buf, len)
113 #endif
114 
115 
116 /* Trace api without trace formatting */
117 
118 #define trace_printf_helper_raw(level, level_ok, ...) \
119 	trace_printf(NULL, 0, (level), (level_ok), false, __VA_ARGS__)
120 
121 /* No formatted trace tagged with level independent */
122 #if (CFG_TRACE_LEVEL <= 0)
123 #define MSG_RAW(...)   (void)0
124 #else
125 #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
126 #endif
127 
128 /* No formatted trace tagged with TRACE_ERROR level */
129 #if (CFG_TRACE_LEVEL < TRACE_ERROR)
130 #define EMSG_RAW(...)   (void)0
131 #else
132 #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
133 #endif
134 
135 /* No formatted trace tagged with TRACE_INFO level */
136 #if (CFG_TRACE_LEVEL < TRACE_INFO)
137 #define IMSG_RAW(...)   (void)0
138 #else
139 #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
140 #endif
141 
142 /* No formatted trace tagged with TRACE_DEBUG level */
143 #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
144 #define DMSG_RAW(...)   (void)0
145 #else
146 #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
147 #endif
148 
149 /* No formatted trace tagged with TRACE_FLOW level */
150 #if (CFG_TRACE_LEVEL < TRACE_FLOW)
151 #define FMSG_RAW(...)   (void)0
152 #else
153 #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
154 #endif
155 
156 #if (CFG_TRACE_LEVEL <= 0)
157 #define SMSG(...)   (void)0
158 #else
159 /*
160  * Synchronised flushed trace, an Always message straight to HW trace IP.
161  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
162  * in another context.
163  */
164 #define SMSG(...)   \
165 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, true, __VA_ARGS__)
166 
167 #endif /* CFG_TRACE_LEVEL */
168 
169 #endif /* TRACE_H */
170