Patch from Bart Van Assche <bart.vanassche@gmail.com>:
[mirror/scst/.git] / scst / include / scst_debug.h
1 /*
2  *  include/scst_debug.h
3  *
4  *  Copyright (C) 2004-2007 Vladislav Bolkhovitin <vst@vlnb.net>
5  *                 and Leonid Stoljar
6  *
7  *  Contains macroses for execution tracing and error reporting
8  *
9  *  This program is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU General Public License
11  *  as published by the Free Software Foundation, version 2
12  *  of the License.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  *  GNU General Public License for more details.
18  */
19
20 #ifndef __SCST_DEBUG_H
21 #define __SCST_DEBUG_H
22
23 #include <linux/autoconf.h>     /* for CONFIG_* */
24 #include <asm/bug.h>            /* for WARN_ON_ONCE */
25
26 #if !defined(EXTRACHECKS) && defined(CONFIG_SCSI_TARGET_EXTRACHECKS)
27 #define EXTRACHECKS
28 #endif
29
30 #if !defined(TRACING) && defined(CONFIG_SCSI_TARGET_TRACING)
31 #define TRACING
32 #endif
33
34 #if !defined(DEBUG) && defined(CONFIG_SCSI_TARGET_DEBUG)
35 #define DEBUG
36 #endif
37
38 #ifdef DEBUG
39
40 #ifndef EXTRACHECKS
41 #define EXTRACHECKS
42 #endif
43
44 #ifndef CONFIG_DEBUG_BUGVERBOSE
45 #define sBUG() do {                                             \
46         printk(KERN_CRIT "BUG at %s:%d\n",                      \
47                __FILE__, __LINE__);                             \
48         BUG();                                                  \
49 } while (0)
50 #else
51 #define sBUG() BUG()
52 #endif
53
54 #define sBUG_ON(p) do {                                         \
55         if (unlikely(p)) {                                      \
56                 printk(KERN_CRIT "BUG at %s:%d (%s)\n",         \
57                        __FILE__, __LINE__, #p);                 \
58                 BUG();                                          \
59         }                                                       \
60 } while (0)
61
62 #else
63
64 #define sBUG() BUG()
65 #define sBUG_ON(p) BUG_ON(p)
66
67 #endif
68
69 #ifndef WARN_ON_ONCE
70 #define WARN_ON_ONCE(condition) ({                              \
71         static int __warned;                                    \
72         typeof(condition) __ret_warn_once = (condition);        \
73                                                                 \
74         if (unlikely(__ret_warn_once))                          \
75                 if (!__warned) {                                \
76                         WARN_ON(1);                             \
77                         __warned = 1;                           \
78                 }                                               \
79         unlikely(__ret_warn_once);                              \
80 })
81 #endif
82
83 #ifdef EXTRACHECKS
84 #define EXTRACHECKS_BUG_ON(a)           sBUG_ON(a)
85 #define EXTRACHECKS_WARN_ON(a)          WARN_ON(a)
86 #define EXTRACHECKS_WARN_ON_ONCE(a)     WARN_ON_ONCE(a)
87 #else
88 #define EXTRACHECKS_BUG_ON(a)
89 #define EXTRACHECKS_WARN_ON(a)
90 #define EXTRACHECKS_WARN_ON_ONCE(a)
91 #endif
92
93 #ifdef DEBUG
94 //#  define LOG_FLAG KERN_DEBUG
95 #  define LOG_FLAG KERN_INFO
96 #  define INFO_FLAG KERN_INFO
97 #  define ERROR_FLAG KERN_INFO
98 #else
99 # define LOG_FLAG KERN_INFO
100 # define INFO_FLAG KERN_INFO
101 # define ERROR_FLAG KERN_ERR
102 #endif
103
104 #define CRIT_FLAG KERN_CRIT
105
106 #define NO_FLAG ""
107
108 #define TRACE_NULL           0x00000000
109 #define TRACE_DEBUG          0x00000001
110 #define TRACE_FUNCTION       0x00000002
111 #define TRACE_LINE           0x00000004
112 #define TRACE_PID            0x00000008
113 #define TRACE_ENTRYEXIT      0x00000010
114 #define TRACE_BUFF           0x00000020
115 #define TRACE_MEMORY         0x00000040
116 #define TRACE_SG_OP          0x00000080
117 #define TRACE_OUT_OF_MEM     0x00000100
118 #define TRACE_MINOR          0x00000200 /* less important events */
119 #define TRACE_MGMT           0x00000400
120 #define TRACE_MGMT_MINOR     0x00000800
121 #define TRACE_MGMT_DEBUG     0x00001000
122 #define TRACE_SCSI           0x00002000
123 #define TRACE_SPECIAL        0x00004000 /* filtering debug, etc */
124 #define TRACE_ALL            0xffffffff
125 /* Flags 0xXXXX0000 are local for users */
126
127 #define PRINT(log_flag, format, args...)  printk("%s" format "\n", log_flag, ## args)
128 #define PRINTN(log_flag, format, args...) printk("%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(DEBUG) || defined(TRACING)
137
138 #ifndef 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  /* DEBUG || TRACING */
181
182 #define TRACE(trace, args...) {}
183 #define PRINT_BUFFER(message, buff, len) {}
184 #define PRINT_BUFF_FLAG(flag, message, buff, len) {}
185
186 #endif /* DEBUG || TRACING */
187
188 #ifdef 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...) __TRACE(TRACE_MGMT_DEBUG|TRACE_SPECIAL, args)
208
209 #define TRACE_BUFFER(message, buff, len)                                \
210 do {                                                                    \
211         if (trace_flag & TRACE_BUFF) {                                  \
212                 char *__tflag = LOG_FLAG;                               \
213                 if (debug_print_prefix(trace_flag, __tflag, NULL, __func__, \
214                                        __LINE__) > 0) {                 \
215                         __tflag = NO_FLAG;                              \
216                 }                                                       \
217                 PRINT(NO_FLAG, "%s%s:", __tflag, message);              \
218                 debug_print_buffer(LOG_FLAG, buff, len);                \
219         }                                                               \
220 } while (0)
221
222 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
223 do {                                                                    \
224         if (trace_flag & (flag)) {                                      \
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 PRINT_LOG_FLAG(log_flag, format, args...)                       \
236 do {                                                                    \
237         char *__tflag = log_flag;                                       \
238         if (debug_print_prefix(trace_flag, __tflag, __LOG_PREFIX,       \
239                                __func__, __LINE__) > 0) {               \
240                 __tflag = NO_FLAG;                                      \
241         }                                                               \
242         PRINT(NO_FLAG, "%s" format, __tflag, args);                     \
243 } while (0)
244
245 #define PRINT_ERROR(format, args...)                                    \
246 do {                                                                    \
247         if (strcmp(ERROR_FLAG, LOG_FLAG)) {                             \
248                 PRINT_LOG_FLAG(LOG_FLAG, "***ERROR*** " format, args);  \
249         }                                                               \
250         PRINT_LOG_FLAG(ERROR_FLAG, "***ERROR*** " format, args);        \
251 } while (0)
252
253 #define PRINT_CRIT_ERROR(format, args...)                               \
254 do {                                                                    \
255         /*  if (strcmp(CRIT_FLAG, LOG_FLAG))                            \
256             {                                                           \
257             PRINT_LOG_FLAG(LOG_FLAG, "***CRITICAL ERROR*** " format, args); \
258             }*/                                                         \
259         PRINT_LOG_FLAG(CRIT_FLAG, "***CRITICAL ERROR*** " format, args); \
260 } while (0)
261
262 #define PRINT_INFO(format, args...)                     \
263 do {                                                    \
264         if (strcmp(INFO_FLAG, LOG_FLAG)) {              \
265                 PRINT_LOG_FLAG(LOG_FLAG, format, args); \
266         }                                               \
267         PRINT_LOG_FLAG(INFO_FLAG, format, args);        \
268 } while (0)
269
270 #define TRACE_ENTRY()                                                   \
271 do {                                                                    \
272         if (trace_flag & TRACE_ENTRYEXIT) {                             \
273                 if (trace_flag & TRACE_PID) {                           \
274                         PRINT(LOG_FLAG, "[%d]: ENTRY %s", current->pid, \
275                                 __func__);                              \
276                 }                                                       \
277                 else {                                                  \
278                         PRINT(LOG_FLAG, "ENTRY %s", __func__);          \
279                 }                                                       \
280         }                                                               \
281 } while (0)
282
283 #define TRACE_EXIT()                                                    \
284 do {                                                                    \
285         if (trace_flag & TRACE_ENTRYEXIT) {                             \
286                 if (trace_flag & TRACE_PID) {                           \
287                         PRINT(LOG_FLAG, "[%d]: EXIT %s", current->pid,  \
288                                 __func__);                              \
289                 }                                                       \
290                 else {                                                  \
291                         PRINT(LOG_FLAG, "EXIT %s", __func__);           \
292                 }                                                       \
293         }                                                               \
294 } while (0)
295
296 #define TRACE_EXIT_RES(res)                                             \
297 do {                                                                    \
298         if (trace_flag & TRACE_ENTRYEXIT) {                             \
299                 if (trace_flag & TRACE_PID) {                           \
300                         PRINT(LOG_FLAG, "[%d]: EXIT %s: %ld", current->pid, \
301                               __func__, (long)(res));                   \
302                 }                                                       \
303                 else {                                                  \
304                         PRINT(LOG_FLAG, "EXIT %s: %ld", __func__, (long)(res)); \
305                 }                                                       \
306         }                                                               \
307 } while (0)
308
309 #define TRACE_EXIT_HRES(res)                                            \
310 do {                                                                    \
311         if (trace_flag & TRACE_ENTRYEXIT) {                             \
312                 if (trace_flag & TRACE_PID) {                           \
313                         PRINT(LOG_FLAG, "[%d]: EXIT %s: 0x%lx", current->pid, \
314                               __func__, (long)(res));                   \
315                 }                                                       \
316                 else {                                                  \
317                         PRINT(LOG_FLAG, "EXIT %s: %lx", __func__, (long)(res)); \
318                 }                                                       \
319         }                                                               \
320 } while (0)
321
322 #else  /* DEBUG */
323
324 #define TRACE_MEM(format, args...) {}
325 #define TRACE_SG(format, args...) {}
326 #define TRACE_DBG(format, args...) {}
327 #define TRACE_DBG_SPECIAL(format, args...) {}
328 #define TRACE_MGMT_DBG(format, args...) {}
329 #define TRACE_MGMT_DBG_SPECIAL(format, args...) {}
330 #define TRACE_BUFFER(message, buff, len) {}
331 #define TRACE_BUFF_FLAG(flag, message, buff, len) {}
332 #define TRACE_ENTRY() {}
333 #define TRACE_EXIT() {}
334 #define TRACE_EXIT_RES(res) {}
335 #define TRACE_EXIT_HRES(res) {}
336
337 #ifdef LOG_PREFIX
338
339 #define PRINT_INFO(format, args...)                             \
340 do {                                                            \
341         PRINT(INFO_FLAG, "%s: " format, LOG_PREFIX, args);      \
342 } while (0)
343
344 #define PRINT_ERROR(format, args...)            \
345 do {                                            \
346         PRINT(ERROR_FLAG, "%s: ***ERROR*** "    \
347               format, LOG_PREFIX, args);        \
348 } while (0)
349
350 #define PRINT_CRIT_ERROR(format, args...)       \
351 do {                                            \
352         PRINT(CRIT_FLAG, "%s: ***CRITICAL ERROR*** "    \
353                 format, LOG_PREFIX, args);              \
354 } while (0)
355
356 #else
357
358 #define PRINT_INFO(format, args...)             \
359 do {                                            \
360         PRINT(INFO_FLAG, format, args);         \
361 } while (0)
362
363 #define PRINT_ERROR(format, args...)            \
364 do {                                            \
365         PRINT(ERROR_FLAG, "***ERROR*** "        \
366                 format, args);                  \
367 } while (0)
368
369 #define PRINT_CRIT_ERROR(format, args...)               \
370 do {                                                    \
371         PRINT(CRIT_FLAG, "***CRITICAL ERROR*** "        \
372                 format, args);                          \
373 } while (0)
374
375 #endif /* LOG_PREFIX */
376
377 #endif /* DEBUG */
378
379 #if defined(DEBUG) && defined(CONFIG_DEBUG_SLAB)
380 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
381 #else
382 #define SCST_SLAB_FLAGS 0L
383 #endif
384
385 #endif /* __SCST_DEBUG_H */