1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef TRACE_H 6 #define TRACE_H 7 8 #include <compiler.h> 9 #include <stdarg.h> 10 #include <stdbool.h> 11 #include <stddef.h> 12 #include <trace_levels.h> 13 14 #define MAX_PRINT_SIZE 256 15 #define MAX_FUNC_PRINT_SIZE 32 16 17 #ifndef TRACE_LEVEL 18 #define TRACE_LEVEL TRACE_MAX 19 #endif 20 21 /* 22 * Symbols provided by the entity that uses this API. 23 */ 24 extern int trace_level; 25 extern const char trace_ext_prefix[]; 26 void trace_ext_puts(const char *str); 27 int trace_ext_get_thread_id(void); 28 void trace_set_level(int level); 29 int trace_get_level(void); 30 void plat_trace_ext_puts(const char *str); 31 32 /* Internal functions used by the macros below */ 33 void trace_vprintf(const char *func, int line, int level, bool level_ok, 34 const char *fmt, va_list args) __printf(5, 0); 35 void trace_printf(const char *func, int line, int level, bool level_ok, 36 const char *fmt, ...) __printf(5, 6); 37 38 #define trace_printf_helper(level, level_ok, ...) \ 39 trace_printf(__func__, __LINE__, (level), (level_ok), \ 40 __VA_ARGS__) 41 42 /* Formatted trace tagged with level independent */ 43 #if (TRACE_LEVEL <= 0) 44 #define MSG(...) (void)0 45 #else 46 #define MSG(...) trace_printf_helper(0, false, __VA_ARGS__) 47 #endif 48 49 /* Formatted trace tagged with TRACE_ERROR level */ 50 #if (TRACE_LEVEL < TRACE_ERROR) 51 #define EMSG(...) (void)0 52 #else 53 #define EMSG(...) trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__) 54 #endif 55 56 /* Formatted trace tagged with TRACE_INFO level */ 57 #if (TRACE_LEVEL < TRACE_INFO) 58 #define IMSG(...) (void)0 59 #else 60 #define IMSG(...) trace_printf_helper(TRACE_INFO, true, __VA_ARGS__) 61 #endif 62 63 /* Formatted trace tagged with TRACE_DEBUG level */ 64 #if (TRACE_LEVEL < TRACE_DEBUG) 65 #define DMSG(...) (void)0 66 #else 67 #define DMSG(...) trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__) 68 #endif 69 70 /* Formatted trace tagged with TRACE_FLOW level */ 71 #if (TRACE_LEVEL < TRACE_FLOW) 72 #define FMSG(...) (void)0 73 #else 74 #define FMSG(...) trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__) 75 #endif 76 77 /* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */ 78 #define INMSG(...) FMSG("> " __VA_ARGS__) 79 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */ 80 #define OUTMSG(...) FMSG("< " __VA_ARGS__) 81 /* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print 82 * an error message if r != 0 */ 83 #define OUTRMSG(r) \ 84 do { \ 85 OUTMSG("r=[%x]", r); \ 86 return r; \ 87 } while (0) 88 89 void dhex_dump(const char *function, int line, int level, 90 const void *buf, int len); 91 #if (TRACE_LEVEL < TRACE_DEBUG) 92 #define DHEXDUMP(buf, len) (void)0 93 #else 94 #define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \ 95 buf, len) 96 #endif 97 98 99 /* Trace api without trace formatting */ 100 101 #define trace_printf_helper_raw(level, level_ok, ...) \ 102 trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__) 103 104 /* No formatted trace tagged with level independent */ 105 #if (TRACE_LEVEL <= 0) 106 #define MSG_RAW(...) (void)0 107 #else 108 #define MSG_RAW(...) trace_printf_helper_raw(0, false, __VA_ARGS__) 109 #endif 110 111 /* No formatted trace tagged with TRACE_ERROR level */ 112 #if (TRACE_LEVEL < TRACE_ERROR) 113 #define EMSG_RAW(...) (void)0 114 #else 115 #define EMSG_RAW(...) trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__) 116 #endif 117 118 /* No formatted trace tagged with TRACE_INFO level */ 119 #if (TRACE_LEVEL < TRACE_INFO) 120 #define IMSG_RAW(...) (void)0 121 #else 122 #define IMSG_RAW(...) trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__) 123 #endif 124 125 /* No formatted trace tagged with TRACE_DEBUG level */ 126 #if (TRACE_LEVEL < TRACE_DEBUG) 127 #define DMSG_RAW(...) (void)0 128 #else 129 #define DMSG_RAW(...) trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__) 130 #endif 131 132 /* No formatted trace tagged with TRACE_FLOW level */ 133 #if (TRACE_LEVEL < TRACE_FLOW) 134 #define FMSG_RAW(...) (void)0 135 #else 136 #define FMSG_RAW(...) trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__) 137 #endif 138 139 #if (TRACE_LEVEL <= 0) 140 #define SMSG(...) (void)0 141 #else 142 /* 143 * Synchronised flushed trace, an Always message straight to HW trace IP. 144 * Current only supported inside OP-TEE kernel, will be just like an EMSG() 145 * in another context. 146 */ 147 #define SMSG(...) \ 148 trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__) 149 150 #endif /* TRACE_LEVEL */ 151 152 #if defined(__KERNEL__) && defined(CFG_UNWIND) 153 #include <kernel/unwind.h> 154 #define _PRINT_STACK 155 #endif 156 157 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_ERROR) 158 #define EPRINT_STACK() print_kernel_stack(TRACE_ERROR) 159 #else 160 #define EPRINT_STACK() (void)0 161 #endif 162 163 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_INFO) 164 #define IPRINT_STACK() print_kernel_stack(TRACE_INFO) 165 #else 166 #define IPRINT_STACK() (void)0 167 #endif 168 169 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_DEBUG) 170 #define DPRINT_STACK() print_kernel_stack(TRACE_DEBUG) 171 #else 172 #define DPRINT_STACK() (void)0 173 #endif 174 175 #if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_FLOW) 176 #define FPRINT_STACK() print_kernel_stack(TRACE_FLOW) 177 #else 178 #define FPRINT_STACK() (void)0 179 #endif 180 181 #if defined(__KERNEL__) && defined(CFG_UNWIND) 182 #undef _PRINT_STACK 183 #endif 184 185 #endif /* TRACE_H */ 186