Whamcloud - gitweb
b=20668
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_debug.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/libcfs_debug.h
37  *
38  * Debug messages and assertions
39  *
40  */
41
42 #ifndef __LIBCFS_DEBUG_H__
43 #define __LIBCFS_DEBUG_H__
44
45 /*
46  *  Debugging
47  */
48 extern unsigned int libcfs_subsystem_debug;
49 extern unsigned int libcfs_stack;
50 extern unsigned int libcfs_debug;
51 extern unsigned int libcfs_printk;
52 extern unsigned int libcfs_console_ratelimit;
53 extern unsigned int libcfs_watchdog_ratelimit;
54 extern cfs_duration_t libcfs_console_max_delay;
55 extern cfs_duration_t libcfs_console_min_delay;
56 extern unsigned int libcfs_console_backoff;
57 extern unsigned int libcfs_debug_binary;
58 extern char debug_file_path_arr[1024];
59 #ifdef __KERNEL__
60 extern char *debug_file_path;
61 #endif
62
63 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
64 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
65
66 /* Has there been an LBUG? */
67 extern unsigned int libcfs_catastrophe;
68 extern unsigned int libcfs_panic_on_lbug;
69
70 /**
71  * Format for debug message headers
72  */
73 struct ptldebug_header {
74         __u32 ph_len;
75         __u32 ph_flags;
76         __u32 ph_subsys;
77         __u32 ph_mask;
78         __u32 ph_cpu_id;
79         __u32 ph_sec;
80         __u64 ph_usec;
81         __u32 ph_stack;
82         __u32 ph_pid;
83         __u32 ph_extern_pid;
84         __u32 ph_line_num;
85 } __attribute__((packed));
86
87
88 #define PH_FLAG_FIRST_RECORD 1
89
90 /* Debugging subsystems (32 bits, non-overlapping) */
91 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
92 #define S_UNDEFINED   0x00000001
93 #define S_MDC         0x00000002
94 #define S_MDS         0x00000004
95 #define S_OSC         0x00000008
96 #define S_OST         0x00000010
97 #define S_CLASS       0x00000020
98 #define S_LOG         0x00000040
99 #define S_LLITE       0x00000080
100 #define S_RPC         0x00000100
101 #define S_MGMT        0x00000200
102 #define S_LNET        0x00000400
103 #define S_LND         0x00000800 /* ALL LNDs */
104 #define S_PINGER      0x00001000
105 #define S_FILTER      0x00002000
106 /* unused */
107 #define S_ECHO        0x00008000
108 #define S_LDLM        0x00010000
109 #define S_LOV         0x00020000
110 #define S_LQUOTA      0x00040000
111 /* unused */
112 /* unused */
113 /* unused */
114 /* unused */
115 #define S_LMV         0x00800000 /* b_new_cmd */
116 /* unused */
117 #define S_SEC         0x02000000 /* upcall cache */
118 #define S_GSS         0x04000000 /* b_new_cmd */
119 /* unused */
120 #define S_MGC         0x10000000
121 #define S_MGS         0x20000000
122 #define S_FID         0x40000000 /* b_new_cmd */
123 #define S_FLD         0x80000000 /* b_new_cmd */
124 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
125
126 /* Debugging masks (32 bits, non-overlapping) */
127 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
128 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
129 #define D_INODE       0x00000002
130 #define D_SUPER       0x00000004
131 #define D_EXT2        0x00000008 /* anything from ext2_debug */
132 #define D_MALLOC      0x00000010 /* print malloc, free information */
133 #define D_CACHE       0x00000020 /* cache-related items */
134 #define D_INFO        0x00000040 /* general information */
135 #define D_IOCTL       0x00000080 /* ioctl related information */
136 #define D_NETERROR    0x00000100 /* network errors */
137 #define D_NET         0x00000200 /* network communications */
138 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
139 #define D_BUFFS       0x00000800
140 #define D_OTHER       0x00001000
141 #define D_DENTRY      0x00002000
142 #define D_NETTRACE    0x00004000
143 #define D_PAGE        0x00008000 /* bulk page handling */
144 #define D_DLMTRACE    0x00010000
145 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
146 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
147 #define D_HA          0x00080000 /* recovery and failover */
148 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
149 #define D_VFSTRACE    0x00200000
150 #define D_READA       0x00400000 /* read-ahead */
151 #define D_MMAP        0x00800000
152 #define D_CONFIG      0x01000000
153 #define D_CONSOLE     0x02000000
154 #define D_QUOTA       0x04000000
155 #define D_SEC         0x08000000
156 /* keep these in sync with lnet/{utils,libcfs}/debug.c */
157
158 #define D_CANTMASK   (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
159
160 #ifndef DEBUG_SUBSYSTEM
161 # define DEBUG_SUBSYSTEM S_UNDEFINED
162 #endif
163
164 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
165 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
166 #define CDEBUG_DEFAULT_BACKOFF   2
167 typedef struct {
168         cfs_time_t      cdls_next;
169         int             cdls_count;
170         cfs_duration_t  cdls_delay;
171 } cfs_debug_limit_state_t;
172
173 #if defined(__KERNEL__) || (defined(__arch_lib__) && !defined(LUSTRE_UTILS))
174
175 #ifdef CDEBUG_ENABLED
176
177 /**
178  * Filters out logging messages based on mask and subsystem.
179  */
180 static inline int cdebug_show(unsigned int mask, unsigned int subsystem)
181 {
182         return mask & D_CANTMASK ||
183                 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
184 }
185
186 #define __CDEBUG(cdls, mask, format, ...)                               \
187 do {                                                                    \
188         CHECK_STACK();                                                  \
189                                                                         \
190         if (cdebug_show(mask, DEBUG_SUBSYSTEM))                         \
191                 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, mask,           \
192                                  __FILE__, __FUNCTION__, __LINE__,      \
193                                  format, ## __VA_ARGS__);               \
194 } while (0)
195
196 #define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
197
198 #define CDEBUG_LIMIT(mask, format, ...)         \
199 do {                                            \
200         static cfs_debug_limit_state_t cdls;    \
201                                                 \
202         __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);\
203 } while (0)
204
205 #else /* !CDEBUG_ENABLED */
206 static inline int cdebug_show(unsigned int mask, unsigned int subsystem)
207 {
208         return 0;
209 }
210 #define CDEBUG(mask, format, ...) (void)(0)
211 #define CDEBUG_LIMIT(mask, format, ...) (void)(0)
212 #warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
213 #endif
214
215 #else /* !__KERNEL__ && (!__arch_lib__ || LUSTRE_UTILS) */
216
217 #define CDEBUG(mask, format, ...)                                       \
218 do {                                                                    \
219         if (((mask) & D_CANTMASK) != 0)                                 \
220                 fprintf(stderr, "(%s:%d:%s()) " format,                 \
221                         __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__);\
222 } while (0)
223
224 #define CDEBUG_LIMIT CDEBUG
225
226 #endif /* !__KERNEL__ ... */
227
228
229 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
230 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
231 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
232
233 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
234 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
235 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
236 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
237                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
238 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
239
240 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
241
242 #ifdef CDEBUG_ENABLED
243
244 #define GOTO(label, rc)                                                 \
245 do {                                                                    \
246         long_ptr_t GOTO__ret = (long_ptr_t)(rc);                        \
247         CDEBUG(D_TRACE,"Process leaving via %s (rc=" LPLU " : " LPLD    \
248                " : " LPLX ")\n", #label, (ulong_ptr_t)GOTO__ret,        \
249                GOTO__ret, GOTO__ret);                                   \
250         goto label;                                                     \
251 } while (0)
252 #else
253 #define GOTO(label, rc) do { ((void)(rc)); goto label; } while (0)
254 #endif
255
256 #ifdef CDEBUG_ENTRY_EXIT
257
258 /*
259  * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
260  * there will be a warning in osx.
261  */
262 #if defined(__GNUC__)
263 #define RETURN(rc)                                                      \
264 do {                                                                    \
265         typeof(rc) RETURN__ret = (rc);                                  \
266         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
267                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
268         EXIT_NESTING;                                                   \
269         return RETURN__ret;                                             \
270 } while (0)
271 #elif defined(_MSC_VER)
272 #define RETURN(rc)                                                      \
273 do {                                                                    \
274         CDEBUG(D_TRACE, "Process leaving.\n");                          \
275         EXIT_NESTING;                                                   \
276         return (rc);                                                    \
277 } while (0)
278 #else
279 # error "Unkown compiler"
280 #endif /* __GNUC__ */
281
282 #define ENTRY                                                           \
283 ENTRY_NESTING;                                                          \
284 do {                                                                    \
285         CDEBUG(D_TRACE, "Process entered\n");                           \
286 } while (0)
287
288 #define EXIT                                                            \
289 do {                                                                    \
290         CDEBUG(D_TRACE, "Process leaving\n");                           \
291         EXIT_NESTING;                                                   \
292 } while(0)
293 #else /* !CDEBUG_ENTRY_EXIT */
294
295 #define RETURN(rc) return (rc)
296 #define ENTRY                           do { } while (0)
297 #define EXIT                            do { } while (0)
298
299 #endif /* !CDEBUG_ENTRY_EXIT */
300
301 #define RETURN_EXIT                                                     \
302 do {                                                                    \
303         EXIT_NESTING;                                                   \
304         return;                                                         \
305 } while (0)
306
307 struct libcfs_debug_msg_data {
308         cfs_debug_limit_state_t *msg_cdls;
309         int                      msg_subsys;
310         const char              *msg_file;
311         const char              *msg_fn;
312         int                      msg_line;
313 };
314
315 #define DEBUG_MSG_DATA_INIT(cdls, subsystem, file, func, ln ) { \
316         /* msg_cdls */          (cdls),       \
317         /* msg_subsys */        (subsystem),  \
318         /* msg_file */          (file),       \
319         /* msg_fn */            (func),       \
320         /* msg_line */          (ln)          \
321     }
322
323
324 extern int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
325                               int subsys, int mask,
326                               const char *file, const char *fn, const int line,
327                               const char *format1, va_list args,
328                               const char *format2, ...)
329         __attribute__ ((format (printf, 9, 10)));
330
331 #define libcfs_debug_vmsg(cdls, subsys, mask, file, fn, line, format, args)   \
332     libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,format,args,NULL,NULL)
333
334 #define libcfs_debug_msg(cdls, subsys, mask, file, fn, line, format, ...)    \
335     libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,NULL,NULL,format, ## __VA_ARGS__)
336
337 #define cdebug_va(cdls, mask, file, func, line, fmt, args)      do {          \
338         CHECK_STACK();                                                        \
339                                                                               \
340         if (cdebug_show(mask, DEBUG_SUBSYSTEM))                               \
341                 libcfs_debug_vmsg(cdls, DEBUG_SUBSYSTEM, (mask),              \
342                                   (file), (func), (line), fmt, args);         \
343 } while(0)
344
345 #define cdebug(cdls, mask, file, func, line, fmt, ...) do {                   \
346         CHECK_STACK();                                                        \
347                                                                               \
348         if (cdebug_show(mask, DEBUG_SUBSYSTEM))                               \
349                 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, (mask),               \
350                                  (file), (func), (line), fmt, ## __VA_ARGS__);\
351 } while(0)
352
353 extern void libcfs_assertion_failed(const char *expr, const char *file,
354                                     const char *fn, const int line);
355
356
357 #if defined(HAVE_BGL_SUPPORT)
358 #define DEBUG_FILE_PATH_DEFAULT "/bgl/ion/tmp/lustre-log"
359 #elif defined(__arch_um__)
360 #define DEBUG_FILE_PATH_DEFAULT "/r/tmp/lustre-log"
361 #elif defined(__WINNT__)
362 #define DEBUG_FILE_PATH_DEFAULT "\\SystemRoot\\temp\\lustre-log"
363 #else
364 #define DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
365 #endif
366
367 #endif  /* __LIBCFS_DEBUG_H__ */