4 * Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2004 - 2005 Leonid Stoljar
6 * Copyright (C) 2007 - 2009 ID7 Ltd.
8 * Contains macroses for execution tracing and error reporting
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation, version 2
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.
21 #ifndef __SCST_DEBUG_H
22 #define __SCST_DEBUG_H
24 #include <linux/autoconf.h> /* for CONFIG_* */
26 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19)
27 #include <linux/bug.h> /* for WARN_ON_ONCE */
30 #if !defined(INSIDE_KERNEL_TREE)
31 #ifdef CONFIG_SCST_DEBUG
33 #ifndef CONFIG_DEBUG_BUGVERBOSE
35 printk(KERN_CRIT "BUG at %s:%d\n", \
36 __FILE__, __LINE__); \
43 #define sBUG_ON(p) do { \
45 printk(KERN_CRIT "BUG at %s:%d (%s)\n", \
46 __FILE__, __LINE__, #p); \
54 #define sBUG_ON(p) BUG_ON(p)
59 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
60 #define WARN_ON_ONCE(condition) ({ \
61 static int __warned; \
62 typeof(condition) __ret_warn_once = (condition); \
64 if (unlikely(__ret_warn_once)) \
69 unlikely(__ret_warn_once); \
73 #ifdef CONFIG_SCST_EXTRACHECKS
74 #define EXTRACHECKS_BUG_ON(a) sBUG_ON(a)
75 #define EXTRACHECKS_WARN_ON(a) WARN_ON(a)
76 #define EXTRACHECKS_WARN_ON_ONCE(a) WARN_ON_ONCE(a)
78 #define EXTRACHECKS_BUG_ON(a)
79 #define EXTRACHECKS_WARN_ON(a)
80 #define EXTRACHECKS_WARN_ON_ONCE(a)
83 #define TRACE_NULL 0x00000000
84 #define TRACE_DEBUG 0x00000001
85 #define TRACE_FUNCTION 0x00000002
86 #define TRACE_LINE 0x00000004
87 #define TRACE_PID 0x00000008
88 #ifndef GENERATING_UPSTREAM_PATCH
89 #define TRACE_ENTRYEXIT 0x00000010
91 #define TRACE_BUFF 0x00000020
92 #define TRACE_MEMORY 0x00000040
93 #define TRACE_SG_OP 0x00000080
94 #define TRACE_OUT_OF_MEM 0x00000100
95 #define TRACE_MINOR 0x00000200 /* less important events */
96 #define TRACE_MGMT 0x00000400
97 #define TRACE_MGMT_MINOR 0x00000800
98 #define TRACE_MGMT_DEBUG 0x00001000
99 #define TRACE_SCSI 0x00002000
100 #define TRACE_SPECIAL 0x00004000 /* filtering debug, etc */
101 #define TRACE_ALL 0xffffffff
102 /* Flags 0xXXXX0000 are local for users */
109 * Note: in the next two printk() statements the KERN_CONT macro is only
110 * present to suppress a checkpatch warning (KERN_CONT is defined as "").
112 #define PRINT(log_flag, format, args...) \
113 printk(log_flag format "\n", ## args)
114 #define PRINTN(log_flag, format, args...) \
115 printk(log_flag format, ## args)
118 #define __LOG_PREFIX LOG_PREFIX
120 #define __LOG_PREFIX NULL
123 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
125 #ifndef CONFIG_SCST_DEBUG
126 #define ___unlikely(a) (a)
128 #define ___unlikely(a) unlikely(a)
132 * We don't print prefix for debug traces to not put additional preasure
133 * on the logging system in case of a lot of logging.
136 extern int debug_print_prefix(unsigned long trace_flag,
137 const char *prefix, const char *func, int line);
138 extern void debug_print_buffer(const void *data, int len);
140 #define TRACE(trace, format, args...) \
142 if (___unlikely(trace_flag & (trace))) { \
143 debug_print_prefix(trace_flag, __LOG_PREFIX, \
144 __func__, __LINE__); \
145 PRINT(KERN_CONT, format, args); \
149 #ifdef CONFIG_SCST_DEBUG
151 #define PRINT_BUFFER(message, buff, len) \
153 PRINT(KERN_INFO, "%s:%s:", __func__, message); \
154 debug_print_buffer(buff, len); \
159 #define PRINT_BUFFER(message, buff, len) \
161 PRINT(KERN_INFO, "%s:", message); \
162 debug_print_buffer(buff, len); \
167 #define PRINT_BUFF_FLAG(flag, message, buff, len) \
169 if (___unlikely(trace_flag & (flag))) { \
170 PRINTN(KERN_INFO, "%s", ""); \
171 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
172 PRINT(KERN_CONT, "%s:", message); \
173 debug_print_buffer(buff, len); \
177 #else /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
179 #define TRACE(trace, args...) do {} while (0)
180 #define PRINT_BUFFER(message, buff, len) do {} while (0)
181 #define PRINT_BUFF_FLAG(flag, message, buff, len) do {} while (0)
183 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
185 #ifdef CONFIG_SCST_DEBUG
187 #define TRACE_DBG_FLAG(trace, format, args...) \
189 if (trace_flag & (trace)) { \
190 PRINTN(KERN_INFO, "%s", ""); \
191 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
192 PRINT(KERN_CONT, format, args); \
196 #define TRACE_MEM(args...) TRACE_DBG_FLAG(TRACE_MEMORY, args)
197 #define TRACE_SG(args...) TRACE_DBG_FLAG(TRACE_SG_OP, args)
198 #define TRACE_DBG(args...) TRACE_DBG_FLAG(TRACE_DEBUG, args)
199 #define TRACE_DBG_SPECIAL(args...) TRACE_DBG_FLAG(TRACE_DEBUG|TRACE_SPECIAL, args)
200 #define TRACE_MGMT_DBG(args...) TRACE_DBG_FLAG(TRACE_MGMT_DEBUG, args)
201 #define TRACE_MGMT_DBG_SPECIAL(args...) \
202 TRACE_DBG_FLAG(TRACE_MGMT_DEBUG|TRACE_SPECIAL, args)
204 #define TRACE_BUFFER(message, buff, len) \
206 if (trace_flag & TRACE_BUFF) { \
207 PRINTN(KERN_INFO, "%s", ""); \
208 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
209 PRINT(KERN_CONT, "%s:", message); \
210 debug_print_buffer(buff, len); \
214 #define TRACE_BUFF_FLAG(flag, message, buff, len) \
216 if (trace_flag & (flag)) { \
217 PRINTN(KERN_INFO, "%s", ""); \
218 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
219 PRINT(KERN_CONT, "%s:", message); \
220 debug_print_buffer(buff, len); \
224 #define PRINT_LOG_FLAG(log_flag, format, args...) \
226 PRINTN(KERN_INFO, "%s", ""); \
227 debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
228 PRINT(KERN_CONT, format, args); \
231 #define PRINT_WARNING(format, args...) \
233 PRINTN(KERN_INFO, "%s", ""); \
234 debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
235 PRINT(KERN_CONT, "***WARNING***: " format, args); \
238 #define PRINT_ERROR(format, args...) \
240 PRINTN(KERN_INFO, "%s", ""); \
241 debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
242 PRINT(KERN_CONT, "***ERROR***: " format, args); \
245 #define PRINT_CRIT_ERROR(format, args...) \
247 PRINTN(KERN_INFO, "%s", ""); \
248 debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
249 PRINT(KERN_CONT, "***CRITICAL ERROR***: " format, args); \
252 #define PRINT_INFO(format, args...) \
254 PRINTN(KERN_INFO, "%s", ""); \
255 debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
256 PRINT(KERN_CONT, format, args); \
259 #ifndef GENERATING_UPSTREAM_PATCH
260 #define TRACE_ENTRY() \
262 if (trace_flag & TRACE_ENTRYEXIT) { \
263 if (trace_flag & TRACE_PID) { \
264 PRINT(KERN_INFO, "[%d]: ENTRY %s", current->pid, \
268 PRINT(KERN_INFO, "ENTRY %s", __func__); \
273 #define TRACE_EXIT() \
275 if (trace_flag & TRACE_ENTRYEXIT) { \
276 if (trace_flag & TRACE_PID) { \
277 PRINT(KERN_INFO, "[%d]: EXIT %s", current->pid, \
281 PRINT(KERN_INFO, "EXIT %s", __func__); \
286 #define TRACE_EXIT_RES(res) \
288 if (trace_flag & TRACE_ENTRYEXIT) { \
289 if (trace_flag & TRACE_PID) { \
290 PRINT(KERN_INFO, "[%d]: EXIT %s: %ld", current->pid, \
291 __func__, (long)(res)); \
294 PRINT(KERN_INFO, "EXIT %s: %ld", \
295 __func__, (long)(res)); \
300 #define TRACE_EXIT_HRES(res) \
302 if (trace_flag & TRACE_ENTRYEXIT) { \
303 if (trace_flag & TRACE_PID) { \
304 PRINT(KERN_INFO, "[%d]: EXIT %s: 0x%lx", current->pid, \
305 __func__, (long)(res)); \
308 PRINT(KERN_INFO, "EXIT %s: %lx", \
309 __func__, (long)(res)); \
315 #else /* CONFIG_SCST_DEBUG */
317 #define TRACE_MEM(format, args...) do {} while (0)
318 #define TRACE_SG(format, args...) do {} while (0)
319 #define TRACE_DBG(format, args...) do {} while (0)
320 #define TRACE_DBG_SPECIAL(format, args...) do {} while (0)
321 #define TRACE_MGMT_DBG(format, args...) do {} while (0)
322 #define TRACE_MGMT_DBG_SPECIAL(format, args...) do {} while (0)
323 #define TRACE_BUFFER(message, buff, len) do {} while (0)
324 #define TRACE_BUFF_FLAG(flag, message, buff, len) do {} while (0)
325 #ifndef GENERATING_UPSTREAM_PATCH
326 #define TRACE_ENTRY() do {} while (0)
327 #define TRACE_EXIT() do {} while (0)
328 #define TRACE_EXIT_RES(res) do {} while (0)
329 #define TRACE_EXIT_HRES(res) do {} while (0)
334 #define PRINT_INFO(format, args...) \
336 PRINT(KERN_INFO, "%s: " format, LOG_PREFIX, args); \
339 #define PRINT_WARNING(format, args...) \
341 PRINT(KERN_INFO, "%s: ***WARNING***: " \
342 format, LOG_PREFIX, args); \
345 #define PRINT_ERROR(format, args...) \
347 PRINT(KERN_INFO, "%s: ***ERROR***: " \
348 format, LOG_PREFIX, args); \
351 #define PRINT_CRIT_ERROR(format, args...) \
353 PRINT(KERN_INFO, "%s: ***CRITICAL ERROR***: " \
354 format, LOG_PREFIX, args); \
359 #define PRINT_INFO(format, args...) \
361 PRINT(KERN_INFO, format, args); \
364 #define PRINT_WARNING(format, args...) \
366 PRINT(KERN_INFO, "***WARNING***: " \
370 #define PRINT_ERROR(format, args...) \
372 PRINT(KERN_ERR, "***ERROR***: " \
376 #define PRINT_CRIT_ERROR(format, args...) \
378 PRINT(KERN_CRIT, "***CRITICAL ERROR***: " \
382 #endif /* LOG_PREFIX */
384 #endif /* CONFIG_SCST_DEBUG */
386 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
387 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
389 #define SCST_SLAB_FLAGS 0L
392 #endif /* __SCST_DEBUG_H */