- New main Makefile target fcst added
[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 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
25 #include <linux/autoconf.h>     /* for CONFIG_* */
26 #else
27 #include <generated/autoconf.h> /* for CONFIG_* */
28 #endif
29
30 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 19)
31 #include <linux/bug.h>          /* for WARN_ON_ONCE */
32 #endif
33
34 #if !defined(INSIDE_KERNEL_TREE)
35 #ifdef CONFIG_SCST_DEBUG
36
37 #ifndef CONFIG_DEBUG_BUGVERBOSE
38 #define sBUG() do {                                             \
39         printk(KERN_CRIT "BUG at %s:%d\n",                      \
40                __FILE__, __LINE__);                             \
41         BUG();                                                  \
42 } while (0)
43 #else
44 #define sBUG() BUG()
45 #endif
46
47 #define sBUG_ON(p) do {                                         \
48         if (unlikely(p)) {                                      \
49                 printk(KERN_CRIT "BUG at %s:%d (%s)\n",         \
50                        __FILE__, __LINE__, #p);                 \
51                 BUG();                                          \
52         }                                                       \
53 } while (0)
54
55 #else
56
57 #define sBUG() BUG()
58 #define sBUG_ON(p) BUG_ON(p)
59
60 #endif
61 #endif
62
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
64 #define WARN_ON_ONCE(condition) ({                              \
65         static int __warned;                                    \
66         typeof(condition) __ret_warn_once = (condition);        \
67                                                                 \
68         if (unlikely(__ret_warn_once))                          \
69                 if (!__warned) {                                \
70                         WARN_ON(1);                             \
71                         __warned = 1;                           \
72                 }                                               \
73         unlikely(__ret_warn_once);                              \
74 })
75 #endif
76
77 #ifdef CONFIG_SCST_EXTRACHECKS
78 #define EXTRACHECKS_BUG_ON(a)           sBUG_ON(a)
79 #define EXTRACHECKS_WARN_ON(a)          WARN_ON(a)
80 #define EXTRACHECKS_WARN_ON_ONCE(a)     WARN_ON_ONCE(a)
81 #else
82 #define EXTRACHECKS_BUG_ON(a)           do { } while (0)
83 #define EXTRACHECKS_WARN_ON(a)          do { } while (0)
84 #define EXTRACHECKS_WARN_ON_ONCE(a)     do { } while (0)
85 #endif
86
87 #define TRACE_NULL           0x00000000
88 #define TRACE_DEBUG          0x00000001
89 #define TRACE_FUNCTION       0x00000002
90 #define TRACE_LINE           0x00000004
91 #define TRACE_PID            0x00000008
92 #ifndef GENERATING_UPSTREAM_PATCH
93 #define TRACE_ENTRYEXIT      0x00000010
94 #endif
95 #define TRACE_BUFF           0x00000020
96 #define TRACE_MEMORY         0x00000040
97 #define TRACE_SG_OP          0x00000080
98 #define TRACE_OUT_OF_MEM     0x00000100
99 #define TRACE_MINOR          0x00000200 /* less important events */
100 #define TRACE_MGMT           0x00000400
101 #define TRACE_MGMT_DEBUG     0x00000800
102 #define TRACE_SCSI           0x00001000
103 #define TRACE_SPECIAL        0x00002000 /* filtering debug, etc */
104 #define TRACE_FLOW_CONTROL   0x00004000 /* flow control in action */
105 #define TRACE_ALL            0xffffffff
106 /* Flags 0xXXXX0000 are local for users */
107
108 #define TRACE_MINOR_AND_MGMT_DBG        (TRACE_MINOR|TRACE_MGMT_DEBUG)
109
110 #ifndef KERN_CONT
111 #define KERN_CONT       ""
112 #endif
113
114 /*
115  * Note: in the next two printk() statements the KERN_CONT macro is only
116  * present to suppress a checkpatch warning (KERN_CONT is defined as "").
117  */
118 #define PRINT(log_flag, format, args...)  \
119                 printk(log_flag format "\n", ## args)
120 #define PRINTN(log_flag, format, args...) \
121                 printk(log_flag format, ## args)
122
123 #ifdef LOG_PREFIX
124 #define __LOG_PREFIX    LOG_PREFIX
125 #else
126 #define __LOG_PREFIX    NULL
127 #endif
128
129 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
130
131 #ifndef CONFIG_SCST_DEBUG
132 #define ___unlikely(a)          (a)
133 #else
134 #define ___unlikely(a)          unlikely(a)
135 #endif
136
137 /*
138  * We don't print prefix for debug traces to not put additional preasure
139  * on the logging system in case of a lot of logging.
140  */
141
142 extern int debug_print_prefix(unsigned long trace_flag,
143         const char *prefix, const char *func, int line);
144 extern void debug_print_buffer(const void *data, int len);
145
146 #define TRACE(trace, format, args...)                                   \
147 do {                                                                    \
148         if (___unlikely(trace_flag & (trace))) {                        \
149                 debug_print_prefix(trace_flag, __LOG_PREFIX,            \
150                                        __func__, __LINE__);             \
151                 PRINT(KERN_CONT, format, args);                         \
152         }                                                               \
153 } while (0)
154
155 #ifdef CONFIG_SCST_DEBUG
156
157 #define PRINT_BUFFER(message, buff, len)                            \
158 do {                                                                \
159         PRINT(KERN_INFO, "%s:%s:", __func__, message);              \
160         debug_print_buffer(buff, len);                              \
161 } while (0)
162
163 #else
164
165 #define PRINT_BUFFER(message, buff, len)                            \
166 do {                                                                \
167         PRINT(KERN_INFO, "%s:", message);                           \
168         debug_print_buffer(buff, len);                              \
169 } while (0)
170
171 #endif
172
173 #define PRINT_BUFF_FLAG(flag, message, buff, len)                       \
174 do {                                                                    \
175         if (___unlikely(trace_flag & (flag))) {                         \
176                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
177                 PRINT(KERN_CONT, "%s:", message);                       \
178                 debug_print_buffer(buff, len);                          \
179         }                                                               \
180 } while (0)
181
182 #else  /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
183
184 #define TRACE(trace, args...) do {} while (0)
185 #define PRINT_BUFFER(message, buff, len) do {} while (0)
186 #define PRINT_BUFF_FLAG(flag, message, buff, len) do {} while (0)
187
188 #endif /* CONFIG_SCST_DEBUG || CONFIG_SCST_TRACING */
189
190 #ifdef CONFIG_SCST_DEBUG
191
192 #define TRACE_DBG_FLAG(trace, format, args...)                          \
193 do {                                                                    \
194         if (trace_flag & (trace)) {                                     \
195                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
196                 PRINT(KERN_CONT, format, args);                         \
197         }                                                               \
198 } while (0)
199
200 #define TRACE_MEM(args...)              TRACE_DBG_FLAG(TRACE_MEMORY, args)
201 #define TRACE_SG(args...)               TRACE_DBG_FLAG(TRACE_SG_OP, args)
202 #define TRACE_DBG(args...)              TRACE_DBG_FLAG(TRACE_DEBUG, args)
203 #define TRACE_DBG_SPECIAL(args...)      TRACE_DBG_FLAG(TRACE_DEBUG|TRACE_SPECIAL, args)
204 #define TRACE_MGMT_DBG(args...)         TRACE_DBG_FLAG(TRACE_MGMT_DEBUG, args)
205 #define TRACE_MGMT_DBG_SPECIAL(args...) \
206                 TRACE_DBG_FLAG(TRACE_MGMT_DEBUG|TRACE_SPECIAL, args)
207
208 #define TRACE_BUFFER(message, buff, len)                                \
209 do {                                                                    \
210         if (trace_flag & TRACE_BUFF) {                                  \
211                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
212                 PRINT(KERN_CONT, "%s:", message);                       \
213                 debug_print_buffer(buff, len);                          \
214         }                                                               \
215 } while (0)
216
217 #define TRACE_BUFF_FLAG(flag, message, buff, len)                       \
218 do {                                                                    \
219         if (trace_flag & (flag)) {                                      \
220                 debug_print_prefix(trace_flag, NULL, __func__, __LINE__);\
221                 PRINT(KERN_CONT, "%s:", message);                       \
222                 debug_print_buffer(buff, len);                          \
223         }                                                               \
224 } while (0)
225
226 #define PRINT_LOG_FLAG(log_flag, format, args...)                       \
227 do {                                                                    \
228         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
229         PRINT(KERN_CONT, format, args);                                 \
230 } while (0)
231
232 #define PRINT_WARNING(format, args...)                                  \
233 do {                                                                    \
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         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
241         PRINT(KERN_CONT, "***ERROR***: " format, args);                 \
242 } while (0)
243
244 #define PRINT_CRIT_ERROR(format, args...)                               \
245 do {                                                                    \
246         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
247         PRINT(KERN_CONT, "***CRITICAL ERROR***: " format, args);        \
248 } while (0)
249
250 #define PRINT_INFO(format, args...)                                     \
251 do {                                                                    \
252         debug_print_prefix(trace_flag, __LOG_PREFIX, __func__, __LINE__);\
253         PRINT(KERN_CONT, format, args);                                 \
254 } while (0)
255
256 #ifndef GENERATING_UPSTREAM_PATCH
257 #define TRACE_ENTRY()                                                   \
258 do {                                                                    \
259         if (trace_flag & TRACE_ENTRYEXIT) {                             \
260                 if (trace_flag & TRACE_PID) {                           \
261                         PRINT(KERN_INFO, "[%d]: ENTRY %s", current->pid, \
262                                 __func__);                              \
263                 }                                                       \
264                 else {                                                  \
265                         PRINT(KERN_INFO, "ENTRY %s", __func__); \
266                 }                                                       \
267         }                                                               \
268 } while (0)
269
270 #define TRACE_EXIT()                                                    \
271 do {                                                                    \
272         if (trace_flag & TRACE_ENTRYEXIT) {                             \
273                 if (trace_flag & TRACE_PID) {                           \
274                         PRINT(KERN_INFO, "[%d]: EXIT %s", current->pid, \
275                                 __func__);                              \
276                 }                                                       \
277                 else {                                                  \
278                         PRINT(KERN_INFO, "EXIT %s", __func__);          \
279                 }                                                       \
280         }                                                               \
281 } while (0)
282
283 #define TRACE_EXIT_RES(res)                                             \
284 do {                                                                    \
285         if (trace_flag & TRACE_ENTRYEXIT) {                             \
286                 if (trace_flag & TRACE_PID) {                           \
287                         PRINT(KERN_INFO, "[%d]: EXIT %s: %ld", current->pid, \
288                               __func__, (long)(res));                   \
289                 }                                                       \
290                 else {                                                  \
291                         PRINT(KERN_INFO, "EXIT %s: %ld",                \
292                                 __func__, (long)(res));                 \
293                 }                                                       \
294         }                                                               \
295 } while (0)
296
297 #define TRACE_EXIT_HRES(res)                                            \
298 do {                                                                    \
299         if (trace_flag & TRACE_ENTRYEXIT) {                             \
300                 if (trace_flag & TRACE_PID) {                           \
301                         PRINT(KERN_INFO, "[%d]: EXIT %s: 0x%lx", current->pid, \
302                               __func__, (long)(res));                   \
303                 }                                                       \
304                 else {                                                  \
305                         PRINT(KERN_INFO, "EXIT %s: %lx",                \
306                                         __func__, (long)(res));         \
307                 }                                                       \
308         }                                                               \
309 } while (0)
310 #endif
311
312 #else  /* CONFIG_SCST_DEBUG */
313
314 #define TRACE_MEM(format, args...) do {} while (0)
315 #define TRACE_SG(format, args...) do {} while (0)
316 #define TRACE_DBG(format, args...) do {} while (0)
317 #define TRACE_DBG_FLAG(format, args...) do {} while (0)
318 #define TRACE_DBG_SPECIAL(format, args...) do {} while (0)
319 #define TRACE_MGMT_DBG(format, args...) do {} while (0)
320 #define TRACE_MGMT_DBG_SPECIAL(format, args...) do {} while (0)
321 #define TRACE_BUFFER(message, buff, len) do {} while (0)
322 #define TRACE_BUFF_FLAG(flag, message, buff, len) do {} while (0)
323
324 #ifndef GENERATING_UPSTREAM_PATCH
325 #define TRACE_ENTRY() do {} while (0)
326 #define TRACE_EXIT() do {} while (0)
327 #define TRACE_EXIT_RES(res) do {} while (0)
328 #define TRACE_EXIT_HRES(res) do {} while (0)
329 #endif
330
331 #ifdef LOG_PREFIX
332
333 #define PRINT_INFO(format, args...)                             \
334 do {                                                            \
335         PRINT(KERN_INFO, "%s: " format, LOG_PREFIX, args);      \
336 } while (0)
337
338 #define PRINT_WARNING(format, args...)          \
339 do {                                            \
340         PRINT(KERN_INFO, "%s: ***WARNING***: "  \
341               format, LOG_PREFIX, args);        \
342 } while (0)
343
344 #define PRINT_ERROR(format, args...)            \
345 do {                                            \
346         PRINT(KERN_INFO, "%s: ***ERROR***: "    \
347               format, LOG_PREFIX, args);        \
348 } while (0)
349
350 #define PRINT_CRIT_ERROR(format, args...)       \
351 do {                                            \
352         PRINT(KERN_INFO, "%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(KERN_INFO, format, args);         \
361 } while (0)
362
363 #define PRINT_WARNING(format, args...)          \
364 do {                                            \
365         PRINT(KERN_INFO, "***WARNING***: "      \
366                 format, args);                  \
367 } while (0)
368
369 #define PRINT_ERROR(format, args...)            \
370 do {                                            \
371         PRINT(KERN_ERR, "***ERROR***: " \
372                 format, args);                  \
373 } while (0)
374
375 #define PRINT_CRIT_ERROR(format, args...)               \
376 do {                                                    \
377         PRINT(KERN_CRIT, "***CRITICAL ERROR***: "       \
378                 format, args);                          \
379 } while (0)
380
381 #endif /* LOG_PREFIX */
382
383 #endif /* CONFIG_SCST_DEBUG */
384
385 #if defined(CONFIG_SCST_DEBUG) && defined(CONFIG_DEBUG_SLAB)
386 #define SCST_SLAB_FLAGS (SLAB_RED_ZONE | SLAB_POISON)
387 #else
388 #define SCST_SLAB_FLAGS 0L
389 #endif
390
391 #endif /* __SCST_DEBUG_H */