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