Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / sfc_netback / ci / tools / debug.h
1 /****************************************************************************
2  * Copyright 2002-2005: Level 5 Networks Inc.
3  * Copyright 2005-2008: Solarflare Communications Inc,
4  *                      9501 Jeronimo Road, Suite 250,
5  *                      Irvine, CA 92618, USA
6  *
7  * Maintained by Solarflare Communications
8  *  <linux-xen-drivers@solarflare.com>
9  *  <onload-dev@solarflare.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 as published
13  * by the Free Software Foundation, incorporated herein by reference.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  ****************************************************************************
24  */
25
26 /*! \cidoxg_include_ci_tools */
27
28 #ifndef __CI_TOOLS_DEBUG_H__
29 #define __CI_TOOLS_DEBUG_H__
30
31 #define CI_LOG_E(x)       x              /* errors      */
32 #define CI_LOG_W(x)       x              /* warnings    */
33 #define CI_LOG_I(x)       x              /* information */
34 #define CI_LOG_V(x)       x              /* verbose     */
35
36 /* Build time asserts. We paste the line number into the type name
37  * so that the macro can be used more than once per file even if the
38  * compiler objects to multiple identical typedefs. Collisions
39  * between use in different header files is still possible. */
40 #ifndef CI_BUILD_ASSERT
41 #define __CI_BUILD_ASSERT_NAME(_x) __CI_BUILD_ASSERT_ILOATHECPP(_x)
42 #define __CI_BUILD_ASSERT_ILOATHECPP(_x)  __CI_BUILD_ASSERT__ ##_x
43 #define CI_BUILD_ASSERT(e)\
44  typedef char  __CI_BUILD_ASSERT_NAME(__LINE__)[(e)?1:-1]
45 #endif
46
47
48 #ifdef NDEBUG
49
50 # define _ci_check(exp, file, line)
51 # define _ci_assert2(e, x, y, file, line)
52 # define _ci_assert(exp, file, line)
53 # define _ci_assert_equal(exp1, exp2, file, line)
54 # define _ci_assert_equiv(exp1, exp2, file, line)
55 # define _ci_assert_nequal(exp1, exp2, file, line)
56 # define _ci_assert_le(exp1, exp2, file, line)
57 # define _ci_assert_lt(exp1, exp2, file, line)
58 # define _ci_assert_ge(exp1, exp2, file, line)
59 # define _ci_assert_gt(exp1, exp2, file, line)
60 # define _ci_assert_impl(exp1, exp2, file, line)
61
62 # define _ci_verify(exp, file, line) \
63   do { \
64     (void)(exp); \
65   } while (0)
66
67 # define CI_DEBUG_TRY(exp) \
68   do { \
69     (void)(exp); \
70   } while (0)
71
72 #define CI_TRACE(exp,fmt)
73 #define CI_TRACE_INT(integer)
74 #define CI_TRACE_INT32(integer)
75 #define CI_TRACE_INT64(integer)
76 #define CI_TRACE_UINT(integer)
77 #define CI_TRACE_UINT32(integer)
78 #define CI_TRACE_UINT64(integer)
79 #define CI_TRACE_HEX(integer)
80 #define CI_TRACE_HEX32(integer)
81 #define CI_TRACE_HEX64(integer)
82 #define CI_TRACE_PTR(pointer)
83 #define CI_TRACE_STRING(string)
84 #define CI_TRACE_MAC(mac)
85 #define CI_TRACE_IP(ip_be32)
86 #define CI_TRACE_ARP(arp_pkt)
87
88 #else
89
90 # define _CI_ASSERT_FMT   "\nfrom %s:%d"
91
92 # define _ci_check(exp, file, line)                             \
93   do {                                                          \
94     if (CI_UNLIKELY(!(exp)))                                    \
95       ci_warn(("ci_check(%s)"_CI_ASSERT_FMT, #exp,              \
96                (file), (line)));                                \
97   } while (0)
98
99 /*
100  * NOTE: ci_fail() emits the file and line where the assert is actually
101  *       coded.
102  */
103
104 # define _ci_assert(exp, file, line)                            \
105   do {                                                          \
106     if (CI_UNLIKELY(!(exp)))                                    \
107       ci_fail(("ci_assert(%s)"_CI_ASSERT_FMT, #exp,             \
108                (file), (line)));                                \
109   } while (0)
110
111 # define _ci_assert2(e, x, y, file, line)  do {                 \
112     if(CI_UNLIKELY( ! (e) ))                                    \
113       ci_fail(("ci_assert(%s)\nwhere [%s=%"CI_PRIx64"] "        \
114                "[%s=%"CI_PRIx64"]\nat %s:%d\nfrom %s:%d", #e    \
115                , #x, (ci_uint64)(ci_uintptr_t)(x)               \
116                , #y, (ci_uint64)(ci_uintptr_t)(y),              \
117                __FILE__, __LINE__, (file), (line)));            \
118   } while (0)
119
120 # define _ci_verify(exp, file, line)                            \
121   do {                                                          \
122     if (CI_UNLIKELY(!(exp)))                                    \
123       ci_fail(("ci_verify(%s)"_CI_ASSERT_FMT, #exp,             \
124                (file), (line)));                                \
125   } while (0)
126
127 # define _ci_assert_equal(x, y, f, l)  _ci_assert2((x)==(y), x, y, (f), (l))
128 # define _ci_assert_nequal(x, y, f, l) _ci_assert2((x)!=(y), x, y, (f), (l))
129 # define _ci_assert_le(x, y, f, l)     _ci_assert2((x)<=(y), x, y, (f), (l))
130 # define _ci_assert_lt(x, y, f, l)     _ci_assert2((x)< (y), x, y, (f), (l))
131 # define _ci_assert_ge(x, y, f, l)     _ci_assert2((x)>=(y), x, y, (f), (l))
132 # define _ci_assert_gt(x, y, f, l)     _ci_assert2((x)> (y), x, y, (f), (l))
133 # define _ci_assert_or(x, y, f, l)     _ci_assert2((x)||(y), x, y, (f), (l))
134 # define _ci_assert_impl(x, y, f, l)   _ci_assert2(!(x) || (y), x, y, (f), (l))
135 # define _ci_assert_equiv(x, y, f, l)  _ci_assert2(!(x)== !(y), x, y, (f), (l))
136
137 #define _ci_assert_equal_msg(exp1, exp2, msg, file, line)       \
138   do {                                                          \
139     if (CI_UNLIKELY((exp1)!=(exp2)))                            \
140       ci_fail(("ci_assert_equal_msg(%s == %s) were "            \
141                "(%"CI_PRIx64":%"CI_PRIx64") with msg[%c%c%c%c]" \
142                _CI_ASSERT_FMT, #exp1, #exp2,                    \
143                (ci_uint64)(ci_uintptr_t)(exp1),                 \
144                (ci_uint64)(ci_uintptr_t)(exp2),                 \
145                (((ci_uint32)msg) >> 24) && 0xff,                \
146                (((ci_uint32)msg) >> 16) && 0xff,                \
147                (((ci_uint32)msg) >> 8 ) && 0xff,                \
148                (((ci_uint32)msg)      ) && 0xff,                \
149                (file), (line)));                                \
150   } while (0)
151
152 # define CI_DEBUG_TRY(exp)  CI_TRY(exp)
153
154 #define CI_TRACE(exp,fmt)                                               \
155   ci_log("%s:%d:%s] " #exp "=" fmt,                                     \
156          __FILE__, __LINE__, __FUNCTION__, (exp))
157
158
159 #define CI_TRACE_INT(integer)                                           \
160   ci_log("%s:%d:%s] " #integer "=%d",                                   \
161          __FILE__, __LINE__, __FUNCTION__, (integer))
162
163
164 #define CI_TRACE_INT32(integer)                                         \
165   ci_log("%s:%d:%s] " #integer "=%d",                                   \
166          __FILE__, __LINE__, __FUNCTION__, ((ci_int32)integer))
167
168
169 #define CI_TRACE_INT64(integer)                                         \
170   ci_log("%s:%d:%s] " #integer "=%lld",                                 \
171          __FILE__, __LINE__, __FUNCTION__, ((ci_int64)integer))
172
173
174 #define CI_TRACE_UINT(integer)                                          \
175   ci_log("%s:%d:%s] " #integer "=%ud",                                  \
176          __FILE__, __LINE__, __FUNCTION__, (integer))
177
178
179 #define CI_TRACE_UINT32(integer)                                        \
180   ci_log("%s:%d:%s] " #integer "=%ud",                                  \
181          __FILE__, __LINE__, __FUNCTION__, ((ci_uint32)integer))
182
183
184 #define CI_TRACE_UINT64(integer)                                        \
185   ci_log("%s:%d:%s] " #integer "=%ulld",                                \
186          __FILE__, __LINE__, __FUNCTION__, ((ci_uint64)integer))
187
188
189 #define CI_TRACE_HEX(integer)                                           \
190   ci_log("%s:%d:%s] " #integer "=0x%x",                                 \
191          __FILE__, __LINE__, __FUNCTION__, (integer))
192
193
194 #define CI_TRACE_HEX32(integer)                                         \
195   ci_log("%s:%d:%s] " #integer "=0x%x",                                 \
196          __FILE__, __LINE__, __FUNCTION__, ((ci_uint32)integer))
197
198
199 #define CI_TRACE_HEX64(integer)                                         \
200   ci_log("%s:%d:%s] " #integer "=0x%llx",                               \
201          __FILE__, __LINE__, __FUNCTION__, ((ci_uint64)integer))
202
203
204 #define CI_TRACE_PTR(pointer)                                           \
205   ci_log("%s:%d:%s] " #pointer "=0x%p",                                 \
206          __FILE__, __LINE__, __FUNCTION__, (pointer))
207
208
209 #define CI_TRACE_STRING(string)                                         \
210   ci_log("%s:%d:%s] " #string "=%s",                                    \
211          __FILE__, __LINE__, __FUNCTION__, (string))
212
213
214 #define CI_TRACE_MAC(mac)                                               \
215   ci_log("%s:%d:%s] " #mac "=" CI_MAC_PRINTF_FORMAT,                    \
216          __FILE__, __LINE__, __FUNCTION__, CI_MAC_PRINTF_ARGS(mac))
217
218
219 #define CI_TRACE_IP(ip_be32)                                            \
220   ci_log("%s:%d:%s] " #ip_be32 "=" CI_IP_PRINTF_FORMAT, __FILE__,       \
221          __LINE__, __FUNCTION__, CI_IP_PRINTF_ARGS(&(ip_be32)))
222
223
224 #define CI_TRACE_ARP(arp_pkt)                                           \
225   ci_log("%s:%d:%s]\n"CI_ARP_PRINTF_FORMAT,                             \
226          __FILE__, __LINE__, __FUNCTION__, CI_ARP_PRINTF_ARGS(arp_pkt))
227
228 #endif  /* NDEBUG */
229
230 #define ci_check(exp) \
231         _ci_check(exp, __FILE__, __LINE__)
232
233 #define ci_assert(exp) \
234         _ci_assert(exp, __FILE__, __LINE__)
235
236 #define ci_verify(exp) \
237         _ci_verify(exp, __FILE__, __LINE__)
238
239 #define ci_assert_equal(exp1, exp2) \
240         _ci_assert_equal(exp1, exp2, __FILE__, __LINE__)
241
242 #define ci_assert_equal_msg(exp1, exp2, msg) \
243         _ci_assert_equal_msg(exp1, exp2, msg, __FILE__, __LINE__)
244
245 #define ci_assert_nequal(exp1, exp2) \
246         _ci_assert_nequal(exp1, exp2, __FILE__, __LINE__)
247
248 #define ci_assert_le(exp1, exp2) \
249         _ci_assert_le(exp1, exp2, __FILE__, __LINE__)
250
251 #define ci_assert_lt(exp1, exp2) \
252         _ci_assert_lt(exp1, exp2, __FILE__, __LINE__)
253
254 #define ci_assert_ge(exp1, exp2) \
255         _ci_assert_ge(exp1, exp2, __FILE__, __LINE__)
256
257 #define ci_assert_gt(exp1, exp2) \
258         _ci_assert_gt(exp1, exp2, __FILE__, __LINE__)
259
260 #define ci_assert_impl(exp1, exp2) \
261         _ci_assert_impl(exp1, exp2, __FILE__, __LINE__)
262
263 #define ci_assert_equiv(exp1, exp2) \
264         _ci_assert_equiv(exp1, exp2, __FILE__, __LINE__)
265
266
267 #define CI_TEST(exp)                            \
268   do{                                           \
269     if( CI_UNLIKELY(!(exp)) )                   \
270       ci_fail(("CI_TEST(%s)", #exp));           \
271   }while(0)
272
273
274 #define CI_TRY(exp)                             \
275   do{                                           \
276     int _trc;                                   \
277     _trc=(exp);                                 \
278     if( CI_UNLIKELY(_trc < 0) )                 \
279       ci_sys_fail(#exp, _trc);                  \
280   }while(0)
281
282
283 #define CI_TRY_RET(exp)                                                  \
284   do{                                                                    \
285     int _trc;                                                            \
286     _trc=(exp);                                                          \
287     if( CI_UNLIKELY(_trc < 0) ) {                                        \
288       ci_log("%s returned %d at %s:%d", #exp, _trc, __FILE__, __LINE__); \
289       return _trc;                                                       \
290     }                                                                    \
291   }while(0)
292
293 #define CI_LOGLEVEL_TRY_RET(logfn, exp)                                    \
294   do{                                                                    \
295     int _trc;                                                            \
296     _trc=(exp);                                                          \
297     if( CI_UNLIKELY(_trc < 0) ) {                                        \
298       logfn (ci_log("%s returned %d at %s:%d", #exp, _trc, __FILE__, __LINE__)); \
299       return _trc;                                                       \
300     }                                                                    \
301   }while(0)
302
303
304 #define CI_SOCK_TRY(exp)                        \
305   do{                                           \
306     ci_sock_err_t _trc;                         \
307     _trc=(exp);                                 \
308     if( CI_UNLIKELY(!ci_sock_errok(_trc)) )     \
309       ci_sys_fail(#exp, _trc.val);              \
310   }while(0)
311
312
313 #define CI_SOCK_TRY_RET(exp)                                                 \
314   do{                                                                        \
315     ci_sock_err_t _trc;                                                      \
316     _trc=(exp);                                                              \
317     if( CI_UNLIKELY(!ci_sock_errok(_trc)) ) {                                \
318       ci_log("%s returned %d at %s:%d", #exp, _trc.val, __FILE__, __LINE__); \
319       return ci_sock_errcode(_trc);                                          \
320     }                                                                        \
321   }while(0)
322
323
324 #define CI_SOCK_TRY_SOCK_RET(exp)                                            \
325   do{                                                                        \
326     ci_sock_err_t _trc;                                                      \
327     _trc=(exp);                                                              \
328     if( CI_UNLIKELY(!ci_sock_errok(_trc)) ) {                                \
329       ci_log("%s returned %d at %s:%d", #exp, _trc.val, __FILE__, __LINE__); \
330       return _trc;                                                           \
331     }                                                                        \
332   }while(0)
333
334 #endif  /* __CI_TOOLS_DEBUG_H__ */
335
336 /*! \cidoxg_end */