- Possibility to operate with single size entries as well as control of the reclamat...
[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 #ifdef CONFIG_SCST_DEBUG
162
163 #define PRINT_BUFFER(message, buff, len)                            \
164 do {                                                                \
165         PRINT(NO_FLAG, "%s:%s:", __func__, message);                \
166         debug_print_buffer(INFO_FLAG, buff, len);                   \
167 } while (0)
168
169 #else
170
171 #define PRINT_BUFFER(message, buff, len)                            \
172 do {                                                                \
173         PRINT(NO_FLAG, "%s:", message);                             \
174         debug_print_buffer(INFO_FLAG, buff, len);                   \
175 } while (0)
176
177 #endif
178
179 #define PRINT_BUFF_FLAG(flag, message, buff, len)                       \
180 do {                                                                    \
181         if (___unlikely(trace_flag & (flag))) {                         \
182                 char *__tflag = INFO_FLAG;                              \
183                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
184                                        __LINE__) > 0) {                 \
185                         __tflag = NO_FLAG;                              \
186                 }                                                       \
187                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
188                 debug_print_buffer(INFO_FLAG, buff, len);               \
189         }                                                               \
190 } while (0)
191
192 #else  /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
193
194 #define TRACE(trace, args...) do {} while (0)
195 #define PRINT_BUFFER(message, buff, len) do {} while (0)
196 #define PRINT_BUFF_FLAG(flag, message, buff, len) do {} while (0)
197
198 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
199
200 #ifdef CONFIG_SCST_DEBUG
201
202 #define TRACE_DBG_FLAG(trace, format, args...)                          \
203 do {                                                                    \
204         if (trace_flag & (trace)) {                                     \
205                 char *__tflag = LOG_FLAG;                               \
206                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__,\
207                                        __LINE__) > 0) {                 \
208                         __tflag = NO_FLAG;                              \
209                 }                                                       \
210                 PRINT(NO_FLAG, "%s" format, __tflag, args);             \
211         }                                                               \
212 } while (0)
213
214 #define TRACE_MEM(args...)              TRACE_DBG_FLAG(TRACE_MEMORY, args)
215 #define TRACE_SG(args...)               TRACE_DBG_FLAG(TRACE_SG_OP, args)
216 #define TRACE_DBG(args...)              TRACE_DBG_FLAG(TRACE_DEBUG, args)
217 #define TRACE_DBG_SPECIAL(args...)      TRACE_DBG_FLAG(TRACE_DEBUG|TRACE_SPECIAL, args)
218 #define TRACE_MGMT_DBG(args...)         TRACE_DBG_FLAG(TRACE_MGMT_DEBUG, args)
219 #define TRACE_MGMT_DBG_SPECIAL(args...) \
220                 TRACE_DBG_FLAG(TRACE_MGMT_DEBUG|TRACE_SPECIAL, args)
221
222 #define TRACE_BUFFER(message, buff, len)                                \
223 do {                                                                    \
224         if (trace_flag & TRACE_BUFF) {                                  \
225                 char *__tflag = LOG_FLAG;                               \
226                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
227                                        __LINE__) > 0) {                 \
228                         __tflag = NO_FLAG;                              \
229                 }                                                       \
230                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
231                 debug_print_buffer(LOG_FLAG, buff, len);                \
232         }                                                               \
233 } while (0)
234
235 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
236 do {                                                                    \
237         if (trace_flag & (flag)) {                                      \
238                 char *__tflag = LOG_FLAG;                               \
239                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
240                                        __LINE__) > 0) {                 \
241                         __tflag = NO_FLAG;                              \
242                 }                                                       \
243                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
244                 debug_print_buffer(LOG_FLAG, buff, len);                \
245         }                                                               \
246 } while (0)
247
248 #define PRINT_LOG_FLAG(log_flag, format, args...)                       \
249 do {                                                                    \
250         char *__tflag = log_flag;                                       \
251         if (debug_print_prefix(trace_flag, __tflag, __LOG_PREFIX,       \
252                                __func__, __LINE__) > 0) {               \
253                 __tflag = NO_FLAG;                                      \
254         }                                                               \
255         PRINT(NO_FLAG, "%s" format, __tflag, args);                     \
256 } while (0)
257
258 #define PRINT_WARNING(format, args...)                                  \
259 do {                                                                    \
260         if (strcmp(INFO_FLAG, LOG_FLAG)) {                              \
261                 PRINT_LOG_FLAG(LOG_FLAG, "***WARNING*** " format, args); \
262         }                                                               \
263         PRINT_LOG_FLAG(INFO_FLAG, "***WARNING*** " format, args);       \
264 } while (0)
265
266 #define PRINT_ERROR(format, args...)                                    \
267 do {                                                                    \
268         if (strcmp(ERROR_FLAG, LOG_FLAG)) {                             \
269                 PRINT_LOG_FLAG(LOG_FLAG, "***ERROR*** " format, args);  \
270         }                                                               \
271         PRINT_LOG_FLAG(ERROR_FLAG, "***ERROR*** " format, args);        \
272 } while (0)
273
274 #define PRINT_CRIT_ERROR(format, args...)                               \
275 do {                                                                    \
276         /*  if (strcmp(CRIT_FLAG, LOG_FLAG))                            \
277             {                                                           \
278             PRINT_LOG_FLAG(LOG_FLAG, "***CRITICAL ERROR*** " format, args); \
279             }*/                                                         \
280         PRINT_LOG_FLAG(CRIT_FLAG, "***CRITICAL ERROR*** " format, args); \
281 } while (0)
282
283 #define PRINT_INFO(format, args...)                     \
284 do {                                                    \
285         if (strcmp(INFO_FLAG, LOG_FLAG)) {              \
286                 PRINT_LOG_FLAG(LOG_FLAG, format, args); \
287         }                                               \
288         PRINT_LOG_FLAG(INFO_FLAG, format, args);        \
289 } while (0)
290
291 #define TRACE_ENTRY()                                                   \
292 do {                                                                    \
293         if (trace_flag & TRACE_ENTRYEXIT) {                             \
294                 if (trace_flag & TRACE_PID) {                           \
295                         PRINT(LOG_FLAG, "[%d]: ENTRY %s", current->pid, \
296                                 __func__);                              \
297                 }                                                       \
298                 else {                                                  \
299                         PRINT(LOG_FLAG, "ENTRY %s", __func__);          \
300                 }                                                       \
301         }                                                               \
302 } while (0)
303
304 #define TRACE_EXIT()                                                    \
305 do {                                                                    \
306         if (trace_flag & TRACE_ENTRYEXIT) {                             \
307                 if (trace_flag & TRACE_PID) {                           \
308                         PRINT(LOG_FLAG, "[%d]: EXIT %s", current->pid,  \
309                                 __func__);                              \
310                 }                                                       \
311                 else {                                                  \
312                         PRINT(LOG_FLAG, "EXIT %s", __func__);           \
313                 }                                                       \
314         }                                                               \
315 } while (0)
316
317 #define TRACE_EXIT_RES(res)                                             \
318 do {                                                                    \
319         if (trace_flag & TRACE_ENTRYEXIT) {                             \
320                 if (trace_flag & TRACE_PID) {                           \
321                         PRINT(LOG_FLAG, "[%d]: EXIT %s: %ld", current->pid, \
322                               __func__, (long)(res));                   \
323                 }                                                       \
324                 else {                                                  \
325                         PRINT(LOG_FLAG, "EXIT %s: %ld",                 \
326                                 __func__, (long)(res));                 \
327                 }                                                       \
328         }                                                               \
329 } while (0)
330
331 #define TRACE_EXIT_HRES(res)                                            \
332 do {                                                                    \
333         if (trace_flag & TRACE_ENTRYEXIT) {                             \
334                 if (trace_flag & TRACE_PID) {                           \
335                         PRINT(LOG_FLAG, "[%d]: EXIT %s: 0x%lx", current->pid, \
336                               __func__, (long)(res));                   \
337                 }                                                       \
338                 else {                                                  \
339                         PRINT(LOG_FLAG, "EXIT %s: %lx",                 \
340                                         __func__, (long)(res));         \
341                 }                                                       \
342         }                                                               \
343 } while (0)
344
345 #else  /* CONFIG_SCST_DEBUG */
346
347 #define TRACE_MEM(format, args...) do {} while (0)
348 #define TRACE_SG(format, args...) do {} while (0)
349 #define TRACE_DBG(format, args...) do {} while (0)
350 #define TRACE_DBG_SPECIAL(format, args...) do {} while (0)
351 #define TRACE_MGMT_DBG(format, args...) do {} while (0)
352 #define TRACE_MGMT_DBG_SPECIAL(format, args...) do {} while (0)
353 #define TRACE_BUFFER(message, buff, len) do {} while (0)
354 #define TRACE_BUFF_FLAG(flag, message, buff, len) do {} while (0)
355 #define TRACE_ENTRY() do {} while (0)
356 #define TRACE_EXIT() do {} while (0)
357 #define TRACE_EXIT_RES(res) do {} while (0)
358 #define TRACE_EXIT_HRES(res) do {} while (0)
359
360 #ifdef LOG_PREFIX
361
362 #define PRINT_INFO(format, args...)                             \
363 do {                                                            \
364         PRINT(INFO_FLAG, "%s: " format, LOG_PREFIX, args);      \
365 } while (0)
366
367 #define PRINT_WARNING(format, args...)          \
368 do {                                            \
369         PRINT(INFO_FLAG, "%s: ***WARNING*** "   \
370               format, LOG_PREFIX, args);        \
371 } while (0)
372
373 #define PRINT_ERROR(format, args...)            \
374 do {                                            \
375         PRINT(ERROR_FLAG, "%s: ***ERROR*** "    \
376               format, LOG_PREFIX, args);        \
377 } while (0)
378
379 #define PRINT_CRIT_ERROR(format, args...)       \
380 do {                                            \
381         PRINT(CRIT_FLAG, "%s: ***CRITICAL ERROR*** "    \
382                 format, LOG_PREFIX, args);              \
383 } while (0)
384
385 #else
386
387 #define PRINT_INFO(format, args...)             \
388 do {                                            \
389         PRINT(INFO_FLAG, format, args);         \
390 } while (0)
391
392 #define PRINT_WARNING(format, args...)          \
393 do {                                            \
394         PRINT(INFO_FLAG, "***WARNING*** "       \
395                 format, args);                  \
396 } while (0)
397
398 #define PRINT_ERROR(format, args...)            \
399 do {                                            \
400         PRINT(ERROR_FLAG, "***ERROR*** "        \
401                 format, args);                  \
402 } while (0)
403
404 #define PRINT_CRIT_ERROR(format, args...)               \
405 do {                                                    \
406         PRINT(CRIT_FLAG, "***CRITICAL ERROR*** "        \
407                 format, args);                          \
408 } while (0)
409
410 #endif /* LOG_PREFIX */
411
412 #endif /* CONFIG_SCST_DEBUG */
413
414 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
415 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
416 #else
417 #define SCST_SLAB_FLAGS 0L
418 #endif
419
420 #endif /* __SCST_DEBUG_H */