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 (c) 2003, 2010, Oracle and/or its affiliates. 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 unsigned int libcfs_debug_mb = 0;
67 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
68 "Total debug buffer size.");
69 EXPORT_SYMBOL(libcfs_debug_mb);
71 unsigned int libcfs_printk = (D_CANTMASK);
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)
453 libcfs_debug_dumplog_internal(arg);
454 cfs_waitq_signal(&debug_ctlwq);
458 void libcfs_debug_dumplog(void)
464 /* we're being careful to ensure that the kernel thread is
465 * able to set our state to running as it exits before we
466 * get to schedule() */
467 cfs_waitlink_init(&wait);
468 set_current_state(TASK_INTERRUPTIBLE);
469 cfs_waitq_add(&debug_ctlwq, &wait);
471 dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
472 (void*)(long)cfs_curproc_pid(),
473 "libcfs_debug_dumper");
475 printk(KERN_ERR "LustreError: cannot start log dump thread: "
476 "%ld\n", PTR_ERR(dumper));
478 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
480 /* be sure to teardown if kernel_thread() failed */
481 cfs_waitq_del(&debug_ctlwq, &wait);
482 set_current_state(TASK_RUNNING);
485 int libcfs_debug_init(unsigned long bufsize)
488 unsigned int max = libcfs_debug_mb;
490 cfs_waitq_init(&debug_ctlwq);
492 if (libcfs_console_max_delay <= 0 || /* not set by user or */
493 libcfs_console_min_delay <= 0 || /* set to invalid values */
494 libcfs_console_min_delay >= libcfs_console_max_delay) {
495 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
496 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
499 /* If libcfs_debug_mb is set to an invalid value or uninitialized
500 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
501 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
504 max = (max / num_possible_cpus());
505 max = (max << (20 - CFS_PAGE_SHIFT));
507 rc = tracefile_init(max);
510 libcfs_register_panic_notifier();
515 int libcfs_debug_cleanup(void)
517 libcfs_unregister_panic_notifier();
522 int libcfs_debug_clear_buffer(void)
528 /* Debug markers, although printed by S_LNET
529 * should not be be marked as such. */
530 #undef DEBUG_SUBSYSTEM
531 #define DEBUG_SUBSYSTEM S_UNDEFINED
532 int libcfs_debug_mark_buffer(const char *text)
534 CDEBUG(D_TRACE,"***************************************************\n");
535 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
536 CDEBUG(D_TRACE,"***************************************************\n");
540 #undef DEBUG_SUBSYSTEM
541 #define DEBUG_SUBSYSTEM S_LNET
543 void libcfs_debug_set_level(unsigned int debug_level)
545 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
547 libcfs_debug = debug_level;
550 EXPORT_SYMBOL(libcfs_debug_dumplog);
551 EXPORT_SYMBOL(libcfs_debug_set_level);
554 #else /* !__KERNEL__ */
556 #include <libcfs/libcfs.h>
558 #ifdef HAVE_CATAMOUNT_DATA_H
559 #include <catamount/data.h>
560 #include <catamount/lputs.h>
562 static char source_nid[16];
563 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
564 static int toconsole = 1;
565 unsigned int libcfs_console_ratelimit = 1;
566 cfs_duration_t libcfs_console_max_delay;
567 cfs_duration_t libcfs_console_min_delay;
568 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
569 #else /* !HAVE_CATAMOUNT_DATA_H */
571 #include <sys/utsname.h>
572 #endif /* HAVE_NETDB_H */
573 struct utsname *tmp_utsname;
574 static char source_nid[sizeof(tmp_utsname->nodename)];
575 #endif /* HAVE_CATAMOUNT_DATA_H */
577 static int source_pid;
578 int smp_processor_id = 1;
579 char debug_file_path[1024];
582 int portals_do_debug_dumplog(void *arg)
584 printf("Look in %s\n", debug_file_name);
589 void portals_debug_print(void)
595 void libcfs_debug_dumplog(void)
597 printf("Look in %s\n", debug_file_name);
601 int libcfs_debug_init(unsigned long bufsize)
603 char *debug_mask = NULL;
604 char *debug_subsys = NULL;
605 char *debug_filename;
607 #ifdef HAVE_CATAMOUNT_DATA_H
608 char *debug_console = NULL;
609 char *debug_ratelimit = NULL;
610 char *debug_max_delay = NULL;
611 char *debug_min_delay = NULL;
612 char *debug_backoff = NULL;
614 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
615 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
617 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
618 source_pid = _my_pid;
620 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
621 if (debug_console != NULL) {
622 toconsole = strtoul(debug_console, NULL, 0);
623 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
625 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
626 if (debug_ratelimit != NULL) {
627 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
628 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
629 libcfs_console_ratelimit);
631 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
632 if (debug_max_delay != NULL)
633 libcfs_console_max_delay =
634 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
635 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
636 if (debug_min_delay != NULL)
637 libcfs_console_min_delay =
638 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
639 if (debug_min_delay || debug_max_delay) {
640 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
641 libcfs_console_max_delay < libcfs_console_min_delay) {
642 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
643 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
644 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
645 "should be greater than "
646 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
647 "and both parameters should be non-null"
648 ": restore default values\n");
650 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
651 " and min delay to %lus\n",
653 cfs_duration_sec(libcfs_console_max_delay),
655 cfs_duration_sec(libcfs_console_min_delay));
658 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
659 if (debug_backoff != NULL) {
660 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
661 if (libcfs_console_backoff <= 0) {
662 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
663 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
664 "restore default value\n");
666 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
667 libcfs_console_backoff);
671 struct utsname myname;
673 if (uname(&myname) == 0)
674 strcpy(source_nid, myname.nodename);
675 source_pid = getpid();
678 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
680 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
682 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
684 libcfs_subsystem_debug =
685 (unsigned int) strtol(debug_subsys, NULL, 0);
687 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
689 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
691 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
693 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
695 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
696 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
697 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
699 if (strcmp(debug_file_name, "stdout") == 0 ||
700 strcmp(debug_file_name, "-") == 0) {
701 debug_file_fd = stdout;
702 } else if (strcmp(debug_file_name, "stderr") == 0) {
703 debug_file_fd = stderr;
704 } else if (debug_file_name[0] != '\0') {
705 debug_file_fd = fopen(debug_file_name, "w");
706 if (debug_file_fd == NULL)
707 fprintf(stderr, "%s: unable to open '%s': %s\n",
708 source_nid, debug_file_name, strerror(errno));
711 if (debug_file_fd == NULL)
712 debug_file_fd = stdout;
717 int libcfs_debug_cleanup(void)
719 if (debug_file_fd != stdout && debug_file_fd != stderr)
720 fclose(debug_file_fd);
724 int libcfs_debug_clear_buffer(void)
729 int libcfs_debug_mark_buffer(char *text)
732 fprintf(debug_file_fd, "*******************************************************************************\n");
733 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
734 fprintf(debug_file_fd, "*******************************************************************************\n");
739 #ifdef HAVE_CATAMOUNT_DATA_H
740 #define CATAMOUNT_MAXLINE (256-4)
741 void catamount_printline(char *buf, size_t size)
748 pos += CATAMOUNT_MAXLINE;
749 prsize -= CATAMOUNT_MAXLINE;
755 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
756 int subsys, int mask,
757 const char *file, const char *fn, const int line,
758 const char *format1, va_list args,
759 const char *format2, ...)
765 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
766 * with linux, where message can`t
767 * be exceed PAGE_SIZE */
769 char *prefix = "Lustre";
771 #ifdef HAVE_CATAMOUNT_DATA_H
772 /* toconsole == 0 - all messages to debug_file_fd
773 * toconsole == 1 - warnings to console, all to debug_file_fd
774 * toconsole > 1 - all debug to console */
775 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
779 if ((!console) && (!debug_file_fd)) {
783 if (mask & (D_EMERG | D_ERROR))
784 prefix = "LustreError";
786 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
787 source_pid, source_nid, file, line, fn);
789 remain = sizeof(buf) - nob;
791 nob += vsnprintf(&buf[nob], remain, format1, args);
794 remain = sizeof(buf) - nob;
795 if ((format2) && (remain > 0)) {
796 va_start(ap, format2);
797 nob += vsnprintf(&buf[nob], remain, format2, ap);
801 #ifdef HAVE_CATAMOUNT_DATA_H
803 /* check rate limit for console */
805 if (libcfs_console_ratelimit &&
806 cdls->cdls_next != 0 && /* not first time ever */
807 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
809 /* skipping a console message */
814 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
815 libcfs_console_max_delay +
816 cfs_time_seconds(10))) {
817 /* last timeout was a long time ago */
818 cdls->cdls_delay /= libcfs_console_backoff * 4;
820 cdls->cdls_delay *= libcfs_console_backoff;
822 if (cdls->cdls_delay <
823 libcfs_console_min_delay)
825 libcfs_console_min_delay;
826 else if (cdls->cdls_delay >
827 libcfs_console_max_delay)
829 libcfs_console_max_delay;
832 /* ensure cdls_next is never zero after it's been seen */
833 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
836 if (cdls != NULL && cdls->cdls_count != 0) {
839 nob = snprintf(buf2, sizeof(buf2),
840 "Skipped %d previous similar message%s\n",
841 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
843 catamount_printline(buf2, nob);
844 cdls->cdls_count = 0;
847 catamount_printline(buf, nob);
850 /* return on toconsole > 1, as we don't want the user getting
851 * spammed by the debug data */
855 if (debug_file_fd == NULL)
858 gettimeofday(&tv, NULL);
860 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
861 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
862 file, line, fn, buf);
868 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
871 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
872 "ASSERTION(%s) failed\n", expr);
876 #endif /* __KERNEL__ */