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