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 EXPORT_SYMBOL(libcfs_subsystem_debug);
40 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
41 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
42 EXPORT_SYMBOL(libcfs_debug);
44 unsigned int libcfs_printk = D_CANTMASK;
45 EXPORT_SYMBOL(libcfs_printk);
47 unsigned int libcfs_console_ratelimit = 1;
48 EXPORT_SYMBOL(libcfs_console_ratelimit);
50 cfs_duration_t libcfs_console_max_delay;
51 EXPORT_SYMBOL(libcfs_console_max_delay);
53 cfs_duration_t libcfs_console_min_delay;
54 EXPORT_SYMBOL(libcfs_console_min_delay);
56 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
57 EXPORT_SYMBOL(libcfs_console_backoff);
59 unsigned int libcfs_debug_binary = 1;
60 EXPORT_SYMBOL(libcfs_debug_binary);
62 unsigned int libcfs_stack;
63 EXPORT_SYMBOL(libcfs_stack);
65 unsigned int portal_enter_debugger;
66 EXPORT_SYMBOL(portal_enter_debugger);
68 unsigned int libcfs_catastrophe;
69 EXPORT_SYMBOL(libcfs_catastrophe);
71 unsigned int libcfs_panic_on_lbug = 0;
72 EXPORT_SYMBOL(libcfs_panic_on_lbug);
74 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
75 EXPORT_SYMBOL(libcfs_kmemory);
77 static cfs_waitq_t debug_ctlwq;
80 char debug_file_path[1024] = "/r/tmp/lustre-log";
82 char debug_file_path[1024] = "/tmp/lustre-log";
85 int libcfs_panic_in_progress;
87 /* libcfs_debug_token2mask() expects the returned
88 * string in lower-case */
90 libcfs_debug_subsys2str(int subsys)
144 /* libcfs_debug_token2mask() expects the returned
145 * string in lower-case */
147 libcfs_debug_dbg2str(int debug)
212 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
214 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
215 libcfs_debug_dbg2str;
221 if (mask == 0) { /* "0" */
225 } else { /* space-separated tokens */
226 for (i = 0; i < 32; i++) {
229 if ((mask & bit) == 0)
233 if (token == NULL) /* unused bit */
236 if (len > 0) { /* separator? */
242 while (*token != 0) {
251 /* terminate 'str' */
261 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
263 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
264 libcfs_debug_dbg2str;
270 /* match against known tokens */
271 for (i = 0; i < 32; i++) {
275 if (token == NULL) /* unused? */
280 if (j == len) { /* end of token */
291 if (str[j] == token[j])
294 if (str[j] < 'A' || 'Z' < str[j])
297 if (str[j] - 'A' + 'a' != token[j])
302 return -EINVAL; /* no match */
306 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
314 /* Allow a number for backwards compatibility */
316 for (n = strlen(str); n > 0; n--)
317 if (!isspace(str[n-1]))
321 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
327 /* <str> must be a list of debug tokens or numbers separated by
328 * whitespace and optionally an operator ('+' or '-'). If an operator
329 * appears first in <str>, '*mask' is used as the starting point
330 * (relative), otherwise 0 is used (absolute). An operator applies to
331 * all following tokens up to the next operator. */
335 while (isspace(*str)) /* skip whitespace */
341 if (*str == '+' || *str == '-') {
344 /* op on first token == relative */
348 while (isspace(*str)) /* skip whitespace */
351 if (*str == 0) /* trailing op */
355 /* find token length */
356 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
359 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
378 void libcfs_debug_dumplog_internal(void *arg)
380 CFS_DECL_JOURNAL_DATA;
384 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
385 debug_file_path, cfs_time_current_sec(), (long)arg);
386 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
387 tracefile_dump_all_pages(debug_file_name);
392 int libcfs_debug_dumplog_thread(void *arg)
395 libcfs_debug_dumplog_internal(arg);
396 cfs_waitq_signal(&debug_ctlwq);
400 void libcfs_debug_dumplog(void)
406 /* we're being careful to ensure that the kernel thread is
407 * able to set our state to running as it exits before we
408 * get to schedule() */
409 cfs_waitlink_init(&wait);
410 set_current_state(TASK_INTERRUPTIBLE);
411 cfs_waitq_add(&debug_ctlwq, &wait);
413 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
414 (void *)(long)cfs_curproc_pid(),
415 CLONE_VM | CLONE_FS | CLONE_FILES);
417 printk(KERN_ERR "LustreError: cannot start log dump thread: "
420 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
422 /* be sure to teardown if kernel_thread() failed */
423 cfs_waitq_del(&debug_ctlwq, &wait);
424 set_current_state(TASK_RUNNING);
427 int libcfs_debug_init(unsigned long bufsize)
431 cfs_waitq_init(&debug_ctlwq);
432 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
433 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
434 rc = tracefile_init();
437 libcfs_register_panic_notifier();
442 int libcfs_debug_cleanup(void)
444 libcfs_unregister_panic_notifier();
449 int libcfs_debug_clear_buffer(void)
455 /* Debug markers, although printed by S_LNET
456 * should not be be marked as such. */
457 #undef DEBUG_SUBSYSTEM
458 #define DEBUG_SUBSYSTEM S_UNDEFINED
459 int libcfs_debug_mark_buffer(char *text)
461 CDEBUG(D_TRACE,"***************************************************\n");
462 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
463 CDEBUG(D_TRACE,"***************************************************\n");
467 #undef DEBUG_SUBSYSTEM
468 #define DEBUG_SUBSYSTEM S_LNET
470 void libcfs_debug_set_level(unsigned int debug_level)
472 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
474 libcfs_debug = debug_level;
477 EXPORT_SYMBOL(libcfs_debug_dumplog);
478 EXPORT_SYMBOL(libcfs_debug_set_level);
481 #else /* !__KERNEL__ */
483 #include <libcfs/libcfs.h>
485 #ifdef HAVE_CATAMOUNT_DATA_H
486 #include <catamount/data.h>
487 #include <catamount/lputs.h>
489 static char source_nid[16];
490 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
491 static int toconsole = 1;
492 unsigned int libcfs_console_ratelimit = 1;
493 cfs_duration_t libcfs_console_max_delay;
494 cfs_duration_t libcfs_console_min_delay;
495 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
496 #else /* !HAVE_CATAMOUNT_DATA_H */
498 #include <sys/utsname.h>
499 #endif /* HAVE_NETDB_H */
500 struct utsname *tmp_utsname;
501 static char source_nid[sizeof(tmp_utsname->nodename)];
502 #endif /* HAVE_CATAMOUNT_DATA_H */
504 static int source_pid;
505 int smp_processor_id = 1;
506 char debug_file_path[1024];
509 int portals_do_debug_dumplog(void *arg)
511 printf("Look in %s\n", debug_file_name);
516 void portals_debug_print(void)
522 void libcfs_debug_dumplog(void)
524 printf("Look in %s\n", debug_file_name);
528 int libcfs_debug_init(unsigned long bufsize)
530 char *debug_mask = NULL;
531 char *debug_subsys = NULL;
532 char *debug_filename;
534 #ifdef HAVE_CATAMOUNT_DATA_H
535 char *debug_console = NULL;
536 char *debug_ratelimit = NULL;
537 char *debug_max_delay = NULL;
538 char *debug_min_delay = NULL;
539 char *debug_backoff = NULL;
541 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
542 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
544 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
545 source_pid = _my_pid;
547 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
548 if (debug_console != NULL) {
549 toconsole = strtoul(debug_console, NULL, 0);
550 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
552 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
553 if (debug_ratelimit != NULL) {
554 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
555 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
556 libcfs_console_ratelimit);
558 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
559 if (debug_max_delay != NULL)
560 libcfs_console_max_delay =
561 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
562 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
563 if (debug_min_delay != NULL)
564 libcfs_console_min_delay =
565 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
566 if (debug_min_delay || debug_max_delay) {
567 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
568 libcfs_console_max_delay < libcfs_console_min_delay) {
569 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
570 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
571 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
572 "should be greater than "
573 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
574 "and both parameters should be non-null"
575 ": restore default values\n");
577 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
578 " and min delay to %lus\n",
580 cfs_duration_sec(libcfs_console_max_delay),
582 cfs_duration_sec(libcfs_console_min_delay));
585 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
586 if (debug_backoff != NULL) {
587 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
588 if (libcfs_console_backoff <= 0) {
589 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
590 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
591 "restore default value\n");
593 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
594 libcfs_console_backoff);
598 struct utsname myname;
600 if (uname(&myname) == 0)
601 strcpy(source_nid, myname.nodename);
602 source_pid = getpid();
605 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
607 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
609 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
611 libcfs_subsystem_debug =
612 (unsigned int) strtol(debug_subsys, NULL, 0);
614 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
616 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
618 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
620 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
622 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
623 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
624 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
626 if (strcmp(debug_file_name, "stdout") == 0 ||
627 strcmp(debug_file_name, "-") == 0) {
628 debug_file_fd = stdout;
629 } else if (strcmp(debug_file_name, "stderr") == 0) {
630 debug_file_fd = stderr;
631 } else if (debug_file_name[0] != '\0') {
632 debug_file_fd = fopen(debug_file_name, "w");
633 if (debug_file_fd == NULL)
634 fprintf(stderr, "%s: unable to open '%s': %s\n",
635 source_nid, debug_file_name, strerror(errno));
638 if (debug_file_fd == NULL)
639 debug_file_fd = stdout;
644 int libcfs_debug_cleanup(void)
646 if (debug_file_fd != stdout && debug_file_fd != stderr)
647 fclose(debug_file_fd);
651 int libcfs_debug_clear_buffer(void)
656 int libcfs_debug_mark_buffer(char *text)
659 fprintf(debug_file_fd, "*******************************************************************************\n");
660 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
661 fprintf(debug_file_fd, "*******************************************************************************\n");
666 #ifdef HAVE_CATAMOUNT_DATA_H
667 #define CATAMOUNT_MAXLINE (256-4)
668 void catamount_printline(char *buf, size_t size)
675 pos += CATAMOUNT_MAXLINE;
676 prsize -= CATAMOUNT_MAXLINE;
682 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
683 int subsys, int mask,
684 const char *file, const char *fn, const int line,
685 const char *format1, va_list args,
686 const char *format2, ...)
692 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
693 * where message can`t be exceed PAGE_SIZE */
695 char *prefix = "Lustre";
697 #ifdef HAVE_CATAMOUNT_DATA_H
698 /* toconsole == 0 - all messages to debug_file_fd
699 * toconsole == 1 - warnings to console, all to debug_file_fd
700 * toconsole > 1 - all debug to console */
701 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
705 if ((!console) && (!debug_file_fd)) {
709 if (mask & (D_EMERG | D_ERROR))
710 prefix = "LustreError";
712 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
713 source_pid, source_nid, file, line, fn);
715 remain = sizeof(buf) - nob;
717 nob += vsnprintf(&buf[nob], remain, format1, args);
720 remain = sizeof(buf) - nob;
721 if ((format2) && (remain > 0)) {
722 va_start(ap, format2);
723 nob += vsnprintf(&buf[nob], remain, format2, ap);
727 #ifdef HAVE_CATAMOUNT_DATA_H
729 /* check rate limit for console */
731 if (libcfs_console_ratelimit &&
732 cdls->cdls_next != 0 && /* not first time ever */
733 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
735 /* skipping a console message */
740 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
741 libcfs_console_max_delay +
742 cfs_time_seconds(10))) {
743 /* last timeout was a long time ago */
744 cdls->cdls_delay /= libcfs_console_backoff * 4;
746 cdls->cdls_delay *= libcfs_console_backoff;
748 if (cdls->cdls_delay <
749 libcfs_console_min_delay)
751 libcfs_console_min_delay;
752 else if (cdls->cdls_delay >
753 libcfs_console_max_delay)
755 libcfs_console_max_delay;
758 /* ensure cdls_next is never zero after it's been seen */
759 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
762 if (cdls != NULL && cdls->cdls_count != 0) {
765 nob = snprintf(buf2, sizeof(buf2),
766 "Skipped %d previous similar message%s\n",
767 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
769 catamount_printline(buf2, nob);
770 cdls->cdls_count = 0;
773 catamount_printline(buf, nob);
776 /* return on toconsole > 1, as we don't want the user getting
777 * spammed by the debug data */
781 if (debug_file_fd == NULL)
784 gettimeofday(&tv, NULL);
786 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
787 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
788 file, line, fn, buf);
794 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
797 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
798 "ASSERTION(%s) failed\n", expr);
802 #endif /* __KERNEL__ */