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