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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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.
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define EXPORT_SYMTAB
45 # define DEBUG_SUBSYSTEM S_LNET
48 #include <libcfs/kp30.h>
49 #include <libcfs/libcfs.h>
50 #include "tracefile.h"
52 static char debug_file_name[1024];
55 unsigned int libcfs_subsystem_debug = ~0;
56 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
57 "Lustre kernel debug subsystem mask");
58 EXPORT_SYMBOL(libcfs_subsystem_debug);
60 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
61 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
62 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
63 "Lustre kernel debug mask");
64 EXPORT_SYMBOL(libcfs_debug);
66 int libcfs_debug_mb = -1;
67 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
68 "Total debug buffer size.");
69 EXPORT_SYMBOL(libcfs_debug_mb);
71 unsigned int libcfs_printk = (D_CANTMASK | D_NETERROR);
72 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
73 "Lustre kernel debug console mask");
74 EXPORT_SYMBOL(libcfs_printk);
76 unsigned int libcfs_console_ratelimit = 1;
77 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
78 "Lustre kernel debug console ratelimit (0 to disable)");
79 EXPORT_SYMBOL(libcfs_console_ratelimit);
81 cfs_duration_t libcfs_console_max_delay;
82 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
83 "Lustre kernel debug console max delay (jiffies)");
84 EXPORT_SYMBOL(libcfs_console_max_delay);
86 cfs_duration_t libcfs_console_min_delay;
87 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
88 "Lustre kernel debug console min delay (jiffies)");
89 EXPORT_SYMBOL(libcfs_console_min_delay);
91 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
92 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
93 "Lustre kernel debug console backoff factor");
94 EXPORT_SYMBOL(libcfs_console_backoff);
96 unsigned int libcfs_debug_binary = 1;
97 EXPORT_SYMBOL(libcfs_debug_binary);
99 unsigned int libcfs_stack;
100 EXPORT_SYMBOL(libcfs_stack);
102 unsigned int portal_enter_debugger;
103 EXPORT_SYMBOL(portal_enter_debugger);
105 unsigned int libcfs_catastrophe;
106 EXPORT_SYMBOL(libcfs_catastrophe);
108 unsigned int libcfs_watchdog_ratelimit = 300;
109 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
111 unsigned int libcfs_panic_on_lbug = 0;
112 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
113 "Lustre kernel panic on LBUG");
114 EXPORT_SYMBOL(libcfs_panic_on_lbug);
116 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
117 EXPORT_SYMBOL(libcfs_kmemory);
119 static cfs_waitq_t debug_ctlwq;
121 #ifdef HAVE_BGL_SUPPORT
122 char debug_file_path_arr[1024] = "/bgl/ion/tmp/lustre-log";
123 #elif defined(__arch_um__)
124 char debug_file_path_arr[1024] = "/r/tmp/lustre-log";
126 char debug_file_path_arr[1024] = "/tmp/lustre-log";
128 /* We need to pass a pointer here, but elsewhere this must be a const */
129 char *debug_file_path = &debug_file_path_arr[0];
130 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
131 "Path for dumping debug logs, "
132 "set 'NONE' to prevent log dumping");
134 int libcfs_panic_in_progress;
136 /* libcfs_debug_token2mask() expects the returned
137 * string in lower-case */
139 libcfs_debug_subsys2str(int subsys)
195 /* libcfs_debug_token2mask() expects the returned
196 * string in lower-case */
198 libcfs_debug_dbg2str(int debug)
263 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
265 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
266 libcfs_debug_dbg2str;
272 if (mask == 0) { /* "0" */
276 } else { /* space-separated tokens */
277 for (i = 0; i < 32; i++) {
280 if ((mask & bit) == 0)
284 if (token == NULL) /* unused bit */
287 if (len > 0) { /* separator? */
293 while (*token != 0) {
302 /* terminate 'str' */
312 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
314 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
315 libcfs_debug_dbg2str;
321 /* match against known tokens */
322 for (i = 0; i < 32; i++) {
326 if (token == NULL) /* unused? */
331 if (j == len) { /* end of token */
342 if (str[j] == token[j])
345 if (str[j] < 'A' || 'Z' < str[j])
348 if (str[j] - 'A' + 'a' != token[j])
353 return -EINVAL; /* no match */
357 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
365 /* Allow a number for backwards compatibility */
367 for (n = strlen(str); n > 0; n--)
368 if (!isspace(str[n-1]))
372 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
378 /* <str> must be a list of debug tokens or numbers separated by
379 * whitespace and optionally an operator ('+' or '-'). If an operator
380 * appears first in <str>, '*mask' is used as the starting point
381 * (relative), otherwise 0 is used (absolute). An operator applies to
382 * all following tokens up to the next operator. */
386 while (isspace(*str)) /* skip whitespace */
392 if (*str == '+' || *str == '-') {
395 /* op on first token == relative */
399 while (isspace(*str)) /* skip whitespace */
402 if (*str == 0) /* trailing op */
406 /* find token length */
407 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
410 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
430 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
432 void libcfs_debug_dumplog_internal(void *arg)
434 CFS_DECL_JOURNAL_DATA;
438 if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
439 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
440 "%s.%ld.%ld", debug_file_path_arr,
441 cfs_time_current_sec(), (long)arg);
442 printk(KERN_ALERT "LustreError: dumping log to %s\n",
445 tracefile_dump_all_pages(debug_file_name);
446 libcfs_run_debug_log_upcall(debug_file_name);
451 int libcfs_debug_dumplog_thread(void *arg)
454 libcfs_debug_dumplog_internal(arg);
455 cfs_waitq_signal(&debug_ctlwq);
459 void libcfs_debug_dumplog(void)
465 /* we're being careful to ensure that the kernel thread is
466 * able to set our state to running as it exits before we
467 * get to schedule() */
468 cfs_waitlink_init(&wait);
469 set_current_state(TASK_INTERRUPTIBLE);
470 cfs_waitq_add(&debug_ctlwq, &wait);
472 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
473 (void *)(long)cfs_curproc_pid(),
474 CLONE_VM | CLONE_FS | CLONE_FILES);
476 printk(KERN_ERR "LustreError: cannot start log dump thread: "
479 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
481 /* be sure to teardown if kernel_thread() failed */
482 cfs_waitq_del(&debug_ctlwq, &wait);
483 set_current_state(TASK_RUNNING);
486 int libcfs_debug_init(unsigned long bufsize)
489 int max = libcfs_debug_mb;
491 cfs_waitq_init(&debug_ctlwq);
492 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
493 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
494 /* If libcfs_debug_mb is set to an invalid value or uninitialized
495 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
496 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
499 max = (max / num_possible_cpus());
500 max = (max << (20 - CFS_PAGE_SHIFT));
502 rc = tracefile_init(max);
505 libcfs_register_panic_notifier();
510 int libcfs_debug_cleanup(void)
512 libcfs_unregister_panic_notifier();
517 int libcfs_debug_clear_buffer(void)
523 /* Debug markers, although printed by S_LNET
524 * should not be be marked as such. */
525 #undef DEBUG_SUBSYSTEM
526 #define DEBUG_SUBSYSTEM S_UNDEFINED
527 int libcfs_debug_mark_buffer(const char *text)
529 CDEBUG(D_TRACE,"***************************************************\n");
530 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
531 CDEBUG(D_TRACE,"***************************************************\n");
535 #undef DEBUG_SUBSYSTEM
536 #define DEBUG_SUBSYSTEM S_LNET
538 void libcfs_debug_set_level(unsigned int debug_level)
540 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
542 libcfs_debug = debug_level;
545 EXPORT_SYMBOL(libcfs_debug_dumplog);
546 EXPORT_SYMBOL(libcfs_debug_set_level);
549 #else /* !__KERNEL__ */
551 #include <libcfs/libcfs.h>
553 #ifdef HAVE_CATAMOUNT_DATA_H
554 #include <catamount/data.h>
555 #include <catamount/lputs.h>
557 static char source_nid[16];
558 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
559 static int toconsole = 1;
560 unsigned int libcfs_console_ratelimit = 1;
561 cfs_duration_t libcfs_console_max_delay;
562 cfs_duration_t libcfs_console_min_delay;
563 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
564 #else /* !HAVE_CATAMOUNT_DATA_H */
566 #include <sys/utsname.h>
567 #endif /* HAVE_NETDB_H */
568 struct utsname *tmp_utsname;
569 static char source_nid[sizeof(tmp_utsname->nodename)];
570 #endif /* HAVE_CATAMOUNT_DATA_H */
572 static int source_pid;
573 int smp_processor_id = 1;
574 char debug_file_path[1024];
577 int portals_do_debug_dumplog(void *arg)
579 printf("Look in %s\n", debug_file_name);
584 void portals_debug_print(void)
590 void libcfs_debug_dumplog(void)
592 printf("Look in %s\n", debug_file_name);
596 int libcfs_debug_init(unsigned long bufsize)
598 char *debug_mask = NULL;
599 char *debug_subsys = NULL;
600 char *debug_filename;
602 #ifdef HAVE_CATAMOUNT_DATA_H
603 char *debug_console = NULL;
604 char *debug_ratelimit = NULL;
605 char *debug_max_delay = NULL;
606 char *debug_min_delay = NULL;
607 char *debug_backoff = NULL;
609 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
610 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
612 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
613 source_pid = _my_pid;
615 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
616 if (debug_console != NULL) {
617 toconsole = strtoul(debug_console, NULL, 0);
618 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
620 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
621 if (debug_ratelimit != NULL) {
622 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
623 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
624 libcfs_console_ratelimit);
626 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
627 if (debug_max_delay != NULL)
628 libcfs_console_max_delay =
629 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
630 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
631 if (debug_min_delay != NULL)
632 libcfs_console_min_delay =
633 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
634 if (debug_min_delay || debug_max_delay) {
635 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
636 libcfs_console_max_delay < libcfs_console_min_delay) {
637 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
638 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
639 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
640 "should be greater than "
641 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
642 "and both parameters should be non-null"
643 ": restore default values\n");
645 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
646 " and min delay to %lus\n",
648 cfs_duration_sec(libcfs_console_max_delay),
650 cfs_duration_sec(libcfs_console_min_delay));
653 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
654 if (debug_backoff != NULL) {
655 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
656 if (libcfs_console_backoff <= 0) {
657 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
658 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
659 "restore default value\n");
661 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
662 libcfs_console_backoff);
666 struct utsname myname;
668 if (uname(&myname) == 0)
669 strcpy(source_nid, myname.nodename);
670 source_pid = getpid();
673 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
675 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
677 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
679 libcfs_subsystem_debug =
680 (unsigned int) strtol(debug_subsys, NULL, 0);
682 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
684 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
686 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
688 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
690 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
691 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
692 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
694 if (strcmp(debug_file_name, "stdout") == 0 ||
695 strcmp(debug_file_name, "-") == 0) {
696 debug_file_fd = stdout;
697 } else if (strcmp(debug_file_name, "stderr") == 0) {
698 debug_file_fd = stderr;
699 } else if (debug_file_name[0] != '\0') {
700 debug_file_fd = fopen(debug_file_name, "w");
701 if (debug_file_fd == NULL)
702 fprintf(stderr, "%s: unable to open '%s': %s\n",
703 source_nid, debug_file_name, strerror(errno));
706 if (debug_file_fd == NULL)
707 debug_file_fd = stdout;
712 int libcfs_debug_cleanup(void)
714 if (debug_file_fd != stdout && debug_file_fd != stderr)
715 fclose(debug_file_fd);
719 int libcfs_debug_clear_buffer(void)
724 int libcfs_debug_mark_buffer(char *text)
727 fprintf(debug_file_fd, "*******************************************************************************\n");
728 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
729 fprintf(debug_file_fd, "*******************************************************************************\n");
734 #ifdef HAVE_CATAMOUNT_DATA_H
735 #define CATAMOUNT_MAXLINE (256-4)
736 void catamount_printline(char *buf, size_t size)
743 pos += CATAMOUNT_MAXLINE;
744 prsize -= CATAMOUNT_MAXLINE;
750 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
751 int subsys, int mask,
752 const char *file, const char *fn, const int line,
753 const char *format1, va_list args,
754 const char *format2, ...)
760 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
761 * with linux, where message can`t
762 * be exceed PAGE_SIZE */
764 char *prefix = "Lustre";
766 #ifdef HAVE_CATAMOUNT_DATA_H
767 /* toconsole == 0 - all messages to debug_file_fd
768 * toconsole == 1 - warnings to console, all to debug_file_fd
769 * toconsole > 1 - all debug to console */
770 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
774 if ((!console) && (!debug_file_fd)) {
778 if (mask & (D_EMERG | D_ERROR))
779 prefix = "LustreError";
781 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
782 source_pid, source_nid, file, line, fn);
784 remain = sizeof(buf) - nob;
786 nob += vsnprintf(&buf[nob], remain, format1, args);
789 remain = sizeof(buf) - nob;
790 if ((format2) && (remain > 0)) {
791 va_start(ap, format2);
792 nob += vsnprintf(&buf[nob], remain, format2, ap);
796 #ifdef HAVE_CATAMOUNT_DATA_H
798 /* check rate limit for console */
800 if (libcfs_console_ratelimit &&
801 cdls->cdls_next != 0 && /* not first time ever */
802 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
804 /* skipping a console message */
809 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
810 libcfs_console_max_delay +
811 cfs_time_seconds(10))) {
812 /* last timeout was a long time ago */
813 cdls->cdls_delay /= libcfs_console_backoff * 4;
815 cdls->cdls_delay *= libcfs_console_backoff;
817 if (cdls->cdls_delay <
818 libcfs_console_min_delay)
820 libcfs_console_min_delay;
821 else if (cdls->cdls_delay >
822 libcfs_console_max_delay)
824 libcfs_console_max_delay;
827 /* ensure cdls_next is never zero after it's been seen */
828 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
831 if (cdls != NULL && cdls->cdls_count != 0) {
834 nob = snprintf(buf2, sizeof(buf2),
835 "Skipped %d previous similar message%s\n",
836 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
838 catamount_printline(buf2, nob);
839 cdls->cdls_count = 0;
842 catamount_printline(buf, nob);
845 /* return on toconsole > 1, as we don't want the user getting
846 * spammed by the debug data */
850 if (debug_file_fd == NULL)
853 gettimeofday(&tv, NULL);
855 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
856 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
857 file, line, fn, buf);
863 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
866 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
867 "ASSERTION(%s) failed\n", expr);
871 #endif /* __KERNEL__ */