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";
106 int libcfs_panic_in_progress;
108 /* libcfs_debug_token2mask() expects the returned
109 * string in lower-case */
111 libcfs_debug_subsys2str(int subsys)
165 /* libcfs_debug_token2mask() expects the returned
166 * string in lower-case */
168 libcfs_debug_dbg2str(int debug)
233 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
235 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
236 libcfs_debug_dbg2str;
242 if (mask == 0) { /* "0" */
246 } else { /* space-separated tokens */
247 for (i = 0; i < 32; i++) {
250 if ((mask & bit) == 0)
254 if (token == NULL) /* unused bit */
257 if (len > 0) { /* separator? */
263 while (*token != 0) {
272 /* terminate 'str' */
282 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
284 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
285 libcfs_debug_dbg2str;
291 /* match against known tokens */
292 for (i = 0; i < 32; i++) {
296 if (token == NULL) /* unused? */
301 if (j == len) { /* end of token */
312 if (str[j] == token[j])
315 if (str[j] < 'A' || 'Z' < str[j])
318 if (str[j] - 'A' + 'a' != token[j])
323 return -EINVAL; /* no match */
327 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
335 /* Allow a number for backwards compatibility */
337 for (n = strlen(str); n > 0; n--)
338 if (!isspace(str[n-1]))
342 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
348 /* <str> must be a list of debug tokens or numbers separated by
349 * whitespace and optionally an operator ('+' or '-'). If an operator
350 * appears first in <str>, '*mask' is used as the starting point
351 * (relative), otherwise 0 is used (absolute). An operator applies to
352 * all following tokens up to the next operator. */
356 while (isspace(*str)) /* skip whitespace */
362 if (*str == '+' || *str == '-') {
365 /* op on first token == relative */
369 while (isspace(*str)) /* skip whitespace */
372 if (*str == 0) /* trailing op */
376 /* find token length */
377 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
380 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
399 void libcfs_debug_dumplog_internal(void *arg)
401 CFS_DECL_JOURNAL_DATA;
405 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
406 debug_file_path, cfs_time_current_sec(), (long)arg);
407 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
408 tracefile_dump_all_pages(debug_file_name);
413 int libcfs_debug_dumplog_thread(void *arg)
416 libcfs_debug_dumplog_internal(arg);
417 cfs_waitq_signal(&debug_ctlwq);
421 void libcfs_debug_dumplog(void)
427 /* we're being careful to ensure that the kernel thread is
428 * able to set our state to running as it exits before we
429 * get to schedule() */
430 cfs_waitlink_init(&wait);
431 set_current_state(TASK_INTERRUPTIBLE);
432 cfs_waitq_add(&debug_ctlwq, &wait);
434 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
435 (void *)(long)cfs_curproc_pid(),
436 CLONE_VM | CLONE_FS | CLONE_FILES);
438 printk(KERN_ERR "LustreError: cannot start log dump thread: "
441 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
443 /* be sure to teardown if kernel_thread() failed */
444 cfs_waitq_del(&debug_ctlwq, &wait);
445 set_current_state(TASK_RUNNING);
448 int libcfs_debug_init(unsigned long bufsize)
451 int max = libcfs_debug_mb;
453 cfs_waitq_init(&debug_ctlwq);
454 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
455 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
456 /* If libcfs_debug_mb is set to an invalid value or uninitialized
457 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
458 if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
461 max = (max / num_possible_cpus());
462 max = (max << (20 - CFS_PAGE_SHIFT));
464 rc = tracefile_init(max);
467 libcfs_register_panic_notifier();
472 int libcfs_debug_cleanup(void)
474 libcfs_unregister_panic_notifier();
479 int libcfs_debug_clear_buffer(void)
485 /* Debug markers, although printed by S_LNET
486 * should not be be marked as such. */
487 #undef DEBUG_SUBSYSTEM
488 #define DEBUG_SUBSYSTEM S_UNDEFINED
489 int libcfs_debug_mark_buffer(char *text)
491 CDEBUG(D_TRACE,"***************************************************\n");
492 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
493 CDEBUG(D_TRACE,"***************************************************\n");
497 #undef DEBUG_SUBSYSTEM
498 #define DEBUG_SUBSYSTEM S_LNET
500 void libcfs_debug_set_level(unsigned int debug_level)
502 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
504 libcfs_debug = debug_level;
507 EXPORT_SYMBOL(libcfs_debug_dumplog);
508 EXPORT_SYMBOL(libcfs_debug_set_level);
511 #else /* !__KERNEL__ */
513 #include <libcfs/libcfs.h>
515 #ifdef HAVE_CATAMOUNT_DATA_H
516 #include <catamount/data.h>
517 #include <catamount/lputs.h>
519 static char source_nid[16];
520 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
521 static int toconsole = 1;
522 unsigned int libcfs_console_ratelimit = 1;
523 cfs_duration_t libcfs_console_max_delay;
524 cfs_duration_t libcfs_console_min_delay;
525 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
526 #else /* !HAVE_CATAMOUNT_DATA_H */
528 #include <sys/utsname.h>
529 #endif /* HAVE_NETDB_H */
530 struct utsname *tmp_utsname;
531 static char source_nid[sizeof(tmp_utsname->nodename)];
532 #endif /* HAVE_CATAMOUNT_DATA_H */
534 static int source_pid;
535 int smp_processor_id = 1;
536 char debug_file_path[1024];
539 int portals_do_debug_dumplog(void *arg)
541 printf("Look in %s\n", debug_file_name);
546 void portals_debug_print(void)
552 void libcfs_debug_dumplog(void)
554 printf("Look in %s\n", debug_file_name);
558 int libcfs_debug_init(unsigned long bufsize)
560 char *debug_mask = NULL;
561 char *debug_subsys = NULL;
562 char *debug_filename;
564 #ifdef HAVE_CATAMOUNT_DATA_H
565 char *debug_console = NULL;
566 char *debug_ratelimit = NULL;
567 char *debug_max_delay = NULL;
568 char *debug_min_delay = NULL;
569 char *debug_backoff = NULL;
571 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
572 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
574 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
575 source_pid = _my_pid;
577 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
578 if (debug_console != NULL) {
579 toconsole = strtoul(debug_console, NULL, 0);
580 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
582 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
583 if (debug_ratelimit != NULL) {
584 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
585 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
586 libcfs_console_ratelimit);
588 debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
589 if (debug_max_delay != NULL)
590 libcfs_console_max_delay =
591 cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
592 debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
593 if (debug_min_delay != NULL)
594 libcfs_console_min_delay =
595 cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
596 if (debug_min_delay || debug_max_delay) {
597 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
598 libcfs_console_max_delay < libcfs_console_min_delay) {
599 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
600 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
601 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
602 "should be greater than "
603 "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
604 "and both parameters should be non-null"
605 ": restore default values\n");
607 CDEBUG(D_INFO, "set liblustre console max delay to %lus"
608 " and min delay to %lus\n",
610 cfs_duration_sec(libcfs_console_max_delay),
612 cfs_duration_sec(libcfs_console_min_delay));
615 debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
616 if (debug_backoff != NULL) {
617 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
618 if (libcfs_console_backoff <= 0) {
619 libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
620 CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
621 "restore default value\n");
623 CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
624 libcfs_console_backoff);
628 struct utsname myname;
630 if (uname(&myname) == 0)
631 strcpy(source_nid, myname.nodename);
632 source_pid = getpid();
635 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
637 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
639 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
641 libcfs_subsystem_debug =
642 (unsigned int) strtol(debug_subsys, NULL, 0);
644 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
646 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
648 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
650 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
652 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
653 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
654 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
656 if (strcmp(debug_file_name, "stdout") == 0 ||
657 strcmp(debug_file_name, "-") == 0) {
658 debug_file_fd = stdout;
659 } else if (strcmp(debug_file_name, "stderr") == 0) {
660 debug_file_fd = stderr;
661 } else if (debug_file_name[0] != '\0') {
662 debug_file_fd = fopen(debug_file_name, "w");
663 if (debug_file_fd == NULL)
664 fprintf(stderr, "%s: unable to open '%s': %s\n",
665 source_nid, debug_file_name, strerror(errno));
668 if (debug_file_fd == NULL)
669 debug_file_fd = stdout;
674 int libcfs_debug_cleanup(void)
676 if (debug_file_fd != stdout && debug_file_fd != stderr)
677 fclose(debug_file_fd);
681 int libcfs_debug_clear_buffer(void)
686 int libcfs_debug_mark_buffer(char *text)
689 fprintf(debug_file_fd, "*******************************************************************************\n");
690 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
691 fprintf(debug_file_fd, "*******************************************************************************\n");
696 #ifdef HAVE_CATAMOUNT_DATA_H
697 #define CATAMOUNT_MAXLINE (256-4)
698 void catamount_printline(char *buf, size_t size)
705 pos += CATAMOUNT_MAXLINE;
706 prsize -= CATAMOUNT_MAXLINE;
712 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
713 int subsys, int mask,
714 const char *file, const char *fn, const int line,
715 const char *format1, va_list args,
716 const char *format2, ...)
722 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
723 * where message can`t be exceed PAGE_SIZE */
725 char *prefix = "Lustre";
727 #ifdef HAVE_CATAMOUNT_DATA_H
728 /* toconsole == 0 - all messages to debug_file_fd
729 * toconsole == 1 - warnings to console, all to debug_file_fd
730 * toconsole > 1 - all debug to console */
731 if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
735 if ((!console) && (!debug_file_fd)) {
739 if (mask & (D_EMERG | D_ERROR))
740 prefix = "LustreError";
742 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
743 source_pid, source_nid, file, line, fn);
745 remain = sizeof(buf) - nob;
747 nob += vsnprintf(&buf[nob], remain, format1, args);
750 remain = sizeof(buf) - nob;
751 if ((format2) && (remain > 0)) {
752 va_start(ap, format2);
753 nob += vsnprintf(&buf[nob], remain, format2, ap);
757 #ifdef HAVE_CATAMOUNT_DATA_H
759 /* check rate limit for console */
761 if (libcfs_console_ratelimit &&
762 cdls->cdls_next != 0 && /* not first time ever */
763 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
765 /* skipping a console message */
770 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
771 libcfs_console_max_delay +
772 cfs_time_seconds(10))) {
773 /* last timeout was a long time ago */
774 cdls->cdls_delay /= libcfs_console_backoff * 4;
776 cdls->cdls_delay *= libcfs_console_backoff;
778 if (cdls->cdls_delay <
779 libcfs_console_min_delay)
781 libcfs_console_min_delay;
782 else if (cdls->cdls_delay >
783 libcfs_console_max_delay)
785 libcfs_console_max_delay;
788 /* ensure cdls_next is never zero after it's been seen */
789 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
792 if (cdls != NULL && cdls->cdls_count != 0) {
795 nob = snprintf(buf2, sizeof(buf2),
796 "Skipped %d previous similar message%s\n",
797 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
799 catamount_printline(buf2, nob);
800 cdls->cdls_count = 0;
803 catamount_printline(buf, nob);
806 /* return on toconsole > 1, as we don't want the user getting
807 * spammed by the debug data */
811 if (debug_file_fd == NULL)
814 gettimeofday(&tv, NULL);
816 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
817 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
818 file, line, fn, buf);
824 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
827 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
828 "ASSERTION(%s) failed\n", expr);
832 #endif /* __KERNEL__ */