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;
67 extern bool libcfs_debug_raw_pointers;
69 int debug_format_buffer_alloc_buffers(void);
70 void debug_format_buffer_free_buffers(void);
71 bool get_debug_raw_pointers(void);
72 void set_debug_raw_pointers(bool value);
74 #ifndef DEBUG_SUBSYSTEM
75 # define DEBUG_SUBSYSTEM S_UNDEFINED
78 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600)) /* jiffies */
79 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
80 #define CDEBUG_DEFAULT_BACKOFF 2
81 struct cfs_debug_limit_state {
82 unsigned long cdls_next;
83 unsigned int cdls_delay;
87 struct libcfs_debug_msg_data {
93 struct cfs_debug_limit_state *msg_cdls;
96 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
98 (msgdata)->msg_subsys = DEBUG_SUBSYSTEM; \
99 (msgdata)->msg_file = (file); \
100 (msgdata)->msg_fn = (func); \
101 (msgdata)->msg_line = (line); \
102 (msgdata)->msg_mask = (mask); \
103 (msgdata)->msg_cdls = (cdls); \
106 #define LIBCFS_DEBUG_MSG_DATA_DECL_LOC(file, func, line, msgdata, mask, cdls)\
107 static struct libcfs_debug_msg_data msgdata = { \
108 .msg_subsys = DEBUG_SUBSYSTEM, \
109 .msg_file = (file), \
111 .msg_line = (line), \
112 .msg_cdls = (cdls) }; \
113 msgdata.msg_mask = (mask)
115 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls) \
116 LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__, \
119 #ifdef CDEBUG_ENABLED
122 * Filters out logging messages based on mask and subsystem.
124 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
126 return mask & D_CANTMASK ||
127 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
130 # define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...) \
132 static struct libcfs_debug_msg_data msgdata; \
134 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
135 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, \
136 &msgdata, mask, cdls); \
137 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__); \
141 # define CDEBUG(mask, format, ...) \
142 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
143 mask, NULL, format, ## __VA_ARGS__)
145 # define CDEBUG_LIMIT(mask, format, ...) \
147 static struct cfs_debug_limit_state cdls; \
149 __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__, \
150 mask, &cdls, format, ## __VA_ARGS__); \
153 # define CDEBUG_LIMIT_LOC(file, func, line, mask, format, ...) \
155 static struct cfs_debug_limit_state cdls; \
157 __CDEBUG_WITH_LOC(file, func, line, \
158 mask, &cdls, format, ## __VA_ARGS__); \
161 # else /* !CDEBUG_ENABLED */
162 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
166 # define CDEBUG(mask, format, ...) (void)(0)
167 # define CDEBUG_LIMIT(mask, format, ...) (void)(0)
168 # define CDEBUG_LIMIT_LOC(file, func, line, mask, format, ...) (void)(0)
169 # warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
170 # endif /* CDEBUG_ENABLED */
173 * Lustre Error Checksum: calculates checksum
174 * of Hex number by XORing each bit.
176 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
177 ((hexnum) >> 8 & 0xf))
179 #define CWARN(format, ...) CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
180 #define CERROR(format, ...) CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
181 #define CNETERR(format, a...) CDEBUG_LIMIT(D_NETERROR, format, ## a)
182 #define CEMERG(format, ...) CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
184 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
185 #define LCONSOLE_INFO(format, ...) CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
186 #define LCONSOLE_WARN(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
187 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
188 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
189 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
191 #define LCONSOLE_EMERG(format, ...) \
192 CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
194 void libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
195 const char *format1, ...)
198 /* other external symbols that tracefile provides: */
199 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
200 const char *knl_buffer, char *append);
202 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
204 #if defined(CDEBUG_ENTRY_EXIT)
206 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
208 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
213 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
214 const char *label, long rc)
216 libcfs_debug_msg(msgdata,
217 "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
221 # define GOTO(label, rc) \
223 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
224 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL); \
225 libcfs_log_goto(&_goto_data, #label, (long)(rc)); \
233 # if BITS_PER_LONG > 32
234 # define RETURN(rc) \
236 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
237 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
238 return (typeof(rc))libcfs_log_return(&msgdata, \
244 # else /* BITS_PER_LONG == 32 */
245 /* We need an on-stack variable, because we cannot case a 32-bit pointer
246 * directly to (long long) without generating a complier warning/error, yet
247 * casting directly to (long) will truncate 64-bit return values. The log
248 * values will print as 32-bit values, but they always have been. LU-1436
250 # define RETURN(rc) \
252 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
253 typeof(rc) __rc = (rc); \
254 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
255 libcfs_log_return(&msgdata, (long)__rc); \
262 # endif /* BITS_PER_LONG > 32 */
264 # define ENTRY CDEBUG(D_TRACE, "Process entered\n")
265 # define EXIT CDEBUG(D_TRACE, "Process leaving\n")
267 #else /* !CDEBUG_ENTRY_EXIT */
269 # define GOTO(label, rc) \
275 # define RETURN(rc) return (rc)
276 # define ENTRY do { } while (0)
277 # define EXIT do { } while (0)
279 #endif /* CDEBUG_ENTRY_EXIT */
281 #define RETURN_EXIT \
287 static inline void cfs_tty_write_msg(const char *msg)
289 struct tty_struct *tty;
291 tty = get_current_tty();
294 mutex_lock(&tty->atomic_write_lock);
296 if (tty->ops->write && tty->count > 0)
297 tty->ops->write(tty, msg, strlen(msg));
299 mutex_unlock(&tty->atomic_write_lock);
300 wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
304 #endif /* __LIBCFS_DEBUG_H__ */