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;
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_panic_on_lbug = 0;
109 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
110 "Lustre kernel panic on LBUG");
111 EXPORT_SYMBOL(libcfs_panic_on_lbug);
113 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
114 EXPORT_SYMBOL(libcfs_kmemory);
116 static cfs_waitq_t debug_ctlwq;
118 #ifdef HAVE_BGL_SUPPORT
119 char debug_file_path_arr[1024] = "/bgl/ion/tmp/lustre-log";
120 #elif defined(__arch_um__)
121 char debug_file_path_arr[1024] = "/r/tmp/lustre-log";
123 char debug_file_path_arr[1024] = "/tmp/lustre-log";
125 /* We need to pass a pointer here, but elsewhere this must be a const */
126 static char *debug_file_path = &debug_file_path_arr[0];
127 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
128 "Path for dumping debug logs, "
129 "set 'NONE' to prevent log dumping");
131 int libcfs_panic_in_progress;
133 /* libcfs_debug_token2mask() expects the returned
134 * string in lower-case */
136 libcfs_debug_subsys2str(int subsys)
192 /* libcfs_debug_token2mask() expects the returned
193 * string in lower-case */
195 libcfs_debug_dbg2str(int debug)
260 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
262 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
263 libcfs_debug_dbg2str;
269 if (mask == 0) { /* "0" */
273 } else { /* space-separated tokens */
274 for (i = 0; i < 32; i++) {
277 if ((mask & bit) == 0)
281 if (token == NULL) /* unused bit */
284 if (len > 0) { /* separator? */
290 while (*token != 0) {
299 /* terminate 'str' */
309 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
311 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
312 libcfs_debug_dbg2str;
318 /* match against known tokens */
319 for (i = 0; i < 32; i++) {
323 if (token == NULL) /* unused? */
328 if (j == len) { /* end of token */
339 if (str[j] == token[j])
342 if (str[j] < 'A' || 'Z' < str[j])
345 if (str[j] - 'A' + 'a' != token[j])
350 return -EINVAL; /* no match */
354 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
362 /* Allow a number for backwards compatibility */
364 for (n = strlen(str); n > 0; n--)
365 if (!isspace(str[n-1]))
369 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
375 /* <str> must be a list of debug tokens or numbers separated by
376 * whitespace and optionally an operator ('+' or '-'). If an operator
377 * appears first in <str>, '*mask' is used as the starting point
378 * (relative), otherwise 0 is used (absolute). An operator applies to
379 * all following tokens up to the next operator. */
383 while (isspace(*str)) /* skip whitespace */
389 if (*str == '+' || *str == '-') {
392 /* op on first token == relative */
396 while (isspace(*str)) /* skip whitespace */
399 if (*str == 0) /* trailing op */
403 /* find token length */
404 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
407 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
427 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
429 void libcfs_debug_dumplog_internal(void *arg)
431 CFS_DECL_JOURNAL_DATA;
435 if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
436 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
437 "%s.%ld.%ld", debug_file_path_arr,
438 cfs_time_current_sec(), (long)arg);
439 printk(KERN_ALERT "LustreError: dumping log to %s\n",
441 tracefile_dump_all_pages(debug_file_name);
442 libcfs_run_debug_log_upcall(debug_file_name);
447 int libcfs_debug_dumplog_thread(void *arg)
450 libcfs_debug_dumplog_internal(arg);
451 cfs_waitq_signal(&debug_ctlwq);
455 void libcfs_debug_dumplog(void)
461 /* we're being careful to ensure that the kernel thread is
462 * able to set our state to running as it exits before we
463 * get to schedule() */
464 cfs_waitlink_init(&wait);
465 set_current_state(TASK_INTERRUPTIBLE);
466 cfs_waitq_add(&debug_ctlwq, &wait);
468 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
469 (void *)(long)cfs_curproc_pid(),
470 CLONE_VM | CLONE_FS | CLONE_FILES);
472 printk(KERN_ERR "LustreError: cannot start log dump thread: "
475 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
477 /* be sure to teardown if kernel_thread() failed */
478 cfs_waitq_del(&debug_ctlwq, &wait);
479 set_current_state(TASK_RUNNING);
482 int libcfs_debug_init(unsigned long bufsize)
485 int max = libcfs_debug_mb;
487 cfs_waitq_init(&debug_ctlwq);
488 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
489 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
490 /* If libcfs_debug_mb is set to an invalid value or uninitialized
491 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
492 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
495 max = (max / num_possible_cpus());
496 max = (max << (20 - CFS_PAGE_SHIFT));
498 rc = tracefile_init(max);
501 libcfs_register_panic_notifier();
506 int libcfs_debug_cleanup(void)
508 libcfs_unregister_panic_notifier();
513 int libcfs_debug_clear_buffer(void)
519 /* Debug markers, although printed by S_LNET
520 * should not be be marked as such. */
521 #undef DEBUG_SUBSYSTEM
522 #define DEBUG_SUBSYSTEM S_UNDEFINED
523 int libcfs_debug_mark_buffer(const char *text)
525 CDEBUG(D_TRACE,"***************************************************\n");
526 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
527 CDEBUG(D_TRACE,"***************************************************\n");
531 #undef DEBUG_SUBSYSTEM
532 #define DEBUG_SUBSYSTEM S_LNET
534 void libcfs_debug_set_level(unsigned int debug_level)
536 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
538 libcfs_debug = debug_level;
541 EXPORT_SYMBOL(libcfs_debug_dumplog);
542 EXPORT_SYMBOL(libcfs_debug_set_level);
545 #else /* !__KERNEL__ */
547 #include <libcfs/libcfs.h>
549 #ifdef HAVE_CATAMOUNT_DATA_H
550 #include <catamount/data.h>
551 #include <catamount/lputs.h>
553 static char source_nid[16];
554 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
555 static int toconsole = 1;
556 unsigned int libcfs_console_ratelimit = 1;
557 cfs_duration_t libcfs_console_max_delay;
558 cfs_duration_t libcfs_console_min_delay;
559 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
560 #else /* !HAVE_CATAMOUNT_DATA_H */
562 #include <sys/utsname.h>
563 #endif /* HAVE_NETDB_H */
564 struct utsname *tmp_utsname;
565 static char source_nid[sizeof(tmp_utsname->nodename)];
566 #endif /* HAVE_CATAMOUNT_DATA_H */
568 static int source_pid;
569 int smp_processor_id = 1;
570 char debug_file_path[1024];
573 int portals_do_debug_dumplog(void *arg)
575 printf("Look in %s\n", debug_file_name);
580 void portals_debug_print(void)
586 void libcfs_debug_dumplog(void)
588 printf("Look in %s\n", debug_file_name);
592 int libcfs_debug_init(unsigned long bufsize)
594 char *debug_mask = NULL;
595 char *debug_subsys = NULL;
596 char *debug_filename;
598 #ifdef HAVE_CATAMOUNT_DATA_H
599 char *debug_console = NULL;
600 char *debug_ratelimit = NULL;
601 char *debug_max_delay = NULL;
602 char *debug_min_delay = NULL;
603 char *debug_backoff = NULL;
605 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
606 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
608 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
609 source_pid = _my_pid;
611 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
612 if (debug_console != NULL) {
613 toconsole = strtoul(debug_console, NULL, 0);
614 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
616 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
617 if (debug_ratelimit != NULL) {
618 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
619 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
620 libcfs_console_ratelimit);
622 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
623 if (debug_max_delay != NULL)
624 libcfs_console_max_delay =
625 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
626 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
627 if (debug_min_delay != NULL)
628 libcfs_console_min_delay =
629 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
630 if (debug_min_delay || debug_max_delay) {
631 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
632 libcfs_console_max_delay < libcfs_console_min_delay) {
633 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
634 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
635 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
636 "should be greater than "
637 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
638 "and both parameters should be non-null"
639 ": restore default values\n");
641 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
642 " and min delay to %lus\n",
644 cfs_duration_sec(libcfs_console_max_delay),
646 cfs_duration_sec(libcfs_console_min_delay));
649 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
650 if (debug_backoff != NULL) {
651 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
652 if (libcfs_console_backoff <= 0) {
653 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
654 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
655 "restore default value\n");
657 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
658 libcfs_console_backoff);
662 struct utsname myname;
664 if (uname(&myname) == 0)
665 strcpy(source_nid, myname.nodename);
666 source_pid = getpid();
669 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
671 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
673 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
675 libcfs_subsystem_debug =
676 (unsigned int) strtol(debug_subsys, NULL, 0);
678 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
680 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
682 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
684 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
686 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
687 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
688 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
690 if (strcmp(debug_file_name, "stdout") == 0 ||
691 strcmp(debug_file_name, "-") == 0) {
692 debug_file_fd = stdout;
693 } else if (strcmp(debug_file_name, "stderr") == 0) {
694 debug_file_fd = stderr;
695 } else if (debug_file_name[0] != '\0') {
696 debug_file_fd = fopen(debug_file_name, "w");
697 if (debug_file_fd == NULL)
698 fprintf(stderr, "%s: unable to open '%s': %s\n",
699 source_nid, debug_file_name, strerror(errno));
702 if (debug_file_fd == NULL)
703 debug_file_fd = stdout;
708 int libcfs_debug_cleanup(void)
710 if (debug_file_fd != stdout && debug_file_fd != stderr)
711 fclose(debug_file_fd);
715 int libcfs_debug_clear_buffer(void)
720 int libcfs_debug_mark_buffer(char *text)
723 fprintf(debug_file_fd, "*******************************************************************************\n");
724 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
725 fprintf(debug_file_fd, "*******************************************************************************\n");
730 #ifdef HAVE_CATAMOUNT_DATA_H
731 #define CATAMOUNT_MAXLINE (256-4)
732 void catamount_printline(char *buf, size_t size)
739 pos += CATAMOUNT_MAXLINE;
740 prsize -= CATAMOUNT_MAXLINE;
746 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
747 int subsys, int mask,
748 const char *file, const char *fn, const int line,
749 const char *format1, va_list args,
750 const char *format2, ...)
756 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
757 * with linux, where message can`t
758 * be exceed PAGE_SIZE */
760 char *prefix = "Lustre";
762 #ifdef HAVE_CATAMOUNT_DATA_H
763 /* toconsole == 0 - all messages to debug_file_fd
764 * toconsole == 1 - warnings to console, all to debug_file_fd
765 * toconsole > 1 - all debug to console */
766 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
770 if ((!console) && (!debug_file_fd)) {
774 if (mask & (D_EMERG | D_ERROR))
775 prefix = "LustreError";
777 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
778 source_pid, source_nid, file, line, fn);
780 remain = sizeof(buf) - nob;
782 nob += vsnprintf(&buf[nob], remain, format1, args);
785 remain = sizeof(buf) - nob;
786 if ((format2) && (remain > 0)) {
787 va_start(ap, format2);
788 nob += vsnprintf(&buf[nob], remain, format2, ap);
792 #ifdef HAVE_CATAMOUNT_DATA_H
794 /* check rate limit for console */
796 if (libcfs_console_ratelimit &&
797 cdls->cdls_next != 0 && /* not first time ever */
798 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
800 /* skipping a console message */
805 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
806 libcfs_console_max_delay +
807 cfs_time_seconds(10))) {
808 /* last timeout was a long time ago */
809 cdls->cdls_delay /= libcfs_console_backoff * 4;
811 cdls->cdls_delay *= libcfs_console_backoff;
813 if (cdls->cdls_delay <
814 libcfs_console_min_delay)
816 libcfs_console_min_delay;
817 else if (cdls->cdls_delay >
818 libcfs_console_max_delay)
820 libcfs_console_max_delay;
823 /* ensure cdls_next is never zero after it's been seen */
824 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
827 if (cdls != NULL && cdls->cdls_count != 0) {
830 nob = snprintf(buf2, sizeof(buf2),
831 "Skipped %d previous similar message%s\n",
832 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
834 catamount_printline(buf2, nob);
835 cdls->cdls_count = 0;
838 catamount_printline(buf, nob);
841 /* return on toconsole > 1, as we don't want the user getting
842 * spammed by the debug data */
846 if (debug_file_fd == NULL)
849 gettimeofday(&tv, NULL);
851 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
852 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
853 file, line, fn, buf);
859 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
862 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
863 "ASSERTION(%s) failed\n", expr);
867 #endif /* __KERNEL__ */