1*1bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */ 24de4bebcSJens Wiklander /* 34de4bebcSJens Wiklander * Copyright (c) 2014, STMicroelectronics International N.V. 44de4bebcSJens Wiklander * All rights reserved. 54de4bebcSJens Wiklander * 64de4bebcSJens Wiklander * Redistribution and use in source and binary forms, with or without 74de4bebcSJens Wiklander * modification, are permitted provided that the following conditions are met: 84de4bebcSJens Wiklander * 94de4bebcSJens Wiklander * 1. Redistributions of source code must retain the above copyright notice, 104de4bebcSJens Wiklander * this list of conditions and the following disclaimer. 114de4bebcSJens Wiklander * 124de4bebcSJens Wiklander * 2. Redistributions in binary form must reproduce the above copyright notice, 134de4bebcSJens Wiklander * this list of conditions and the following disclaimer in the documentation 144de4bebcSJens Wiklander * and/or other materials provided with the distribution. 154de4bebcSJens Wiklander * 164de4bebcSJens Wiklander * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 174de4bebcSJens Wiklander * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 184de4bebcSJens Wiklander * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 194de4bebcSJens Wiklander * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 204de4bebcSJens Wiklander * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 214de4bebcSJens Wiklander * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 224de4bebcSJens Wiklander * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 234de4bebcSJens Wiklander * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 244de4bebcSJens Wiklander * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 254de4bebcSJens Wiklander * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 264de4bebcSJens Wiklander * POSSIBILITY OF SUCH DAMAGE. 274de4bebcSJens Wiklander */ 284de4bebcSJens Wiklander #ifndef TRACE_H 294de4bebcSJens Wiklander #define TRACE_H 304de4bebcSJens Wiklander 314de4bebcSJens Wiklander #include <stdbool.h> 324de4bebcSJens Wiklander #include <stddef.h> 334de4bebcSJens Wiklander #include <compiler.h> 344de4bebcSJens Wiklander #include <trace_levels.h> 354de4bebcSJens Wiklander 364de4bebcSJens Wiklander #define MAX_PRINT_SIZE 256 374de4bebcSJens Wiklander #define MAX_FUNC_PRINT_SIZE 32 384de4bebcSJens Wiklander 398a1e7b89SJerome Forissier #ifndef TRACE_LEVEL 408a1e7b89SJerome Forissier #define TRACE_LEVEL TRACE_MAX 414de4bebcSJens Wiklander #endif 424de4bebcSJens Wiklander 434de4bebcSJens Wiklander /* 444de4bebcSJens Wiklander * Symbols provided by the entity that uses this API. 454de4bebcSJens Wiklander */ 460e91d797SJean-Michel Delorme extern int trace_level; 474de4bebcSJens Wiklander extern const char trace_ext_prefix[]; 48bfc6487bSJerome Forissier void trace_ext_puts(const char *str); 494de4bebcSJens Wiklander int trace_ext_get_thread_id(void); 50e5dc28ccSPascal Brand void trace_set_level(int level); 51e5dc28ccSPascal Brand int trace_get_level(void); 524de4bebcSJens Wiklander 534de4bebcSJens Wiklander /* Internal functions used by the macros below */ 5444202a48SPascal Brand void trace_printf(const char *func, int line, int level, bool level_ok, 55bfc6487bSJerome Forissier const char *fmt, ...) __printf(5, 6); 564de4bebcSJens Wiklander 5744202a48SPascal Brand #define trace_printf_helper(level, level_ok, ...) \ 5844202a48SPascal Brand trace_printf(__func__, __LINE__, (level), (level_ok), \ 59bfc6487bSJerome Forissier __VA_ARGS__) 6044202a48SPascal Brand 6144202a48SPascal Brand /* Formatted trace tagged with level independent */ 628a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0) 6344202a48SPascal Brand #define MSG(...) (void)0 6444202a48SPascal Brand #else 6544202a48SPascal Brand #define MSG(...) trace_printf_helper(0, false, __VA_ARGS__) 6644202a48SPascal Brand #endif 674de4bebcSJens Wiklander 684de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_ERROR level */ 698a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_ERROR) 704de4bebcSJens Wiklander #define EMSG(...) (void)0 714de4bebcSJens Wiklander #else 7244202a48SPascal Brand #define EMSG(...) trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__) 734de4bebcSJens Wiklander #endif 744de4bebcSJens Wiklander 754de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_INFO level */ 768a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_INFO) 774de4bebcSJens Wiklander #define IMSG(...) (void)0 784de4bebcSJens Wiklander #else 7944202a48SPascal Brand #define IMSG(...) trace_printf_helper(TRACE_INFO, true, __VA_ARGS__) 804de4bebcSJens Wiklander #endif 814de4bebcSJens Wiklander 824de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_DEBUG level */ 838a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG) 844de4bebcSJens Wiklander #define DMSG(...) (void)0 854de4bebcSJens Wiklander #else 8644202a48SPascal Brand #define DMSG(...) trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__) 874de4bebcSJens Wiklander #endif 884de4bebcSJens Wiklander 894de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level */ 908a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_FLOW) 914de4bebcSJens Wiklander #define FMSG(...) (void)0 924de4bebcSJens Wiklander #else 9344202a48SPascal Brand #define FMSG(...) trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__) 944de4bebcSJens Wiklander #endif 954de4bebcSJens Wiklander 964de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */ 974de4bebcSJens Wiklander #define INMSG(...) FMSG("> " __VA_ARGS__) 984de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */ 994de4bebcSJens Wiklander #define OUTMSG(...) FMSG("< " __VA_ARGS__) 1004de4bebcSJens Wiklander /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print 1014de4bebcSJens Wiklander * an error message if r != 0 */ 1024de4bebcSJens Wiklander #define OUTRMSG(r) \ 1034de4bebcSJens Wiklander do { \ 1044de4bebcSJens Wiklander OUTMSG("r=[%x]", r); \ 1054de4bebcSJens Wiklander return r; \ 1064de4bebcSJens Wiklander } while (0) 1074de4bebcSJens Wiklander 108e5dc28ccSPascal Brand void dhex_dump(const char *function, int line, int level, 109e5dc28ccSPascal Brand const void *buf, int len); 1108a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG) 1114de4bebcSJens Wiklander #define DHEXDUMP(buf, len) (void)0 1124de4bebcSJens Wiklander #else 1134de4bebcSJens Wiklander #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \ 1144de4bebcSJens Wiklander buf, len) 1154de4bebcSJens Wiklander #endif 1164de4bebcSJens Wiklander 11744202a48SPascal Brand 1184de4bebcSJens Wiklander /* Trace api without trace formatting */ 1194de4bebcSJens Wiklander 12044202a48SPascal Brand #define trace_printf_helper_raw(level, level_ok, ...) \ 121bfc6487bSJerome Forissier trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__) 12244202a48SPascal Brand 12344202a48SPascal Brand /* No formatted trace tagged with level independent */ 1248a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0) 12544202a48SPascal Brand #define MSG_RAW(...) (void)0 12644202a48SPascal Brand #else 12744202a48SPascal Brand #define MSG_RAW(...) trace_printf_helper_raw(0, false, __VA_ARGS__) 12844202a48SPascal Brand #endif 12944202a48SPascal Brand 1304de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_ERROR level */ 1318a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_ERROR) 1324de4bebcSJens Wiklander #define EMSG_RAW(...) (void)0 1334de4bebcSJens Wiklander #else 13444202a48SPascal Brand #define EMSG_RAW(...) trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__) 1354de4bebcSJens Wiklander #endif 1364de4bebcSJens Wiklander 1374de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_INFO level */ 1388a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_INFO) 1394de4bebcSJens Wiklander #define IMSG_RAW(...) (void)0 1404de4bebcSJens Wiklander #else 14144202a48SPascal Brand #define IMSG_RAW(...) trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__) 1424de4bebcSJens Wiklander #endif 1434de4bebcSJens Wiklander 1444de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_DEBUG level */ 1458a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_DEBUG) 1464de4bebcSJens Wiklander #define DMSG_RAW(...) (void)0 1474de4bebcSJens Wiklander #else 14844202a48SPascal Brand #define DMSG_RAW(...) trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__) 1494de4bebcSJens Wiklander #endif 1504de4bebcSJens Wiklander 1514de4bebcSJens Wiklander /* No formatted trace tagged with TRACE_FLOW level */ 1528a1e7b89SJerome Forissier #if (TRACE_LEVEL < TRACE_FLOW) 1534de4bebcSJens Wiklander #define FMSG_RAW(...) (void)0 1544de4bebcSJens Wiklander #else 15544202a48SPascal Brand #define FMSG_RAW(...) trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__) 1564de4bebcSJens Wiklander #endif 1574de4bebcSJens Wiklander 1588a1e7b89SJerome Forissier #if (TRACE_LEVEL <= 0) 1594de4bebcSJens Wiklander #define SMSG(...) (void)0 1604de4bebcSJens Wiklander #else 1614de4bebcSJens Wiklander /* 1624de4bebcSJens Wiklander * Synchronised flushed trace, an Always message straight to HW trace IP. 1634de4bebcSJens Wiklander * Current only supported inside OP-TEE kernel, will be just like an EMSG() 1644de4bebcSJens Wiklander * in another context. 1654de4bebcSJens Wiklander */ 1664de4bebcSJens Wiklander #define SMSG(...) \ 167bfc6487bSJerome Forissier trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__) 1684de4bebcSJens Wiklander 1698a1e7b89SJerome Forissier #endif /* TRACE_LEVEL */ 1704de4bebcSJens Wiklander 17131a29642SJerome Forissier #if defined(__KERNEL__) && defined(CFG_UNWIND) 172a681fabaSJerome Forissier #include <kernel/unwind.h> 173a681fabaSJerome Forissier #define _PRINT_STACK 174a681fabaSJerome Forissier #endif 175a681fabaSJerome Forissier 176a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_ERROR) 17731a29642SJerome Forissier #define EPRINT_STACK() print_kernel_stack(TRACE_ERROR) 178a681fabaSJerome Forissier #else 179a681fabaSJerome Forissier #define EPRINT_STACK() (void)0 180a681fabaSJerome Forissier #endif 181a681fabaSJerome Forissier 182a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_INFO) 18331a29642SJerome Forissier #define IPRINT_STACK() print_kernel_stack(TRACE_INFO) 184a681fabaSJerome Forissier #else 185a681fabaSJerome Forissier #define IPRINT_STACK() (void)0 186a681fabaSJerome Forissier #endif 187a681fabaSJerome Forissier 188a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_DEBUG) 18931a29642SJerome Forissier #define DPRINT_STACK() print_kernel_stack(TRACE_DEBUG) 190a681fabaSJerome Forissier #else 191a681fabaSJerome Forissier #define DPRINT_STACK() (void)0 192a681fabaSJerome Forissier #endif 193a681fabaSJerome Forissier 194a681fabaSJerome Forissier #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_FLOW) 19531a29642SJerome Forissier #define FPRINT_STACK() print_kernel_stack(TRACE_FLOW) 196a681fabaSJerome Forissier #else 197a681fabaSJerome Forissier #define FPRINT_STACK() (void)0 198a681fabaSJerome Forissier #endif 199a681fabaSJerome Forissier 20031a29642SJerome Forissier #if defined(__KERNEL__) && defined(CFG_UNWIND) 201a681fabaSJerome Forissier #undef _PRINT_STACK 202a681fabaSJerome Forissier #endif 203a681fabaSJerome Forissier 2044de4bebcSJens Wiklander #endif /* TRACE_H */ 205