1197aa579fdbe9e961a055eb0b49687a0c7b4c5e
[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 #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
90 #endif
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 */
103
104 #ifndef KERN_CONT
105 #define KERN_CONT       ""
106 #endif
107
108 /*
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 "").
111  */
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)
116
117 #ifdef LOG_PREFIX
118 #define __LOG_PREFIX    LOG_PREFIX
119 #else
120 #define __LOG_PREFIX    NULL
121 #endif
122
123 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
124
125 #ifndef CONFIG_SCST_DEBUG
126 #define ___unlikely(a)          (a)
127 #else
128 #define ___unlikely(a)          unlikely(a)
129 #endif
130
131 /*
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.
134  */
135
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);
139
140 #define TRACE(trace, format, args...)                                   \
141 do {                                                                    \
142         if (___unlikely(trace_flag & (trace))) {                        \
143                 debug_print_prefix(trace_flag, __LOG_PREFIX,            \
144                                        __func__, __LINE__);             \
145                 PRINT(KERN_CONT, format, args);                         \
146         }                                                               \
147 } while (0)
148
149 #ifdef CONFIG_SCST_DEBUG
150
151 #define PRINT_BUFFER(message, buff, len)                            \
152 do {                                                                \
153         PRINT(KERN_INFO, "%s:%s:", __func__, message);              \
154         debug_print_buffer(buff, len);                              \
155 } while (0)
156
157 #else
158
159 #define PRINT_BUFFER(message, buff, len)                            \
160 do {                                                                \
161         PRINT(KERN_INFO, "%s:", message);                           \
162         debug_print_buffer(buff, len);                              \
163 } while (0)
164
165 #endif
166
167 #define PRINT_BUFF_FLAG(flag, message, buff, len)                       \
168 do {                                                                    \
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);                          \
174         }                                                               \
175 } while (0)
176
177 #else  /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
178
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)
182
183 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
184
185 #ifdef CONFIG_SCST_DEBUG
186
187 #define TRACE_DBG_FLAG(trace, format, args...)                          \
188 do {                                                                    \
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);                         \
193         }                                                               \
194 } while (0)
195
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)
203
204 #define TRACE_BUFFER(message, buff, len)                                \
205 do {                                                                    \
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);                          \
211         }                                                               \
212 } while (0)
213
214 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
215 do {                                                                    \
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);                          \
221         }                                                               \
222 } while (0)
223
224 #define PRINT_LOG_FLAG(log_flag, format, args...)                       \
225 do {                                                                    \
226         PRINTN(KERN_INFO, "%s", "");                                    \
227         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
228         PRINT(KERN_CONT, format, args);                                 \
229 } while (0)
230
231 #define PRINT_WARNING(format, args...)                                  \
232 do {                                                                    \
233         PRINTN(KERN_INFO, "%s", "");                                    \
234         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
235         PRINT(KERN_CONT, "***WARNING***: " format, args);               \
236 } while (0)
237
238 #define PRINT_ERROR(format, args...)                                    \
239 do {                                                                    \
240         PRINTN(KERN_INFO, "%s", "");                                    \
241         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
242         PRINT(KERN_CONT, "***ERROR***: " format, args);                 \
243 } while (0)
244
245 #define PRINT_CRIT_ERROR(format, args...)                               \
246 do {                                                                    \
247         PRINTN(KERN_INFO, "%s", "");                                    \
248         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
249         PRINT(KERN_CONT, "***CRITICAL ERROR***: " format, args);        \
250 } while (0)
251
252 #define PRINT_INFO(format, args...)                                     \
253 do {                                                                    \
254         PRINTN(KERN_INFO, "%s", "");                                    \
255         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
256         PRINT(KERN_CONT, format, args);                                 \
257 } while (0)
258
259 #ifndef GENERATING_UPSTREAM_PATCH
260 #define TRACE_ENTRY()                                                   \
261 do {                                                                    \
262         if (trace_flag & TRACE_ENTRYEXIT) {                             \
263                 if (trace_flag & TRACE_PID) {                           \
264                         PRINT(KERN_INFO, "[%d]: ENTRY %s", current->pid, \
265                                 __func__);                              \
266                 }                                                       \
267                 else {                                                  \
268                         PRINT(KERN_INFO, "ENTRY %s", __func__); \
269                 }                                                       \
270         }                                                               \
271 } while (0)
272
273 #define TRACE_EXIT()                                                    \
274 do {                                                                    \
275         if (trace_flag & TRACE_ENTRYEXIT) {                             \
276                 if (trace_flag & TRACE_PID) {                           \
277                         PRINT(KERN_INFO, "[%d]: EXIT %s", current->pid, \
278                                 __func__);                              \
279                 }                                                       \
280                 else {                                                  \
281                         PRINT(KERN_INFO, "EXIT %s", __func__);          \
282                 }                                                       \
283         }                                                               \
284 } while (0)
285
286 #define TRACE_EXIT_RES(res)                                             \
287 do {                                                                    \
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));                   \
292                 }                                                       \
293                 else {                                                  \
294                         PRINT(KERN_INFO, "EXIT %s: %ld",                \
295                                 __func__, (long)(res));                 \
296                 }                                                       \
297         }                                                               \
298 } while (0)
299
300 #define TRACE_EXIT_HRES(res)                                            \
301 do {                                                                    \
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));                   \
306                 }                                                       \
307                 else {                                                  \
308                         PRINT(KERN_INFO, "EXIT %s: %lx",                \
309                                         __func__, (long)(res));         \
310                 }                                                       \
311         }                                                               \
312 } while (0)
313 #endif
314
315 #else  /* CONFIG_SCST_DEBUG */
316
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)
330 #endif
331
332 #ifdef LOG_PREFIX
333
334 #define PRINT_INFO(format, args...)                             \
335 do {                                                            \
336         PRINT(KERN_INFO, "%s: " format, LOG_PREFIX, args);      \
337 } while (0)
338
339 #define PRINT_WARNING(format, args...)          \
340 do {                                            \
341         PRINT(KERN_INFO, "%s: ***WARNING***: "  \
342               format, LOG_PREFIX, args);        \
343 } while (0)
344
345 #define PRINT_ERROR(format, args...)            \
346 do {                                            \
347         PRINT(KERN_INFO, "%s: ***ERROR***: "    \
348               format, LOG_PREFIX, args);        \
349 } while (0)
350
351 #define PRINT_CRIT_ERROR(format, args...)       \
352 do {                                            \
353         PRINT(KERN_INFO, "%s: ***CRITICAL ERROR***: "   \
354                 format, LOG_PREFIX, args);              \
355 } while (0)
356
357 #else
358
359 #define PRINT_INFO(format, args...)             \
360 do {                                            \
361         PRINT(KERN_INFO, format, args);         \
362 } while (0)
363
364 #define PRINT_WARNING(format, args...)          \
365 do {                                            \
366         PRINT(KERN_INFO, "***WARNING***: "      \
367                 format, args);                  \
368 } while (0)
369
370 #define PRINT_ERROR(format, args...)            \
371 do {                                            \
372         PRINT(KERN_ERR, "***ERROR***: " \
373                 format, args);                  \
374 } while (0)
375
376 #define PRINT_CRIT_ERROR(format, args...)               \
377 do {                                                    \
378         PRINT(KERN_CRIT, "***CRITICAL ERROR***: "       \
379                 format, args);                          \
380 } while (0)
381
382 #endif /* LOG_PREFIX */
383
384 #endif /* CONFIG_SCST_DEBUG */
385
386 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
387 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
388 #else
389 #define SCST_SLAB_FLAGS 0L
390 #endif
391
392 #endif /* __SCST_DEBUG_H */