a20611413b7ed0676a93bf0f3e008ab04998edf8
[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)           do { } while (0)
79 #define EXTRACHECKS_WARN_ON(a)          do { } while (0)
80 #define EXTRACHECKS_WARN_ON_ONCE(a)     do { } while (0)
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_DEBUG     0x00000800
98 #define TRACE_SCSI           0x00001000
99 #define TRACE_SPECIAL        0x00002000 /* filtering debug, etc */
100 #define TRACE_FLOW_CONTROL   0x00004000 /* flow control in action */
101 #define TRACE_ALL            0xffffffff
102 /* Flags 0xXXXX0000 are local for users */
103
104 #define TRACE_MINOR_AND_MGMT_DBG        (TRACE_MINOR|TRACE_MGMT_DEBUG)
105
106 #ifndef KERN_CONT
107 #define KERN_CONT       ""
108 #endif
109
110 /*
111  * Note: in the next two printk() statements the KERN_CONT macro is only
112  * present to suppress a checkpatch warning (KERN_CONT is defined as "").
113  */
114 #define PRINT(log_flag, format, args...)  \
115                 printk(log_flag format "\n", ## args)
116 #define PRINTN(log_flag, format, args...) \
117                 printk(log_flag format, ## args)
118
119 #ifdef LOG_PREFIX
120 #define __LOG_PREFIX    LOG_PREFIX
121 #else
122 #define __LOG_PREFIX    NULL
123 #endif
124
125 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
126
127 #ifndef CONFIG_SCST_DEBUG
128 #define ___unlikely(a)          (a)
129 #else
130 #define ___unlikely(a)          unlikely(a)
131 #endif
132
133 /*
134  * We don't print prefix for debug traces to not put additional preasure
135  * on the logging system in case of a lot of logging.
136  */
137
138 extern int debug_print_prefix(unsigned long trace_flag,
139         const char *prefix, const char *func, int line);
140 extern void debug_print_buffer(const void *data, int len);
141
142 #define TRACE(trace, format, args...)                                   \
143 do {                                                                    \
144         if (___unlikely(trace_flag & (trace))) {                        \
145                 debug_print_prefix(trace_flag, __LOG_PREFIX,            \
146                                        __func__, __LINE__);             \
147                 PRINT(KERN_CONT, format, args);                         \
148         }                                                               \
149 } while (0)
150
151 #ifdef CONFIG_SCST_DEBUG
152
153 #define PRINT_BUFFER(message, buff, len)                            \
154 do {                                                                \
155         PRINT(KERN_INFO, "%s:%s:", __func__, message);              \
156         debug_print_buffer(buff, len);                              \
157 } while (0)
158
159 #else
160
161 #define PRINT_BUFFER(message, buff, len)                            \
162 do {                                                                \
163         PRINT(KERN_INFO, "%s:", message);                           \
164         debug_print_buffer(buff, len);                              \
165 } while (0)
166
167 #endif
168
169 #define PRINT_BUFF_FLAG(flag, message, buff, len)                       \
170 do {                                                                    \
171         if (___unlikely(trace_flag & (flag))) {                         \
172                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
173                 PRINT(KERN_CONT, "%s:", message);                       \
174                 debug_print_buffer(buff, len);                          \
175         }                                                               \
176 } while (0)
177
178 #else  /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
179
180 #define TRACE(trace, args...) do {} while (0)
181 #define PRINT_BUFFER(message, buff, len) do {} while (0)
182 #define PRINT_BUFF_FLAG(flag, message, buff, len) do {} while (0)
183
184 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
185
186 #ifdef CONFIG_SCST_DEBUG
187
188 #define TRACE_DBG_FLAG(trace, format, args...)                          \
189 do {                                                                    \
190         if (trace_flag & (trace)) {                                     \
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                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
208                 PRINT(KERN_CONT, "%s:", message);                       \
209                 debug_print_buffer(buff, len);                          \
210         }                                                               \
211 } while (0)
212
213 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
214 do {                                                                    \
215         if (trace_flag & (flag)) {                                      \
216                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
217                 PRINT(KERN_CONT, "%s:", message);                       \
218                 debug_print_buffer(buff, len);                          \
219         }                                                               \
220 } while (0)
221
222 #define PRINT_LOG_FLAG(log_flag, format, args...)                       \
223 do {                                                                    \
224         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
225         PRINT(KERN_CONT, format, args);                                 \
226 } while (0)
227
228 #define PRINT_WARNING(format, args...)                                  \
229 do {                                                                    \
230         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
231         PRINT(KERN_CONT, "***WARNING***: " format, args);               \
232 } while (0)
233
234 #define PRINT_ERROR(format, args...)                                    \
235 do {                                                                    \
236         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
237         PRINT(KERN_CONT, "***ERROR***: " format, args);                 \
238 } while (0)
239
240 #define PRINT_CRIT_ERROR(format, args...)                               \
241 do {                                                                    \
242         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
243         PRINT(KERN_CONT, "***CRITICAL ERROR***: " format, args);        \
244 } while (0)
245
246 #define PRINT_INFO(format, args...)                                     \
247 do {                                                                    \
248         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
249         PRINT(KERN_CONT, format, args);                                 \
250 } while (0)
251
252 #ifndef GENERATING_UPSTREAM_PATCH
253 #define TRACE_ENTRY()                                                   \
254 do {                                                                    \
255         if (trace_flag & TRACE_ENTRYEXIT) {                             \
256                 if (trace_flag & TRACE_PID) {                           \
257                         PRINT(KERN_INFO, "[%d]: ENTRY %s", current->pid, \
258                                 __func__);                              \
259                 }                                                       \
260                 else {                                                  \
261                         PRINT(KERN_INFO, "ENTRY %s", __func__); \
262                 }                                                       \
263         }                                                               \
264 } while (0)
265
266 #define TRACE_EXIT()                                                    \
267 do {                                                                    \
268         if (trace_flag & TRACE_ENTRYEXIT) {                             \
269                 if (trace_flag & TRACE_PID) {                           \
270                         PRINT(KERN_INFO, "[%d]: EXIT %s", current->pid, \
271                                 __func__);                              \
272                 }                                                       \
273                 else {                                                  \
274                         PRINT(KERN_INFO, "EXIT %s", __func__);          \
275                 }                                                       \
276         }                                                               \
277 } while (0)
278
279 #define TRACE_EXIT_RES(res)                                             \
280 do {                                                                    \
281         if (trace_flag & TRACE_ENTRYEXIT) {                             \
282                 if (trace_flag & TRACE_PID) {                           \
283                         PRINT(KERN_INFO, "[%d]: EXIT %s: %ld", current->pid, \
284                               __func__, (long)(res));                   \
285                 }                                                       \
286                 else {                                                  \
287                         PRINT(KERN_INFO, "EXIT %s: %ld",                \
288                                 __func__, (long)(res));                 \
289                 }                                                       \
290         }                                                               \
291 } while (0)
292
293 #define TRACE_EXIT_HRES(res)                                            \
294 do {                                                                    \
295         if (trace_flag & TRACE_ENTRYEXIT) {                             \
296                 if (trace_flag & TRACE_PID) {                           \
297                         PRINT(KERN_INFO, "[%d]: EXIT %s: 0x%lx", current->pid, \
298                               __func__, (long)(res));                   \
299                 }                                                       \
300                 else {                                                  \
301                         PRINT(KERN_INFO, "EXIT %s: %lx",                \
302                                         __func__, (long)(res));         \
303                 }                                                       \
304         }                                                               \
305 } while (0)
306 #endif
307
308 #else  /* CONFIG_SCST_DEBUG */
309
310 #define TRACE_MEM(format, args...) do {} while (0)
311 #define TRACE_SG(format, args...) do {} while (0)
312 #define TRACE_DBG(format, args...) do {} while (0)
313 #define TRACE_DBG_FLAG(format, args...) do {} while (0)
314 #define TRACE_DBG_SPECIAL(format, args...) do {} while (0)
315 #define TRACE_MGMT_DBG(format, args...) do {} while (0)
316 #define TRACE_MGMT_DBG_SPECIAL(format, args...) do {} while (0)
317 #define TRACE_BUFFER(message, buff, len) do {} while (0)
318 #define TRACE_BUFF_FLAG(flag, message, buff, len) do {} while (0)
319
320 #ifndef GENERATING_UPSTREAM_PATCH
321 #define TRACE_ENTRY() do {} while (0)
322 #define TRACE_EXIT() do {} while (0)
323 #define TRACE_EXIT_RES(res) do {} while (0)
324 #define TRACE_EXIT_HRES(res) do {} while (0)
325 #endif
326
327 #ifdef LOG_PREFIX
328
329 #define PRINT_INFO(format, args...)                             \
330 do {                                                            \
331         PRINT(KERN_INFO, "%s: " format, LOG_PREFIX, args);      \
332 } while (0)
333
334 #define PRINT_WARNING(format, args...)          \
335 do {                                            \
336         PRINT(KERN_INFO, "%s: ***WARNING***: "  \
337               format, LOG_PREFIX, args);        \
338 } while (0)
339
340 #define PRINT_ERROR(format, args...)            \
341 do {                                            \
342         PRINT(KERN_INFO, "%s: ***ERROR***: "    \
343               format, LOG_PREFIX, args);        \
344 } while (0)
345
346 #define PRINT_CRIT_ERROR(format, args...)       \
347 do {                                            \
348         PRINT(KERN_INFO, "%s: ***CRITICAL ERROR***: "   \
349                 format, LOG_PREFIX, args);              \
350 } while (0)
351
352 #else
353
354 #define PRINT_INFO(format, args...)             \
355 do {                                            \
356         PRINT(KERN_INFO, format, args);         \
357 } while (0)
358
359 #define PRINT_WARNING(format, args...)          \
360 do {                                            \
361         PRINT(KERN_INFO, "***WARNING***: "      \
362                 format, args);                  \
363 } while (0)
364
365 #define PRINT_ERROR(format, args...)            \
366 do {                                            \
367         PRINT(KERN_ERR, "***ERROR***: " \
368                 format, args);                  \
369 } while (0)
370
371 #define PRINT_CRIT_ERROR(format, args...)               \
372 do {                                                    \
373         PRINT(KERN_CRIT, "***CRITICAL ERROR***: "       \
374                 format, args);                          \
375 } while (0)
376
377 #endif /* LOG_PREFIX */
378
379 #endif /* CONFIG_SCST_DEBUG */
380
381 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
382 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
383 #else
384 #define SCST_SLAB_FLAGS 0L
385 #endif
386
387 #endif /* __SCST_DEBUG_H */