xref: /optee_os/lib/libutils/ext/include/trace.h (revision 44202a48c5de6db267056151a60995d7ed171383)
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);
484de4bebcSJens Wiklander 
494de4bebcSJens Wiklander /* Internal functions used by the macros below */
50*44202a48SPascal Brand void trace_printf(const char *func, int line, int level, bool level_ok,
51*44202a48SPascal Brand 		  bool sync, const char *fmt, ...) __printf(6, 7);
524de4bebcSJens Wiklander 
53*44202a48SPascal Brand #define trace_printf_helper(level, level_ok, ...) \
54*44202a48SPascal Brand 	trace_printf(__func__, __LINE__, (level), (level_ok), \
55*44202a48SPascal Brand 		     false, __VA_ARGS__)
56*44202a48SPascal Brand 
57*44202a48SPascal Brand /* Formatted trace tagged with level independent */
58*44202a48SPascal Brand #if (CFG_TRACE_LEVEL == 0)
59*44202a48SPascal Brand #define MSG(...)   (void)0
60*44202a48SPascal Brand #else
61*44202a48SPascal Brand #define MSG(...)   trace_printf_helper(0, false, __VA_ARGS__)
62*44202a48SPascal Brand #endif
634de4bebcSJens Wiklander 
644de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_ERROR level */
654de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_ERROR)
664de4bebcSJens Wiklander #define EMSG(...)   (void)0
674de4bebcSJens Wiklander #else
68*44202a48SPascal Brand #define EMSG(...)   trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
694de4bebcSJens Wiklander #endif
704de4bebcSJens Wiklander 
714de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_INFO level */
724de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_INFO)
734de4bebcSJens Wiklander #define IMSG(...)   (void)0
744de4bebcSJens Wiklander #else
75*44202a48SPascal Brand #define IMSG(...)   trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
764de4bebcSJens Wiklander #endif
774de4bebcSJens Wiklander 
784de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_DEBUG level */
794de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
804de4bebcSJens Wiklander #define DMSG(...)   (void)0
814de4bebcSJens Wiklander #else
82*44202a48SPascal Brand #define DMSG(...)   trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
834de4bebcSJens Wiklander #endif
844de4bebcSJens Wiklander 
854de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level */
864de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_FLOW)
874de4bebcSJens Wiklander #define FMSG(...)   (void)0
884de4bebcSJens Wiklander #else
89*44202a48SPascal Brand #define FMSG(...)   trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
904de4bebcSJens Wiklander #endif
914de4bebcSJens Wiklander 
924de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
934de4bebcSJens Wiklander #define INMSG(...)     FMSG("> " __VA_ARGS__)
944de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
954de4bebcSJens Wiklander #define OUTMSG(...)    FMSG("< " __VA_ARGS__)
964de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
974de4bebcSJens Wiklander  * an error message if r != 0 */
984de4bebcSJens Wiklander #define OUTRMSG(r)			\
994de4bebcSJens Wiklander 	do {				\
1004de4bebcSJens Wiklander 		OUTMSG("r=[%x]", r);	\
1014de4bebcSJens Wiklander 		return r;		\
1024de4bebcSJens Wiklander 	} while (0)
1034de4bebcSJens Wiklander 
1044de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
1054de4bebcSJens Wiklander #define DHEXDUMP(buf, len) (void)0
1064de4bebcSJens Wiklander #else
1074de4bebcSJens Wiklander #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
1084de4bebcSJens Wiklander 				     buf, len)
1094de4bebcSJens Wiklander void dhex_dump(const char *function, int line, int level,
1104de4bebcSJens Wiklander 	       const void *buf, int len);
1114de4bebcSJens Wiklander #endif
1124de4bebcSJens Wiklander 
113*44202a48SPascal Brand 
1144de4bebcSJens Wiklander /* Trace api without trace formatting */
1154de4bebcSJens Wiklander 
116*44202a48SPascal Brand #define trace_printf_helper_raw(level, level_ok, ...) \
117*44202a48SPascal Brand 	trace_printf(NULL, 0, (level), (level_ok), false, __VA_ARGS__)
118*44202a48SPascal Brand 
119*44202a48SPascal Brand /* No formatted trace tagged with level independent */
120*44202a48SPascal Brand #if (CFG_TRACE_LEVEL == 0)
121*44202a48SPascal Brand #define MSG_RAW(...)   (void)0
122*44202a48SPascal Brand #else
123*44202a48SPascal Brand #define MSG_RAW(...)   trace_printf_helper_raw(0, false, __VA_ARGS__)
124*44202a48SPascal Brand #endif
125*44202a48SPascal Brand 
1264de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_ERROR level */
1274de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_ERROR)
1284de4bebcSJens Wiklander #define EMSG_RAW(...)   (void)0
1294de4bebcSJens Wiklander #else
130*44202a48SPascal Brand #define EMSG_RAW(...)   trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
1314de4bebcSJens Wiklander #endif
1324de4bebcSJens Wiklander 
1334de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_INFO level */
1344de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_INFO)
1354de4bebcSJens Wiklander #define IMSG_RAW(...)   (void)0
1364de4bebcSJens Wiklander #else
137*44202a48SPascal Brand #define IMSG_RAW(...)   trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
1384de4bebcSJens Wiklander #endif
1394de4bebcSJens Wiklander 
1404de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_DEBUG level */
1414de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_DEBUG)
1424de4bebcSJens Wiklander #define DMSG_RAW(...)   (void)0
1434de4bebcSJens Wiklander #else
144*44202a48SPascal Brand #define DMSG_RAW(...)   trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
1454de4bebcSJens Wiklander #endif
1464de4bebcSJens Wiklander 
1474de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_FLOW level */
1484de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL < TRACE_FLOW)
1494de4bebcSJens Wiklander #define FMSG_RAW(...)   (void)0
1504de4bebcSJens Wiklander #else
151*44202a48SPascal Brand #define FMSG_RAW(...)   trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
1524de4bebcSJens Wiklander #endif
1534de4bebcSJens Wiklander 
1544de4bebcSJens Wiklander #if (CFG_TRACE_LEVEL == 0)
1554de4bebcSJens Wiklander #define SMSG(...)   (void)0
1564de4bebcSJens Wiklander static inline void trace_set_level(int level __unused)
1574de4bebcSJens Wiklander {
1584de4bebcSJens Wiklander }
1594de4bebcSJens Wiklander 
1604de4bebcSJens Wiklander static inline int trace_get_level(void)
1614de4bebcSJens Wiklander {
1624de4bebcSJens Wiklander 	return 0;
1634de4bebcSJens Wiklander }
1644de4bebcSJens Wiklander #else
1654de4bebcSJens Wiklander /*
1664de4bebcSJens Wiklander  * Synchronised flushed trace, an Always message straight to HW trace IP.
1674de4bebcSJens Wiklander  * Current only supported inside OP-TEE kernel, will be just like an EMSG()
1684de4bebcSJens Wiklander  * in another context.
1694de4bebcSJens Wiklander  */
1704de4bebcSJens Wiklander #define SMSG(...)   \
171*44202a48SPascal Brand 	trace_printf(__func__, __LINE__, TRACE_ERROR, true, true, __VA_ARGS__)
1724de4bebcSJens Wiklander 
1734de4bebcSJens Wiklander /* Accessors */
1744de4bebcSJens Wiklander void trace_set_level(int level);
1754de4bebcSJens Wiklander int trace_get_level(void);
1764de4bebcSJens Wiklander 
1774de4bebcSJens Wiklander #endif /* CFG_TRACE_LEVEL */
1784de4bebcSJens Wiklander 
1794de4bebcSJens Wiklander #endif /* TRACE_H */
180