e2dba9edec74512deb9675b2611dce507e1ab441
[mirror/scst/.git] / usr / fileio / debug.h
1 /*
2  *  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 __DEBUG_H
21 #define __DEBUG_H
22
23 #include <sys/types.h>
24 #include <linux/unistd.h>
25 #include <errno.h>
26
27 extern pid_t gettid(void);
28
29 #define sBUG() assert(0)
30 #define sBUG_ON(p) assert(!(p))
31
32 #ifdef EXTRACHECKS
33 #define EXTRACHECKS_BUG_ON(a)   sBUG_ON(a)
34 #else
35 #define EXTRACHECKS_BUG_ON(a)
36 #endif
37
38 #define TRACE_NULL           0x00000000
39 #define TRACE_DEBUG          0x00000001
40 #define TRACE_FUNCTION       0x00000002
41 #define TRACE_LINE           0x00000004
42 #define TRACE_PID            0x00000008
43 #define TRACE_ENTRYEXIT      0x00000010
44 #define TRACE_BUFF           0x00000020
45 #define TRACE_MEMORY         0x00000040
46 #define TRACE_SG             0x00000080
47 #define TRACE_OUT_OF_MEM     0x00000100
48 #define TRACE_MINOR          0x00000200 /* less important events */
49 #define TRACE_MGMT           0x00000400
50 #define TRACE_MGMT_DEBUG     0x00000800
51 #define TRACE_SCSI           0x00001000
52 #define TRACE_SPECIAL        0x00002000 /* filtering debug, etc */
53 #define TRACE_TIME           0x00004000
54 #define TRACE_ORDER          0x00008000
55 #define TRACE_ALL            0xffffffff
56
57 #define PRINT(format, args...)  fprintf(stdout, format "\n", ## args);
58 #define PRINTN(format, args...) fprintf(stdout, format, ## args);
59
60 extern char *app_name;
61 #define LOG_PREFIX      app_name
62
63 #if defined(DEBUG) || defined(TRACING)
64
65 extern unsigned long trace_flag;
66
67 extern int debug_init(void);
68 extern void debug_done(void);
69
70 extern int debug_print_prefix(unsigned long trace_flag, const char *func, int line);
71 extern void debug_print_buffer(const void *data, int len);
72
73 #define TRACE(trace, format, args...)                           \
74 do {                                                            \
75   if (trace_flag & (trace))                                     \
76   {                                                             \
77     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
78     PRINT(format, args);                                        \
79   }                                                             \
80 } while(0)
81
82 #define TRACE_BUFFER(message, buff, len)                        \
83 do {                                                            \
84   if (trace_flag & TRACE_BUFF)                                  \
85   {                                                             \
86     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
87     PRINT("%s:", message);                                      \
88     debug_print_buffer(buff, len);                              \
89   }                                                             \
90 } while(0)
91
92 #define TRACE_BUFF_FLAG(flag, message, buff, len)               \
93 do {                                                            \
94   if (trace_flag & (flag))                                      \
95   {                                                             \
96     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
97     PRINT("%s:", message);                                      \
98     debug_print_buffer(buff, len);                              \
99   }                                                             \
100 } while(0)
101
102 #else  /* DEBUG || TRACING */
103
104 #define TRACE(trace, args...) {}
105 #define TRACE_BUFFER(message, buff, len) {}
106 #define TRACE_BUFF_FLAG(flag, message, buff, len) {}
107
108 #endif /* DEBUG || TRACING */
109
110 #ifdef DEBUG
111
112 #include <assert.h>
113
114 #define TRACE_MEM(format, args...)                              \
115 do {                                                            \
116   if (trace_flag & TRACE_MEMORY)                                \
117   {                                                             \
118     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
119     PRINT(format, args);                                        \
120   }                                                             \
121 } while(0)
122
123 #define TRACE_DBG(format, args...)                              \
124 do {                                                            \
125   if (trace_flag & TRACE_DEBUG)                                 \
126   {                                                             \
127     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
128     PRINT(format, args);                                        \
129   }                                                             \
130 } while(0)
131
132 #define TRACE_MGMT_DBG(format, args...)                         \
133 do {                                                            \
134   if (trace_flag & TRACE_MGMT_DEBUG)                            \
135   {                                                             \
136     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
137     PRINT(format, args);                                        \
138   }                                                             \
139 } while(0)
140
141 #define PRINT_ERROR_PR(format, args...)                         \
142 do {                                                            \
143   TRACE(trace_flag, "%s: ***ERROR*** " format,                  \
144         LOG_PREFIX, args);                                      \
145 } while(0)
146
147 #define PRINT_INFO_PR(format, args...)                          \
148 do {                                                            \
149   TRACE(trace_flag, "%s: "                                      \
150         format, LOG_PREFIX, args);                              \
151 } while(0)
152
153 #define PRINT_ERROR(format, args...)                            \
154 do {                                                            \
155   TRACE(trace_flag, "***ERROR*** " format, args);               \
156 } while(0)
157
158 #define PRINT_INFO(format, args...)                             \
159 do {                                                            \
160   TRACE(trace_flag, format, args);                              \
161 } while(0)
162
163 #define TRACE_ENTRY()                                           \
164 do {                                                            \
165   if (trace_flag & TRACE_ENTRYEXIT)                             \
166   {                                                             \
167     if (trace_flag & TRACE_PID)                                 \
168     {                                                           \
169       PRINT("[%d]: ENTRY %s", gettid(),                         \
170           __FUNCTION__);                                        \
171     }                                                           \
172     else                                                        \
173     {                                                           \
174       PRINT("ENTRY %s", __FUNCTION__);                          \
175     }                                                           \
176   }                                                             \
177 } while(0)
178
179 #define TRACE_EXIT()                                            \
180 do {                                                            \
181   if (trace_flag & TRACE_ENTRYEXIT)                             \
182   {                                                             \
183     if (trace_flag & TRACE_PID)                                 \
184     {                                                           \
185       PRINT("[%d]: EXIT %s", gettid(),                          \
186           __FUNCTION__);                                        \
187     }                                                           \
188     else                                                        \
189     {                                                           \
190       PRINT("EXIT %s", __FUNCTION__);                           \
191     }                                                           \
192   }                                                             \
193 } while(0)
194
195 #define TRACE_EXIT_RES(res)                                     \
196 do {                                                            \
197   if (trace_flag & TRACE_ENTRYEXIT)                             \
198   {                                                             \
199     if (trace_flag & TRACE_PID)                                 \
200     {                                                           \
201       PRINT("[%d]: EXIT %s: %ld", gettid(),                     \
202         __FUNCTION__, (long)(res));                             \
203     }                                                           \
204     else                                                        \
205     {                                                           \
206       PRINT("EXIT %s: %ld", __FUNCTION__, (long)(res));         \
207     }                                                           \
208   }                                                             \
209 } while(0)
210
211 #define TRACE_EXIT_HRES(res)                                    \
212 do {                                                            \
213   if (trace_flag & TRACE_ENTRYEXIT)                             \
214   {                                                             \
215     if (trace_flag & TRACE_PID)                                 \
216     {                                                           \
217       PRINT("[%d]: EXIT %s: 0x%lx", gettid(),                   \
218         __FUNCTION__, (long)(res));                             \
219     }                                                           \
220     else                                                        \
221     {                                                           \
222       PRINT("EXIT %s: %lx", __FUNCTION__, (long)(res));         \
223     }                                                           \
224   }                                                             \
225 } while(0)
226
227 #else  /* DEBUG */
228
229 #define NDEBUG
230 #include <assert.h>
231
232 #define TRACE_MEM(format, args...) {}
233 #define TRACE_DBG(format, args...) {}
234 #define TRACE_MGMT_DBG(format, args...) {}
235 #define TRACE_ENTRY() {}
236 #define TRACE_EXIT() {}
237 #define TRACE_EXIT_RES(res) {}
238 #define TRACE_EXIT_HRES(res) {}
239
240 #define PRINT_INFO_PR(format, args...)                          \
241 do {                                                            \
242   PRINT("%s: " format, LOG_PREFIX, args);                       \
243 } while(0)
244
245 #define PRINT_ERROR_PR(format, args...)                         \
246 do {                                                            \
247   PRINT("%s: ***ERROR*** "                                      \
248         format, LOG_PREFIX, args);                              \
249 } while(0)
250
251 #define PRINT_INFO(format, args...)                             \
252 do {                                                            \
253   PRINT(format, args);                                          \
254 } while(0)
255
256 #define PRINT_ERROR(format, args...)                            \
257 do {                                                            \
258   PRINT("***ERROR*** " format, args);                           \
259 } while(0)
260
261 #endif /* DEBUG */
262
263 #endif /* __DEBUG_H */