4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/libcfs_debug.h
38 * Debug messages and assertions
42 #ifndef __LIBCFS_DEBUG_H__
43 #define __LIBCFS_DEBUG_H__
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 unsigned int libcfs_console_max_delay;
55 extern unsigned int 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[PATH_MAX];
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);
63 /* Has there been an LBUG? */
64 extern unsigned int libcfs_catastrophe;
65 extern unsigned int libcfs_panic_on_lbug;
68 * Format for debug message headers
70 struct ptldebug_header {
83 } __attribute__((packed));
86 #define PH_FLAG_FIRST_RECORD 1
88 /* Debugging subsystems (32 bits, non-overlapping) */
89 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
90 #define S_UNDEFINED 0x00000001
91 #define S_MDC 0x00000002
92 #define S_MDS 0x00000004
93 #define S_OSC 0x00000008
94 #define S_OST 0x00000010
95 #define S_CLASS 0x00000020
96 #define S_LOG 0x00000040
97 #define S_LLITE 0x00000080
98 #define S_RPC 0x00000100
99 #define S_MGMT 0x00000200
100 #define S_LNET 0x00000400
101 #define S_LND 0x00000800 /* ALL LNDs */
102 #define S_PINGER 0x00001000
103 #define S_FILTER 0x00002000
105 #define S_ECHO 0x00008000
106 #define S_LDLM 0x00010000
107 #define S_LOV 0x00020000
108 #define S_LQUOTA 0x00040000
109 #define S_OSD 0x00080000
113 #define S_LMV 0x00800000 /* b_new_cmd */
115 #define S_SEC 0x02000000 /* upcall cache */
116 #define S_GSS 0x04000000 /* b_new_cmd */
118 #define S_MGC 0x10000000
119 #define S_MGS 0x20000000
120 #define S_FID 0x40000000 /* b_new_cmd */
121 #define S_FLD 0x80000000 /* b_new_cmd */
122 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
124 /* Debugging masks (32 bits, non-overlapping) */
125 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
126 #define D_TRACE 0x00000001 /* ENTRY/EXIT markers */
127 #define D_INODE 0x00000002
128 #define D_SUPER 0x00000004
129 #define D_EXT2 0x00000008 /* anything from ext2_debug */
130 #define D_MALLOC 0x00000010 /* print malloc, free information */
131 #define D_CACHE 0x00000020 /* cache-related items */
132 #define D_INFO 0x00000040 /* general information */
133 #define D_IOCTL 0x00000080 /* ioctl related information */
134 #define D_NETERROR 0x00000100 /* network errors */
135 #define D_NET 0x00000200 /* network communications */
136 #define D_WARNING 0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
137 #define D_BUFFS 0x00000800
138 #define D_OTHER 0x00001000
139 #define D_DENTRY 0x00002000
140 #define D_NETTRACE 0x00004000
141 #define D_PAGE 0x00008000 /* bulk page handling */
142 #define D_DLMTRACE 0x00010000
143 #define D_ERROR 0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
144 #define D_EMERG 0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
145 #define D_HA 0x00080000 /* recovery and failover */
146 #define D_RPCTRACE 0x00100000 /* for distributed debugging */
147 #define D_VFSTRACE 0x00200000
148 #define D_READA 0x00400000 /* read-ahead */
149 #define D_MMAP 0x00800000
150 #define D_CONFIG 0x01000000
151 #define D_CONSOLE 0x02000000
152 #define D_QUOTA 0x04000000
153 #define D_SEC 0x08000000
154 #define D_LFSCK 0x10000000 /* For both OI scrub and LFSCK */
155 /* keep these in sync with lnet/{utils,libcfs}/debug.c */
157 #define D_HSM D_TRACE
159 #define D_CANTMASK (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
161 #ifndef DEBUG_SUBSYSTEM
162 # define DEBUG_SUBSYSTEM S_UNDEFINED
165 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600)) /* jiffies */
166 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
167 #define CDEBUG_DEFAULT_BACKOFF 2
169 cfs_time_t cdls_next;
170 unsigned int cdls_delay;
172 } cfs_debug_limit_state_t;
174 struct libcfs_debug_msg_data {
175 const char *msg_file;
180 cfs_debug_limit_state_t *msg_cdls;
183 #define LIBCFS_DEBUG_MSG_DATA_INIT(data, mask, cdls) \
185 (data)->msg_subsys = DEBUG_SUBSYSTEM; \
186 (data)->msg_file = __FILE__; \
187 (data)->msg_fn = __FUNCTION__; \
188 (data)->msg_line = __LINE__; \
189 (data)->msg_cdls = (cdls); \
190 (data)->msg_mask = (mask); \
193 #define LIBCFS_DEBUG_MSG_DATA_DECL(dataname, mask, cdls) \
194 static struct libcfs_debug_msg_data dataname = { \
195 .msg_subsys = DEBUG_SUBSYSTEM, \
196 .msg_file = __FILE__, \
197 .msg_fn = __FUNCTION__, \
198 .msg_line = __LINE__, \
199 .msg_cdls = (cdls) }; \
200 dataname.msg_mask = (mask);
202 #if defined(__KERNEL__) || (defined(__arch_lib__) && !defined(LUSTRE_UTILS))
204 #ifdef CDEBUG_ENABLED
207 * Filters out logging messages based on mask and subsystem.
209 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
211 return mask & D_CANTMASK ||
212 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
215 #define __CDEBUG(cdls, mask, format, ...) \
217 static struct libcfs_debug_msg_data msgdata; \
219 CFS_CHECK_STACK(&msgdata, mask, cdls); \
221 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
222 LIBCFS_DEBUG_MSG_DATA_INIT(&msgdata, mask, cdls); \
223 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__); \
227 #define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
229 #define CDEBUG_LIMIT(mask, format, ...) \
231 static cfs_debug_limit_state_t cdls; \
233 __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);\
236 #else /* !CDEBUG_ENABLED */
237 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
241 #define CDEBUG(mask, format, ...) (void)(0)
242 #define CDEBUG_LIMIT(mask, format, ...) (void)(0)
243 #warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
246 #else /* !__KERNEL__ && (!__arch_lib__ || LUSTRE_UTILS) */
247 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
251 #define CDEBUG(mask, format, ...) \
253 if (((mask) & D_CANTMASK) != 0) \
254 fprintf(stderr, "(%s:%d:%s()) " format, \
255 __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__);\
258 #define CDEBUG_LIMIT CDEBUG
260 #endif /* !__KERNEL__ ... */
263 #define CWARN(format, ...) CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
264 #define CERROR(format, ...) CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
265 #define CNETERR(format, a...) CDEBUG_LIMIT(D_NETERROR, format, ## a)
266 #define CEMERG(format, ...) CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
268 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
269 #define LCONSOLE_INFO(format, ...) CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
270 #define LCONSOLE_WARN(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
271 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
272 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
273 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
275 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
277 #ifdef CDEBUG_ENABLED
279 void libcfs_log_goto(struct libcfs_debug_msg_data *, const char *, long_ptr_t);
280 #define GOTO(label, rc) \
282 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
283 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
284 libcfs_log_goto(&msgdata, #label, (long_ptr_t)(rc)); \
291 #define GOTO(label, rc) do { ((void)(rc)); goto label; } while (0)
294 #ifdef CDEBUG_ENTRY_EXIT
297 * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
298 * there will be a warning in osx.
300 #if defined(__GNUC__)
302 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
303 #if BITS_PER_LONG > 32
307 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
308 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
309 return (typeof(rc))libcfs_log_return(&msgdata, \
315 #else /* BITS_PER_LONG == 32 */
316 /* We need an on-stack variable, because we cannot case a 32-bit pointer
317 * directly to (long long) without generating a complier warning/error, yet
318 * casting directly to (long) will truncate 64-bit return values. The log
319 * values will print as 32-bit values, but they always have been. LU-1436
324 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
325 typeof(rc) __rc = (rc); \
326 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
327 libcfs_log_return(&msgdata, (long_ptr_t)__rc); \
333 #endif /* BITS_PER_LONG > 32 */
335 #elif defined(_MSC_VER)
338 CDEBUG(D_TRACE, "Process leaving.\n"); \
343 # error "Unkown compiler"
344 #endif /* __GNUC__ */
349 CDEBUG(D_TRACE, "Process entered\n"); \
354 CDEBUG(D_TRACE, "Process leaving\n"); \
357 #else /* !CDEBUG_ENTRY_EXIT */
359 #define RETURN(rc) return (rc)
360 #define ENTRY do { } while (0)
361 #define EXIT do { } while (0)
363 #endif /* !CDEBUG_ENTRY_EXIT */
365 #define RETURN_EXIT \
371 extern int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
372 const char *format1, ...)
373 __attribute__ ((format (printf, 2, 3)));
375 extern int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
377 va_list args, const char *format2, ...)
378 __attribute__ ((format (printf, 4, 5)));
380 /* other external symbols that tracefile provides: */
381 extern int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
382 const char *usr_buffer, int usr_buffer_nob);
383 extern int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
384 const char *knl_buffer, char *append);
386 #if defined(__WINNT__)
387 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "\\SystemRoot\\temp\\lustre-log"
389 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
392 #endif /* __LIBCFS_DEBUG_H__ */