Whamcloud - gitweb
LU-13581 build: xarray and lockdep_is_held const clash
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/libcfs_debug.h
33  *
34  * Debug messages and assertions
35  *
36  */
37
38 #ifndef __LIBCFS_DEBUG_H__
39 #define __LIBCFS_DEBUG_H__
40
41 #include <stdarg.h>
42 #include <linux/limits.h>
43 #include <uapi/linux/lnet/libcfs_debug.h>
44
45 /*
46  *  Debugging
47  */
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];
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
63 /* Has there been an LBUG? */
64 extern unsigned int libcfs_catastrophe;
65 extern unsigned int libcfs_panic_on_lbug;
66
67 #ifndef DEBUG_SUBSYSTEM
68 # define DEBUG_SUBSYSTEM S_UNDEFINED
69 #endif
70
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;
77         int             cdls_count;
78 };
79
80 struct libcfs_debug_msg_data {
81         const char                      *msg_file;
82         const char                      *msg_fn;
83         int                              msg_subsys;
84         int                              msg_line;
85         int                              msg_mask;
86         struct cfs_debug_limit_state    *msg_cdls;
87 };
88
89 #define LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata, mask, cdls)\
90 do {                                                                    \
91         (msgdata)->msg_subsys = DEBUG_SUBSYSTEM;                        \
92         (msgdata)->msg_file   = (file);                                 \
93         (msgdata)->msg_fn     = (func);                                 \
94         (msgdata)->msg_line   = (line);                                 \
95         (msgdata)->msg_mask   = (mask);                                 \
96         (msgdata)->msg_cdls   = (cdls);                                 \
97 } while (0)
98
99 #define LIBCFS_DEBUG_MSG_DATA_DECL_LOC(file, func, line, msgdata, mask, cdls)\
100         static struct libcfs_debug_msg_data msgdata = {                 \
101                 .msg_subsys = DEBUG_SUBSYSTEM,                          \
102                 .msg_file   = (file),                                   \
103                 .msg_fn     = (func),                                   \
104                 .msg_line   = (line),                                   \
105                 .msg_cdls   = (cdls) };                                 \
106         msgdata.msg_mask   = (mask)
107
108 #define LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, cdls)                 \
109         LIBCFS_DEBUG_MSG_DATA_DECL_LOC(__FILE__, __func__, __LINE__,    \
110                                        msgdata, mask, cdls)
111
112 #ifdef CDEBUG_ENABLED
113
114 #if !defined(__x86_64__)
115 # ifdef __ia64__
116 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
117                           ((unsigned long)__builtin_dwarf_cfa() &       \
118                            (THREAD_SIZE - 1)))
119 # else
120 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
121                           ((unsigned long)__builtin_frame_address(0) &  \
122                            (THREAD_SIZE - 1)))
123 # endif /* __ia64__ */
124
125 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
126 do {                                                                    \
127         if (unlikely(CDEBUG_STACK() > libcfs_stack)) {                  \
128                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata,   \
129                                            D_WARNING, NULL);            \
130                 libcfs_stack = CDEBUG_STACK();                          \
131                 libcfs_debug_msg(msgdata, "maximum lustre stack %u\n",  \
132                                  libcfs_stack);                         \
133                 (msgdata)->msg_mask = mask;                             \
134                 (msgdata)->msg_cdls = cdls;                             \
135                 dump_stack();                                           \
136                 /*panic("LBUG");*/                                      \
137         }                                                               \
138 } while (0)
139 #else /* __x86_64__ */
140 #define CDEBUG_STACK() (0L)
141 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
142         do {} while (0)
143 #endif /* __x86_64__ */
144
145 #define CFS_CHECK_STACK(msgdata, mask, cdls)                            \
146         __CHECK_STACK_WITH_LOC(__FILE__, __func__, __LINE__,            \
147                                msgdata, mask, cdls)
148 /**
149  * Filters out logging messages based on mask and subsystem.
150  */
151 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
152 {
153         return mask & D_CANTMASK ||
154                ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
155 }
156
157 #  define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...)  \
158 do {                                                                    \
159         static struct libcfs_debug_msg_data msgdata;                    \
160                                                                         \
161         __CHECK_STACK_WITH_LOC(file, func, line, &msgdata, mask, cdls); \
162                                                                         \
163         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
164                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line,            \
165                                            &msgdata, mask, cdls);       \
166                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
167         }                                                               \
168 } while (0)
169
170 #  define CDEBUG(mask, format, ...)                                     \
171         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
172                           mask, NULL, format, ## __VA_ARGS__)
173
174 #  define CDEBUG_LIMIT(mask, format, ...)                               \
175 do {                                                                    \
176         static struct cfs_debug_limit_state cdls;                       \
177                                                                         \
178         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
179                           mask, &cdls, format, ## __VA_ARGS__);         \
180 } while (0)
181
182 # else /* !CDEBUG_ENABLED */
183 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
184 {
185         return 0;
186 }
187 #  define CDEBUG(mask, format, ...) (void)(0)
188 #  define CDEBUG_LIMIT(mask, format, ...) (void)(0)
189 #  warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
190 # endif /* CDEBUG_ENABLED */
191
192 /*
193  * Lustre Error Checksum: calculates checksum
194  * of Hex number by XORing each bit.
195  */
196 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
197                            ((hexnum) >> 8 & 0xf))
198
199 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
200 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
201 #define CNETERR(format, a...)       CDEBUG_LIMIT(D_NETERROR, format, ## a)
202 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
203
204 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
205 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
206 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
207 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
208                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
209 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
210
211 #define LCONSOLE_EMERG(format, ...) \
212         CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
213
214 #if defined(CDEBUG_ENTRY_EXIT)
215
216 void libcfs_log_goto(struct libcfs_debug_msg_data *goto_data,
217                      const char *label, long rc);
218
219 # define GOTO(label, rc)                                                      \
220 do {                                                                          \
221         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
222                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);        \
223                 libcfs_log_goto(&_goto_data, #label, (long)(rc));             \
224         } else {                                                              \
225                 (void)(rc);                                                   \
226         }                                                                     \
227                                                                               \
228         goto label;                                                           \
229 } while (0)
230
231
232 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
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 extern int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
288                             const char *format1, ...)
289         __attribute__ ((format (printf, 2, 3)));
290
291 /* other external symbols that tracefile provides: */
292 extern int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
293                                    const char __user *usr_buffer,
294                                    int usr_buffer_nob);
295 extern int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
296                                     const char *knl_buffer, char *append);
297
298 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
299
300 #endif  /* __LIBCFS_DEBUG_H__ */