Whamcloud - gitweb
LU-9859 libcfs: change libcfs_log_* functions to inline
[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/limits.h>
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_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 #if !defined(__x86_64__)
116 # ifdef __ia64__
117 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
118                           ((unsigned long)__builtin_dwarf_cfa() &       \
119                            (THREAD_SIZE - 1)))
120 # else
121 #  define CDEBUG_STACK() (THREAD_SIZE -                                 \
122                           ((unsigned long)__builtin_frame_address(0) &  \
123                            (THREAD_SIZE - 1)))
124 # endif /* __ia64__ */
125
126 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
127 do {                                                                    \
128         if (unlikely(CDEBUG_STACK() > libcfs_stack)) {                  \
129                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line, msgdata,   \
130                                            D_WARNING, NULL);            \
131                 libcfs_stack = CDEBUG_STACK();                          \
132                 libcfs_debug_msg(msgdata, "maximum lustre stack %u\n",  \
133                                  libcfs_stack);                         \
134                 (msgdata)->msg_mask = mask;                             \
135                 (msgdata)->msg_cdls = cdls;                             \
136                 dump_stack();                                           \
137                 /*panic("LBUG");*/                                      \
138         }                                                               \
139 } while (0)
140 #else /* __x86_64__ */
141 #define CDEBUG_STACK() (0L)
142 #define __CHECK_STACK_WITH_LOC(file, func, line, msgdata, mask, cdls)   \
143         do {} while (0)
144 #endif /* __x86_64__ */
145
146 #define CFS_CHECK_STACK(msgdata, mask, cdls)                            \
147         __CHECK_STACK_WITH_LOC(__FILE__, __func__, __LINE__,            \
148                                msgdata, mask, cdls)
149 /**
150  * Filters out logging messages based on mask and subsystem.
151  */
152 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
153 {
154         return mask & D_CANTMASK ||
155                ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
156 }
157
158 #  define __CDEBUG_WITH_LOC(file, func, line, mask, cdls, format, ...)  \
159 do {                                                                    \
160         static struct libcfs_debug_msg_data msgdata;                    \
161                                                                         \
162         __CHECK_STACK_WITH_LOC(file, func, line, &msgdata, mask, cdls); \
163                                                                         \
164         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
165                 LIBCFS_DEBUG_MSG_DATA_INIT(file, func, line,            \
166                                            &msgdata, mask, cdls);       \
167                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
168         }                                                               \
169 } while (0)
170
171 #  define CDEBUG(mask, format, ...)                                     \
172         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
173                           mask, NULL, format, ## __VA_ARGS__)
174
175 #  define CDEBUG_LIMIT(mask, format, ...)                               \
176 do {                                                                    \
177         static struct cfs_debug_limit_state cdls;                       \
178                                                                         \
179         __CDEBUG_WITH_LOC(__FILE__, __func__, __LINE__,                 \
180                           mask, &cdls, format, ## __VA_ARGS__);         \
181 } while (0)
182
183 # else /* !CDEBUG_ENABLED */
184 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
185 {
186         return 0;
187 }
188 #  define CDEBUG(mask, format, ...) (void)(0)
189 #  define CDEBUG_LIMIT(mask, format, ...) (void)(0)
190 #  warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
191 # endif /* CDEBUG_ENABLED */
192
193 /*
194  * Lustre Error Checksum: calculates checksum
195  * of Hex number by XORing each bit.
196  */
197 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
198                            ((hexnum) >> 8 & 0xf))
199
200 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
201 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
202 #define CNETERR(format, a...)       CDEBUG_LIMIT(D_NETERROR, format, ## a)
203 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
204
205 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
206 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
207 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
208 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
209                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
210 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
211
212 #define LCONSOLE_EMERG(format, ...) \
213         CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
214
215 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
216                      const char *format1, ...)
217         __printf(2, 3);
218
219 /* other external symbols that tracefile provides: */
220 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
221                              const char *knl_buffer, char *append);
222
223 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
224
225 #if defined(CDEBUG_ENTRY_EXIT)
226
227 static inline long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
228 {
229         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
230                          rc, rc, rc);
231         return rc;
232 }
233
234 static inline void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata,
235                                    const char *label, long rc)
236 {
237         libcfs_debug_msg(msgdata,
238                          "Process leaving via %s (rc=%lu : %ld : %#lx)\n",
239                          label, rc, rc, rc);
240 }
241
242 # define GOTO(label, rc)                                                      \
243 do {                                                                          \
244         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
245                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);        \
246                 libcfs_log_goto(&_goto_data, #label, (long)(rc));             \
247         } else {                                                              \
248                 (void)(rc);                                                   \
249         }                                                                     \
250                                                                               \
251         goto label;                                                           \
252 } while (0)
253
254 # if BITS_PER_LONG > 32
255 #  define RETURN(rc)                                                          \
256 do {                                                                          \
257         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
258                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
259                 return (typeof(rc))libcfs_log_return(&msgdata,                \
260                                                      (long)(rc));             \
261         }                                                                     \
262                                                                               \
263         return rc;                                                            \
264 } while (0)
265 # else /* BITS_PER_LONG == 32 */
266 /* We need an on-stack variable, because we cannot case a 32-bit pointer
267  * directly to (long long) without generating a complier warning/error, yet
268  * casting directly to (long) will truncate 64-bit return values. The log
269  * values will print as 32-bit values, but they always have been. LU-1436
270  */
271 #  define RETURN(rc)                                                          \
272 do {                                                                          \
273         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                      \
274                 typeof(rc) __rc = (rc);                                       \
275                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);           \
276                 libcfs_log_return(&msgdata, (long)__rc);                      \
277                 return __rc;                                                  \
278         }                                                                     \
279                                                                               \
280         return rc;                                                            \
281 } while (0)
282
283 # endif /* BITS_PER_LONG > 32 */
284
285 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
286 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
287
288 #else /* !CDEBUG_ENTRY_EXIT */
289
290 # define GOTO(label, rc)                                                \
291         do {                                                            \
292                 ((void)(rc));                                           \
293                 goto label;                                             \
294         } while (0)
295
296 # define RETURN(rc) return (rc)
297 # define ENTRY  do { } while (0)
298 # define EXIT   do { } while (0)
299
300 #endif /* CDEBUG_ENTRY_EXIT */
301
302 #define RETURN_EXIT                                                     \
303 do {                                                                    \
304         EXIT;                                                           \
305         return;                                                         \
306 } while (0)
307
308 void cfs_debug_init(void);
309
310 #endif  /* __LIBCFS_DEBUG_H__ */