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/tty.h>
41 #include <linux/limits.h>
42 #include <uapi/linux/lnet/libcfs_debug.h>
47 extern unsigned int libcfs_subsystem_debug;
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
116 * Filters out logging messages based on mask and subsystem.
118 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
120 return mask & D_CANTMASK ||
121 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
124 # define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...) \
126 static struct libcfs_debug_msg_data msgdata; \
128 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
129 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, \
130 &msgdata, mask, cdls); \
131 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__); \
135 # define CDEBUG(mask, format, ...) \
136 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
137 mask, NULL, format, ## __VA_ARGS__)
139 # define CDEBUG_LIMIT(mask, format, ...) \
141 static struct cfs_debug_limit_state cdls; \
143 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
144 mask, &cdls, format, ## __VA_ARGS__); \
147 # else /* !CDEBUG_ENABLED */
148 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
152 # define CDEBUG(mask, format, ...) (void)(0)
153 # define CDEBUG_LIMIT(mask, format, ...) (void)(0)
154 # warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
155 # endif /* CDEBUG_ENABLED */
158 * Lustre Error Checksum: calculates checksum
159 * of Hex number by XORing each bit.
161 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
162 ((hexnum) >> 8 & 0xf))
164 #define CWARN(format, ...) CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
165 #define CERROR(format, ...) CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
166 #define CNETERR(format, a...) CDEBUG_LIMIT(D_NETERROR, format, ## a)
167 #define CEMERG(format, ...) CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
169 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
170 #define LCONSOLE_INFO(format, ...) CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
171 #define LCONSOLE_WARN(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
172 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
173 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
174 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
176 #define LCONSOLE_EMERG(format, ...) \
177 CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
179 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
180 const char *format1, ...)
183 /* other external symbols that tracefile provides: */
184 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
185 const char *knl_buffer, char *append);
187 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
189 #if defined(CDEBUG_ENTRY_EXIT)
191 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
193 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
198 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
199 const char *label, long rc)
201 libcfs_debug_msg(msgdata,
202 "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
206 # define GOTO(label, rc) \
208 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
209 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL); \
210 libcfs_log_goto(&_goto_data, #label, (long)(rc)); \
218 # if BITS_PER_LONG > 32
219 # define RETURN(rc) \
221 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
222 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
223 return (typeof(rc))libcfs_log_return(&msgdata, \
229 # else /* BITS_PER_LONG == 32 */
230 /* We need an on-stack variable, because we cannot case a 32-bit pointer
231 * directly to (long long) without generating a complier warning/error, yet
232 * casting directly to (long) will truncate 64-bit return values. The log
233 * values will print as 32-bit values, but they always have been. LU-1436
235 # define RETURN(rc) \
237 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
238 typeof(rc) __rc = (rc); \
239 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
240 libcfs_log_return(&msgdata, (long)__rc); \
247 # endif /* BITS_PER_LONG > 32 */
249 # define ENTRY CDEBUG(D_TRACE, "Process entered\n")
250 # define EXIT CDEBUG(D_TRACE, "Process leaving\n")
252 #else /* !CDEBUG_ENTRY_EXIT */
254 # define GOTO(label, rc) \
260 # define RETURN(rc) return (rc)
261 # define ENTRY do { } while (0)
262 # define EXIT do { } while (0)
264 #endif /* CDEBUG_ENTRY_EXIT */
266 #define RETURN_EXIT \
272 static inline void cfs_tty_write_msg(const char *msg)
274 struct tty_struct *tty;
276 tty = get_current_tty();
279 mutex_lock(&tty->atomic_write_lock);
281 if (tty->ops->write && tty->count > 0)
282 tty->ops->write(tty, msg, strlen(msg));
284 mutex_unlock(&tty->atomic_write_lock);
285 wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
289 #endif /* __LIBCFS_DEBUG_H__ */