xref: /optee_os/lib/libutils/ext/include/trace.h (revision e5dc28ccb831dc8101907e187d284758ad837a6b)
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 
384de4bebcSJens Wiklander #ifndef CFG_TRACE_LEVEL
394de4bebcSJens Wiklander #define CFG_TRACE_LEVEL TRACE_MAX
404de4bebcSJens Wiklander #endif
414de4bebcSJens Wiklander 
424de4bebcSJens Wiklander /*
434de4bebcSJens Wiklander  * Symbols provided by the entity that uses this API.
444de4bebcSJens Wiklander  */
454de4bebcSJens Wiklander extern const char trace_ext_prefix[];
464de4bebcSJens Wiklander void trace_ext_puts(bool sync, const char *str);
474de4bebcSJens Wiklander int trace_ext_get_thread_id(void);
48*e5dc28ccSPascal Brand void trace_set_level(int level);
49*e5dc28ccSPascal Brand int trace_get_level(void);
504de4bebcSJens Wiklander 
514de4bebcSJens Wiklander /* Internal functions used by the macros below */
5244202a48SPascal Brand void trace_printf(const char *func, int line, int level, bool level_ok,
5344202a48SPascal Brand 		  bool sync, const char *fmt, ...) __printf(6, 7);
544de4bebcSJens Wiklander 
5544202a48SPascal Brand #define trace_printf_helper(level, level_ok, ...) \
5644202a48SPascal Brand 	trace_printf(__func__, __LINE__, (level), (level_ok), \
5744202a48SPascal Brand 		     false, __VA_ARGS__)
5844202a48SPascal Brand 
5944202a48SPascal Brand /* Formatted trace tagged with level independent */
60*e5dc28ccSPascal Brand #if (CFG_TRACE_LEVEL <= 0)
6144202a48SPascal Brand #define MSG(...)   (void)0
6244202a48SPascal Brand #else
6344202a48SPascal Brand #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
6444202a48SPascal Brand #endif
654de4bebcSJens Wiklander 
664de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_ERROR level */
674de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_ERROR)
684de4bebcSJens Wiklander #define EMSG(...)   (void)0
694de4bebcSJens Wiklander #else
7044202a48SPascal Brand #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
714de4bebcSJens Wiklander #endif
724de4bebcSJens Wiklander 
734de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_INFO level */
744de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_INFO)
754de4bebcSJens Wiklander #define IMSG(...)   (void)0
764de4bebcSJens Wiklander #else
7744202a48SPascal Brand #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
784de4bebcSJens Wiklander #endif
794de4bebcSJens Wiklander 
804de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_DEBUG level */
814de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
824de4bebcSJens Wiklander #define DMSG(...)   (void)0
834de4bebcSJens Wiklander #else
8444202a48SPascal Brand #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
854de4bebcSJens Wiklander #endif
864de4bebcSJens Wiklander 
874de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level */
884de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_FLOW)
894de4bebcSJens Wiklander #define FMSG(...)   (void)0
904de4bebcSJens Wiklander #else
9144202a48SPascal Brand #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
924de4bebcSJens Wiklander #endif
934de4bebcSJens Wiklander 
944de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
954de4bebcSJens Wiklander #define INMSG(...)     FMSG("> " __VA_ARGS__)
964de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
974de4bebcSJens Wiklander #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
984de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
994de4bebcSJens Wiklander  * an error message if r != 0 */
1004de4bebcSJens Wiklander #define OUTRMSG(r)			\
1014de4bebcSJens Wiklander 	do {				\
1024de4bebcSJens Wiklander 		OUTMSG("r=[%x]", r);	\
1034de4bebcSJens Wiklander 		return r;		\
1044de4bebcSJens Wiklander 	} while (0)
1054de4bebcSJens Wiklander 
106*e5dc28ccSPascal Brand void dhex_dump(const char *function, int line, int level,
107*e5dc28ccSPascal Brand 	       const void *buf, int len);
1084de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
1094de4bebcSJens Wiklander #define DHEXDUMP(buf, len) (void)0
1104de4bebcSJens Wiklander #else
1114de4bebcSJens Wiklander #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
1124de4bebcSJens Wiklander 				     buf, len)
1134de4bebcSJens Wiklander #endif
1144de4bebcSJens Wiklander 
11544202a48SPascal Brand 
1164de4bebcSJens Wiklander /* Trace api without trace formatting */
1174de4bebcSJens Wiklander 
11844202a48SPascal Brand #define trace_printf_helper_raw(level, level_ok, ...) \
11944202a48SPascal Brand 	trace_printf(NULL, 0, (level), (level_ok), false, __VA_ARGS__)
12044202a48SPascal Brand 
12144202a48SPascal Brand /* No formatted trace tagged with level independent */
122*e5dc28ccSPascal Brand #if (CFG_TRACE_LEVEL <= 0)
12344202a48SPascal Brand #define MSG_RAW(...)   (void)0
12444202a48SPascal Brand #else
12544202a48SPascal Brand #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
12644202a48SPascal Brand #endif
12744202a48SPascal Brand 
1284de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_ERROR level */
1294de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_ERROR)
1304de4bebcSJens Wiklander #define EMSG_RAW(...)   (void)0
1314de4bebcSJens Wiklander #else
13244202a48SPascal Brand #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
1334de4bebcSJens Wiklander #endif
1344de4bebcSJens Wiklander 
1354de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_INFO level */
1364de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_INFO)
1374de4bebcSJens Wiklander #define IMSG_RAW(...)   (void)0
1384de4bebcSJens Wiklander #else
13944202a48SPascal Brand #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
1404de4bebcSJens Wiklander #endif
1414de4bebcSJens Wiklander 
1424de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_DEBUG level */
1434de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
1444de4bebcSJens Wiklander #define DMSG_RAW(...)   (void)0
1454de4bebcSJens Wiklander #else
14644202a48SPascal Brand #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
1474de4bebcSJens Wiklander #endif
1484de4bebcSJens Wiklander 
1494de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_FLOW level */
1504de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_FLOW)
1514de4bebcSJens Wiklander #define FMSG_RAW(...)   (void)0
1524de4bebcSJens Wiklander #else
15344202a48SPascal Brand #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
1544de4bebcSJens Wiklander #endif
1554de4bebcSJens Wiklander 
156*e5dc28ccSPascal Brand #if (CFG_TRACE_LEVEL <= 0)
1574de4bebcSJens Wiklander #define SMSG(...)   (void)0
1584de4bebcSJens Wiklander #else
1594de4bebcSJens Wiklander /*
1604de4bebcSJens Wiklander  * Synchronised flushed trace, an Always message straight to HW trace IP.
1614de4bebcSJens Wiklander  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
1624de4bebcSJens Wiklander  * in another context.
1634de4bebcSJens Wiklander  */
1644de4bebcSJens Wiklander #define SMSG(...)   \
16544202a48SPascal Brand 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, true, __VA_ARGS__)
1664de4bebcSJens Wiklander 
1674de4bebcSJens Wiklander #endif /* CFG_TRACE_LEVEL */
1684de4bebcSJens Wiklander 
1694de4bebcSJens Wiklander #endif /* TRACE_H */
170