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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/include/libcfs/libcfs_debug.h
34 * Debug messages and assertions
38 #ifndef __LIBCFS_DEBUG_H__
39 #define __LIBCFS_DEBUG_H__
42 #include <linux/limits.h>
43 #include <uapi/linux/lnet/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;
67 #ifndef DEBUG_SUBSYSTEM
68 # define DEBUG_SUBSYSTEM S_UNDEFINED
71 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600)) /* jiffies */
72 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
73 #define CDEBUG_DEFAULT_BACKOFF 2
74 struct cfs_debug_limit_state {
75 unsigned long cdls_next;
76 unsigned int cdls_delay;
80 struct libcfs_debug_msg_data {
86 struct cfs_debug_limit_state *msg_cdls;
89 #define LIBCFS_DEBUG_MSG_DATA_INIT(data, mask, cdls) \
91 (data)->msg_subsys = DEBUG_SUBSYSTEM; \
92 (data)->msg_file = __FILE__; \
93 (data)->msg_fn = __FUNCTION__; \
94 (data)->msg_line = __LINE__; \
95 (data)->msg_cdls = (cdls); \
96 (data)->msg_mask = (mask); \
99 #define LIBCFS_DEBUG_MSG_DATA_DECL(dataname, mask, cdls) \
100 static struct libcfs_debug_msg_data dataname = { \
101 .msg_subsys = DEBUG_SUBSYSTEM, \
102 .msg_file = __FILE__, \
103 .msg_fn = __FUNCTION__, \
104 .msg_line = __LINE__, \
105 .msg_cdls = (cdls) }; \
106 dataname.msg_mask = (mask);
108 #ifdef CDEBUG_ENABLED
110 #if !defined(__x86_64__)
112 # define CDEBUG_STACK() (THREAD_SIZE - \
113 ((unsigned long)__builtin_dwarf_cfa() & \
116 # define CDEBUG_STACK() (THREAD_SIZE - \
117 ((unsigned long)__builtin_frame_address(0) & \
119 # endif /* __ia64__ */
121 #define __CHECK_STACK(msgdata, mask, cdls) \
123 if (unlikely(CDEBUG_STACK() > libcfs_stack)) { \
124 LIBCFS_DEBUG_MSG_DATA_INIT(msgdata, D_WARNING, NULL); \
125 libcfs_stack = CDEBUG_STACK(); \
126 libcfs_debug_msg(msgdata, \
127 "maximum lustre stack %lu\n", \
129 (msgdata)->msg_mask = mask; \
130 (msgdata)->msg_cdls = cdls; \
135 #define CFS_CHECK_STACK(msgdata, mask, cdls) __CHECK_STACK(msgdata, mask, cdls)
136 #else /* __x86_64__ */
137 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while (0)
138 #define CDEBUG_STACK() (0L)
139 #endif /* __x86_64__ */
142 * Filters out logging messages based on mask and subsystem.
144 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
146 return mask & D_CANTMASK ||
147 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
150 # define __CDEBUG(cdls, mask, format, ...) \
152 static struct libcfs_debug_msg_data msgdata; \
154 CFS_CHECK_STACK(&msgdata, mask, cdls); \
156 if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) { \
157 LIBCFS_DEBUG_MSG_DATA_INIT(&msgdata, mask, cdls); \
158 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__); \
162 # define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
164 # define CDEBUG_LIMIT(mask, format, ...) \
166 static struct cfs_debug_limit_state cdls; \
168 __CDEBUG(&cdls, mask, format, ## __VA_ARGS__); \
171 # else /* !CDEBUG_ENABLED */
172 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
176 # define CDEBUG(mask, format, ...) (void)(0)
177 # define CDEBUG_LIMIT(mask, format, ...) (void)(0)
178 # warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
179 # endif /* CDEBUG_ENABLED */
182 * Lustre Error Checksum: calculates checksum
183 * of Hex number by XORing each bit.
185 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
186 ((hexnum) >> 8 & 0xf))
188 #define CWARN(format, ...) CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
189 #define CERROR(format, ...) CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
190 #define CNETERR(format, a...) CDEBUG_LIMIT(D_NETERROR, format, ## a)
191 #define CEMERG(format, ...) CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
193 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
194 #define LCONSOLE_INFO(format, ...) CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
195 #define LCONSOLE_WARN(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
196 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
197 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
198 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
200 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
202 #if defined(CDEBUG_ENTRY_EXIT)
204 void libcfs_log_goto(struct libcfs_debug_msg_data *goto_data,
205 const char *label, long rc);
207 # define GOTO(label, rc) \
209 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
210 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL); \
211 libcfs_log_goto(&_goto_data, #label, (long)(rc)); \
220 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
221 # if BITS_PER_LONG > 32
222 # define RETURN(rc) \
224 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
225 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
226 return (typeof(rc))libcfs_log_return(&msgdata, \
232 # else /* BITS_PER_LONG == 32 */
233 /* We need an on-stack variable, because we cannot case a 32-bit pointer
234 * directly to (long long) without generating a complier warning/error, yet
235 * casting directly to (long) will truncate 64-bit return values. The log
236 * values will print as 32-bit values, but they always have been. LU-1436
238 # define RETURN(rc) \
240 if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) { \
241 typeof(rc) __rc = (rc); \
242 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL); \
243 libcfs_log_return(&msgdata, (long)__rc); \
250 # endif /* BITS_PER_LONG > 32 */
252 # define ENTRY CDEBUG(D_TRACE, "Process entered\n")
253 # define EXIT CDEBUG(D_TRACE, "Process leaving\n")
255 #else /* !CDEBUG_ENTRY_EXIT */
257 # define GOTO(label, rc) \
263 # define RETURN(rc) return (rc)
264 # define ENTRY do { } while (0)
265 # define EXIT do { } while (0)
267 #endif /* CDEBUG_ENTRY_EXIT */
269 #define RETURN_EXIT \
275 extern int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
276 const char *format1, ...)
277 __attribute__ ((format (printf, 2, 3)));
279 extern int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
281 va_list args, const char *format2, ...)
282 __attribute__ ((format (printf, 4, 5)));
284 /* other external symbols that tracefile provides: */
285 extern int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
286 const char __user *usr_buffer,
288 extern int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
289 const char *knl_buffer, char *append);
291 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
293 #endif /* __LIBCFS_DEBUG_H__ */