Whamcloud - gitweb
66f2f2cada58840fd949714ca40ff36f45ed2bb1
[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 libcfs_debug_file_path_arr[1024];
59
60 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
61 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
62
63 /* Has there been an LBUG? */
64 extern unsigned int libcfs_catastrophe;
65 extern unsigned int libcfs_panic_on_lbug;
66
67 /**
68  * three types of trace_data in linux
69  */
70 typedef enum {
71         CFS_TCD_TYPE_PROC = 0,
72         CFS_TCD_TYPE_SOFTIRQ,
73         CFS_TCD_TYPE_IRQ,
74         CFS_TCD_TYPE_MAX
75 } cfs_trace_buf_type_t;
76
77 /**
78  * Format for debug message headers
79  */
80 struct ptldebug_header {
81         __u32 ph_len;
82         __u32 ph_flags;
83         __u32 ph_subsys;
84         __u32 ph_mask;
85         __u16 ph_cpu_id;
86         __u16 ph_type;
87         __u32 ph_sec;
88         __u64 ph_usec;
89         __u32 ph_stack;
90         __u32 ph_pid;
91         __u32 ph_extern_pid;
92         __u32 ph_line_num;
93 } __attribute__((packed));
94
95
96 #define PH_FLAG_FIRST_RECORD 1
97
98 /* Debugging subsystems (32 bits, non-overlapping) */
99 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
100 #define S_UNDEFINED   0x00000001
101 #define S_MDC         0x00000002
102 #define S_MDS         0x00000004
103 #define S_OSC         0x00000008
104 #define S_OST         0x00000010
105 #define S_CLASS       0x00000020
106 #define S_LOG         0x00000040
107 #define S_LLITE       0x00000080
108 #define S_RPC         0x00000100
109 #define S_MGMT        0x00000200
110 #define S_LNET        0x00000400
111 #define S_LND         0x00000800 /* ALL LNDs */
112 #define S_PINGER      0x00001000
113 #define S_FILTER      0x00002000
114 /* unused */
115 #define S_ECHO        0x00008000
116 #define S_LDLM        0x00010000
117 #define S_LOV         0x00020000
118 #define S_LQUOTA      0x00040000
119 /* unused */
120 /* unused */
121 /* unused */
122 /* unused */
123 #define S_LMV         0x00800000 /* b_new_cmd */
124 /* unused */
125 #define S_SEC         0x02000000 /* upcall cache */
126 #define S_GSS         0x04000000 /* b_new_cmd */
127 /* unused */
128 #define S_MGC         0x10000000
129 #define S_MGS         0x20000000
130 #define S_FID         0x40000000 /* b_new_cmd */
131 #define S_FLD         0x80000000 /* b_new_cmd */
132 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
133
134 /* Debugging masks (32 bits, non-overlapping) */
135 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
136 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
137 #define D_INODE       0x00000002
138 #define D_SUPER       0x00000004
139 #define D_EXT2        0x00000008 /* anything from ext2_debug */
140 #define D_MALLOC      0x00000010 /* print malloc, free information */
141 #define D_CACHE       0x00000020 /* cache-related items */
142 #define D_INFO        0x00000040 /* general information */
143 #define D_IOCTL       0x00000080 /* ioctl related information */
144 #define D_NETERROR    0x00000100 /* network errors */
145 #define D_NET         0x00000200 /* network communications */
146 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
147 #define D_BUFFS       0x00000800
148 #define D_OTHER       0x00001000
149 #define D_DENTRY      0x00002000
150 #define D_NETTRACE    0x00004000
151 #define D_PAGE        0x00008000 /* bulk page handling */
152 #define D_DLMTRACE    0x00010000
153 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
154 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
155 #define D_HA          0x00080000 /* recovery and failover */
156 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
157 #define D_VFSTRACE    0x00200000
158 #define D_READA       0x00400000 /* read-ahead */
159 #define D_MMAP        0x00800000
160 #define D_CONFIG      0x01000000
161 #define D_CONSOLE     0x02000000
162 #define D_QUOTA       0x04000000
163 #define D_SEC         0x08000000
164 /* keep these in sync with lnet/{utils,libcfs}/debug.c */
165
166 #define D_HSM         D_TRACE
167
168 #define D_CANTMASK   (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
169
170 #ifndef DEBUG_SUBSYSTEM
171 # define DEBUG_SUBSYSTEM S_UNDEFINED
172 #endif
173
174 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
175 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
176 #define CDEBUG_DEFAULT_BACKOFF   2
177 typedef struct {
178         cfs_time_t      cdls_next;
179         int             cdls_count;
180         cfs_duration_t  cdls_delay;
181 } cfs_debug_limit_state_t;
182
183 #if defined(__KERNEL__) || (defined(__arch_lib__) && !defined(LUSTRE_UTILS))
184
185 #ifdef CDEBUG_ENABLED
186
187 /**
188  * Filters out logging messages based on mask and subsystem.
189  */
190 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
191 {
192         return mask & D_CANTMASK ||
193                 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
194 }
195
196 #define __CDEBUG(cdls, mask, format, ...)                               \
197 do {                                                                    \
198         CFS_CHECK_STACK();                                              \
199                                                                         \
200         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM))                     \
201                 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, mask,           \
202                                  __FILE__, __FUNCTION__, __LINE__,      \
203                                  format, ## __VA_ARGS__);               \
204 } while (0)
205
206 #define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
207
208 #define CDEBUG_LIMIT(mask, format, ...)         \
209 do {                                            \
210         static cfs_debug_limit_state_t cdls;    \
211                                                 \
212         __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);\
213 } while (0)
214
215 #else /* !CDEBUG_ENABLED */
216 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
217 {
218         return 0;
219 }
220 #define CDEBUG(mask, format, ...) (void)(0)
221 #define CDEBUG_LIMIT(mask, format, ...) (void)(0)
222 #warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
223 #endif
224
225 #else /* !__KERNEL__ && (!__arch_lib__ || LUSTRE_UTILS) */
226
227 #define CDEBUG(mask, format, ...)                                       \
228 do {                                                                    \
229         if (((mask) & D_CANTMASK) != 0)                                 \
230                 fprintf(stderr, "(%s:%d:%s()) " format,                 \
231                         __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__);\
232 } while (0)
233
234 #define CDEBUG_LIMIT CDEBUG
235
236 #endif /* !__KERNEL__ ... */
237
238
239 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
240 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
241 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
242
243 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
244 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
245 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
246 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
247                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
248 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
249
250 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
251
252 #ifdef CDEBUG_ENABLED
253
254 #define GOTO(label, rc)                                                 \
255 do {                                                                    \
256         long_ptr_t GOTO__ret = (long_ptr_t)(rc);                        \
257         CDEBUG(D_TRACE,"Process leaving via %s (rc=" LPLU " : " LPLD    \
258                " : " LPLX ")\n", #label, (ulong_ptr_t)GOTO__ret,        \
259                GOTO__ret, GOTO__ret);                                   \
260         goto label;                                                     \
261 } while (0)
262 #else
263 #define GOTO(label, rc) do { ((void)(rc)); goto label; } while (0)
264 #endif
265
266 #ifdef CDEBUG_ENTRY_EXIT
267
268 /*
269  * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
270  * there will be a warning in osx.
271  */
272 #if defined(__GNUC__)
273 #define RETURN(rc)                                                      \
274 do {                                                                    \
275         typeof(rc) RETURN__ret = (rc);                                  \
276         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
277                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
278         EXIT_NESTING;                                                   \
279         return RETURN__ret;                                             \
280 } while (0)
281 #elif defined(_MSC_VER)
282 #define RETURN(rc)                                                      \
283 do {                                                                    \
284         CDEBUG(D_TRACE, "Process leaving.\n");                          \
285         EXIT_NESTING;                                                   \
286         return (rc);                                                    \
287 } while (0)
288 #else
289 # error "Unkown compiler"
290 #endif /* __GNUC__ */
291
292 #define ENTRY                                                           \
293 ENTRY_NESTING;                                                          \
294 do {                                                                    \
295         CDEBUG(D_TRACE, "Process entered\n");                           \
296 } while (0)
297
298 #define EXIT                                                            \
299 do {                                                                    \
300         CDEBUG(D_TRACE, "Process leaving\n");                           \
301         EXIT_NESTING;                                                   \
302 } while(0)
303 #else /* !CDEBUG_ENTRY_EXIT */
304
305 #define RETURN(rc) return (rc)
306 #define ENTRY                           do { } while (0)
307 #define EXIT                            do { } while (0)
308
309 #endif /* !CDEBUG_ENTRY_EXIT */
310
311 #define RETURN_EXIT                                                     \
312 do {                                                                    \
313         EXIT_NESTING;                                                   \
314         return;                                                         \
315 } while (0)
316
317 struct libcfs_debug_msg_data {
318         cfs_debug_limit_state_t *msg_cdls;
319         int                      msg_subsys;
320         const char              *msg_file;
321         const char              *msg_fn;
322         int                      msg_line;
323 };
324
325 #define DEBUG_MSG_DATA_INIT(cdls, subsystem, file, func, ln ) { \
326         /* msg_cdls */          (cdls),       \
327         /* msg_subsys */        (subsystem),  \
328         /* msg_file */          (file),       \
329         /* msg_fn */            (func),       \
330         /* msg_line */          (ln)          \
331     }
332
333
334 extern int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
335                               int subsys, int mask,
336                               const char *file, const char *fn, const int line,
337                               const char *format1, va_list args,
338                               const char *format2, ...)
339         __attribute__ ((format (printf, 9, 10)));
340
341 #define libcfs_debug_vmsg(cdls, subsys, mask, file, fn, line, format, args)   \
342     libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,format,args,NULL,NULL)
343
344 #define libcfs_debug_msg(cdls, subsys, mask, file, fn, line, format, ...)    \
345     libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,NULL,NULL,format, ## __VA_ARGS__)
346
347 #define cdebug_va(cdls, mask, file, func, line, fmt, args)      do {          \
348         CFS_CHECK_STACK();                                                    \
349                                                                               \
350         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM))                           \
351                 libcfs_debug_vmsg(cdls, DEBUG_SUBSYSTEM, (mask),              \
352                                   (file), (func), (line), fmt, args);         \
353 } while(0)
354
355 #define cdebug(cdls, mask, file, func, line, fmt, ...) do {                   \
356         CFS_CHECK_STACK();                                                    \
357                                                                               \
358         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM))                           \
359                 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, (mask),               \
360                                  (file), (func), (line), fmt, ## __VA_ARGS__);\
361 } while(0)
362
363 extern void libcfs_assertion_failed(const char *expr, const char *file,
364                                     const char *fn, const int line);
365
366 /* one more external symbol that tracefile provides: */
367 extern int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
368                                     const char *knl_buffer, char *append);
369
370
371 #if defined(HAVE_BGL_SUPPORT)
372 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/bgl/ion/tmp/lustre-log"
373 #elif defined(__arch_um__)
374 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/r/tmp/lustre-log"
375 #elif defined(__WINNT__)
376 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "\\SystemRoot\\temp\\lustre-log"
377 #else
378 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
379 #endif
380
381 #endif  /* __LIBCFS_DEBUG_H__ */