1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/debug.c
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define EXPORT_SYMTAB
45 # define DEBUG_SUBSYSTEM S_LNET
48 #include <libcfs/libcfs.h>
49 #include "tracefile.h"
51 static char debug_file_name[1024];
54 unsigned int libcfs_subsystem_debug = ~0;
55 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
56 "Lustre kernel debug subsystem mask");
57 EXPORT_SYMBOL(libcfs_subsystem_debug);
59 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
60 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
61 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
62 "Lustre kernel debug mask");
63 EXPORT_SYMBOL(libcfs_debug);
65 int libcfs_debug_mb = -1;
66 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
67 "Total debug buffer size.");
68 EXPORT_SYMBOL(libcfs_debug_mb);
70 unsigned int libcfs_printk = D_CANTMASK;
71 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
72 "Lustre kernel debug console mask");
73 EXPORT_SYMBOL(libcfs_printk);
75 unsigned int libcfs_console_ratelimit = 1;
76 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
77 "Lustre kernel debug console ratelimit (0 to disable)");
78 EXPORT_SYMBOL(libcfs_console_ratelimit);
80 cfs_duration_t libcfs_console_max_delay;
81 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
82 "Lustre kernel debug console max delay (jiffies)");
83 EXPORT_SYMBOL(libcfs_console_max_delay);
85 cfs_duration_t libcfs_console_min_delay;
86 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
87 "Lustre kernel debug console min delay (jiffies)");
88 EXPORT_SYMBOL(libcfs_console_min_delay);
90 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
91 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
92 "Lustre kernel debug console backoff factor");
93 EXPORT_SYMBOL(libcfs_console_backoff);
95 unsigned int libcfs_debug_binary = 1;
96 EXPORT_SYMBOL(libcfs_debug_binary);
98 unsigned int libcfs_stack;
99 EXPORT_SYMBOL(libcfs_stack);
101 unsigned int portal_enter_debugger;
102 EXPORT_SYMBOL(portal_enter_debugger);
104 unsigned int libcfs_catastrophe;
105 EXPORT_SYMBOL(libcfs_catastrophe);
107 unsigned int libcfs_panic_on_lbug = 0;
108 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
109 "Lustre kernel panic on LBUG");
110 EXPORT_SYMBOL(libcfs_panic_on_lbug);
112 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
113 EXPORT_SYMBOL(libcfs_kmemory);
115 static cfs_waitq_t debug_ctlwq;
118 char debug_file_path[1024] = "/r/tmp/lustre-log";
120 char debug_file_path[1024] = "/tmp/lustre-log";
122 char *debug_file_path_p = &debug_file_path[0];
124 CFS_MODULE_PARM(debug_file_path_p, "s", charp, 0644,
125 "Path for dumping debug logs, "
126 "set 'NONE' to prevent log dumping");
128 int libcfs_panic_in_progress;
130 /* libcfs_debug_token2mask() expects the returned
131 * string in lower-case */
133 libcfs_debug_subsys2str(int subsys)
187 /* libcfs_debug_token2mask() expects the returned
188 * string in lower-case */
190 libcfs_debug_dbg2str(int debug)
255 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
257 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
258 libcfs_debug_dbg2str;
264 if (mask == 0) { /* "0" */
268 } else { /* space-separated tokens */
269 for (i = 0; i < 32; i++) {
272 if ((mask & bit) == 0)
276 if (token == NULL) /* unused bit */
279 if (len > 0) { /* separator? */
285 while (*token != 0) {
294 /* terminate 'str' */
304 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
306 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
307 libcfs_debug_dbg2str;
313 /* match against known tokens */
314 for (i = 0; i < 32; i++) {
318 if (token == NULL) /* unused? */
323 if (j == len) { /* end of token */
334 if (str[j] == token[j])
337 if (str[j] < 'A' || 'Z' < str[j])
340 if (str[j] - 'A' + 'a' != token[j])
345 return -EINVAL; /* no match */
349 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
357 /* Allow a number for backwards compatibility */
359 for (n = strlen(str); n > 0; n--)
360 if (!isspace(str[n-1]))
364 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
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. */
378 while (isspace(*str)) /* skip whitespace */
384 if (*str == '+' || *str == '-') {
387 /* op on first token == relative */
391 while (isspace(*str)) /* skip whitespace */
394 if (*str == 0) /* trailing op */
398 /* find token length */
399 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
402 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
421 void libcfs_debug_dumplog_internal(void *arg)
423 CFS_DECL_JOURNAL_DATA;
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(),
431 printk(KERN_ALERT "LustreError: dumping log to %s\n",
433 tracefile_dump_all_pages(debug_file_name);
438 int libcfs_debug_dumplog_thread(void *arg)
441 libcfs_debug_dumplog_internal(arg);
442 cfs_waitq_signal(&debug_ctlwq);
446 void libcfs_debug_dumplog(void)
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);
459 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
460 (void *)(long)cfs_curproc_pid(),
461 CLONE_VM | CLONE_FS | CLONE_FILES);
463 printk(KERN_ERR "LustreError: cannot start log dump thread: "
466 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
468 /* be sure to teardown if kernel_thread() failed */
469 cfs_waitq_del(&debug_ctlwq, &wait);
470 set_current_state(TASK_RUNNING);
473 int libcfs_debug_init(unsigned long bufsize)
476 int max = libcfs_debug_mb;
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()) {
486 max = (max / num_possible_cpus());
487 max = (max << (20 - CFS_PAGE_SHIFT));
489 rc = tracefile_init(max);
492 libcfs_register_panic_notifier();
497 int libcfs_debug_cleanup(void)
499 libcfs_unregister_panic_notifier();
504 int libcfs_debug_clear_buffer(void)
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)
516 CDEBUG(D_TRACE,"***************************************************\n");
517 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
518 CDEBUG(D_TRACE,"***************************************************\n");
522 #undef DEBUG_SUBSYSTEM
523 #define DEBUG_SUBSYSTEM S_LNET
525 void libcfs_debug_set_level(unsigned int debug_level)
527 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
529 libcfs_debug = debug_level;
532 EXPORT_SYMBOL(libcfs_debug_dumplog);
533 EXPORT_SYMBOL(libcfs_debug_set_level);
536 #else /* !__KERNEL__ */
538 #include <libcfs/libcfs.h>
540 #ifdef HAVE_CATAMOUNT_DATA_H
541 #include <catamount/data.h>
542 #include <catamount/lputs.h>
544 static char source_nid[16];
545 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
546 static int toconsole = 1;
547 unsigned int libcfs_console_ratelimit = 1;
548 cfs_duration_t libcfs_console_max_delay;
549 cfs_duration_t libcfs_console_min_delay;
550 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
551 #else /* !HAVE_CATAMOUNT_DATA_H */
553 #include <sys/utsname.h>
554 #endif /* HAVE_NETDB_H */
555 struct utsname *tmp_utsname;
556 static char source_nid[sizeof(tmp_utsname->nodename)];
557 #endif /* HAVE_CATAMOUNT_DATA_H */
559 static int source_pid;
560 int smp_processor_id = 1;
561 char debug_file_path[1024];
564 int portals_do_debug_dumplog(void *arg)
566 printf("Look in %s\n", debug_file_name);
571 void portals_debug_print(void)
577 void libcfs_debug_dumplog(void)
579 printf("Look in %s\n", debug_file_name);
583 int libcfs_debug_init(unsigned long bufsize)
585 char *debug_mask = NULL;
586 char *debug_subsys = NULL;
587 char *debug_filename;
589 #ifdef HAVE_CATAMOUNT_DATA_H
590 char *debug_console = NULL;
591 char *debug_ratelimit = NULL;
592 char *debug_max_delay = NULL;
593 char *debug_min_delay = NULL;
594 char *debug_backoff = NULL;
596 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
597 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
599 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
600 source_pid = _my_pid;
602 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
603 if (debug_console != NULL) {
604 toconsole = strtoul(debug_console, NULL, 0);
605 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
607 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
608 if (debug_ratelimit != NULL) {
609 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
610 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
611 libcfs_console_ratelimit);
613 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
614 if (debug_max_delay != NULL)
615 libcfs_console_max_delay =
616 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
617 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
618 if (debug_min_delay != NULL)
619 libcfs_console_min_delay =
620 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
621 if (debug_min_delay || debug_max_delay) {
622 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
623 libcfs_console_max_delay < libcfs_console_min_delay) {
624 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
625 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
626 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
627 "should be greater than "
628 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
629 "and both parameters should be non-null"
630 ": restore default values\n");
632 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
633 " and min delay to %lus\n",
635 cfs_duration_sec(libcfs_console_max_delay),
637 cfs_duration_sec(libcfs_console_min_delay));
640 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
641 if (debug_backoff != NULL) {
642 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
643 if (libcfs_console_backoff <= 0) {
644 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
645 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
646 "restore default value\n");
648 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
649 libcfs_console_backoff);
653 struct utsname myname;
655 if (uname(&myname) == 0)
656 strcpy(source_nid, myname.nodename);
657 source_pid = getpid();
660 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
662 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
664 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
666 libcfs_subsystem_debug =
667 (unsigned int) strtol(debug_subsys, NULL, 0);
669 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
671 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
673 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
675 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
677 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
678 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
679 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
681 if (strcmp(debug_file_name, "stdout") == 0 ||
682 strcmp(debug_file_name, "-") == 0) {
683 debug_file_fd = stdout;
684 } else if (strcmp(debug_file_name, "stderr") == 0) {
685 debug_file_fd = stderr;
686 } else if (debug_file_name[0] != '\0') {
687 debug_file_fd = fopen(debug_file_name, "w");
688 if (debug_file_fd == NULL)
689 fprintf(stderr, "%s: unable to open '%s': %s\n",
690 source_nid, debug_file_name, strerror(errno));
693 if (debug_file_fd == NULL)
694 debug_file_fd = stdout;
699 int libcfs_debug_cleanup(void)
701 if (debug_file_fd != stdout && debug_file_fd != stderr)
702 fclose(debug_file_fd);
706 int libcfs_debug_clear_buffer(void)
711 int libcfs_debug_mark_buffer(char *text)
714 fprintf(debug_file_fd, "*******************************************************************************\n");
715 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
716 fprintf(debug_file_fd, "*******************************************************************************\n");
721 #ifdef HAVE_CATAMOUNT_DATA_H
722 #define CATAMOUNT_MAXLINE (256-4)
723 void catamount_printline(char *buf, size_t size)
730 pos += CATAMOUNT_MAXLINE;
731 prsize -= CATAMOUNT_MAXLINE;
737 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
738 int subsys, int mask,
739 const char *file, const char *fn, const int line,
740 const char *format1, va_list args,
741 const char *format2, ...)
747 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
748 * with linux, where message can`t
749 * be exceed PAGE_SIZE */
751 char *prefix = "Lustre";
753 #ifdef HAVE_CATAMOUNT_DATA_H
754 /* toconsole == 0 - all messages to debug_file_fd
755 * toconsole == 1 - warnings to console, all to debug_file_fd
756 * toconsole > 1 - all debug to console */
757 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
761 if ((!console) && (!debug_file_fd)) {
765 if (mask & (D_EMERG | D_ERROR))
766 prefix = "LustreError";
768 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
769 source_pid, source_nid, file, line, fn);
771 remain = sizeof(buf) - nob;
773 nob += vsnprintf(&buf[nob], remain, format1, args);
776 remain = sizeof(buf) - nob;
777 if ((format2) && (remain > 0)) {
778 va_start(ap, format2);
779 nob += vsnprintf(&buf[nob], remain, format2, ap);
783 #ifdef HAVE_CATAMOUNT_DATA_H
785 /* check rate limit for console */
787 if (libcfs_console_ratelimit &&
788 cdls->cdls_next != 0 && /* not first time ever */
789 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
791 /* skipping a console message */
796 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
797 libcfs_console_max_delay +
798 cfs_time_seconds(10))) {
799 /* last timeout was a long time ago */
800 cdls->cdls_delay /= libcfs_console_backoff * 4;
802 cdls->cdls_delay *= libcfs_console_backoff;
804 if (cdls->cdls_delay <
805 libcfs_console_min_delay)
807 libcfs_console_min_delay;
808 else if (cdls->cdls_delay >
809 libcfs_console_max_delay)
811 libcfs_console_max_delay;
814 /* ensure cdls_next is never zero after it's been seen */
815 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
818 if (cdls != NULL && cdls->cdls_count != 0) {
821 nob = snprintf(buf2, sizeof(buf2),
822 "Skipped %d previous similar message%s\n",
823 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
825 catamount_printline(buf2, nob);
826 cdls->cdls_count = 0;
829 catamount_printline(buf, nob);
832 /* return on toconsole > 1, as we don't want the user getting
833 * spammed by the debug data */
837 if (debug_file_fd == NULL)
840 gettimeofday(&tv, NULL);
842 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
843 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
844 file, line, fn, buf);
850 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
853 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
854 "ASSERTION(%s) failed\n", expr);
858 #endif /* __KERNEL__ */