1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
5 * Author: Phil Schwan <phil@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # define EXPORT_SYMTAB
27 # define DEBUG_SUBSYSTEM S_LNET
30 #include <libcfs/libcfs.h>
31 #include "tracefile.h"
33 static char debug_file_name[1024];
36 unsigned int libcfs_subsystem_debug = ~0;
37 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
38 "Lustre kernel debug subsystem mask");
39 EXPORT_SYMBOL(libcfs_subsystem_debug);
41 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
42 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
43 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
44 "Lustre kernel debug mask");
45 EXPORT_SYMBOL(libcfs_debug);
47 int libcfs_debug_mb = -1;
48 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
49 "Total debug buffer size.");
50 EXPORT_SYMBOL(libcfs_debug_mb);
52 unsigned int libcfs_printk = D_CANTMASK;
53 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
54 "Lustre kernel debug console mask");
55 EXPORT_SYMBOL(libcfs_printk);
57 unsigned int libcfs_console_ratelimit = 1;
58 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
59 "Lustre kernel debug console ratelimit (0 to disable)");
60 EXPORT_SYMBOL(libcfs_console_ratelimit);
62 cfs_duration_t libcfs_console_max_delay;
63 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
64 "Lustre kernel debug console max delay (jiffies)");
65 EXPORT_SYMBOL(libcfs_console_max_delay);
67 cfs_duration_t libcfs_console_min_delay;
68 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
69 "Lustre kernel debug console min delay (jiffies)");
70 EXPORT_SYMBOL(libcfs_console_min_delay);
72 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
73 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
74 "Lustre kernel debug console backoff factor");
75 EXPORT_SYMBOL(libcfs_console_backoff);
77 unsigned int libcfs_debug_binary = 1;
78 EXPORT_SYMBOL(libcfs_debug_binary);
80 unsigned int libcfs_stack;
81 EXPORT_SYMBOL(libcfs_stack);
83 unsigned int portal_enter_debugger;
84 EXPORT_SYMBOL(portal_enter_debugger);
86 unsigned int libcfs_catastrophe;
87 EXPORT_SYMBOL(libcfs_catastrophe);
89 unsigned int libcfs_panic_on_lbug = 0;
90 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
91 "Lustre kernel panic on LBUG");
92 EXPORT_SYMBOL(libcfs_panic_on_lbug);
94 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
95 EXPORT_SYMBOL(libcfs_kmemory);
97 static cfs_waitq_t debug_ctlwq;
100 char debug_file_path[1024] = "/r/tmp/lustre-log";
102 char debug_file_path[1024] = "/tmp/lustre-log";
104 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
105 "Path for dumping debug logs, "
106 "set 'NONE' to prevent log dumping");
108 int libcfs_panic_in_progress;
110 /* libcfs_debug_token2mask() expects the returned
111 * string in lower-case */
113 libcfs_debug_subsys2str(int subsys)
167 /* libcfs_debug_token2mask() expects the returned
168 * string in lower-case */
170 libcfs_debug_dbg2str(int debug)
235 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
237 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
238 libcfs_debug_dbg2str;
244 if (mask == 0) { /* "0" */
248 } else { /* space-separated tokens */
249 for (i = 0; i < 32; i++) {
252 if ((mask & bit) == 0)
256 if (token == NULL) /* unused bit */
259 if (len > 0) { /* separator? */
265 while (*token != 0) {
274 /* terminate 'str' */
284 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
286 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
287 libcfs_debug_dbg2str;
293 /* match against known tokens */
294 for (i = 0; i < 32; i++) {
298 if (token == NULL) /* unused? */
303 if (j == len) { /* end of token */
314 if (str[j] == token[j])
317 if (str[j] < 'A' || 'Z' < str[j])
320 if (str[j] - 'A' + 'a' != token[j])
325 return -EINVAL; /* no match */
329 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
337 /* Allow a number for backwards compatibility */
339 for (n = strlen(str); n > 0; n--)
340 if (!isspace(str[n-1]))
344 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
350 /* <str> must be a list of debug tokens or numbers separated by
351 * whitespace and optionally an operator ('+' or '-'). If an operator
352 * appears first in <str>, '*mask' is used as the starting point
353 * (relative), otherwise 0 is used (absolute). An operator applies to
354 * all following tokens up to the next operator. */
358 while (isspace(*str)) /* skip whitespace */
364 if (*str == '+' || *str == '-') {
367 /* op on first token == relative */
371 while (isspace(*str)) /* skip whitespace */
374 if (*str == 0) /* trailing op */
378 /* find token length */
379 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
382 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
401 void libcfs_debug_dumplog_internal(void *arg)
403 CFS_DECL_JOURNAL_DATA;
407 if (strncmp(debug_file_path, "NONE", 4) != 0) {
408 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
409 "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(),
411 printk(KERN_ALERT "LustreError: dumping log to %s\n",
413 tracefile_dump_all_pages(debug_file_name);
418 int libcfs_debug_dumplog_thread(void *arg)
421 libcfs_debug_dumplog_internal(arg);
422 cfs_waitq_signal(&debug_ctlwq);
426 void libcfs_debug_dumplog(void)
432 /* we're being careful to ensure that the kernel thread is
433 * able to set our state to running as it exits before we
434 * get to schedule() */
435 cfs_waitlink_init(&wait);
436 set_current_state(TASK_INTERRUPTIBLE);
437 cfs_waitq_add(&debug_ctlwq, &wait);
439 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
440 (void *)(long)cfs_curproc_pid(),
441 CLONE_VM | CLONE_FS | CLONE_FILES);
443 printk(KERN_ERR "LustreError: cannot start log dump thread: "
446 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
448 /* be sure to teardown if kernel_thread() failed */
449 cfs_waitq_del(&debug_ctlwq, &wait);
450 set_current_state(TASK_RUNNING);
453 int libcfs_debug_init(unsigned long bufsize)
456 int max = libcfs_debug_mb;
458 cfs_waitq_init(&debug_ctlwq);
459 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
460 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
461 /* If libcfs_debug_mb is set to an invalid value or uninitialized
462 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
463 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
466 max = (max / num_possible_cpus());
467 max = (max << (20 - CFS_PAGE_SHIFT));
469 rc = tracefile_init(max);
472 libcfs_register_panic_notifier();
477 int libcfs_debug_cleanup(void)
479 libcfs_unregister_panic_notifier();
484 int libcfs_debug_clear_buffer(void)
490 /* Debug markers, although printed by S_LNET
491 * should not be be marked as such. */
492 #undef DEBUG_SUBSYSTEM
493 #define DEBUG_SUBSYSTEM S_UNDEFINED
494 int libcfs_debug_mark_buffer(char *text)
496 CDEBUG(D_TRACE,"***************************************************\n");
497 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
498 CDEBUG(D_TRACE,"***************************************************\n");
502 #undef DEBUG_SUBSYSTEM
503 #define DEBUG_SUBSYSTEM S_LNET
505 void libcfs_debug_set_level(unsigned int debug_level)
507 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
509 libcfs_debug = debug_level;
512 EXPORT_SYMBOL(libcfs_debug_dumplog);
513 EXPORT_SYMBOL(libcfs_debug_set_level);
516 #else /* !__KERNEL__ */
518 #include <libcfs/libcfs.h>
520 #ifdef HAVE_CATAMOUNT_DATA_H
521 #include <catamount/data.h>
522 #include <catamount/lputs.h>
524 static char source_nid[16];
525 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
526 static int toconsole = 1;
527 unsigned int libcfs_console_ratelimit = 1;
528 cfs_duration_t libcfs_console_max_delay;
529 cfs_duration_t libcfs_console_min_delay;
530 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
531 #else /* !HAVE_CATAMOUNT_DATA_H */
533 #include <sys/utsname.h>
534 #endif /* HAVE_NETDB_H */
535 struct utsname *tmp_utsname;
536 static char source_nid[sizeof(tmp_utsname->nodename)];
537 #endif /* HAVE_CATAMOUNT_DATA_H */
539 static int source_pid;
540 int smp_processor_id = 1;
541 char debug_file_path[1024];
544 int portals_do_debug_dumplog(void *arg)
546 printf("Look in %s\n", debug_file_name);
551 void portals_debug_print(void)
557 void libcfs_debug_dumplog(void)
559 printf("Look in %s\n", debug_file_name);
563 int libcfs_debug_init(unsigned long bufsize)
565 char *debug_mask = NULL;
566 char *debug_subsys = NULL;
567 char *debug_filename;
569 #ifdef HAVE_CATAMOUNT_DATA_H
570 char *debug_console = NULL;
571 char *debug_ratelimit = NULL;
572 char *debug_max_delay = NULL;
573 char *debug_min_delay = NULL;
574 char *debug_backoff = NULL;
576 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
577 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
579 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
580 source_pid = _my_pid;
582 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
583 if (debug_console != NULL) {
584 toconsole = strtoul(debug_console, NULL, 0);
585 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
587 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
588 if (debug_ratelimit != NULL) {
589 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
590 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
591 libcfs_console_ratelimit);
593 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
594 if (debug_max_delay != NULL)
595 libcfs_console_max_delay =
596 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
597 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
598 if (debug_min_delay != NULL)
599 libcfs_console_min_delay =
600 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
601 if (debug_min_delay || debug_max_delay) {
602 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
603 libcfs_console_max_delay < libcfs_console_min_delay) {
604 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
605 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
606 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
607 "should be greater than "
608 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
609 "and both parameters should be non-null"
610 ": restore default values\n");
612 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
613 " and min delay to %lus\n",
615 cfs_duration_sec(libcfs_console_max_delay),
617 cfs_duration_sec(libcfs_console_min_delay));
620 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
621 if (debug_backoff != NULL) {
622 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
623 if (libcfs_console_backoff <= 0) {
624 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
625 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
626 "restore default value\n");
628 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
629 libcfs_console_backoff);
633 struct utsname myname;
635 if (uname(&myname) == 0)
636 strcpy(source_nid, myname.nodename);
637 source_pid = getpid();
640 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
642 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
644 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
646 libcfs_subsystem_debug =
647 (unsigned int) strtol(debug_subsys, NULL, 0);
649 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
651 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
653 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
655 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
657 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
658 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
659 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
661 if (strcmp(debug_file_name, "stdout") == 0 ||
662 strcmp(debug_file_name, "-") == 0) {
663 debug_file_fd = stdout;
664 } else if (strcmp(debug_file_name, "stderr") == 0) {
665 debug_file_fd = stderr;
666 } else if (debug_file_name[0] != '\0') {
667 debug_file_fd = fopen(debug_file_name, "w");
668 if (debug_file_fd == NULL)
669 fprintf(stderr, "%s: unable to open '%s': %s\n",
670 source_nid, debug_file_name, strerror(errno));
673 if (debug_file_fd == NULL)
674 debug_file_fd = stdout;
679 int libcfs_debug_cleanup(void)
681 if (debug_file_fd != stdout && debug_file_fd != stderr)
682 fclose(debug_file_fd);
686 int libcfs_debug_clear_buffer(void)
691 int libcfs_debug_mark_buffer(char *text)
694 fprintf(debug_file_fd, "*******************************************************************************\n");
695 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
696 fprintf(debug_file_fd, "*******************************************************************************\n");
701 #ifdef HAVE_CATAMOUNT_DATA_H
702 #define CATAMOUNT_MAXLINE (256-4)
703 void catamount_printline(char *buf, size_t size)
710 pos += CATAMOUNT_MAXLINE;
711 prsize -= CATAMOUNT_MAXLINE;
717 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
718 int subsys, int mask,
719 const char *file, const char *fn, const int line,
720 const char *format1, va_list args,
721 const char *format2, ...)
727 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
728 * with linux, where message can`t
729 * be exceed PAGE_SIZE */
731 char *prefix = "Lustre";
733 #ifdef HAVE_CATAMOUNT_DATA_H
734 /* toconsole == 0 - all messages to debug_file_fd
735 * toconsole == 1 - warnings to console, all to debug_file_fd
736 * toconsole > 1 - all debug to console */
737 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
741 if ((!console) && (!debug_file_fd)) {
745 if (mask & (D_EMERG | D_ERROR))
746 prefix = "LustreError";
748 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
749 source_pid, source_nid, file, line, fn);
751 remain = sizeof(buf) - nob;
753 nob += vsnprintf(&buf[nob], remain, format1, args);
756 remain = sizeof(buf) - nob;
757 if ((format2) && (remain > 0)) {
758 va_start(ap, format2);
759 nob += vsnprintf(&buf[nob], remain, format2, ap);
763 #ifdef HAVE_CATAMOUNT_DATA_H
765 /* check rate limit for console */
767 if (libcfs_console_ratelimit &&
768 cdls->cdls_next != 0 && /* not first time ever */
769 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
771 /* skipping a console message */
776 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
777 libcfs_console_max_delay +
778 cfs_time_seconds(10))) {
779 /* last timeout was a long time ago */
780 cdls->cdls_delay /= libcfs_console_backoff * 4;
782 cdls->cdls_delay *= libcfs_console_backoff;
784 if (cdls->cdls_delay <
785 libcfs_console_min_delay)
787 libcfs_console_min_delay;
788 else if (cdls->cdls_delay >
789 libcfs_console_max_delay)
791 libcfs_console_max_delay;
794 /* ensure cdls_next is never zero after it's been seen */
795 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
798 if (cdls != NULL && cdls->cdls_count != 0) {
801 nob = snprintf(buf2, sizeof(buf2),
802 "Skipped %d previous similar message%s\n",
803 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
805 catamount_printline(buf2, nob);
806 cdls->cdls_count = 0;
809 catamount_printline(buf, nob);
812 /* return on toconsole > 1, as we don't want the user getting
813 * spammed by the debug data */
817 if (debug_file_fd == NULL)
820 gettimeofday(&tv, NULL);
822 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
823 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
824 file, line, fn, buf);
830 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
833 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
834 "ASSERTION(%s) failed\n", expr);
838 #endif /* __KERNEL__ */