Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / libcfs / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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/libcfs/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 # define DEBUG_SUBSYSTEM S_LNET
46
47 #include <libcfs/libcfs.h>
48 #include "tracefile.h"
49
50 static char debug_file_name[1024];
51
52 unsigned int libcfs_subsystem_debug = ~0;
53 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
54                 "Lustre kernel debug subsystem mask");
55 EXPORT_SYMBOL(libcfs_subsystem_debug);
56
57 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
58                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
59 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
60                 "Lustre kernel debug mask");
61 EXPORT_SYMBOL(libcfs_debug);
62
63 int libcfs_debug_mb = -1;
64 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
65                 "Total debug buffer size.");
66 EXPORT_SYMBOL(libcfs_debug_mb);
67
68 unsigned int libcfs_printk = D_CANTMASK;
69 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
70                 "Lustre kernel debug console mask");
71 EXPORT_SYMBOL(libcfs_printk);
72
73 unsigned int libcfs_console_ratelimit = 1;
74 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
75                 "Lustre kernel debug console ratelimit (0 to disable)");
76 EXPORT_SYMBOL(libcfs_console_ratelimit);
77
78 cfs_duration_t libcfs_console_max_delay;
79 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
80                 "Lustre kernel debug console max delay (jiffies)");
81 EXPORT_SYMBOL(libcfs_console_max_delay);
82
83 cfs_duration_t libcfs_console_min_delay;
84 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
85                 "Lustre kernel debug console min delay (jiffies)");
86 EXPORT_SYMBOL(libcfs_console_min_delay);
87
88 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
89 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
90                 "Lustre kernel debug console backoff factor");
91 EXPORT_SYMBOL(libcfs_console_backoff);
92
93 unsigned int libcfs_debug_binary = 1;
94 EXPORT_SYMBOL(libcfs_debug_binary);
95
96 unsigned int libcfs_stack;
97 EXPORT_SYMBOL(libcfs_stack);
98
99 unsigned int portal_enter_debugger;
100 EXPORT_SYMBOL(portal_enter_debugger);
101
102 unsigned int libcfs_catastrophe;
103 EXPORT_SYMBOL(libcfs_catastrophe);
104
105 unsigned int libcfs_panic_on_lbug = 0;
106 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
107                 "Lustre kernel panic on LBUG");
108 EXPORT_SYMBOL(libcfs_panic_on_lbug);
109
110 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
111 EXPORT_SYMBOL(libcfs_kmemory);
112
113 static cfs_waitq_t debug_ctlwq;
114
115 #ifdef HAVE_BGL_SUPPORT
116 char debug_file_path[1024] = "/bgl/ion/tmp/lustre-log";
117 #elif defined(__arch_um__)
118 char debug_file_path[1024] = "/r/tmp/lustre-log";
119 #else
120 char debug_file_path[1024] = "/tmp/lustre-log";
121 #endif
122 char *debug_file_path_p = &debug_file_path[0];
123
124 CFS_MODULE_PARM(debug_file_path_p, "s", charp, 0644,
125                 "Path for dumping debug logs, "
126                 "set 'NONE' to prevent log dumping");
127
128 int libcfs_panic_in_progress;
129
130 /* libcfs_debug_token2mask() expects the returned
131  * string in lower-case */
132 const char *
133 libcfs_debug_subsys2str(int subsys)
134 {
135         switch (subsys) {
136         default:
137                 return NULL;
138         case S_UNDEFINED:
139                 return "undefined";
140         case S_MDC:
141                 return "mdc";
142         case S_MDS:
143                 return "mds";
144         case S_OSC:
145                 return "osc";
146         case S_OST:
147                 return "ost";
148         case S_CLASS:
149                 return "class";
150         case S_LOG:
151                 return "log";
152         case S_LLITE:
153                 return "llite";
154         case S_RPC:
155                 return "rpc";
156         case S_LNET:
157                 return "lnet";
158         case S_LND:
159                 return "lnd";
160         case S_PINGER:
161                 return "pinger";
162         case S_FILTER:
163                 return "filter";
164         case S_ECHO:
165                 return "echo";
166         case S_LDLM:
167                 return "ldlm";
168         case S_LOV:
169                 return "lov";
170         case S_LMV:
171                 return "lmv";
172         case S_SEC:
173                 return "sec";
174         case S_GSS:
175                 return "gss";
176         case S_MGC:
177                 return "mgc";
178         case S_MGS:
179                 return "mgs";
180         case S_FID:
181                 return "fid";
182         case S_FLD:
183                 return "fld";
184         }
185 }
186
187 /* libcfs_debug_token2mask() expects the returned
188  * string in lower-case */
189 const char *
190 libcfs_debug_dbg2str(int debug)
191 {
192         switch (debug) {
193         default:
194                 return NULL;
195         case D_TRACE:
196                 return "trace";
197         case D_INODE:
198                 return "inode";
199         case D_SUPER:
200                 return "super";
201         case D_EXT2:
202                 return "ext2";
203         case D_MALLOC:
204                 return "malloc";
205         case D_CACHE:
206                 return "cache";
207         case D_INFO:
208                 return "info";
209         case D_IOCTL:
210                 return "ioctl";
211         case D_NETERROR:
212                 return "neterror";
213         case D_NET:
214                 return "net";
215         case D_WARNING:
216                 return "warning";
217         case D_BUFFS:
218                 return "buffs";
219         case D_OTHER:
220                 return "other";
221         case D_DENTRY:
222                 return "dentry";
223         case D_NETTRACE:
224                 return "nettrace";
225         case D_PAGE:
226                 return "page";
227         case D_DLMTRACE:
228                 return "dlmtrace";
229         case D_ERROR:
230                 return "error";
231         case D_EMERG:
232                 return "emerg";
233         case D_HA:
234                 return "ha";
235         case D_RPCTRACE:
236                 return "rpctrace";
237         case D_VFSTRACE:
238                 return "vfstrace";
239         case D_READA:
240                 return "reada";
241         case D_MMAP:
242                 return "mmap";
243         case D_CONFIG:
244                 return "config";
245         case D_CONSOLE:
246                 return "console";
247         case D_QUOTA:
248                 return "quota";
249         case D_SEC:
250                 return "sec";
251         }
252 }
253
254 int
255 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
256 {
257         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
258                                                  libcfs_debug_dbg2str;
259         int           len = 0;
260         const char   *token;
261         int           bit;
262         int           i;
263
264         if (mask == 0) {                        /* "0" */
265                 if (size > 0)
266                         str[0] = '0';
267                 len = 1;
268         } else {                                /* space-separated tokens */
269                 for (i = 0; i < 32; i++) {
270                         bit = 1 << i;
271
272                         if ((mask & bit) == 0)
273                                 continue;
274
275                         token = fn(bit);
276                         if (token == NULL)              /* unused bit */
277                                 continue;
278
279                         if (len > 0) {                  /* separator? */
280                                 if (len < size)
281                                         str[len] = ' ';
282                                 len++;
283                         }
284
285                         while (*token != 0) {
286                                 if (len < size)
287                                         str[len] = *token;
288                                 token++;
289                                 len++;
290                         }
291                 }
292         }
293
294         /* terminate 'str' */
295         if (len < size)
296                 str[len] = 0;
297         else
298                 str[size - 1] = 0;
299
300         return len;
301 }
302
303 int
304 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
305 {
306         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
307                                                  libcfs_debug_dbg2str;
308         int           i;
309         int           j;
310         int           bit;
311         const char   *token;
312
313         /* match against known tokens */
314         for (i = 0; i < 32; i++) {
315                 bit = 1 << i;
316
317                 token = fn(bit);
318                 if (token == NULL)              /* unused? */
319                         continue;
320
321                 /* strcasecmp */
322                 for (j = 0; ; j++) {
323                         if (j == len) {         /* end of token */
324                                 if (token[j] == 0) {
325                                         *mask = bit;
326                                         return 0;
327                                 }
328                                 break;
329                         }
330
331                         if (token[j] == 0)
332                                 break;
333
334                         if (str[j] == token[j])
335                                 continue;
336
337                         if (str[j] < 'A' || 'Z' < str[j])
338                                 break;
339
340                         if (str[j] - 'A' + 'a' != token[j])
341                                 break;
342                 }
343         }
344
345         return -EINVAL;                         /* no match */
346 }
347
348 int
349 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
350 {
351         int         m = 0;
352         char        op = 0;
353         int         matched;
354         int         n;
355         int         t;
356
357         /* Allow a number for backwards compatibility */
358
359         for (n = strlen(str); n > 0; n--)
360                 if (!isspace(str[n-1]))
361                         break;
362         matched = n;
363
364         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
365             matched == n) {
366                 *mask = m;
367                 return 0;
368         }
369
370         /* <str> must be a list of debug tokens or numbers separated by
371          * whitespace and optionally an operator ('+' or '-').  If an operator
372          * appears first in <str>, '*mask' is used as the starting point
373          * (relative), otherwise 0 is used (absolute).  An operator applies to
374          * all following tokens up to the next operator. */
375
376         matched = 0;
377         while (*str != 0) {
378                 while (isspace(*str)) /* skip whitespace */
379                         str++;
380
381                 if (*str == 0)
382                         break;
383
384                 if (*str == '+' || *str == '-') {
385                         op = *str++;
386
387                         /* op on first token == relative */
388                         if (!matched)
389                                 m = *mask;
390
391                         while (isspace(*str)) /* skip whitespace */
392                                 str++;
393
394                         if (*str == 0)          /* trailing op */
395                                 return -EINVAL;
396                 }
397
398                 /* find token length */
399                 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
400
401                 /* match token */
402                 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
403                         return -EINVAL;
404
405                 matched = 1;
406                 if (op == '-')
407                         m &= ~t;
408                 else
409                         m |= t;
410
411                 str += n;
412         }
413
414         if (!matched)
415                 return -EINVAL;
416
417         *mask = m;
418         return 0;
419 }
420
421 void libcfs_debug_dumplog_internal(void *arg)
422 {
423         CFS_DECL_JOURNAL_DATA;
424
425         CFS_PUSH_JOURNAL;
426
427         if (strncmp(debug_file_path, "NONE", 4) != 0) {
428                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
429                          "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(),
430                          (long)arg);
431                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
432                        debug_file_name);
433                 tracefile_dump_all_pages(debug_file_name);
434         }
435         CFS_POP_JOURNAL;
436 }
437
438 int libcfs_debug_dumplog_thread(void *arg)
439 {
440         cfs_daemonize("");
441         libcfs_debug_dumplog_internal(arg);
442         cfs_waitq_signal(&debug_ctlwq);
443         return 0;
444 }
445
446 void libcfs_debug_dumplog(void)
447 {
448         int            rc;
449         cfs_waitlink_t wait;
450         ENTRY;
451
452         /* we're being careful to ensure that the kernel thread is
453          * able to set our state to running as it exits before we
454          * get to schedule() */
455         cfs_waitlink_init(&wait);
456         set_current_state(TASK_INTERRUPTIBLE);
457         cfs_waitq_add(&debug_ctlwq, &wait);
458
459         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
460                                (void *)(long)cfs_curproc_pid(),
461                                CLONE_VM | CLONE_FS | CLONE_FILES);
462         if (rc < 0)
463                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
464                        "%d\n", rc);
465         else
466                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
467
468         /* be sure to teardown if kernel_thread() failed */
469         cfs_waitq_del(&debug_ctlwq, &wait);
470         set_current_state(TASK_RUNNING);
471 }
472
473 int libcfs_debug_init(unsigned long bufsize)
474 {
475         int    rc = 0;
476         int    max = libcfs_debug_mb;
477
478         cfs_waitq_init(&debug_ctlwq);
479         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
480         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
481         /* If libcfs_debug_mb is set to an invalid value or uninitialized
482          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
483         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
484                 max = TCD_MAX_PAGES;
485         } else {
486                 max = (max / num_possible_cpus());
487                 max = (max << (20 - CFS_PAGE_SHIFT));
488         }
489         rc = tracefile_init(max);
490
491         if (rc == 0)
492                 libcfs_register_panic_notifier();
493
494         return rc;
495 }
496
497 int libcfs_debug_cleanup(void)
498 {
499         libcfs_unregister_panic_notifier();
500         tracefile_exit();
501         return 0;
502 }
503
504 int libcfs_debug_clear_buffer(void)
505 {
506         trace_flush_pages();
507         return 0;
508 }
509
510 /* Debug markers, although printed by S_LNET
511  * should not be be marked as such. */
512 #undef DEBUG_SUBSYSTEM
513 #define DEBUG_SUBSYSTEM S_UNDEFINED
514 int libcfs_debug_mark_buffer(char *text)
515 {
516         CDEBUG(D_TRACE,"***************************************************\n");
517         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
518         CDEBUG(D_TRACE,"***************************************************\n");
519
520         return 0;
521 }
522 #undef DEBUG_SUBSYSTEM
523 #define DEBUG_SUBSYSTEM S_LNET
524
525 void libcfs_debug_set_level(unsigned int debug_level)
526 {
527         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
528                debug_level);
529         libcfs_debug = debug_level;
530 }
531
532 EXPORT_SYMBOL(libcfs_debug_dumplog);
533 EXPORT_SYMBOL(libcfs_debug_set_level);