Whamcloud - gitweb
LU-6245 uapi: move libcfs/lnet UAPI header into own uapi directory
[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, 2014, 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 <uapi/linux/lnet/libcfs_debug.h>
42
43 /*
44  *  Debugging
45  */
46 extern unsigned int libcfs_subsystem_debug;
47 extern unsigned int libcfs_stack;
48 extern unsigned int libcfs_debug;
49 extern unsigned int libcfs_printk;
50 extern unsigned int libcfs_console_ratelimit;
51 extern unsigned int libcfs_watchdog_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_arr[PATH_MAX];
57
58 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
59 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
60
61 /* Has there been an LBUG? */
62 extern unsigned int libcfs_catastrophe;
63 extern unsigned int libcfs_panic_on_lbug;
64
65 #ifndef DEBUG_SUBSYSTEM
66 # define DEBUG_SUBSYSTEM S_UNDEFINED
67 #endif
68
69 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
70 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
71 #define CDEBUG_DEFAULT_BACKOFF   2
72 struct cfs_debug_limit_state {
73         unsigned long   cdls_next;
74         unsigned int    cdls_delay;
75         int             cdls_count;
76 };
77
78 struct libcfs_debug_msg_data {
79         const char                      *msg_file;
80         const char                      *msg_fn;
81         int                              msg_subsys;
82         int                              msg_line;
83         int                              msg_mask;
84         struct cfs_debug_limit_state    *msg_cdls;
85 };
86
87 #define LIBCFS_DEBUG_MSG_DATA_INIT(data, mask, cdls)        \
88 do {                                                        \
89         (data)->msg_subsys = DEBUG_SUBSYSTEM;               \
90         (data)->msg_file   = __FILE__;                      \
91         (data)->msg_fn     = __FUNCTION__;                  \
92         (data)->msg_line   = __LINE__;                      \
93         (data)->msg_cdls   = (cdls);                        \
94         (data)->msg_mask   = (mask);                        \
95 } while (0)
96
97 #define LIBCFS_DEBUG_MSG_DATA_DECL(dataname, mask, cdls)    \
98         static struct libcfs_debug_msg_data dataname = {    \
99                .msg_subsys = DEBUG_SUBSYSTEM,               \
100                .msg_file   = __FILE__,                      \
101                .msg_fn     = __FUNCTION__,                  \
102                .msg_line   = __LINE__,                      \
103                .msg_cdls   = (cdls)         };              \
104         dataname.msg_mask   = (mask);
105
106 #ifdef CDEBUG_ENABLED
107
108 /**
109  * Filters out logging messages based on mask and subsystem.
110  */
111 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
112 {
113         return mask & D_CANTMASK ||
114                 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
115 }
116
117 #  define __CDEBUG(cdls, mask, format, ...)                             \
118 do {                                                                    \
119         static struct libcfs_debug_msg_data msgdata;                    \
120                                                                         \
121         CFS_CHECK_STACK(&msgdata, mask, cdls);                          \
122                                                                         \
123         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
124                 LIBCFS_DEBUG_MSG_DATA_INIT(&msgdata, mask, cdls);       \
125                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
126         }                                                               \
127 } while (0)
128
129 #  define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
130
131 #  define CDEBUG_LIMIT(mask, format, ...)                               \
132 do {                                                                    \
133         static struct cfs_debug_limit_state cdls;                       \
134                                                                         \
135         __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);                  \
136 } while (0)
137
138 # else /* !CDEBUG_ENABLED */
139 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
140 {
141         return 0;
142 }
143 #  define CDEBUG(mask, format, ...) (void)(0)
144 #  define CDEBUG_LIMIT(mask, format, ...) (void)(0)
145 #  warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
146 # endif /* CDEBUG_ENABLED */
147
148 /*
149  * Lustre Error Checksum: calculates checksum
150  * of Hex number by XORing each bit.
151  */
152 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
153                            ((hexnum) >> 8 & 0xf))
154
155 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
156 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
157 #define CNETERR(format, a...)       CDEBUG_LIMIT(D_NETERROR, format, ## a)
158 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
159
160 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
161 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
162 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
163 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
164                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
165 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
166
167 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
168
169 #if defined(CDEBUG_ENTRY_EXIT)
170
171 void libcfs_log_goto(struct libcfs_debug_msg_data *goto_data,
172                      const char *label, long rc);
173
174 # define GOTO(label, rc)                                                \
175 do {                                                                    \
176         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
177                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);  \
178                 libcfs_log_goto(&_goto_data, #label, (long)(rc));       \
179         } else {                                                        \
180                 (void)(rc);                                             \
181         }                                                               \
182                                                                         \
183         goto label;                                                     \
184 } while (0)
185
186
187 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
188 # if BITS_PER_LONG > 32
189 #  define RETURN(rc)                                                    \
190 do {                                                                    \
191         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
192                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);     \
193                 return (typeof(rc))libcfs_log_return(&msgdata,          \
194                                                      (long)(rc));       \
195         }                                                               \
196                                                                         \
197         return (rc);                                                    \
198 } while (0)
199 # else /* BITS_PER_LONG == 32 */
200 /* We need an on-stack variable, because we cannot case a 32-bit pointer
201  * directly to (long long) without generating a complier warning/error, yet
202  * casting directly to (long) will truncate 64-bit return values. The log
203  * values will print as 32-bit values, but they always have been. LU-1436
204  */
205 #  define RETURN(rc)                                                    \
206 do {                                                                    \
207         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
208                 typeof(rc) __rc = (rc);                                 \
209                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);     \
210                 libcfs_log_return(&msgdata, (long)__rc);                \
211                 return __rc;                                            \
212         }                                                               \
213                                                                         \
214         return (rc);                                                    \
215 } while (0)
216
217 # endif /* BITS_PER_LONG > 32 */
218
219 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
220 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
221
222 #else /* !CDEBUG_ENTRY_EXIT */
223
224 # define GOTO(label, rc)                                                \
225         do {                                                            \
226                 ((void)(rc));                                           \
227                 goto label;                                             \
228         } while (0)
229
230 # define RETURN(rc) return (rc)
231 # define ENTRY  do { } while (0)
232 # define EXIT   do { } while (0)
233
234 #endif /* CDEBUG_ENTRY_EXIT */
235
236 #define RETURN_EXIT                                                     \
237 do {                                                                    \
238         EXIT;                                                           \
239         return;                                                         \
240 } while (0)
241
242 extern int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
243                             const char *format1, ...)
244         __attribute__ ((format (printf, 2, 3)));
245
246 extern int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
247                               const char *format1,
248                               va_list args, const char *format2, ...)
249         __attribute__ ((format (printf, 4, 5)));
250
251 /* other external symbols that tracefile provides: */
252 extern int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
253                                    const char __user *usr_buffer,
254                                    int usr_buffer_nob);
255 extern int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
256                                     const char *knl_buffer, char *append);
257
258 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
259
260 #endif  /* __LIBCFS_DEBUG_H__ */