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/kp30.h>
31 #include <libcfs/libcfs.h>
32 #include "tracefile.h"
34 static char debug_file_name[1024];
37 unsigned int libcfs_subsystem_debug = ~0;
38 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
39 "Lustre kernel debug subsystem mask");
40 EXPORT_SYMBOL(libcfs_subsystem_debug);
42 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
43 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
44 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
45 "Lustre kernel debug mask");
46 EXPORT_SYMBOL(libcfs_debug);
48 int libcfs_debug_mb = -1;
49 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
50 "Total debug buffer size.");
51 EXPORT_SYMBOL(libcfs_debug_mb);
53 unsigned int libcfs_printk = D_CANTMASK;
54 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
55 "Lustre kernel debug console mask");
56 EXPORT_SYMBOL(libcfs_printk);
58 unsigned int libcfs_console_ratelimit = 1;
59 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
60 "Lustre kernel debug console ratelimit (0 to disable)");
61 EXPORT_SYMBOL(libcfs_console_ratelimit);
63 cfs_duration_t libcfs_console_max_delay;
64 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
65 "Lustre kernel debug console max delay (jiffies)");
66 EXPORT_SYMBOL(libcfs_console_max_delay);
68 cfs_duration_t libcfs_console_min_delay;
69 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
70 "Lustre kernel debug console min delay (jiffies)");
71 EXPORT_SYMBOL(libcfs_console_min_delay);
73 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
74 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
75 "Lustre kernel debug console backoff factor");
76 EXPORT_SYMBOL(libcfs_console_backoff);
78 unsigned int libcfs_debug_binary = 1;
79 EXPORT_SYMBOL(libcfs_debug_binary);
81 unsigned int libcfs_stack;
82 EXPORT_SYMBOL(libcfs_stack);
84 unsigned int portal_enter_debugger;
85 EXPORT_SYMBOL(portal_enter_debugger);
87 unsigned int libcfs_catastrophe;
88 EXPORT_SYMBOL(libcfs_catastrophe);
90 unsigned int libcfs_panic_on_lbug = 0;
91 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
92 "Lustre kernel panic on LBUG");
93 EXPORT_SYMBOL(libcfs_panic_on_lbug);
95 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
96 EXPORT_SYMBOL(libcfs_kmemory);
98 static cfs_waitq_t debug_ctlwq;
101 char debug_file_path[1024] = "/r/tmp/lustre-log";
103 char debug_file_path[1024] = "/tmp/lustre-log";
105 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
106 "Path for dumping debug logs, "
107 "set 'NONE' to prevent log dumping");
109 int libcfs_panic_in_progress;
111 /* libcfs_debug_token2mask() expects the returned
112 * string in lower-case */
114 libcfs_debug_subsys2str(int subsys)
168 /* libcfs_debug_token2mask() expects the returned
169 * string in lower-case */
171 libcfs_debug_dbg2str(int debug)
236 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
238 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
239 libcfs_debug_dbg2str;
245 if (mask == 0) { /* "0" */
249 } else { /* space-separated tokens */
250 for (i = 0; i < 32; i++) {
253 if ((mask & bit) == 0)
257 if (token == NULL) /* unused bit */
260 if (len > 0) { /* separator? */
266 while (*token != 0) {
275 /* terminate 'str' */
285 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
287 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
288 libcfs_debug_dbg2str;
294 /* match against known tokens */
295 for (i = 0; i < 32; i++) {
299 if (token == NULL) /* unused? */
304 if (j == len) { /* end of token */
315 if (str[j] == token[j])
318 if (str[j] < 'A' || 'Z' < str[j])
321 if (str[j] - 'A' + 'a' != token[j])
326 return -EINVAL; /* no match */
330 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
338 /* Allow a number for backwards compatibility */
340 for (n = strlen(str); n > 0; n--)
341 if (!isspace(str[n-1]))
345 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
351 /* <str> must be a list of debug tokens or numbers separated by
352 * whitespace and optionally an operator ('+' or '-'). If an operator
353 * appears first in <str>, '*mask' is used as the starting point
354 * (relative), otherwise 0 is used (absolute). An operator applies to
355 * all following tokens up to the next operator. */
359 while (isspace(*str)) /* skip whitespace */
365 if (*str == '+' || *str == '-') {
368 /* op on first token == relative */
372 while (isspace(*str)) /* skip whitespace */
375 if (*str == 0) /* trailing op */
379 /* find token length */
380 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
383 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
402 void libcfs_debug_dumplog_internal(void *arg)
404 CFS_DECL_JOURNAL_DATA;
408 if (strncmp(debug_file_path, "NONE", 4) != 0) {
409 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
410 "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(),
412 printk(KERN_ALERT "LustreError: dumping log to %s\n",
414 tracefile_dump_all_pages(debug_file_name);
419 int libcfs_debug_dumplog_thread(void *arg)
422 libcfs_debug_dumplog_internal(arg);
423 cfs_waitq_signal(&debug_ctlwq);
427 void libcfs_debug_dumplog(void)
433 /* we're being careful to ensure that the kernel thread is
434 * able to set our state to running as it exits before we
435 * get to schedule() */
436 cfs_waitlink_init(&wait);
437 set_current_state(TASK_INTERRUPTIBLE);
438 cfs_waitq_add(&debug_ctlwq, &wait);
440 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
441 (void *)(long)cfs_curproc_pid(),
442 CLONE_VM | CLONE_FS | CLONE_FILES);
444 printk(KERN_ERR "LustreError: cannot start log dump thread: "
447 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
449 /* be sure to teardown if kernel_thread() failed */
450 cfs_waitq_del(&debug_ctlwq, &wait);
451 set_current_state(TASK_RUNNING);
454 int libcfs_debug_init(unsigned long bufsize)
457 int max = libcfs_debug_mb;
459 cfs_waitq_init(&debug_ctlwq);
460 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
461 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
462 /* If libcfs_debug_mb is set to an invalid value or uninitialized
463 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
464 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
467 max = (max / num_possible_cpus());
468 max = (max << (20 - CFS_PAGE_SHIFT));
470 rc = tracefile_init(max);
473 libcfs_register_panic_notifier();
478 int libcfs_debug_cleanup(void)
480 libcfs_unregister_panic_notifier();
485 int libcfs_debug_clear_buffer(void)
491 /* Debug markers, although printed by S_LNET
492 * should not be be marked as such. */
493 #undef DEBUG_SUBSYSTEM
494 #define DEBUG_SUBSYSTEM S_UNDEFINED
495 int libcfs_debug_mark_buffer(char *text)
497 CDEBUG(D_TRACE,"***************************************************\n");
498 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
499 CDEBUG(D_TRACE,"***************************************************\n");
503 #undef DEBUG_SUBSYSTEM
504 #define DEBUG_SUBSYSTEM S_LNET
506 void libcfs_debug_set_level(unsigned int debug_level)
508 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
510 libcfs_debug = debug_level;
513 EXPORT_SYMBOL(libcfs_debug_dumplog);
514 EXPORT_SYMBOL(libcfs_debug_set_level);
517 #else /* !__KERNEL__ */
519 #include <libcfs/libcfs.h>
521 #ifdef HAVE_CATAMOUNT_DATA_H
522 #include <catamount/data.h>
523 #include <catamount/lputs.h>
525 static char source_nid[16];
526 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
527 static int toconsole = 1;
528 unsigned int libcfs_console_ratelimit = 1;
529 cfs_duration_t libcfs_console_max_delay;
530 cfs_duration_t libcfs_console_min_delay;
531 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
532 #else /* !HAVE_CATAMOUNT_DATA_H */
534 #include <sys/utsname.h>
535 #endif /* HAVE_NETDB_H */
536 struct utsname *tmp_utsname;
537 static char source_nid[sizeof(tmp_utsname->nodename)];
538 #endif /* HAVE_CATAMOUNT_DATA_H */
540 static int source_pid;
541 int smp_processor_id = 1;
542 char debug_file_path[1024];
545 int portals_do_debug_dumplog(void *arg)
547 printf("Look in %s\n", debug_file_name);
552 void portals_debug_print(void)
558 void libcfs_debug_dumplog(void)
560 printf("Look in %s\n", debug_file_name);
564 int libcfs_debug_init(unsigned long bufsize)
566 char *debug_mask = NULL;
567 char *debug_subsys = NULL;
568 char *debug_filename;
570 #ifdef HAVE_CATAMOUNT_DATA_H
571 char *debug_console = NULL;
572 char *debug_ratelimit = NULL;
573 char *debug_max_delay = NULL;
574 char *debug_min_delay = NULL;
575 char *debug_backoff = NULL;
577 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
578 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
580 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
581 source_pid = _my_pid;
583 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
584 if (debug_console != NULL) {
585 toconsole = strtoul(debug_console, NULL, 0);
586 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
588 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
589 if (debug_ratelimit != NULL) {
590 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
591 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
592 libcfs_console_ratelimit);
594 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
595 if (debug_max_delay != NULL)
596 libcfs_console_max_delay =
597 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
598 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
599 if (debug_min_delay != NULL)
600 libcfs_console_min_delay =
601 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
602 if (debug_min_delay || debug_max_delay) {
603 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
604 libcfs_console_max_delay < libcfs_console_min_delay) {
605 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
606 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
607 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
608 "should be greater than "
609 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
610 "and both parameters should be non-null"
611 ": restore default values\n");
613 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
614 " and min delay to %lus\n",
616 cfs_duration_sec(libcfs_console_max_delay),
618 cfs_duration_sec(libcfs_console_min_delay));
621 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
622 if (debug_backoff != NULL) {
623 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
624 if (libcfs_console_backoff <= 0) {
625 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
626 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
627 "restore default value\n");
629 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
630 libcfs_console_backoff);
634 struct utsname myname;
636 if (uname(&myname) == 0)
637 strcpy(source_nid, myname.nodename);
638 source_pid = getpid();
641 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
643 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
645 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
647 libcfs_subsystem_debug =
648 (unsigned int) strtol(debug_subsys, NULL, 0);
650 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
652 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
654 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
656 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
658 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
659 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
660 "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
662 if (strcmp(debug_file_name, "stdout") == 0 ||
663 strcmp(debug_file_name, "-") == 0) {
664 debug_file_fd = stdout;
665 } else if (strcmp(debug_file_name, "stderr") == 0) {
666 debug_file_fd = stderr;
667 } else if (debug_file_name[0] != '\0') {
668 debug_file_fd = fopen(debug_file_name, "w");
669 if (debug_file_fd == NULL)
670 fprintf(stderr, "%s: unable to open '%s': %s\n",
671 source_nid, debug_file_name, strerror(errno));
674 if (debug_file_fd == NULL)
675 debug_file_fd = stdout;
680 int libcfs_debug_cleanup(void)
682 if (debug_file_fd != stdout && debug_file_fd != stderr)
683 fclose(debug_file_fd);
687 int libcfs_debug_clear_buffer(void)
692 int libcfs_debug_mark_buffer(char *text)
695 fprintf(debug_file_fd, "*******************************************************************************\n");
696 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
697 fprintf(debug_file_fd, "*******************************************************************************\n");
702 #ifdef HAVE_CATAMOUNT_DATA_H
703 #define CATAMOUNT_MAXLINE (256-4)
704 void catamount_printline(char *buf, size_t size)
711 pos += CATAMOUNT_MAXLINE;
712 prsize -= CATAMOUNT_MAXLINE;
718 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
719 int subsys, int mask,
720 const char *file, const char *fn, const int line,
721 const char *format1, va_list args,
722 const char *format2, ...)
728 char buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
729 * with linux, where message can`t
730 * be exceed PAGE_SIZE */
732 char *prefix = "Lustre";
734 #ifdef HAVE_CATAMOUNT_DATA_H
735 /* toconsole == 0 - all messages to debug_file_fd
736 * toconsole == 1 - warnings to console, all to debug_file_fd
737 * toconsole > 1 - all debug to console */
738 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
742 if ((!console) && (!debug_file_fd)) {
746 if (mask & (D_EMERG | D_ERROR))
747 prefix = "LustreError";
749 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
750 source_pid, source_nid, file, line, fn);
752 remain = sizeof(buf) - nob;
754 nob += vsnprintf(&buf[nob], remain, format1, args);
757 remain = sizeof(buf) - nob;
758 if ((format2) && (remain > 0)) {
759 va_start(ap, format2);
760 nob += vsnprintf(&buf[nob], remain, format2, ap);
764 #ifdef HAVE_CATAMOUNT_DATA_H
766 /* check rate limit for console */
768 if (libcfs_console_ratelimit &&
769 cdls->cdls_next != 0 && /* not first time ever */
770 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
772 /* skipping a console message */
777 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
778 libcfs_console_max_delay +
779 cfs_time_seconds(10))) {
780 /* last timeout was a long time ago */
781 cdls->cdls_delay /= libcfs_console_backoff * 4;
783 cdls->cdls_delay *= libcfs_console_backoff;
785 if (cdls->cdls_delay <
786 libcfs_console_min_delay)
788 libcfs_console_min_delay;
789 else if (cdls->cdls_delay >
790 libcfs_console_max_delay)
792 libcfs_console_max_delay;
795 /* ensure cdls_next is never zero after it's been seen */
796 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
799 if (cdls != NULL && cdls->cdls_count != 0) {
802 nob = snprintf(buf2, sizeof(buf2),
803 "Skipped %d previous similar message%s\n",
804 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
806 catamount_printline(buf2, nob);
807 cdls->cdls_count = 0;
810 catamount_printline(buf, nob);
813 /* return on toconsole > 1, as we don't want the user getting
814 * spammed by the debug data */
818 if (debug_file_fd == NULL)
821 gettimeofday(&tv, NULL);
823 fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
824 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
825 file, line, fn, buf);
831 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
834 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
835 "ASSERTION(%s) failed\n", expr);
839 #endif /* __KERNEL__ */