Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_debug.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * libcfs/include/libcfs/libcfs_debug.h
32  *
33  * Debug messages and assertions
34  *
35  */
36
37 #ifndef __LIBCFS_DEBUG_H__
38 #define __LIBCFS_DEBUG_H__
39
40 #include <linux/tty.h>
41 #include <linux/limits.h>
42 #include <uapi/linux/lnet/libcfs_debug.h>
43
44 /*
45  *  Debugging
46  */
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;
57
58 struct task_struct;
59
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);
63
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;
68
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);
73
74 #ifndef DEBUG_SUBSYSTEM
75 # define DEBUG_SUBSYSTEM S_UNDEFINED
76 #endif
77
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;
84         int             cdls_count;
85 };
86
87 struct libcfs_debug_msg_data {
88         const char                      *msg_file;
89         const char                      *msg_fn;
90         int                              msg_subsys;
91         int                              msg_line;
92         int                              msg_mask;
93         struct cfs_debug_limit_state    *msg_cdls;
94 };
95
96 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
97 do {                                                                    \
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);                                 \
104 } while (0)
105
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),                                   \
110                 .msg_fn     = (func),                                   \
111                 .msg_line   = (line),                                   \
112                 .msg_cdls   = (cdls) };                                 \
113         msgdata.msg_mask   = (mask)
114
115 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls)                 \
116         LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__,    \
117                                        msgdata, mask, cdls)
118
119 #ifdef CDEBUG_ENABLED
120
121 /**
122  * Filters out logging messages based on mask and subsystem.
123  */
124 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
125 {
126         return mask & D_CANTMASK ||
127                ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
128 }
129
130 #  define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...)  \
131 do {                                                                    \
132         static struct libcfs_debug_msg_data msgdata;                    \
133                                                                         \
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__);     \
138         }                                                               \
139 } while (0)
140
141 #  define CDEBUG(mask, format, ...)                                     \
142         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
143                           mask, NULL, format, ## __VA_ARGS__)
144
145 #  define CDEBUG_LIMIT(mask, format, ...)                               \
146 do {                                                                    \
147         static struct cfs_debug_limit_state cdls;                       \
148                                                                         \
149         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
150                           mask, &cdls, format, ## __VA_ARGS__);         \
151 } while (0)
152
153 #  define CDEBUG_LIMIT_LOC(file, func, line, mask, format, ...)         \
154 do {                                                                    \
155         static struct cfs_debug_limit_state cdls;                       \
156                                                                         \
157         __CDEBUG_WITH_LOC(file, func, line,                             \
158                           mask, &cdls, format, ## __VA_ARGS__);         \
159 } while (0)
160
161 # else /* !CDEBUG_ENABLED */
162 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
163 {
164         return 0;
165 }
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 */
171
172 /*
173  * Lustre Error Checksum: calculates checksum
174  * of Hex number by XORing each bit.
175  */
176 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
177                            ((hexnum) >> 8 & 0xf))
178
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__)
183
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__)
190
191 #define LCONSOLE_EMERG(format, ...) \
192         CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
193
194 void libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
195                       const char *format1, ...)
196         __printf(2, 3);
197
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);
201
202 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
203
204 #if defined(CDEBUG_ENTRY_EXIT)
205
206 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
207 {
208         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
209                          rc, rc, rc);
210         return rc;
211 }
212
213 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
214                                    const char *label, long rc)
215 {
216         libcfs_debug_msg(msgdata,
217                          "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
218                          label, rc, rc, rc);
219 }
220
221 # define GOTO(label, rc)                                                      \
222 do {                                                                          \
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));             \
226         } else {                                                              \
227                 (void)(rc);                                                   \
228         }                                                                     \
229                                                                               \
230         goto label;                                                           \
231 } while (0)
232
233 # if BITS_PER_LONG > 32
234 #  define RETURN(rc)                                                          \
235 do {                                                                          \
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,                \
239                                                      (long)(rc));             \
240         }                                                                     \
241                                                                               \
242         return rc;                                                            \
243 } while (0)
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
249  */
250 #  define RETURN(rc)                                                          \
251 do {                                                                          \
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);                      \
256                 return __rc;                                                  \
257         }                                                                     \
258                                                                               \
259         return rc;                                                            \
260 } while (0)
261
262 # endif /* BITS_PER_LONG > 32 */
263
264 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
265 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
266
267 #else /* !CDEBUG_ENTRY_EXIT */
268
269 # define GOTO(label, rc)                                                \
270         do {                                                            \
271                 ((void)(rc));                                           \
272                 goto label;                                             \
273         } while (0)
274
275 # define RETURN(rc) return (rc)
276 # define ENTRY  do { } while (0)
277 # define EXIT   do { } while (0)
278
279 #endif /* CDEBUG_ENTRY_EXIT */
280
281 #define RETURN_EXIT                                                     \
282 do {                                                                    \
283         EXIT;                                                           \
284         return;                                                         \
285 } while (0)
286
287 static inline void cfs_tty_write_msg(const char *msg)
288 {
289         struct tty_struct *tty;
290
291         tty = get_current_tty();
292         if (!tty)
293                 return;
294         mutex_lock(&tty->atomic_write_lock);
295         tty_lock(tty);
296         if (tty->ops->write && tty->count > 0)
297                 tty->ops->write(tty, msg, strlen(msg));
298         tty_unlock(tty);
299         mutex_unlock(&tty->atomic_write_lock);
300         wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
301         tty_kref_put(tty);
302 }
303
304 #endif  /* __LIBCFS_DEBUG_H__ */