summaryrefslogtreecommitdiff
path: root/hifi/xaf/hifi-dpf/include/xf-debug.h
blob: c416cc01cf808f112ac350e7fab099f4c67a210a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

/*******************************************************************************
 * xf-debug.h
 *
 * Debugging interface for Xtensa Audio DSP codec server
 *
 *******************************************************************************/

#ifndef __XF_H
#error  "xf-debug.h mustn't be included directly"
#endif

#include "dsp_debug.h"

/*******************************************************************************
 * Auxiliary macros (put into "xf-types.h"?)
 ******************************************************************************/

#ifndef offset_of
#define offset_of(type, member)         \
    ((int)&(((const type *)(0))->member))
#endif

#ifndef container_of
#define container_of(ptr, type, member) \
    ((type *)((void *)(ptr) - offset_of(type, member)))
#endif 

/* ...next power-of-two calculation */
#define xf_next_power_of_two(v)     __xf_power_of_two_1((v) - 1)
#define __xf_power_of_two_1(v)      __xf_power_of_two_2((v) | ((v) >> 1))
#define __xf_power_of_two_2(v)      __xf_power_of_two_3((v) | ((v) >> 2))
#define __xf_power_of_two_3(v)      __xf_power_of_two_4((v) | ((v) >> 4))
#define __xf_power_of_two_4(v)      __xf_power_of_two_5((v) | ((v) >> 8))
#define __xf_power_of_two_5(v)      __xf_power_of_two_6((v) | ((v) >> 16))
#define __xf_power_of_two_6(v)      ((v) + 1)

/* ...check if non-zero value is a power-of-two */
#define xf_is_power_of_two(v)       (((v) & ((v) - 1)) == 0)

/*******************************************************************************
 * Bug check for constant conditions (file scope)
 ******************************************************************************/

#define __C_BUG(n)      __C_BUG2(n)
#define __C_BUG2(n)     __c_bug_##n
#define C_BUG(expr)     typedef char __C_BUG(__LINE__)[(expr) ? -1 : 1]

/*******************************************************************************
 * Compilation-time types control
 ******************************************************************************/

#if XF_DEBUG
#define __C_TYPE_CONTROL(d, type)       ((void) ((d) != (type*) 0))
#else
#define __C_TYPE_CONTROL(d, type)       ((void) 0)
#endif

/*******************************************************************************
 * Unused variable
 ******************************************************************************/

#define C_UNUSED(v)                     (void)(0 ? (v) = (v), 1 : 0)

/*******************************************************************************
 * Auxiliary macros
 ******************************************************************************/

/* ...define a stub for unused declarator */
#define __xf_stub(tag, line)            __xf_stub2(tag, line)
#define __xf_stub2(tag, line)           typedef int __xf_##tag##_##line

/* ...convert anything into string */
#define __xf_string(x)                  __xf_string2(x)
#define __xf_string2(x)                 #x

/*******************************************************************************
 * Tracing facility
 ******************************************************************************/

#if XF_TRACE

/* ...tracing to communication processor */
extern int  xf_trace(const char *format, ...) __attribute__((format (printf, 1, 2)));

/* ...tracing facility initialization */
extern void xf_trace_init(const char *banner);

/* ...initialize tracing facility */
//#define TRACE_INIT(banner)              (xf_trace_init(banner))
#define TRACE_INIT(banner)              ({ dsp_debug_init(); DSP_TRACE("\n"banner); })

/* ...trace tag definition */
#define TRACE_TAG(tag, on)              enum { __xf_trace_##tag = on }

/* ...check if the trace tag is enabled */
#define TRACE_CFG(tag)                  (__xf_trace_##tag)

/* ...tagged tracing primitive */
#define TRACE(tag, fmt, ...)            (void)(__xf_trace_##tag ? __xf_trace(tag, __xf_format##fmt, ## __VA_ARGS__), 1 : 0)

/*******************************************************************************
 * Tagged tracing formats
 ******************************************************************************/

/* ...tracing primitive */
#define __xf_trace(tag, fmt, ...)       \
    ({ __attribute__((unused)) const char *__xf_tag = #tag; DSP_TRACE(fmt, ## __VA_ARGS__); })

/* ...just a format string */
#define __xf_format_n(fmt)              fmt

/* ...module tag and trace tag shown */
#define __xf_format_b(fmt)              "[%s.%s] " fmt, __xf_string(MODULE_TAG), __xf_tag

/* ...module tag, trace tag, file name and line shown */
#define __xf_format_x(fmt)              "[%s.%s] - %s@%d - " fmt,  __xf_string(MODULE_TAG), __xf_tag, __FILE__, __LINE__

/*******************************************************************************
 * Globally defined tags
 ******************************************************************************/

/* ...unconditionally OFF */
TRACE_TAG(0, 0);

/* ...unconditionally ON */
TRACE_TAG(1, 1);

/* ...error output - on by default */
TRACE_TAG(ERROR, 1);

#else

#define TRACE_INIT(banner)              (void)0
#define TRACE_TAG(tag, on)              __xf_stub(trace_##tag, __LINE__)
#define TRACE_CFG(tag)			0
#define TRACE(tag, fmt, ...)            (void)0
#define __xf_trace(tag, fmt, ...)       (void)0

#endif  /* XF_TRACE */

/*******************************************************************************
 * Bugchecks
 ******************************************************************************/

#if XF_DEBUG

/* ...run-time bugcheck */
#define BUG(cond, fmt, ...)                                     \
do                                                              \
{                                                               \
    if (cond)                                                   \
    {                                                           \
        /* ...output message */                                 \
        __xf_trace(BUG, __xf_format##fmt, ## __VA_ARGS__);      \
                                                                \
        /* ...and die (tbd) */                                  \
        __xf_abort();                                           \
    }                                                           \
}                                                               \
while (0)

#else
#define BUG(cond, fmt, ...)             (void)0
#endif  /* XF_DEBUG */

/*******************************************************************************
 * Run-time error processing
 ******************************************************************************/

/* ...check the API call succeeds */
#define XF_CHK_API(cond)                                \
({                                                      \
    int __ret;                                          \
                                                        \
    if ((__ret = (int)(cond)) < 0)                      \
    {                                                   \
        TRACE(ERROR, _x("API error: %d"), __ret);       \
        return __ret;                                   \
    }                                                   \
    __ret;                                              \
})

/* ...check the condition is true */
#define XF_CHK_ERR(cond, error)                         \
({                                                      \
    int __ret;                                          \
                                                        \
    if (!(__ret = (int)(cond)))                         \
    {                                                   \
        TRACE(ERROR, _x("check failed: %d"), __ret);    \
        return (error);                                 \
    }                                                   \
    __ret;                                              \
})