Fixes issues cased by recent change of cdb_len to unsigned. Reported by Krzysztof...
[mirror/scst/.git] / scst / include / scst_debug.h
1 /*
2  *  include/scst_debug.h
3  *
4  *  Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5  *  Copyright (C) 2004 - 2005 Leonid Stoljar
6  *  Copyright (C) 2007 - 2009 ID7 Ltd.
7  *
8  *  Contains macroses for execution tracing and error reporting
9  *
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
13  *  of the License.
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
21 #ifndef __SCST_DEBUG_H
22 #define __SCST_DEBUG_H
23
24 #include <linux/autoconf.h>     /* for CONFIG_* */
25
26 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19)
27 #include <linux/bug.h>          /* for WARN_ON_ONCE */
28 #endif
29
30 #if !defined(INSIDE_KERNEL_TREE)
31 #ifdef CONFIG_SCST_DEBUG
32
33 #ifndef CONFIG_DEBUG_BUGVERBOSE
34 #define sBUG() do {                                             \
35         printk(KERN_CRIT "BUG at %s:%d\n",                      \
36                __FILE__, __LINE__);                             \
37         BUG();                                                  \
38 } while (0)
39 #else
40 #define sBUG() BUG()
41 #endif
42
43 #define sBUG_ON(p) do {                                         \
44         if (unlikely(p)) {                                      \
45                 printk(KERN_CRIT "BUG at %s:%d (%s)\n",         \
46                        __FILE__, __LINE__, #p);                 \
47                 BUG();                                          \
48         }                                                       \
49 } while (0)
50
51 #else
52
53 #define sBUG() BUG()
54 #define sBUG_ON(p) BUG_ON(p)
55
56 #endif
57 #endif
58
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);        \
63                                                                 \
64         if (unlikely(__ret_warn_once))                          \
65                 if (!__warned) {                                \
66                         WARN_ON(1);                             \
67                         __warned = 1;                           \
68                 }                                               \
69         unlikely(__ret_warn_once);                              \
70 })
71 #endif
72
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)
77 #else
78 #define EXTRACHECKS_BUG_ON(a)
79 #define EXTRACHECKS_WARN_ON(a)
80 #define EXTRACHECKS_WARN_ON_ONCE(a)
81 #endif
82
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
88 #else
89 # define LOG_FLAG KERN_INFO
90 # define INFO_FLAG KERN_INFO
91 # define ERROR_FLAG KERN_ERR
92 #endif
93
94 #define CRIT_FLAG KERN_CRIT
95
96 #define NO_FLAG ""
97
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 */
116
117 #ifndef KERN_CONT
118 #define KERN_CONT       ""
119 #endif
120
121 /*
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 "").
124  */
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)
129
130 #ifdef LOG_PREFIX
131 #define __LOG_PREFIX    LOG_PREFIX
132 #else
133 #define __LOG_PREFIX    NULL
134 #endif
135
136 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
137
138 #ifndef CONFIG_SCST_DEBUG
139 #define ___unlikely(a)          (a)
140 #else
141 #define ___unlikely(a)          unlikely(a)
142 #endif
143
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,
147         int len);
148
149 #define TRACE(trace, format, args...)                                     \
150 do {                                                                      \
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) {         \
155                         __tflag = NO_FLAG;                                \
156                 }                                                         \
157                 PRINT(NO_FLAG, "%s" format, __tflag, args);               \
158         }                                                                 \
159 } while (0)
160
161 #define PRINT_BUFFER(message, buff, len)                            \
162 do {                                                                \
163         PRINT(NO_FLAG, "%s:", message);                             \
164         debug_print_buffer(INFO_FLAG, buff, len);                   \
165 } while (0)
166
167 #define PRINT_BUFF_FLAG(flag, message, buff, len)                       \
168 do {                                                                    \
169         if (___unlikely(trace_flag & (flag))) {                         \
170                 char *__tflag = INFO_FLAG;                              \
171                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
172                                        __LINE__) > 0) {                 \
173                         __tflag = NO_FLAG;                              \
174                 }                                                       \
175                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
176                 debug_print_buffer(INFO_FLAG, buff, len);               \
177         }                                                               \
178 } while (0)
179
180 #else  /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
181
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)
185
186 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
187
188 #ifdef CONFIG_SCST_DEBUG
189
190 #define __TRACE(trace, format, args...)                                 \
191 do {                                                                    \
192         if (trace_flag & (trace)) {                                     \
193                 char *__tflag = LOG_FLAG;                               \
194                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
195                                        __LINE__) > 0) {                 \
196                         __tflag = NO_FLAG;                              \
197                 }                                                       \
198                 PRINT(NO_FLAG, "%s" format, __tflag, args);             \
199         }                                                               \
200 } while (0)
201
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)
209
210 #define TRACE_BUFFER(message, buff, len)                                \
211 do {                                                                    \
212         if (trace_flag & TRACE_BUFF) {                                  \
213                 char *__tflag = LOG_FLAG;                               \
214                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
215                                        __LINE__) > 0) {                 \
216                         __tflag = NO_FLAG;                              \
217                 }                                                       \
218                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
219                 debug_print_buffer(LOG_FLAG, buff, len);                \
220         }                                                               \
221 } while (0)
222
223 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
224 do {                                                                    \
225         if (trace_flag & (flag)) {                                      \
226                 char *__tflag = LOG_FLAG;                               \
227                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
228                                        __LINE__) > 0) {                 \
229                         __tflag = NO_FLAG;                              \
230                 }                                                       \
231                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
232                 debug_print_buffer(LOG_FLAG, buff, len);                \
233         }                                                               \
234 } while (0)
235
236 #define PRINT_LOG_FLAG(log_flag, format, args...)                       \
237 do {                                                                    \
238         char *__tflag = log_flag;                                       \
239         if (debug_print_prefix(trace_flag, __tflag, __LOG_PREFIX,       \
240                                __func__, __LINE__) > 0) {               \
241                 __tflag = NO_FLAG;                                      \
242         }                                                               \
243         PRINT(NO_FLAG, "%s" format, __tflag, args);                     \
244 } while (0)
245
246 #define PRINT_WARNING(format, args...)                                  \
247 do {                                                                    \
248         if (strcmp(INFO_FLAG, LOG_FLAG)) {                              \
249                 PRINT_LOG_FLAG(LOG_FLAG, "***WARNING*** " format, args); \
250         }                                                               \
251         PRINT_LOG_FLAG(INFO_FLAG, "***WARNING*** " format, args);       \
252 } while (0)
253
254 #define PRINT_ERROR(format, args...)                                    \
255 do {                                                                    \
256         if (strcmp(ERROR_FLAG, LOG_FLAG)) {                             \
257                 PRINT_LOG_FLAG(LOG_FLAG, "***ERROR*** " format, args);  \
258         }                                                               \
259         PRINT_LOG_FLAG(ERROR_FLAG, "***ERROR*** " format, args);        \
260 } while (0)
261
262 #define PRINT_CRIT_ERROR(format, args...)                               \
263 do {                                                                    \
264         /*  if (strcmp(CRIT_FLAG, LOG_FLAG))                            \
265             {                                                           \
266             PRINT_LOG_FLAG(LOG_FLAG, "***CRITICAL ERROR*** " format, args); \
267             }*/                                                         \
268         PRINT_LOG_FLAG(CRIT_FLAG, "***CRITICAL ERROR*** " format, args); \
269 } while (0)
270
271 #define PRINT_INFO(format, args...)                     \
272 do {                                                    \
273         if (strcmp(INFO_FLAG, LOG_FLAG)) {              \
274                 PRINT_LOG_FLAG(LOG_FLAG, format, args); \
275         }                                               \
276         PRINT_LOG_FLAG(INFO_FLAG, format, args);        \
277 } while (0)
278
279 #define TRACE_ENTRY()                                                   \
280 do {                                                                    \
281         if (trace_flag & TRACE_ENTRYEXIT) {                             \
282                 if (trace_flag & TRACE_PID) {                           \
283                         PRINT(LOG_FLAG, "[%d]: ENTRY %s", current->pid, \
284                                 __func__);                              \
285                 }                                                       \
286                 else {                                                  \
287                         PRINT(LOG_FLAG, "ENTRY %s", __func__);          \
288                 }                                                       \
289         }                                                               \
290 } while (0)
291
292 #define TRACE_EXIT()                                                    \
293 do {                                                                    \
294         if (trace_flag & TRACE_ENTRYEXIT) {                             \
295                 if (trace_flag & TRACE_PID) {                           \
296                         PRINT(LOG_FLAG, "[%d]: EXIT %s", current->pid,  \
297                                 __func__);                              \
298                 }                                                       \
299                 else {                                                  \
300                         PRINT(LOG_FLAG, "EXIT %s", __func__);           \
301                 }                                                       \
302         }                                                               \
303 } while (0)
304
305 #define TRACE_EXIT_RES(res)                                             \
306 do {                                                                    \
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));                   \
311                 }                                                       \
312                 else {                                                  \
313                         PRINT(LOG_FLAG, "EXIT %s: %ld",                 \
314                                 __func__, (long)(res));                 \
315                 }                                                       \
316         }                                                               \
317 } while (0)
318
319 #define TRACE_EXIT_HRES(res)                                            \
320 do {                                                                    \
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));                   \
325                 }                                                       \
326                 else {                                                  \
327                         PRINT(LOG_FLAG, "EXIT %s: %lx",                 \
328                                         __func__, (long)(res));         \
329                 }                                                       \
330         }                                                               \
331 } while (0)
332
333 #else  /* CONFIG_SCST_DEBUG */
334
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)
347
348 #ifdef LOG_PREFIX
349
350 #define PRINT_INFO(format, args...)                             \
351 do {                                                            \
352         PRINT(INFO_FLAG, "%s: " format, LOG_PREFIX, args);      \
353 } while (0)
354
355 #define PRINT_WARNING(format, args...)          \
356 do {                                            \
357         PRINT(INFO_FLAG, "%s: ***WARNING*** "   \
358               format, LOG_PREFIX, args);        \
359 } while (0)
360
361 #define PRINT_ERROR(format, args...)            \
362 do {                                            \
363         PRINT(ERROR_FLAG, "%s: ***ERROR*** "    \
364               format, LOG_PREFIX, args);        \
365 } while (0)
366
367 #define PRINT_CRIT_ERROR(format, args...)       \
368 do {                                            \
369         PRINT(CRIT_FLAG, "%s: ***CRITICAL ERROR*** "    \
370                 format, LOG_PREFIX, args);              \
371 } while (0)
372
373 #else
374
375 #define PRINT_INFO(format, args...)             \
376 do {                                            \
377         PRINT(INFO_FLAG, format, args);         \
378 } while (0)
379
380 #define PRINT_WARNING(format, args...)          \
381 do {                                            \
382         PRINT(INFO_FLAG, "***WARNING*** "       \
383                 format, args);                  \
384 } while (0)
385
386 #define PRINT_ERROR(format, args...)            \
387 do {                                            \
388         PRINT(ERROR_FLAG, "***ERROR*** "        \
389                 format, args);                  \
390 } while (0)
391
392 #define PRINT_CRIT_ERROR(format, args...)               \
393 do {                                                    \
394         PRINT(CRIT_FLAG, "***CRITICAL ERROR*** "        \
395                 format, args);                          \
396 } while (0)
397
398 #endif /* LOG_PREFIX */
399
400 #endif /* CONFIG_SCST_DEBUG */
401
402 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
403 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
404 #else
405 #define SCST_SLAB_FLAGS 0L
406 #endif
407
408 #endif /* __SCST_DEBUG_H */