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