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 TEEC_TRACE_H
28 #define TEEC_TRACE_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 
38 #ifndef BINARY_PREFIX
39 #error "BINARY_PREFIX not defined"
40 #endif
41 
42 /*
43  * Trace levels.
44  *
45  * ERROR is used when some kind of error has happened, this is most likely the
46  * print you will use most of the time when you report some kind of error.
47  *
48  * INFO is used when you want to print some 'normal' text to the user.
49  * This is the default level.
50  *
51  * DEBUG is used to print extra information to enter deeply in the module.
52  *
53  * FLOW is used to print the execution flox, typically the in/out of functions.
54  *
55  * */
56 
57 #define TRACE_ERROR  1
58 #define TRACE_INFO   2
59 #define TRACE_DEBUG  3
60 #define TRACE_FLOW   4
61 
62 #if defined(DEBUGLEVEL_0) && !defined(DEBUGLEVEL)
63 #define DEBUGLEVEL TRACE_ERROR
64 #endif
65 
66 #if defined(DEBUGLEVEL_1) && !defined(DEBUGLEVEL)
67 #define DEBUGLEVEL TRACE_ERROR
68 #endif
69 
70 #if defined(DEBUGLEVEL_2) && !defined(DEBUGLEVEL)
71 #define DEBUGLEVEL TRACE_INFO
72 #endif
73 
74 #if defined(DEBUGLEVEL_3) && !defined(DEBUGLEVEL)
75 #define DEBUGLEVEL TRACE_DEBUG
76 #endif
77 
78 #if defined(DEBUGLEVEL_4) && !defined(DEBUGLEVEL)
79 #define DEBUGLEVEL TRACE_FLOW
80 #endif
81 
82 #ifndef DEBUGLEVEL
83 /* Default debug level. */
84 #define DEBUGLEVEL TRACE_INFO
85 #endif
86 
87 /*
88  * This define make sure that parameters are checked in the same manner as it
89  * is done in the normal printf function.
90  */
91 #define __PRINTFLIKE(__fmt, __varargs) __attribute__\
92 	((__format__(__printf__, __fmt, __varargs)))
93 
94 void _dprintf(const char *function, int line, int level, const char *prefix,
95 	      const char *fmt, ...) __PRINTFLIKE(5, 6);
96 
97 #define dprintf(level, x...) do { \
98 		if ((level) <= DEBUGLEVEL) { \
99 			_dprintf(__func__, __LINE__, level, \
100 				 BINARY_PREFIX, x); \
101 		} \
102 	} while (0)
103 
104 #define EMSG(fmt, ...)   dprintf(TRACE_ERROR, fmt "\n", ##__VA_ARGS__)
105 #define IMSG(fmt, ...)   dprintf(TRACE_INFO, fmt "\n", ##__VA_ARGS__)
106 #define DMSG(fmt, ...)   dprintf(TRACE_DEBUG, fmt "\n", ##__VA_ARGS__)
107 #define FMSG(fmt, ...)   dprintf(TRACE_FLOW, fmt "\n", ##__VA_ARGS__)
108 
109 #define INMSG(fmt, ...)  FMSG("> " fmt, ##__VA_ARGS__)
110 #define OUTMSG(fmt, ...) FMSG("< " fmt, ##__VA_ARGS__)
111 #define OUTRMSG(r)                              \
112 	do {                                            \
113 		if (r)                                      \
114 			EMSG("Function returns with [%d]", r);  \
115 		OUTMSG("r=[%d]", r);                        \
116 		return r;                                   \
117 	} while (0)
118 
119 #define dprintf_raw(level, x...) do { \
120 		if ((level) <= DEBUGLEVEL) \
121 			_dprintf(0, 0, (level), BINARY_PREFIX, x); \
122 	} while (0)
123 
124 #define EMSG_RAW(fmt, ...)   dprintf_raw(TRACE_ERROR, fmt, ##__VA_ARGS__)
125 #define IMSG_RAW(fmt, ...)   dprintf_raw(TRACE_INFO, fmt, ##__VA_ARGS__)
126 #define DMSG_RAW(fmt, ...)   dprintf_raw(TRACE_DEBUG, fmt, ##__VA_ARGS__)
127 #define FMSG_RAW(fmt, ...)   dprintf_raw(TRACE_FLOW, fmt, ##__VA_ARGS__)
128 
129 /*
130  * This function will hex and ascii dump a buffer.
131  *
132  * Note that this function will only print if debug flag
133  * DEBUGLEVEL is INFO or FLOOD.
134  *
135  * @param bname     Information string describing the buffer.
136  * @param buffer    Pointer to the buffer.
137  * @param blen      Length of the buffer.
138  *
139  * @return void
140  */
141 void dump_buffer(const char *bname, const uint8_t *buffer, size_t blen);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif
148