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
7 * Maintained by Solarflare Communications
8 * <linux-xen-drivers@solarflare.com>
9 * <onload-dev@solarflare.com>
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.
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.
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 ****************************************************************************
26 /*! \cidoxg_include_ci_tools */
28 #ifndef __CI_TOOLS_DEBUG_H__
29 #define __CI_TOOLS_DEBUG_H__
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 */
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]
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)
62 # define _ci_verify(exp, file, line) \
67 # define CI_DEBUG_TRY(exp) \
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)
90 # define _CI_ASSERT_FMT "\nfrom %s:%d"
92 # define _ci_check(exp, file, line) \
94 if (CI_UNLIKELY(!(exp))) \
95 ci_warn(("ci_check(%s)"_CI_ASSERT_FMT, #exp, \
100 * NOTE: ci_fail() emits the file and line where the assert is actually
104 # define _ci_assert(exp, file, line) \
106 if (CI_UNLIKELY(!(exp))) \
107 ci_fail(("ci_assert(%s)"_CI_ASSERT_FMT, #exp, \
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))); \
120 # define _ci_verify(exp, file, line) \
122 if (CI_UNLIKELY(!(exp))) \
123 ci_fail(("ci_verify(%s)"_CI_ASSERT_FMT, #exp, \
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))
137 #define _ci_assert_equal_msg(exp1, exp2, msg, file, line) \
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, \
152 # define CI_DEBUG_TRY(exp) CI_TRY(exp)
154 #define CI_TRACE(exp,fmt) \
155 ci_log("%s:%d:%s] " #exp "=" fmt, \
156 __FILE__, __LINE__, __FUNCTION__, (exp))
159 #define CI_TRACE_INT(integer) \
160 ci_log("%s:%d:%s] " #integer "=%d", \
161 __FILE__, __LINE__, __FUNCTION__, (integer))
164 #define CI_TRACE_INT32(integer) \
165 ci_log("%s:%d:%s] " #integer "=%d", \
166 __FILE__, __LINE__, __FUNCTION__, ((ci_int32)integer))
169 #define CI_TRACE_INT64(integer) \
170 ci_log("%s:%d:%s] " #integer "=%lld", \
171 __FILE__, __LINE__, __FUNCTION__, ((ci_int64)integer))
174 #define CI_TRACE_UINT(integer) \
175 ci_log("%s:%d:%s] " #integer "=%ud", \
176 __FILE__, __LINE__, __FUNCTION__, (integer))
179 #define CI_TRACE_UINT32(integer) \
180 ci_log("%s:%d:%s] " #integer "=%ud", \
181 __FILE__, __LINE__, __FUNCTION__, ((ci_uint32)integer))
184 #define CI_TRACE_UINT64(integer) \
185 ci_log("%s:%d:%s] " #integer "=%ulld", \
186 __FILE__, __LINE__, __FUNCTION__, ((ci_uint64)integer))
189 #define CI_TRACE_HEX(integer) \
190 ci_log("%s:%d:%s] " #integer "=0x%x", \
191 __FILE__, __LINE__, __FUNCTION__, (integer))
194 #define CI_TRACE_HEX32(integer) \
195 ci_log("%s:%d:%s] " #integer "=0x%x", \
196 __FILE__, __LINE__, __FUNCTION__, ((ci_uint32)integer))
199 #define CI_TRACE_HEX64(integer) \
200 ci_log("%s:%d:%s] " #integer "=0x%llx", \
201 __FILE__, __LINE__, __FUNCTION__, ((ci_uint64)integer))
204 #define CI_TRACE_PTR(pointer) \
205 ci_log("%s:%d:%s] " #pointer "=0x%p", \
206 __FILE__, __LINE__, __FUNCTION__, (pointer))
209 #define CI_TRACE_STRING(string) \
210 ci_log("%s:%d:%s] " #string "=%s", \
211 __FILE__, __LINE__, __FUNCTION__, (string))
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))
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)))
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))
230 #define ci_check(exp) \
231 _ci_check(exp, __FILE__, __LINE__)
233 #define ci_assert(exp) \
234 _ci_assert(exp, __FILE__, __LINE__)
236 #define ci_verify(exp) \
237 _ci_verify(exp, __FILE__, __LINE__)
239 #define ci_assert_equal(exp1, exp2) \
240 _ci_assert_equal(exp1, exp2, __FILE__, __LINE__)
242 #define ci_assert_equal_msg(exp1, exp2, msg) \
243 _ci_assert_equal_msg(exp1, exp2, msg, __FILE__, __LINE__)
245 #define ci_assert_nequal(exp1, exp2) \
246 _ci_assert_nequal(exp1, exp2, __FILE__, __LINE__)
248 #define ci_assert_le(exp1, exp2) \
249 _ci_assert_le(exp1, exp2, __FILE__, __LINE__)
251 #define ci_assert_lt(exp1, exp2) \
252 _ci_assert_lt(exp1, exp2, __FILE__, __LINE__)
254 #define ci_assert_ge(exp1, exp2) \
255 _ci_assert_ge(exp1, exp2, __FILE__, __LINE__)
257 #define ci_assert_gt(exp1, exp2) \
258 _ci_assert_gt(exp1, exp2, __FILE__, __LINE__)
260 #define ci_assert_impl(exp1, exp2) \
261 _ci_assert_impl(exp1, exp2, __FILE__, __LINE__)
263 #define ci_assert_equiv(exp1, exp2) \
264 _ci_assert_equiv(exp1, exp2, __FILE__, __LINE__)
267 #define CI_TEST(exp) \
269 if( CI_UNLIKELY(!(exp)) ) \
270 ci_fail(("CI_TEST(%s)", #exp)); \
274 #define CI_TRY(exp) \
278 if( CI_UNLIKELY(_trc < 0) ) \
279 ci_sys_fail(#exp, _trc); \
283 #define CI_TRY_RET(exp) \
287 if( CI_UNLIKELY(_trc < 0) ) { \
288 ci_log("%s returned %d at %s:%d", #exp, _trc, __FILE__, __LINE__); \
293 #define CI_LOGLEVEL_TRY_RET(logfn, exp) \
297 if( CI_UNLIKELY(_trc < 0) ) { \
298 logfn (ci_log("%s returned %d at %s:%d", #exp, _trc, __FILE__, __LINE__)); \
304 #define CI_SOCK_TRY(exp) \
306 ci_sock_err_t _trc; \
308 if( CI_UNLIKELY(!ci_sock_errok(_trc)) ) \
309 ci_sys_fail(#exp, _trc.val); \
313 #define CI_SOCK_TRY_RET(exp) \
315 ci_sock_err_t _trc; \
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); \
324 #define CI_SOCK_TRY_SOCK_RET(exp) \
326 ci_sock_err_t _trc; \
328 if( CI_UNLIKELY(!ci_sock_errok(_trc)) ) { \
329 ci_log("%s returned %d at %s:%d", #exp, _trc.val, __FILE__, __LINE__); \
334 #endif /* __CI_TOOLS_DEBUG_H__ */