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