xref: /optee_os/lib/libutils/ext/include/trace.h (revision 31a2964210f0d04eb6e8d940c445a3d0c9c8705f)
14de4bebcSJens Wiklander /*
24de4bebcSJens Wiklander  * Copyright (c) 2014, STMicroelectronics International N.V.
34de4bebcSJens Wiklander  * All rights reserved.
44de4bebcSJens Wiklander  *
54de4bebcSJens Wiklander  * Redistribution and use in source and binary forms, with or without
64de4bebcSJens Wiklander  * modification, are permitted provided that the following conditions are met:
74de4bebcSJens Wiklander  *
84de4bebcSJens Wiklander  * 1. Redistributions of source code must retain the above copyright notice,
94de4bebcSJens Wiklander  * this list of conditions and the following disclaimer.
104de4bebcSJens Wiklander  *
114de4bebcSJens Wiklander  * 2. Redistributions in binary form must reproduce the above copyright notice,
124de4bebcSJens Wiklander  * this list of conditions and the following disclaimer in the documentation
134de4bebcSJens Wiklander  * and/or other materials provided with the distribution.
144de4bebcSJens Wiklander  *
154de4bebcSJens Wiklander  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
164de4bebcSJens Wiklander  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174de4bebcSJens Wiklander  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
184de4bebcSJens Wiklander  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
194de4bebcSJens Wiklander  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
204de4bebcSJens Wiklander  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
214de4bebcSJens Wiklander  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
224de4bebcSJens Wiklander  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
234de4bebcSJens Wiklander  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
244de4bebcSJens Wiklander  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
254de4bebcSJens Wiklander  * POSSIBILITY OF SUCH DAMAGE.
264de4bebcSJens Wiklander  */
274de4bebcSJens Wiklander #ifndef TRACE_H
284de4bebcSJens Wiklander #define TRACE_H
294de4bebcSJens Wiklander 
304de4bebcSJens Wiklander #include <stdbool.h>
314de4bebcSJens Wiklander #include <stddef.h>
324de4bebcSJens Wiklander #include <compiler.h>
334de4bebcSJens Wiklander #include <trace_levels.h>
344de4bebcSJens Wiklander 
354de4bebcSJens Wiklander #define MAX_PRINT_SIZE      256
364de4bebcSJens Wiklander #define MAX_FUNC_PRINT_SIZE 32
374de4bebcSJens Wiklander 
388a1e7b89SJerome Forissier #ifndef TRACE_LEVEL
398a1e7b89SJerome Forissier #define TRACE_LEVEL TRACE_MAX
404de4bebcSJens Wiklander #endif
414de4bebcSJens Wiklander 
424de4bebcSJens Wiklander /*
434de4bebcSJens Wiklander  * Symbols provided by the entity that uses this API.
444de4bebcSJens Wiklander  */
450e91d797SJean-Michel Delorme extern int trace_level;
464de4bebcSJens Wiklander extern const char trace_ext_prefix[];
47bfc6487bSJerome Forissier void trace_ext_puts(const char *str);
484de4bebcSJens Wiklander int trace_ext_get_thread_id(void);
49e5dc28ccSPascal Brand void trace_set_level(int level);
50e5dc28ccSPascal Brand int trace_get_level(void);
514de4bebcSJens Wiklander 
524de4bebcSJens Wiklander /* Internal functions used by the macros below */
5344202a48SPascal Brand void trace_printf(const char *func, int line, int level, bool level_ok,
54bfc6487bSJerome Forissier 		  const char *fmt, ...) __printf(5, 6);
554de4bebcSJens Wiklander 
5644202a48SPascal Brand #define trace_printf_helper(level, level_ok, ...) \
5744202a48SPascal Brand 	trace_printf(__func__, __LINE__, (level), (level_ok), \
58bfc6487bSJerome Forissier 		     __VA_ARGS__)
5944202a48SPascal Brand 
6044202a48SPascal Brand /* Formatted trace tagged with level independent */
618a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0)
6244202a48SPascal Brand #define MSG(...)   (void)0
6344202a48SPascal Brand #else
6444202a48SPascal Brand #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
6544202a48SPascal Brand #endif
664de4bebcSJens Wiklander 
674de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_ERROR level */
688a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_ERROR)
694de4bebcSJens Wiklander #define EMSG(...)   (void)0
704de4bebcSJens Wiklander #else
7144202a48SPascal Brand #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
724de4bebcSJens Wiklander #endif
734de4bebcSJens Wiklander 
744de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_INFO level */
758a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_INFO)
764de4bebcSJens Wiklander #define IMSG(...)   (void)0
774de4bebcSJens Wiklander #else
7844202a48SPascal Brand #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
794de4bebcSJens Wiklander #endif
804de4bebcSJens Wiklander 
814de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_DEBUG level */
828a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG)
834de4bebcSJens Wiklander #define DMSG(...)   (void)0
844de4bebcSJens Wiklander #else
8544202a48SPascal Brand #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
864de4bebcSJens Wiklander #endif
874de4bebcSJens Wiklander 
884de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level */
898a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_FLOW)
904de4bebcSJens Wiklander #define FMSG(...)   (void)0
914de4bebcSJens Wiklander #else
9244202a48SPascal Brand #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
934de4bebcSJens Wiklander #endif
944de4bebcSJens Wiklander 
954de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
964de4bebcSJens Wiklander #define INMSG(...)     FMSG("> " __VA_ARGS__)
974de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
984de4bebcSJens Wiklander #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
994de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
1004de4bebcSJens Wiklander  * an error message if r != 0 */
1014de4bebcSJens Wiklander #define OUTRMSG(r)			\
1024de4bebcSJens Wiklander 	do {				\
1034de4bebcSJens Wiklander 		OUTMSG("r=[%x]", r);	\
1044de4bebcSJens Wiklander 		return r;		\
1054de4bebcSJens Wiklander 	} while (0)
1064de4bebcSJens Wiklander 
107e5dc28ccSPascal Brand void dhex_dump(const char *function, int line, int level,
108e5dc28ccSPascal Brand 	       const void *buf, int len);
1098a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG)
1104de4bebcSJens Wiklander #define DHEXDUMP(buf, len) (void)0
1114de4bebcSJens Wiklander #else
1124de4bebcSJens Wiklander #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
1134de4bebcSJens Wiklander 				     buf, len)
1144de4bebcSJens Wiklander #endif
1154de4bebcSJens Wiklander 
11644202a48SPascal Brand 
1174de4bebcSJens Wiklander /* Trace api without trace formatting */
1184de4bebcSJens Wiklander 
11944202a48SPascal Brand #define trace_printf_helper_raw(level, level_ok, ...) \
120bfc6487bSJerome Forissier 	trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__)
12144202a48SPascal Brand 
12244202a48SPascal Brand /* No formatted trace tagged with level independent */
1238a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0)
12444202a48SPascal Brand #define MSG_RAW(...)   (void)0
12544202a48SPascal Brand #else
12644202a48SPascal Brand #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
12744202a48SPascal Brand #endif
12844202a48SPascal Brand 
1294de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_ERROR level */
1308a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_ERROR)
1314de4bebcSJens Wiklander #define EMSG_RAW(...)   (void)0
1324de4bebcSJens Wiklander #else
13344202a48SPascal Brand #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
1344de4bebcSJens Wiklander #endif
1354de4bebcSJens Wiklander 
1364de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_INFO level */
1378a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_INFO)
1384de4bebcSJens Wiklander #define IMSG_RAW(...)   (void)0
1394de4bebcSJens Wiklander #else
14044202a48SPascal Brand #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
1414de4bebcSJens Wiklander #endif
1424de4bebcSJens Wiklander 
1434de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_DEBUG level */
1448a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG)
1454de4bebcSJens Wiklander #define DMSG_RAW(...)   (void)0
1464de4bebcSJens Wiklander #else
14744202a48SPascal Brand #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
1484de4bebcSJens Wiklander #endif
1494de4bebcSJens Wiklander 
1504de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_FLOW level */
1518a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_FLOW)
1524de4bebcSJens Wiklander #define FMSG_RAW(...)   (void)0
1534de4bebcSJens Wiklander #else
15444202a48SPascal Brand #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
1554de4bebcSJens Wiklander #endif
1564de4bebcSJens Wiklander 
1578a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0)
1584de4bebcSJens Wiklander #define SMSG(...)   (void)0
1594de4bebcSJens Wiklander #else
1604de4bebcSJens Wiklander /*
1614de4bebcSJens Wiklander  * Synchronised flushed trace, an Always message straight to HW trace IP.
1624de4bebcSJens Wiklander  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
1634de4bebcSJens Wiklander  * in another context.
1644de4bebcSJens Wiklander  */
1654de4bebcSJens Wiklander #define SMSG(...)   \
166bfc6487bSJerome Forissier 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__)
1674de4bebcSJens Wiklander 
1688a1e7b89SJerome Forissier #endif /* TRACE_LEVEL */
1694de4bebcSJens Wiklander 
170*31a29642SJerome Forissier #if defined(__KERNEL__) && defined(CFG_UNWIND)
171a681fabaSJerome Forissier #include <kernel/unwind.h>
172a681fabaSJerome Forissier #define _PRINT_STACK
173a681fabaSJerome Forissier #endif
174a681fabaSJerome Forissier 
175a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_ERROR)
176*31a29642SJerome Forissier #define EPRINT_STACK() print_kernel_stack(TRACE_ERROR)
177a681fabaSJerome Forissier #else
178a681fabaSJerome Forissier #define EPRINT_STACK() (void)0
179a681fabaSJerome Forissier #endif
180a681fabaSJerome Forissier 
181a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_INFO)
182*31a29642SJerome Forissier #define IPRINT_STACK() print_kernel_stack(TRACE_INFO)
183a681fabaSJerome Forissier #else
184a681fabaSJerome Forissier #define IPRINT_STACK() (void)0
185a681fabaSJerome Forissier #endif
186a681fabaSJerome Forissier 
187a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_DEBUG)
188*31a29642SJerome Forissier #define DPRINT_STACK() print_kernel_stack(TRACE_DEBUG)
189a681fabaSJerome Forissier #else
190a681fabaSJerome Forissier #define DPRINT_STACK() (void)0
191a681fabaSJerome Forissier #endif
192a681fabaSJerome Forissier 
193a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_FLOW)
194*31a29642SJerome Forissier #define FPRINT_STACK() print_kernel_stack(TRACE_FLOW)
195a681fabaSJerome Forissier #else
196a681fabaSJerome Forissier #define FPRINT_STACK() (void)0
197a681fabaSJerome Forissier #endif
198a681fabaSJerome Forissier 
199*31a29642SJerome Forissier #if defined(__KERNEL__) && defined(CFG_UNWIND)
200a681fabaSJerome Forissier #undef _PRINT_STACK
201a681fabaSJerome Forissier #endif
202a681fabaSJerome Forissier 
2034de4bebcSJens Wiklander #endif /* TRACE_H */
204