Whamcloud - gitweb
LU-6245 libcfs: remove libcfs userland time abstraction
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/libcfs_debug.h
37  *
38  * Debug messages and assertions
39  *
40  */
41
42 #ifndef __LIBCFS_DEBUG_H__
43 #define __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 /**
68  * Format for debug message headers
69  */
70 struct ptldebug_header {
71         __u32 ph_len;
72         __u32 ph_flags;
73         __u32 ph_subsys;
74         __u32 ph_mask;
75         __u16 ph_cpu_id;
76         __u16 ph_type;
77         __u32 ph_sec;
78         __u64 ph_usec;
79         __u32 ph_stack;
80         __u32 ph_pid;
81         __u32 ph_extern_pid;
82         __u32 ph_line_num;
83 } __attribute__((packed));
84
85
86 #define PH_FLAG_FIRST_RECORD 1
87
88 /* Debugging subsystems (32 bits, non-overlapping) */
89 #define S_UNDEFINED     0x00000001
90 #define S_MDC           0x00000002
91 #define S_MDS           0x00000004
92 #define S_OSC           0x00000008
93 #define S_OST           0x00000010
94 #define S_CLASS         0x00000020
95 #define S_LOG           0x00000040
96 #define S_LLITE         0x00000080
97 #define S_RPC           0x00000100
98 #define S_MGMT          0x00000200
99 #define S_LNET          0x00000400
100 #define S_LND           0x00000800 /* ALL LNDs */
101 #define S_PINGER        0x00001000
102 #define S_FILTER        0x00002000
103 /* unused */
104 #define S_ECHO          0x00008000
105 #define S_LDLM          0x00010000
106 #define S_LOV           0x00020000
107 #define S_LQUOTA        0x00040000
108 #define S_OSD           0x00080000
109 #define S_LFSCK         0x00100000
110 /* unused */
111 /* unused */
112 #define S_LMV           0x00800000 /* b_new_cmd */
113 /* unused */
114 #define S_SEC           0x02000000 /* upcall cache */
115 #define S_GSS           0x04000000 /* b_new_cmd */
116 /* unused */
117 #define S_MGC           0x10000000
118 #define S_MGS           0x20000000
119 #define S_FID           0x40000000 /* b_new_cmd */
120 #define S_FLD           0x80000000 /* b_new_cmd */
121
122 #define LIBCFS_DEBUG_SUBSYS_NAMES {                                     \
123         "undefined", "mdc", "mds", "osc", "ost", "class", "log",        \
124         "llite", "rpc", "mgmt", "lnet", "lnd", "pinger", "filter", "",  \
125         "echo", "ldlm", "lov", "lquota", "osd", "lfsck", "", "", "lmv", \
126          "", "sec", "gss", "", "mgc", "mgs", "fid", "fld", NULL }
127
128 /* Debugging masks (32 bits, non-overlapping) */
129 #define D_TRACE         0x00000001 /* ENTRY/EXIT markers */
130 #define D_INODE         0x00000002
131 #define D_SUPER         0x00000004
132 #define D_EXT2          0x00000008 /* anything from ext2_debug */
133 #define D_MALLOC        0x00000010 /* print malloc, free information */
134 #define D_CACHE         0x00000020 /* cache-related items */
135 #define D_INFO          0x00000040 /* general information */
136 #define D_IOCTL         0x00000080 /* ioctl related information */
137 #define D_NETERROR      0x00000100 /* network errors */
138 #define D_NET           0x00000200 /* network communications */
139 #define D_WARNING       0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
140 #define D_BUFFS         0x00000800
141 #define D_OTHER         0x00001000
142 #define D_DENTRY        0x00002000
143 #define D_NETTRACE      0x00004000
144 #define D_PAGE          0x00008000 /* bulk page handling */
145 #define D_DLMTRACE      0x00010000
146 #define D_ERROR         0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
147 #define D_EMERG         0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
148 #define D_HA            0x00080000 /* recovery and failover */
149 #define D_RPCTRACE      0x00100000 /* for distributed debugging */
150 #define D_VFSTRACE      0x00200000
151 #define D_READA         0x00400000 /* read-ahead */
152 #define D_MMAP          0x00800000
153 #define D_CONFIG        0x01000000
154 #define D_CONSOLE       0x02000000
155 #define D_QUOTA         0x04000000
156 #define D_SEC           0x08000000
157 #define D_LFSCK         0x10000000 /* For both OI scrub and LFSCK */
158 #define D_HSM           0x20000000
159
160 #define LIBCFS_DEBUG_MASKS_NAMES {                                      \
161         "trace", "inode", "super", "ext2", "malloc", "cache", "info",   \
162         "ioctl", "neterror", "net", "warning", "buffs", "other",        \
163         "dentry", "nettrace", "page", "dlmtrace", "error", "emerg",     \
164         "ha", "rpctrace", "vfstrace", "reada", "mmap", "config",        \
165         "console", "quota", "sec", "lfsck", "hsm", NULL }
166
167 #define D_CANTMASK   (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
168
169 #ifndef DEBUG_SUBSYSTEM
170 # define DEBUG_SUBSYSTEM S_UNDEFINED
171 #endif
172
173 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
174 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
175 #define CDEBUG_DEFAULT_BACKOFF   2
176 struct cfs_debug_limit_state {
177         unsigned long   cdls_next;
178         unsigned int    cdls_delay;
179         int             cdls_count;
180 };
181
182 struct libcfs_debug_msg_data {
183         const char                      *msg_file;
184         const char                      *msg_fn;
185         int                              msg_subsys;
186         int                              msg_line;
187         int                              msg_mask;
188         struct cfs_debug_limit_state    *msg_cdls;
189 };
190
191 #define LIBCFS_DEBUG_MSG_DATA_INIT(data, mask, cdls)        \
192 do {                                                        \
193         (data)->msg_subsys = DEBUG_SUBSYSTEM;               \
194         (data)->msg_file   = __FILE__;                      \
195         (data)->msg_fn     = __FUNCTION__;                  \
196         (data)->msg_line   = __LINE__;                      \
197         (data)->msg_cdls   = (cdls);                        \
198         (data)->msg_mask   = (mask);                        \
199 } while (0)
200
201 #define LIBCFS_DEBUG_MSG_DATA_DECL(dataname, mask, cdls)    \
202         static struct libcfs_debug_msg_data dataname = {    \
203                .msg_subsys = DEBUG_SUBSYSTEM,               \
204                .msg_file   = __FILE__,                      \
205                .msg_fn     = __FUNCTION__,                  \
206                .msg_line   = __LINE__,                      \
207                .msg_cdls   = (cdls)         };              \
208         dataname.msg_mask   = (mask);
209
210 #ifdef __KERNEL__
211
212 # ifdef CDEBUG_ENABLED
213
214 /**
215  * Filters out logging messages based on mask and subsystem.
216  */
217 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
218 {
219         return mask & D_CANTMASK ||
220                 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
221 }
222
223 #  define __CDEBUG(cdls, mask, format, ...)                             \
224 do {                                                                    \
225         static struct libcfs_debug_msg_data msgdata;                    \
226                                                                         \
227         CFS_CHECK_STACK(&msgdata, mask, cdls);                          \
228                                                                         \
229         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
230                 LIBCFS_DEBUG_MSG_DATA_INIT(&msgdata, mask, cdls);       \
231                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
232         }                                                               \
233 } while (0)
234
235 #  define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
236
237 #  define CDEBUG_LIMIT(mask, format, ...)                               \
238 do {                                                                    \
239         static struct cfs_debug_limit_state cdls;                       \
240                                                                         \
241         __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);                  \
242 } while (0)
243
244 # else /* !CDEBUG_ENABLED */
245 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
246 {
247         return 0;
248 }
249 #  define CDEBUG(mask, format, ...) (void)(0)
250 #  define CDEBUG_LIMIT(mask, format, ...) (void)(0)
251 #  warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
252 # endif /* CDEBUG_ENABLED */
253
254 #else /* !__KERNEL__ */
255 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
256 {
257         return 0;
258 }
259 # define CDEBUG(mask, format, ...)                                      \
260 do {                                                                    \
261         if (((mask) & D_CANTMASK) != 0)                                 \
262                 fprintf(stderr, "(%s:%d:%s()) " format,                 \
263                         __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__);\
264 } while (0)
265
266 # define CDEBUG_LIMIT CDEBUG
267
268 #endif /* __KERNEL__ */
269
270 /*
271  * Lustre Error Checksum: calculates checksum
272  * of Hex number by XORing each bit.
273  */
274 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
275                            ((hexnum) >> 8 & 0xf))
276
277 #define CWARN(format, ...)          CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
278 #define CERROR(format, ...)         CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
279 #define CNETERR(format, a...)       CDEBUG_LIMIT(D_NETERROR, format, ## a)
280 #define CEMERG(format, ...)         CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
281
282 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
283 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
284 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
285 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
286                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
287 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
288
289 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
290
291 #if defined(CDEBUG_ENTRY_EXIT) && defined(__KERNEL__)
292
293 void libcfs_log_goto(struct libcfs_debug_msg_data *goto_data,
294                      const char *label, long_ptr_t rc);
295
296 # define GOTO(label, rc)                                                \
297 do {                                                                    \
298         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
299                 LIBCFS_DEBUG_MSG_DATA_DECL(_goto_data, D_TRACE, NULL);  \
300                 libcfs_log_goto(&_goto_data, #label, (long_ptr_t)(rc)); \
301         } else {                                                        \
302                 (void)(rc);                                             \
303         }                                                               \
304                                                                         \
305         goto label;                                                     \
306 } while (0)
307
308
309 long libcfs_log_return(struct libcfs_debug_msg_data *, long rc);
310 # if BITS_PER_LONG > 32
311 #  define RETURN(rc)                                                    \
312 do {                                                                    \
313         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
314                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);     \
315                 return (typeof(rc))libcfs_log_return(&msgdata,          \
316                                                      (long)(rc));       \
317         }                                                               \
318                                                                         \
319         return (rc);                                                    \
320 } while (0)
321 # else /* BITS_PER_LONG == 32 */
322 /* We need an on-stack variable, because we cannot case a 32-bit pointer
323  * directly to (long long) without generating a complier warning/error, yet
324  * casting directly to (long) will truncate 64-bit return values. The log
325  * values will print as 32-bit values, but they always have been. LU-1436
326  */
327 #  define RETURN(rc)                                                    \
328 do {                                                                    \
329         if (cfs_cdebug_show(D_TRACE, DEBUG_SUBSYSTEM)) {                \
330                 typeof(rc) __rc = (rc);                                 \
331                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_TRACE, NULL);     \
332                 libcfs_log_return(&msgdata, (long_ptr_t)__rc);          \
333                 return __rc;                                            \
334         }                                                               \
335                                                                         \
336         return (rc);                                                    \
337 } while (0)
338
339 # endif /* BITS_PER_LONG > 32 */
340
341 # define ENTRY  CDEBUG(D_TRACE, "Process entered\n")
342 # define EXIT   CDEBUG(D_TRACE, "Process leaving\n")
343
344 #else /* !CDEBUG_ENTRY_EXIT || !__KERNEL__ */
345
346 # define GOTO(label, rc)                                                \
347         do {                                                            \
348                 ((void)(rc));                                           \
349                 goto label;                                             \
350         } while (0)
351
352 # define RETURN(rc) return (rc)
353 # define ENTRY  do { } while (0)
354 # define EXIT   do { } while (0)
355
356 #endif /* CDEBUG_ENTRY_EXIT && __KERNEL__ */
357
358 #define RETURN_EXIT                                                     \
359 do {                                                                    \
360         EXIT;                                                           \
361         return;                                                         \
362 } while (0)
363
364 extern int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
365                             const char *format1, ...)
366         __attribute__ ((format (printf, 2, 3)));
367
368 extern int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
369                               const char *format1,
370                               va_list args, const char *format2, ...)
371         __attribute__ ((format (printf, 4, 5)));
372
373 #ifdef __KERNEL__
374 /* other external symbols that tracefile provides: */
375 extern int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
376                                    const char __user *usr_buffer,
377                                    int usr_buffer_nob);
378 extern int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
379                                     const char *knl_buffer, char *append);
380 #endif /* __KERNEL__ */
381
382 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
383
384 #endif  /* __LIBCFS_DEBUG_H__ */