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