- Version changed in scst_user_spec.txt
[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 static inline int debug_init(void) { return 0; }
109 static inline void debug_done(void) {}
110
111 #endif /* DEBUG || TRACING */
112
113 #ifdef DEBUG
114
115 #include <assert.h>
116
117 #define TRACE_MEM(format, args...)                              \
118 do {                                                            \
119   if (trace_flag & TRACE_MEMORY)                                \
120   {                                                             \
121     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
122     PRINT(format, args);                                        \
123   }                                                             \
124 } while(0)
125
126 #define TRACE_DBG(format, args...)                              \
127 do {                                                            \
128   if (trace_flag & TRACE_DEBUG)                                 \
129   {                                                             \
130     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
131     PRINT(format, args);                                        \
132   }                                                             \
133 } while(0)
134
135 #define TRACE_MGMT_DBG(format, args...)                         \
136 do {                                                            \
137   if (trace_flag & TRACE_MGMT_DEBUG)                            \
138   {                                                             \
139     debug_print_prefix(trace_flag, __FUNCTION__, __LINE__);     \
140     PRINT(format, args);                                        \
141   }                                                             \
142 } while(0)
143
144 #define PRINT_ERROR_PR(format, args...)                         \
145 do {                                                            \
146   TRACE(trace_flag, "%s: ***ERROR*** " format,                  \
147         LOG_PREFIX, args);                                      \
148 } while(0)
149
150 #define PRINT_INFO_PR(format, args...)                          \
151 do {                                                            \
152   TRACE(trace_flag, "%s: "                                      \
153         format, LOG_PREFIX, args);                              \
154 } while(0)
155
156 #define PRINT_ERROR(format, args...)                            \
157 do {                                                            \
158   TRACE(trace_flag, "***ERROR*** " format, args);               \
159 } while(0)
160
161 #define PRINT_INFO(format, args...)                             \
162 do {                                                            \
163   TRACE(trace_flag, format, args);                              \
164 } while(0)
165
166 #define TRACE_ENTRY()                                           \
167 do {                                                            \
168   if (trace_flag & TRACE_ENTRYEXIT)                             \
169   {                                                             \
170     if (trace_flag & TRACE_PID)                                 \
171     {                                                           \
172       PRINT("[%d]: ENTRY %s", gettid(),                         \
173           __FUNCTION__);                                        \
174     }                                                           \
175     else                                                        \
176     {                                                           \
177       PRINT("ENTRY %s", __FUNCTION__);                          \
178     }                                                           \
179   }                                                             \
180 } while(0)
181
182 #define TRACE_EXIT()                                            \
183 do {                                                            \
184   if (trace_flag & TRACE_ENTRYEXIT)                             \
185   {                                                             \
186     if (trace_flag & TRACE_PID)                                 \
187     {                                                           \
188       PRINT("[%d]: EXIT %s", gettid(),                          \
189           __FUNCTION__);                                        \
190     }                                                           \
191     else                                                        \
192     {                                                           \
193       PRINT("EXIT %s", __FUNCTION__);                           \
194     }                                                           \
195   }                                                             \
196 } while(0)
197
198 #define TRACE_EXIT_RES(res)                                     \
199 do {                                                            \
200   if (trace_flag & TRACE_ENTRYEXIT)                             \
201   {                                                             \
202     if (trace_flag & TRACE_PID)                                 \
203     {                                                           \
204       PRINT("[%d]: EXIT %s: %ld", gettid(),                     \
205         __FUNCTION__, (long)(res));                             \
206     }                                                           \
207     else                                                        \
208     {                                                           \
209       PRINT("EXIT %s: %ld", __FUNCTION__, (long)(res));         \
210     }                                                           \
211   }                                                             \
212 } while(0)
213
214 #define TRACE_EXIT_HRES(res)                                    \
215 do {                                                            \
216   if (trace_flag & TRACE_ENTRYEXIT)                             \
217   {                                                             \
218     if (trace_flag & TRACE_PID)                                 \
219     {                                                           \
220       PRINT("[%d]: EXIT %s: 0x%lx", gettid(),                   \
221         __FUNCTION__, (long)(res));                             \
222     }                                                           \
223     else                                                        \
224     {                                                           \
225       PRINT("EXIT %s: %lx", __FUNCTION__, (long)(res));         \
226     }                                                           \
227   }                                                             \
228 } while(0)
229
230 #else  /* DEBUG */
231
232 #define NDEBUG
233 #include <assert.h>
234
235 #define TRACE_MEM(format, args...) {}
236 #define TRACE_DBG(format, args...) {}
237 #define TRACE_MGMT_DBG(format, args...) {}
238 #define TRACE_ENTRY() {}
239 #define TRACE_EXIT() {}
240 #define TRACE_EXIT_RES(res) {}
241 #define TRACE_EXIT_HRES(res) {}
242
243 #define PRINT_INFO_PR(format, args...)                          \
244 do {                                                            \
245   PRINT("%s: " format, LOG_PREFIX, args);                       \
246 } while(0)
247
248 #define PRINT_ERROR_PR(format, args...)                         \
249 do {                                                            \
250   PRINT("%s: ***ERROR*** "                                      \
251         format, LOG_PREFIX, args);                              \
252 } while(0)
253
254 #define PRINT_INFO(format, args...)                             \
255 do {                                                            \
256   PRINT(format, args);                                          \
257 } while(0)
258
259 #define PRINT_ERROR(format, args...)                            \
260 do {                                                            \
261   PRINT("***ERROR*** " format, args);                           \
262 } while(0)
263
264 #endif /* DEBUG */
265
266 #endif /* __DEBUG_H */