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