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 #ifdef CONFIG_SCST_DEBUG
84 /*# define LOG_FLAG KERN_DEBUG*/
85 # define LOG_FLAG KERN_INFO
86 # define INFO_FLAG KERN_INFO
87 # define ERROR_FLAG KERN_INFO
89 # define LOG_FLAG KERN_INFO
90 # define INFO_FLAG KERN_INFO
91 # define ERROR_FLAG KERN_ERR
94 #define CRIT_FLAG KERN_CRIT
98 #define TRACE_NULL 0x00000000
99 #define TRACE_DEBUG 0x00000001
100 #define TRACE_FUNCTION 0x00000002
101 #define TRACE_LINE 0x00000004
102 #define TRACE_PID 0x00000008
103 #define TRACE_ENTRYEXIT 0x00000010
104 #define TRACE_BUFF 0x00000020
105 #define TRACE_MEMORY 0x00000040
106 #define TRACE_SG_OP 0x00000080
107 #define TRACE_OUT_OF_MEM 0x00000100
108 #define TRACE_MINOR 0x00000200 /* less important events */
109 #define TRACE_MGMT 0x00000400
110 #define TRACE_MGMT_MINOR 0x00000800
111 #define TRACE_MGMT_DEBUG 0x00001000
112 #define TRACE_SCSI 0x00002000
113 #define TRACE_SPECIAL 0x00004000 /* filtering debug, etc */
114 #define TRACE_ALL 0xffffffff
115 /* Flags 0xXXXX0000 are local for users */
122 * Note: in the next two printk() statements the KERN_CONT macro is only
123 * present to suppress a checkpatch warning (KERN_CONT is defined as "").
125 #define PRINT(log_flag, format, args...) \
126 printk(KERN_CONT "%s" format "\n", log_flag, ## args)
127 #define PRINTN(log_flag, format, args...) \
128 printk(KERN_CONT "%s" format, log_flag, ## args)
131 #define __LOG_PREFIX LOG_PREFIX
133 #define __LOG_PREFIX NULL
136 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
138 #ifndef CONFIG_SCST_DEBUG
139 #define ___unlikely(a) (a)
141 #define ___unlikely(a) unlikely(a)
144 extern int debug_print_prefix(unsigned long trace_flag, const char *log_level,
145 const char *prefix, const char *func, int line);
146 extern void debug_print_buffer(const char *log_level, const void *data,
149 #define TRACE(trace, format, args...) \
151 if (___unlikely(trace_flag & (trace))) { \
152 char *__tflag = LOG_FLAG; \
153 if (debug_print_prefix(trace_flag, __tflag, __LOG_PREFIX, \
154 __func__, __LINE__) > 0) { \
157 PRINT(NO_FLAG, "%s" format, __tflag, args); \
161 #define PRINT_BUFFER(message, buff, len) \
163 PRINT(NO_FLAG, "%s:", message); \
164 debug_print_buffer(INFO_FLAG, buff, len); \
167 #define PRINT_BUFF_FLAG(flag, message, buff, len) \
169 if (___unlikely(trace_flag & (flag))) { \
170 char *__tflag = INFO_FLAG; \
171 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
175 PRINT(NO_FLAG, "%s%s:", __tflag, message); \
176 debug_print_buffer(INFO_FLAG, buff, len); \
180 #else /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
182 #define TRACE(trace, args...) do {} while (0)
183 #define PRINT_BUFFER(message, buff, len) do {} while (0)
184 #define PRINT_BUFF_FLAG(flag, message, buff, len) do {} while (0)
186 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
188 #ifdef CONFIG_SCST_DEBUG
190 #define __TRACE(trace, format, args...) \
192 if (trace_flag & (trace)) { \
193 char *__tflag = LOG_FLAG; \
194 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
198 PRINT(NO_FLAG, "%s" format, __tflag, args); \
202 #define TRACE_MEM(args...) __TRACE(TRACE_MEMORY, args)
203 #define TRACE_SG(args...) __TRACE(TRACE_SG_OP, args)
204 #define TRACE_DBG(args...) __TRACE(TRACE_DEBUG, args)
205 #define TRACE_DBG_SPECIAL(args...) __TRACE(TRACE_DEBUG|TRACE_SPECIAL, args)
206 #define TRACE_MGMT_DBG(args...) __TRACE(TRACE_MGMT_DEBUG, args)
207 #define TRACE_MGMT_DBG_SPECIAL(args...) \
208 __TRACE(TRACE_MGMT_DEBUG|TRACE_SPECIAL, args)
210 #define TRACE_BUFFER(message, buff, len) \
212 if (trace_flag & TRACE_BUFF) { \
213 char *__tflag = LOG_FLAG; \
214 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
218 PRINT(NO_FLAG, "%s%s:", __tflag, message); \
219 debug_print_buffer(LOG_FLAG, buff, len); \
223 #define TRACE_BUFF_FLAG(flag, message, buff, len) \
225 if (trace_flag & (flag)) { \
226 char *__tflag = LOG_FLAG; \
227 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
231 PRINT(NO_FLAG, "%s%s:", __tflag, message); \
232 debug_print_buffer(LOG_FLAG, buff, len); \
236 #define PRINT_LOG_FLAG(log_flag, format, args...) \
238 char *__tflag = log_flag; \
239 if (debug_print_prefix(trace_flag, __tflag, __LOG_PREFIX, \
240 __func__, __LINE__) > 0) { \
243 PRINT(NO_FLAG, "%s" format, __tflag, args); \
246 #define PRINT_WARNING(format, args...) \
248 if (strcmp(INFO_FLAG, LOG_FLAG)) { \
249 PRINT_LOG_FLAG(LOG_FLAG, "***WARNING*** " format, args); \
251 PRINT_LOG_FLAG(INFO_FLAG, "***WARNING*** " format, args); \
254 #define PRINT_ERROR(format, args...) \
256 if (strcmp(ERROR_FLAG, LOG_FLAG)) { \
257 PRINT_LOG_FLAG(LOG_FLAG, "***ERROR*** " format, args); \
259 PRINT_LOG_FLAG(ERROR_FLAG, "***ERROR*** " format, args); \
262 #define PRINT_CRIT_ERROR(format, args...) \
264 /* if (strcmp(CRIT_FLAG, LOG_FLAG)) \
266 PRINT_LOG_FLAG(LOG_FLAG, "***CRITICAL ERROR*** " format, args); \
268 PRINT_LOG_FLAG(CRIT_FLAG, "***CRITICAL ERROR*** " format, args); \
271 #define PRINT_INFO(format, args...) \
273 if (strcmp(INFO_FLAG, LOG_FLAG)) { \
274 PRINT_LOG_FLAG(LOG_FLAG, format, args); \
276 PRINT_LOG_FLAG(INFO_FLAG, format, args); \
279 #define TRACE_ENTRY() \
281 if (trace_flag & TRACE_ENTRYEXIT) { \
282 if (trace_flag & TRACE_PID) { \
283 PRINT(LOG_FLAG, "[%d]: ENTRY %s", current->pid, \
287 PRINT(LOG_FLAG, "ENTRY %s", __func__); \
292 #define TRACE_EXIT() \
294 if (trace_flag & TRACE_ENTRYEXIT) { \
295 if (trace_flag & TRACE_PID) { \
296 PRINT(LOG_FLAG, "[%d]: EXIT %s", current->pid, \
300 PRINT(LOG_FLAG, "EXIT %s", __func__); \
305 #define TRACE_EXIT_RES(res) \
307 if (trace_flag & TRACE_ENTRYEXIT) { \
308 if (trace_flag & TRACE_PID) { \
309 PRINT(LOG_FLAG, "[%d]: EXIT %s: %ld", current->pid, \
310 __func__, (long)(res)); \
313 PRINT(LOG_FLAG, "EXIT %s: %ld", \
314 __func__, (long)(res)); \
319 #define TRACE_EXIT_HRES(res) \
321 if (trace_flag & TRACE_ENTRYEXIT) { \
322 if (trace_flag & TRACE_PID) { \
323 PRINT(LOG_FLAG, "[%d]: EXIT %s: 0x%lx", current->pid, \
324 __func__, (long)(res)); \
327 PRINT(LOG_FLAG, "EXIT %s: %lx", \
328 __func__, (long)(res)); \
333 #else /* CONFIG_SCST_DEBUG */
335 #define TRACE_MEM(format, args...) do {} while (0)
336 #define TRACE_SG(format, args...) do {} while (0)
337 #define TRACE_DBG(format, args...) do {} while (0)
338 #define TRACE_DBG_SPECIAL(format, args...) do {} while (0)
339 #define TRACE_MGMT_DBG(format, args...) do {} while (0)
340 #define TRACE_MGMT_DBG_SPECIAL(format, args...) do {} while (0)
341 #define TRACE_BUFFER(message, buff, len) do {} while (0)
342 #define TRACE_BUFF_FLAG(flag, message, buff, len) do {} while (0)
343 #define TRACE_ENTRY() do {} while (0)
344 #define TRACE_EXIT() do {} while (0)
345 #define TRACE_EXIT_RES(res) do {} while (0)
346 #define TRACE_EXIT_HRES(res) do {} while (0)
350 #define PRINT_INFO(format, args...) \
352 PRINT(INFO_FLAG, "%s: " format, LOG_PREFIX, args); \
355 #define PRINT_WARNING(format, args...) \
357 PRINT(INFO_FLAG, "%s: ***WARNING*** " \
358 format, LOG_PREFIX, args); \
361 #define PRINT_ERROR(format, args...) \
363 PRINT(ERROR_FLAG, "%s: ***ERROR*** " \
364 format, LOG_PREFIX, args); \
367 #define PRINT_CRIT_ERROR(format, args...) \
369 PRINT(CRIT_FLAG, "%s: ***CRITICAL ERROR*** " \
370 format, LOG_PREFIX, args); \
375 #define PRINT_INFO(format, args...) \
377 PRINT(INFO_FLAG, format, args); \
380 #define PRINT_WARNING(format, args...) \
382 PRINT(INFO_FLAG, "***WARNING*** " \
386 #define PRINT_ERROR(format, args...) \
388 PRINT(ERROR_FLAG, "***ERROR*** " \
392 #define PRINT_CRIT_ERROR(format, args...) \
394 PRINT(CRIT_FLAG, "***CRITICAL ERROR*** " \
398 #endif /* LOG_PREFIX */
400 #endif /* CONFIG_SCST_DEBUG */
402 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
403 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
405 #define SCST_SLAB_FLAGS 0L
408 #endif /* __SCST_DEBUG_H */