Whamcloud - gitweb
New release 2.15.64
[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(format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, format, ## __VA_ARGS__)
188 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
189
190 void libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
191                       const char *format1, ...)
192         __printf(2, 3);
193
194 /* other external symbols that tracefile provides: */
195 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
196                              const char *knl_buffer, char *append);
197
198 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
199
200 #if defined(CDEBUG_ENTRY_EXIT)
201
202 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
203 {
204         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
205                          rc, rc, rc);
206         return rc;
207 }
208
209 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
210                                    const char *label, long rc)
211 {
212         libcfs_debug_msg(msgdata,
213                          "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
214                          label, rc, rc, rc);
215 }
216
217 # define GOTO(label, rc)                                                      \
218 do {                                                                          \
219         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
220                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);        \
221                 libcfs_log_goto(&_goto_data, #label, (long)(rc));             \
222         } else {                                                              \
223                 (void)(rc);                                                   \
224         }                                                                     \
225                                                                               \
226         goto label;                                                           \
227 } while (0)
228
229 # if BITS_PER_LONG > 32
230 #  define RETURN(rc)                                                          \
231 do {                                                                          \
232         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
233                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
234                 return (typeof(rc))libcfs_log_return(&msgdata,                \
235                                                      (long)(rc));             \
236         }                                                                     \
237                                                                               \
238         return rc;                                                            \
239 } while (0)
240 # else /* BITS_PER_LONG == 32 */
241 /* We need an on-stack variable, because we cannot case a 32-bit pointer
242  * directly to (long long) without generating a complier warning/error, yet
243  * casting directly to (long) will truncate 64-bit return values. The log
244  * values will print as 32-bit values, but they always have been. LU-1436
245  */
246 #  define RETURN(rc)                                                          \
247 do {                                                                          \
248         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
249                 typeof(rc) __rc = (rc);                                       \
250                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
251                 libcfs_log_return(&msgdata, (long)__rc);                      \
252                 return __rc;                                                  \
253         }                                                                     \
254                                                                               \
255         return rc;                                                            \
256 } while (0)
257
258 # endif /* BITS_PER_LONG > 32 */
259
260 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
261 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
262
263 #else /* !CDEBUG_ENTRY_EXIT */
264
265 # define GOTO(label, rc)                                                \
266         do {                                                            \
267                 ((void)(rc));                                           \
268                 goto label;                                             \
269         } while (0)
270
271 # define RETURN(rc) return (rc)
272 # define ENTRY  do { } while (0)
273 # define EXIT   do { } while (0)
274
275 #endif /* CDEBUG_ENTRY_EXIT */
276
277 #define RETURN_EXIT                                                     \
278 do {                                                                    \
279         EXIT;                                                           \
280         return;                                                         \
281 } while (0)
282
283 static inline void cfs_tty_write_msg(const char *msg)
284 {
285         struct tty_struct *tty;
286
287         tty = get_current_tty();
288         if (!tty)
289                 return;
290         mutex_lock(&tty->atomic_write_lock);
291         tty_lock(tty);
292         if (tty->ops->write && tty->count > 0)
293                 tty->ops->write(tty, msg, strlen(msg));
294         tty_unlock(tty);
295         mutex_unlock(&tty->atomic_write_lock);
296         wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
297         tty_kref_put(tty);
298 }
299
300 #endif  /* __LIBCFS_DEBUG_H__ */