TA-REF
trace.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27#ifndef TRACE_H
28#define TRACE_H
29
30#include <stdbool.h>
31#include <stddef.h>
32#include <compiler.h>
33#include <trace_levels.h>
34
35#ifndef DOXYGEN_SHOULD_SKIP_THIS
36#define MAX_PRINT_SIZE 256
37#define MAX_FUNC_PRINT_SIZE 32
38
39#ifndef TRACE_LEVEL
40#define TRACE_LEVEL TRACE_MAX
41#endif
42#endif /*DOXYGEN_SHOULD_SKIP_THIS*/
43
44/*
45 * Symbols provided by the entity that uses this API.
46 */
47extern int trace_level;
48extern const char trace_ext_prefix[];
49void trace_ext_puts(const char *str);
51void trace_set_level(int level);
53
54/* Internal functions used by the macros below */
55void trace_printf(const char *func, int line, int level, bool level_ok,
56 const char *fmt, ...) __printf(5, 6);
57
58#ifndef DOXYGEN_SHOULD_SKIP_THIS
59#define trace_printf_helper(level, level_ok, ...) \
60 trace_printf(__func__, __LINE__, (level), (level_ok), \
61 __VA_ARGS__)
62
63/* Formatted trace tagged with level independent */
64#if (TRACE_LEVEL <= 0)
65#define MSG(...) (void)0
66#else
67#define MSG(...) trace_printf_helper(0, false, __VA_ARGS__)
68#endif
69
70/* Formatted trace tagged with TRACE_ERROR level */
71#if (TRACE_LEVEL < TRACE_ERROR)
72#define EMSG(...) (void)0
73#else
74#define EMSG(...) trace_printf_helper(TRACE_ERROR, true, __VA_ARGS__)
75#endif
76
77/* Formatted trace tagged with TRACE_INFO level */
78#if (TRACE_LEVEL < TRACE_INFO)
79#define IMSG(...) (void)0
80#else
81#define IMSG(...) trace_printf_helper(TRACE_INFO, true, __VA_ARGS__)
82#endif
83
84/* Formatted trace tagged with TRACE_DEBUG level */
85#if (TRACE_LEVEL < TRACE_DEBUG)
86#define DMSG(...) (void)0
87#else
88#define DMSG(...) trace_printf_helper(TRACE_DEBUG, true, __VA_ARGS__)
89#endif
90
91/* Formatted trace tagged with TRACE_FLOW level */
92#if (TRACE_LEVEL < TRACE_FLOW)
93#define FMSG(...) (void)0
94#else
95#define FMSG(...) trace_printf_helper(TRACE_FLOW, true, __VA_ARGS__)
96#endif
97
98/* Formatted trace tagged with TRACE_FLOW level and prefix with '> ' */
99#define INMSG(...) FMSG("> " __VA_ARGS__)
100/* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' */
101#define OUTMSG(...) FMSG("< " __VA_ARGS__)
102/* Formatted trace tagged with TRACE_FLOW level and prefix with '< ' and print
103 * an error message if r != 0 */
104#define OUTRMSG(r) \
105 do { \
106 OUTMSG("r=[%x]", r); \
107 return r; \
108 } while (0)
109
110#endif /*DOXYGEN_SHOULD_SKIP_THIS*/
111
112void dhex_dump(const char *function, int line, int level,
113 const void *buf, int len);
114
115
116#ifndef DOXYGEN_SHOULD_SKIP_THIS
117#if (TRACE_LEVEL < TRACE_DEBUG)
118#define DHEXDUMP(buf, len) (void)0
119#else
120#define DHEXDUMP(buf, len) dhex_dump(__func__, __LINE__, TRACE_DEBUG, \
121 buf, len)
122#endif
123
124
125/* Trace api without trace formatting */
126
127#define trace_printf_helper_raw(level, level_ok, ...) \
128 trace_printf(NULL, 0, (level), (level_ok), __VA_ARGS__)
129
130/* No formatted trace tagged with level independent */
131#if (TRACE_LEVEL <= 0)
132#define MSG_RAW(...) (void)0
133#else
134#define MSG_RAW(...) trace_printf_helper_raw(0, false, __VA_ARGS__)
135#endif
136
137/* No formatted trace tagged with TRACE_ERROR level */
138#if (TRACE_LEVEL < TRACE_ERROR)
139#define EMSG_RAW(...) (void)0
140#else
141#define EMSG_RAW(...) trace_printf_helper_raw(TRACE_ERROR, true, __VA_ARGS__)
142#endif
143
144/* No formatted trace tagged with TRACE_INFO level */
145#if (TRACE_LEVEL < TRACE_INFO)
146#define IMSG_RAW(...) (void)0
147#else
148#define IMSG_RAW(...) trace_printf_helper_raw(TRACE_INFO, true, __VA_ARGS__)
149#endif
150
151/* No formatted trace tagged with TRACE_DEBUG level */
152#if (TRACE_LEVEL < TRACE_DEBUG)
153#define DMSG_RAW(...) (void)0
154#else
155#define DMSG_RAW(...) trace_printf_helper_raw(TRACE_DEBUG, true, __VA_ARGS__)
156#endif
157
158/* No formatted trace tagged with TRACE_FLOW level */
159#if (TRACE_LEVEL < TRACE_FLOW)
160#define FMSG_RAW(...) (void)0
161#else
162#define FMSG_RAW(...) trace_printf_helper_raw(TRACE_FLOW, true, __VA_ARGS__)
163#endif
164
165#if (TRACE_LEVEL <= 0)
166#define SMSG(...) (void)0
167#else
168/*
169 * Synchronised flushed trace, an Always message straight to HW trace IP.
170 * Current only supported inside OP-TEE kernel, will be just like an EMSG()
171 * in another context.
172 */
173#define SMSG(...) \
174 trace_printf(__func__, __LINE__, TRACE_ERROR, true, __VA_ARGS__)
175
176#endif /* TRACE_LEVEL */
177
178#if defined(__KERNEL__) && defined(CFG_UNWIND)
179#include <kernel/unwind.h>
180#define _PRINT_STACK
181#endif
182
183#if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_ERROR)
184#define EPRINT_STACK() print_kernel_stack(TRACE_ERROR)
185#else
186#define EPRINT_STACK() (void)0
187#endif
188
189#if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_INFO)
190#define IPRINT_STACK() print_kernel_stack(TRACE_INFO)
191#else
192#define IPRINT_STACK() (void)0
193#endif
194
195#if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_DEBUG)
196#define DPRINT_STACK() print_kernel_stack(TRACE_DEBUG)
197#else
198#define DPRINT_STACK() (void)0
199#endif
200
201#if defined(_PRINT_STACK) && (TRACE_LEVEL >= TRACE_FLOW)
202#define FPRINT_STACK() print_kernel_stack(TRACE_FLOW)
203#else
204#define FPRINT_STACK() (void)0
205#endif
206
207#if defined(__KERNEL__) && defined(CFG_UNWIND)
208#undef _PRINT_STACK
209#endif
210
211#endif /*DOXYGEN_SHOULD_SKIP_THIS*/
212#endif /* TRACE_H */
void trace_set_level(int level)
int trace_get_level(void)
int trace_level
const char trace_ext_prefix[]
void void dhex_dump(const char *function, int line, int level, const void *buf, int len)
int trace_ext_get_thread_id(void)
void trace_printf(const char *func, int line, int level, bool level_ok, const char *fmt,...) __printf(5
void trace_ext_puts(const char *str)