Whamcloud - gitweb
LU-17242 debug: remove CFS_CHECK_STACK
[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
68 #ifndef DEBUG_SUBSYSTEM
69 # define DEBUG_SUBSYSTEM S_UNDEFINED
70 #endif
71
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;
78         int             cdls_count;
79 };
80
81 struct libcfs_debug_msg_data {
82         const char                      *msg_file;
83         const char                      *msg_fn;
84         int                              msg_subsys;
85         int                              msg_line;
86         int                              msg_mask;
87         struct cfs_debug_limit_state    *msg_cdls;
88 };
89
90 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
91 do {                                                                    \
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);                                 \
98 } while (0)
99
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),                                   \
104                 .msg_fn     = (func),                                   \
105                 .msg_line   = (line),                                   \
106                 .msg_cdls   = (cdls) };                                 \
107         msgdata.msg_mask   = (mask)
108
109 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls)                 \
110         LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__,    \
111                                        msgdata, mask, cdls)
112
113 #ifdef CDEBUG_ENABLED
114
115 /**
116  * Filters out logging messages based on mask and subsystem.
117  */
118 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
119 {
120         return mask & D_CANTMASK ||
121                ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
122 }
123
124 #  define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...)  \
125 do {                                                                    \
126         static struct libcfs_debug_msg_data msgdata;                    \
127                                                                         \
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__);     \
132         }                                                               \
133 } while (0)
134
135 #  define CDEBUG(mask, format, ...)                                     \
136         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
137                           mask, NULL, format, ## __VA_ARGS__)
138
139 #  define CDEBUG_LIMIT(mask, format, ...)                               \
140 do {                                                                    \
141         static struct cfs_debug_limit_state cdls;                       \
142                                                                         \
143         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
144                           mask, &cdls, format, ## __VA_ARGS__);         \
145 } while (0)
146
147 # else /* !CDEBUG_ENABLED */
148 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
149 {
150         return 0;
151 }
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 */
156
157 /*
158  * Lustre Error Checksum: calculates checksum
159  * of Hex number by XORing each bit.
160  */
161 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
162                            ((hexnum) >> 8 & 0xf))
163
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__)
168
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__)
175
176 #define LCONSOLE_EMERG(format, ...) \
177         CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
178
179 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
180                      const char *format1, ...)
181         __printf(2, 3);
182
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);
186
187 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
188
189 #if defined(CDEBUG_ENTRY_EXIT)
190
191 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
192 {
193         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
194                          rc, rc, rc);
195         return rc;
196 }
197
198 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
199                                    const char *label, long rc)
200 {
201         libcfs_debug_msg(msgdata,
202                          "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
203                          label, rc, rc, rc);
204 }
205
206 # define GOTO(label, rc)                                                      \
207 do {                                                                          \
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));             \
211         } else {                                                              \
212                 (void)(rc);                                                   \
213         }                                                                     \
214                                                                               \
215         goto label;                                                           \
216 } while (0)
217
218 # if BITS_PER_LONG > 32
219 #  define RETURN(rc)                                                          \
220 do {                                                                          \
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,                \
224                                                      (long)(rc));             \
225         }                                                                     \
226                                                                               \
227         return rc;                                                            \
228 } while (0)
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
234  */
235 #  define RETURN(rc)                                                          \
236 do {                                                                          \
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);                      \
241                 return __rc;                                                  \
242         }                                                                     \
243                                                                               \
244         return rc;                                                            \
245 } while (0)
246
247 # endif /* BITS_PER_LONG > 32 */
248
249 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
250 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
251
252 #else /* !CDEBUG_ENTRY_EXIT */
253
254 # define GOTO(label, rc)                                                \
255         do {                                                            \
256                 ((void)(rc));                                           \
257                 goto label;                                             \
258         } while (0)
259
260 # define RETURN(rc) return (rc)
261 # define ENTRY  do { } while (0)
262 # define EXIT   do { } while (0)
263
264 #endif /* CDEBUG_ENTRY_EXIT */
265
266 #define RETURN_EXIT                                                     \
267 do {                                                                    \
268         EXIT;                                                           \
269         return;                                                         \
270 } while (0)
271
272 static inline void cfs_tty_write_msg(const char *msg)
273 {
274         struct tty_struct *tty;
275
276         tty = get_current_tty();
277         if (!tty)
278                 return;
279         mutex_lock(&tty->atomic_write_lock);
280         tty_lock(tty);
281         if (tty->ops->write && tty->count > 0)
282                 tty->ops->write(tty, msg, strlen(msg));
283         tty_unlock(tty);
284         mutex_unlock(&tty->atomic_write_lock);
285         wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
286         tty_kref_put(tty);
287 }
288
289 #endif  /* __LIBCFS_DEBUG_H__ */