Whamcloud - gitweb
LU-16518 libcfs: fix clang build errors
[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_stack;
49 extern unsigned int libcfs_debug;
50 extern unsigned int libcfs_printk;
51 extern unsigned int libcfs_watchdog_ratelimit;
52 extern unsigned int libcfs_console_ratelimit;
53 extern unsigned int libcfs_console_max_delay;
54 extern unsigned int libcfs_console_min_delay;
55 extern unsigned int libcfs_console_backoff;
56 extern unsigned int libcfs_debug_binary;
57 extern char *libcfs_debug_file_path;
58
59 struct task_struct;
60
61 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
62 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
63 void libcfs_debug_dumpstack(struct task_struct *tsk);
64
65 /* Has there been an LBUG? */
66 extern unsigned int libcfs_catastrophe;
67 extern unsigned int libcfs_panic_on_lbug;
68
69 #ifndef DEBUG_SUBSYSTEM
70 # define DEBUG_SUBSYSTEM S_UNDEFINED
71 #endif
72
73 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
74 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
75 #define CDEBUG_DEFAULT_BACKOFF   2
76 struct cfs_debug_limit_state {
77         unsigned long   cdls_next;
78         unsigned int    cdls_delay;
79         int             cdls_count;
80 };
81
82 struct libcfs_debug_msg_data {
83         const char                      *msg_file;
84         const char                      *msg_fn;
85         int                              msg_subsys;
86         int                              msg_line;
87         int                              msg_mask;
88         struct cfs_debug_limit_state    *msg_cdls;
89 };
90
91 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
92 do {                                                                    \
93         (msgdata)->msg_subsys = DEBUG_SUBSYSTEM;                        \
94         (msgdata)->msg_file   = (file);                                 \
95         (msgdata)->msg_fn     = (func);                                 \
96         (msgdata)->msg_line   = (line);                                 \
97         (msgdata)->msg_mask   = (mask);                                 \
98         (msgdata)->msg_cdls   = (cdls);                                 \
99 } while (0)
100
101 #define LIBCFS_DEBUG_MSG_DATA_DECL_LOC(file, func, line, msgdata, mask, cdls)\
102         static struct libcfs_debug_msg_data msgdata = {                 \
103                 .msg_subsys = DEBUG_SUBSYSTEM,                          \
104                 .msg_file   = (file),                                   \
105                 .msg_fn     = (func),                                   \
106                 .msg_line   = (line),                                   \
107                 .msg_cdls   = (cdls) };                                 \
108         msgdata.msg_mask   = (mask)
109
110 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls)                 \
111         LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__,    \
112                                        msgdata, mask, cdls)
113
114 #ifdef CDEBUG_ENABLED
115
116 #if !defined(__x86_64__)
117 # ifdef __ia64__
118 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
119                           ((unsigned long)__builtin_dwarf_cfa() &       \
120                            (THREAD_SIZE - 1)))
121 # else
122 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
123                           ((unsigned long)__builtin_frame_address(0) &  \
124                            (THREAD_SIZE - 1)))
125 # endif /* __ia64__ */
126
127 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
128 do {                                                                    \
129         if (unlikely(CDEBUG_STACK() > libcfs_stack)) {                  \
130                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata,   \
131                                            D_WARNING, NULL);            \
132                 libcfs_stack = CDEBUG_STACK();                          \
133                 libcfs_debug_msg(msgdata, "maximum lustre stack %u\n",  \
134                                  libcfs_stack);                         \
135                 (msgdata)->msg_mask = mask;                             \
136                 (msgdata)->msg_cdls = cdls;                             \
137                 dump_stack();                                           \
138                 /*panic("LBUG");*/                                      \
139         }                                                               \
140 } while (0)
141 #else /* __x86_64__ */
142 #define CDEBUG_STACK() (0L)
143 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
144         do {} while (0)
145 #endif /* __x86_64__ */
146
147 #define CFS_CHECK_STACK(msgdata, mask, cdls)                            \
148         __CHECK_STACK_WITH_LOC(__FILE__, __func__, __LINE__,            \
149                                msgdata, mask, cdls)
150 /**
151  * Filters out logging messages based on mask and subsystem.
152  */
153 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
154 {
155         return mask & D_CANTMASK ||
156                ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
157 }
158
159 #  define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...)  \
160 do {                                                                    \
161         static struct libcfs_debug_msg_data msgdata;                    \
162                                                                         \
163         __CHECK_STACK_WITH_LOC(file, func, line, &msgdata, mask, cdls); \
164                                                                         \
165         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
166                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line,            \
167                                            &msgdata, mask, cdls);       \
168                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
169         }                                                               \
170 } while (0)
171
172 #  define CDEBUG(mask, format, ...)                                     \
173         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
174                           mask, NULL, format, ## __VA_ARGS__)
175
176 #  define CDEBUG_LIMIT(mask, format, ...)                               \
177 do {                                                                    \
178         static struct cfs_debug_limit_state cdls;                       \
179                                                                         \
180         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
181                           mask, &cdls, format, ## __VA_ARGS__);         \
182 } while (0)
183
184 # else /* !CDEBUG_ENABLED */
185 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
186 {
187         return 0;
188 }
189 #  define CDEBUG(mask, format, ...) (void)(0)
190 #  define CDEBUG_LIMIT(mask, format, ...) (void)(0)
191 #  warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
192 # endif /* CDEBUG_ENABLED */
193
194 /*
195  * Lustre Error Checksum: calculates checksum
196  * of Hex number by XORing each bit.
197  */
198 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
199                            ((hexnum) >> 8 & 0xf))
200
201 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
202 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
203 #define CNETERR(format, a...)       CDEBUG_LIMIT(D_NETERROR, format, ## a)
204 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
205
206 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
207 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
208 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
209 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
210                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
211 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
212
213 #define LCONSOLE_EMERG(format, ...) \
214         CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
215
216 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
217                      const char *format1, ...)
218         __printf(2, 3);
219
220 /* other external symbols that tracefile provides: */
221 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
222                              const char *knl_buffer, char *append);
223
224 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
225
226 #if defined(CDEBUG_ENTRY_EXIT)
227
228 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
229 {
230         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
231                          rc, rc, rc);
232         return rc;
233 }
234
235 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
236                                    const char *label, long rc)
237 {
238         libcfs_debug_msg(msgdata,
239                          "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
240                          label, rc, rc, rc);
241 }
242
243 # define GOTO(label, rc)                                                      \
244 do {                                                                          \
245         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
246                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);        \
247                 libcfs_log_goto(&_goto_data, #label, (long)(rc));             \
248         } else {                                                              \
249                 (void)(rc);                                                   \
250         }                                                                     \
251                                                                               \
252         goto label;                                                           \
253 } while (0)
254
255 # if BITS_PER_LONG > 32
256 #  define RETURN(rc)                                                          \
257 do {                                                                          \
258         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
259                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
260                 return (typeof(rc))libcfs_log_return(&msgdata,                \
261                                                      (long)(rc));             \
262         }                                                                     \
263                                                                               \
264         return rc;                                                            \
265 } while (0)
266 # else /* BITS_PER_LONG == 32 */
267 /* We need an on-stack variable, because we cannot case a 32-bit pointer
268  * directly to (long long) without generating a complier warning/error, yet
269  * casting directly to (long) will truncate 64-bit return values. The log
270  * values will print as 32-bit values, but they always have been. LU-1436
271  */
272 #  define RETURN(rc)                                                          \
273 do {                                                                          \
274         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
275                 typeof(rc) __rc = (rc);                                       \
276                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
277                 libcfs_log_return(&msgdata, (long)__rc);                      \
278                 return __rc;                                                  \
279         }                                                                     \
280                                                                               \
281         return rc;                                                            \
282 } while (0)
283
284 # endif /* BITS_PER_LONG > 32 */
285
286 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
287 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
288
289 #else /* !CDEBUG_ENTRY_EXIT */
290
291 # define GOTO(label, rc)                                                \
292         do {                                                            \
293                 ((void)(rc));                                           \
294                 goto label;                                             \
295         } while (0)
296
297 # define RETURN(rc) return (rc)
298 # define ENTRY  do { } while (0)
299 # define EXIT   do { } while (0)
300
301 #endif /* CDEBUG_ENTRY_EXIT */
302
303 #define RETURN_EXIT                                                     \
304 do {                                                                    \
305         EXIT;                                                           \
306         return;                                                         \
307 } while (0)
308
309 void cfs_debug_init(void);
310
311 static inline void cfs_tty_write_msg(const char *msg)
312 {
313         struct tty_struct *tty;
314
315         tty = get_current_tty();
316         if (!tty)
317                 return;
318         mutex_lock(&tty->atomic_write_lock);
319         tty_lock(tty);
320         if (tty->ops->write && tty->count > 0)
321                 tty->ops->write(tty, msg, strlen(msg));
322         tty_unlock(tty);
323         mutex_unlock(&tty->atomic_write_lock);
324         wake_up_interruptible_poll(&tty->write_wait, POLL_OUT);
325         tty_kref_put(tty);
326 }
327
328 #endif  /* __LIBCFS_DEBUG_H__ */