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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * libcfs/include/libcfs/libcfs_debug.h
33 * Debug messages and assertions
37 #ifndef __LIBCFS_DEBUG_H__
38 #define __LIBCFS_DEBUG_H__
40 #include <linux/limits.h>
41 #include <uapi/linux/lnet/libcfs_debug.h>
46 extern unsigned int libcfs_subsystem_debug;
47 extern unsigned int libcfs_stack;
48 extern unsigned int libcfs_debug;
49 extern unsigned int libcfs_printk;
50 extern unsigned int libcfs_watchdog_ratelimit;
51 extern unsigned int libcfs_console_ratelimit;
52 extern unsigned int libcfs_console_max_delay;
53 extern unsigned int libcfs_console_min_delay;
54 extern unsigned int libcfs_console_backoff;
55 extern unsigned int libcfs_debug_binary;
56 extern char *libcfs_debug_file_path;
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 void libcfs_debug_dumpstack(struct task_struct *tsk);
64 /* Has there been an LBUG? */
65 extern unsigned int libcfs_catastrophe;
66 extern unsigned int libcfs_panic_on_lbug;
68 #ifndef DEBUG_SUBSYSTEM
69 # define DEBUG_SUBSYSTEM S_UNDEFINED
72 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600)) /* jiffies */
73 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
74 #define CDEBUG_DEFAULT_BACKOFF 2
75 struct cfs_debug_limit_state {
76 unsigned long cdls_next;
77 unsigned int cdls_delay;
81 struct libcfs_debug_msg_data {
87 struct cfs_debug_limit_state *msg_cdls;
90 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
92 (msgdata)->msg_subsys = DEBUG_SUBSYSTEM; \
93 (msgdata)->msg_file = (file); \
94 (msgdata)->msg_fn = (func); \
95 (msgdata)->msg_line = (line); \
96 (msgdata)->msg_mask = (mask); \
97 (msgdata)->msg_cdls = (cdls); \
100 #define LIBCFS_DEBUG_MSG_DATA_DECL_LOC(file, func, line, msgdata, mask, cdls)\
101 static struct libcfs_debug_msg_data msgdata = { \
102 .msg_subsys = DEBUG_SUBSYSTEM, \
103 .msg_file = (file), \
105 .msg_line = (line), \
106 .msg_cdls = (cdls) }; \
107 msgdata.msg_mask = (mask)
109 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls) \
110 LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__, \
113 #ifdef CDEBUG_ENABLED
115 #if !defined(__x86_64__)
117 # define CDEBUG_STACK() (THREAD_SIZE - \
118 ((unsigned long)__builtin_dwarf_cfa() & \
121 # define CDEBUG_STACK() (THREAD_SIZE - \
122 ((unsigned long)__builtin_frame_address(0) & \
124 # endif /* __ia64__ */
126 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls) \
128 if (unlikely(CDEBUG_STACK() > libcfs_stack)) { \
129 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, \
131 libcfs_stack = CDEBUG_STACK(); \
132 libcfs_debug_msg(msgdata, "maximum lustre stack %u\n", \
134 (msgdata)->msg_mask = mask; \
135 (msgdata)->msg_cdls = cdls; \
140 #else /* __x86_64__ */
141 #define CDEBUG_STACK() (0L)
142 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls) \
144 #endif /* __x86_64__ */
146 #define CFS_CHECK_STACK(msgdata, mask, cdls) \
147 __CHECK_STACK_WITH_LOC(__FILE__, __func__, __LINE__, \
150 * Filters out logging messages based on mask and subsystem.
152 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
154 return mask & D_CANTMASK ||
155 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
158 # define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...) \
160 static struct libcfs_debug_msg_data msgdata; \
162 __CHECK_STACK_WITH_LOC(file, func, line, &msgdata, mask, cdls); \
164 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
165 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, \
166 &msgdata, mask, cdls); \
167 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__); \
171 # define CDEBUG(mask, format, ...) \
172 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
173 mask, NULL, format, ## __VA_ARGS__)
175 # define CDEBUG_LIMIT(mask, format, ...) \
177 static struct cfs_debug_limit_state cdls; \
179 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
180 mask, &cdls, format, ## __VA_ARGS__); \
183 # else /* !CDEBUG_ENABLED */
184 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
188 # define CDEBUG(mask, format, ...) (void)(0)
189 # define CDEBUG_LIMIT(mask, format, ...) (void)(0)
190 # warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
191 # endif /* CDEBUG_ENABLED */
194 * Lustre Error Checksum: calculates checksum
195 * of Hex number by XORing each bit.
197 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
198 ((hexnum) >> 8 & 0xf))
200 #define CWARN(format, ...) CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
201 #define CERROR(format, ...) CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
202 #define CNETERR(format, a...) CDEBUG_LIMIT(D_NETERROR, format, ## a)
203 #define CEMERG(format, ...) CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
205 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
206 #define LCONSOLE_INFO(format, ...) CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
207 #define LCONSOLE_WARN(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
208 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
209 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
210 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
212 #define LCONSOLE_EMERG(format, ...) \
213 CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
215 #if defined(CDEBUG_ENTRY_EXIT)
217 void libcfs_log_goto(struct libcfs_debug_msg_data *goto_data,
218 const char *label, long rc);
220 # define GOTO(label, rc) \
222 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
223 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL); \
224 libcfs_log_goto(&_goto_data, #label, (long)(rc)); \
233 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
234 # if BITS_PER_LONG > 32
235 # define RETURN(rc) \
237 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
238 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
239 return (typeof(rc))libcfs_log_return(&msgdata, \
245 # else /* BITS_PER_LONG == 32 */
246 /* We need an on-stack variable, because we cannot case a 32-bit pointer
247 * directly to (long long) without generating a complier warning/error, yet
248 * casting directly to (long) will truncate 64-bit return values. The log
249 * values will print as 32-bit values, but they always have been. LU-1436
251 # define RETURN(rc) \
253 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
254 typeof(rc) __rc = (rc); \
255 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
256 libcfs_log_return(&msgdata, (long)__rc); \
263 # endif /* BITS_PER_LONG > 32 */
265 # define ENTRY CDEBUG(D_TRACE, "Process entered\n")
266 # define EXIT CDEBUG(D_TRACE, "Process leaving\n")
268 #else /* !CDEBUG_ENTRY_EXIT */
270 # define GOTO(label, rc) \
276 # define RETURN(rc) return (rc)
277 # define ENTRY do { } while (0)
278 # define EXIT do { } while (0)
280 #endif /* CDEBUG_ENTRY_EXIT */
282 #define RETURN_EXIT \
288 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
289 const char *format1, ...)
292 /* other external symbols that tracefile provides: */
293 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
294 const char *knl_buffer, char *append);
296 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
298 void cfs_debug_init(void);
300 #endif /* __LIBCFS_DEBUG_H__ */