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;
45 EXPORT_SYMBOL(libcfs_printk);
47 unsigned int libcfs_console_ratelimit = 1;
48 EXPORT_SYMBOL(libcfs_console_ratelimit);
50 unsigned int libcfs_debug_binary = 1;
51 EXPORT_SYMBOL(libcfs_debug_binary);
53 unsigned int libcfs_stack;
54 EXPORT_SYMBOL(libcfs_stack);
56 unsigned int portal_enter_debugger;
57 EXPORT_SYMBOL(portal_enter_debugger);
59 unsigned int libcfs_catastrophe;
60 EXPORT_SYMBOL(libcfs_catastrophe);
62 unsigned int libcfs_panic_on_lbug = 0;
63 EXPORT_SYMBOL(libcfs_panic_on_lbug);
65 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
66 EXPORT_SYMBOL(libcfs_kmemory);
68 static cfs_waitq_t debug_ctlwq;
71 char debug_file_path[1024] = "/r/tmp/lustre-log";
73 char debug_file_path[1024] = "/tmp/lustre-log";
76 int libcfs_panic_in_progress;
78 /* libcfs_debug_token2mask() expects the returned
79 * string in lower-case */
81 libcfs_debug_subsys2str(int subsys)
135 /* libcfs_debug_token2mask() expects the returned
136 * string in lower-case */
138 libcfs_debug_dbg2str(int debug)
203 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
205 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
206 libcfs_debug_dbg2str;
212 if (mask == 0) { /* "0" */
216 } else { /* space-separated tokens */
217 for (i = 0; i < 32; i++) {
220 if ((mask & bit) == 0)
224 if (token == NULL) /* unused bit */
227 if (len > 0) { /* separator? */
233 while (*token != 0) {
242 /* terminate 'str' */
252 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
254 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
255 libcfs_debug_dbg2str;
261 /* match against known tokens */
262 for (i = 0; i < 32; i++) {
266 if (token == NULL) /* unused? */
271 if (j == len) { /* end of token */
282 if (str[j] == token[j])
285 if (str[j] < 'A' || 'Z' < str[j])
288 if (str[j] - 'A' + 'a' != token[j])
293 return -EINVAL; /* no match */
297 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
305 /* Allow a number for backwards compatibility */
307 for (n = strlen(str); n > 0; n--)
308 if (!isspace(str[n-1]))
312 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
318 /* <str> must be a list of debug tokens or numbers separated by
319 * whitespace and optionally an operator ('+' or '-'). If an operator
320 * appears first in <str>, '*mask' is used as the starting point
321 * (relative), otherwise 0 is used (absolute). An operator applies to
322 * all following tokens up to the next operator. */
326 while (isspace(*str)) /* skip whitespace */
332 if (*str == '+' || *str == '-') {
335 /* op on first token == relative */
339 while (isspace(*str)) /* skip whitespace */
342 if (*str == 0) /* trailing op */
346 /* find token length */
347 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
350 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
369 void libcfs_debug_dumplog_internal(void *arg)
371 CFS_DECL_JOURNAL_DATA;
375 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
376 debug_file_path, cfs_time_current_sec(), (long)arg);
377 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
378 tracefile_dump_all_pages(debug_file_name);
383 int libcfs_debug_dumplog_thread(void *arg)
386 libcfs_debug_dumplog_internal(arg);
387 cfs_waitq_signal(&debug_ctlwq);
391 void libcfs_debug_dumplog(void)
397 /* we're being careful to ensure that the kernel thread is
398 * able to set our state to running as it exits before we
399 * get to schedule() */
400 cfs_waitlink_init(&wait);
401 set_current_state(TASK_INTERRUPTIBLE);
402 cfs_waitq_add(&debug_ctlwq, &wait);
404 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
405 (void *)(long)cfs_curproc_pid(),
406 CLONE_VM | CLONE_FS | CLONE_FILES);
408 printk(KERN_ERR "LustreError: cannot start log dump thread: "
411 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
413 /* be sure to teardown if kernel_thread() failed */
414 cfs_waitq_del(&debug_ctlwq, &wait);
415 set_current_state(TASK_RUNNING);
418 int libcfs_debug_init(unsigned long bufsize)
422 cfs_waitq_init(&debug_ctlwq);
423 rc = tracefile_init();
426 libcfs_register_panic_notifier();
431 int libcfs_debug_cleanup(void)
433 libcfs_unregister_panic_notifier();
438 int libcfs_debug_clear_buffer(void)
444 /* Debug markers, although printed by S_LNET
445 * should not be be marked as such. */
446 #undef DEBUG_SUBSYSTEM
447 #define DEBUG_SUBSYSTEM S_UNDEFINED
448 int libcfs_debug_mark_buffer(char *text)
450 CDEBUG(D_TRACE,"***************************************************\n");
451 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
452 CDEBUG(D_TRACE,"***************************************************\n");
456 #undef DEBUG_SUBSYSTEM
457 #define DEBUG_SUBSYSTEM S_LNET
459 void libcfs_debug_set_level(unsigned int debug_level)
461 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
463 libcfs_debug = debug_level;
466 EXPORT_SYMBOL(libcfs_debug_dumplog);
467 EXPORT_SYMBOL(libcfs_debug_set_level);
470 #else /* !__KERNEL__ */
472 #include <libcfs/libcfs.h>
474 #ifdef HAVE_CATAMOUNT_DATA_H
475 #include <catamount/data.h>
476 #include <catamount/lputs.h>
478 static char source_nid[16];
479 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
480 static int toconsole = 1;
481 unsigned int libcfs_console_ratelimit = 1;
482 #else /* !HAVE_CATAMOUNT_DATA_H */
484 #include <sys/utsname.h>
485 #endif /* HAVE_NETDB_H */
486 struct utsname *tmp_utsname;
487 static char source_nid[sizeof(tmp_utsname->nodename)];
488 #endif /* HAVE_CATAMOUNT_DATA_H */
490 static int source_pid;
491 int smp_processor_id = 1;
492 char debug_file_path[1024];
495 int portals_do_debug_dumplog(void *arg)
497 printf("Look in %s\n", debug_file_name);
502 void portals_debug_print(void)
508 void libcfs_debug_dumplog(void)
510 printf("Look in %s\n", debug_file_name);
514 int libcfs_debug_init(unsigned long bufsize)
516 char *debug_mask = NULL;
517 char *debug_subsys = NULL;
518 char *debug_filename;
520 #ifdef HAVE_CATAMOUNT_DATA_H
521 char *debug_console = NULL;
522 char *debug_ratelimit = NULL;
524 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
525 source_pid = _my_pid;
527 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
528 if (debug_console != NULL) {
529 toconsole = strtoul(debug_console, NULL, 0);
530 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
532 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
533 if (debug_ratelimit != NULL) {
534 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
535 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n", libcfs_console_ratelimit);
538 struct utsname myname;
540 if (uname(&myname) == 0)
541 strcpy(source_nid, myname.nodename);
542 source_pid = getpid();
545 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
547 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
549 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
551 libcfs_subsystem_debug =
552 (unsigned int) strtol(debug_subsys, NULL, 0);
554 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
556 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
558 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
560 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
562 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
563 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
564 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
566 if (strcmp(debug_file_name, "stdout") == 0 ||
567 strcmp(debug_file_name, "-") == 0) {
568 debug_file_fd = stdout;
569 } else if (strcmp(debug_file_name, "stderr") == 0) {
570 debug_file_fd = stderr;
571 } else if (debug_file_name[0] != '\0') {
572 debug_file_fd = fopen(debug_file_name, "w");
573 if (debug_file_fd == NULL)
574 fprintf(stderr, "%s: unable to open '%s': %s\n",
575 source_nid, debug_file_name, strerror(errno));
578 if (debug_file_fd == NULL)
579 debug_file_fd = stdout;
584 int libcfs_debug_cleanup(void)
586 if (debug_file_fd != stdout && debug_file_fd != stderr)
587 fclose(debug_file_fd);
591 int libcfs_debug_clear_buffer(void)
596 int libcfs_debug_mark_buffer(char *text)
599 fprintf(debug_file_fd, "*******************************************************************************\n");
600 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
601 fprintf(debug_file_fd, "*******************************************************************************\n");
606 #ifdef HAVE_CATAMOUNT_DATA_H
607 #define CATAMOUNT_MAXLINE (256-4)
608 void catamount_printline(char *buf, size_t size)
615 pos += CATAMOUNT_MAXLINE;
616 prsize -= CATAMOUNT_MAXLINE;
622 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
623 int subsys, int mask,
624 const char *file, const char *fn, const int line,
625 const char *format1, va_list args,
626 const char *format2, ...)
632 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
633 * where message can`t be exceed PAGE_SIZE */
635 char *prefix = "Lustre";
637 #ifdef HAVE_CATAMOUNT_DATA_H
638 /* toconsole == 0 - all messages to debug_file_fd
639 * toconsole == 1 - warnings to console, all to debug_file_fd
640 * toconsole > 1 - all debug to console */
641 if ( ((mask & D_CANTMASK) &&
642 (toconsole == 1)) || (toconsole > 1)) {
647 if ((!console) && (!debug_file_fd)) {
651 if (mask & (D_EMERG | D_ERROR))
652 prefix = "LustreError";
654 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
655 source_pid, source_nid, file, line, fn);
657 remain = sizeof(buf) - nob;
659 nob += vsnprintf(&buf[nob], remain, format1, args);
662 remain = sizeof(buf) - nob;
663 if ((format2) && (remain > 0)) {
664 va_start(ap, format2);
665 nob += vsnprintf(&buf[nob], remain, format2, ap);
669 #ifdef HAVE_CATAMOUNT_DATA_H
671 /* check rate limit for console */
673 cfs_time_t t = cdls->cdls_next +
674 cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
675 cfs_duration_t dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
677 if (libcfs_console_ratelimit &&
678 cdls->cdls_next != 0 && /* not first time ever */
679 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
681 /* skipping a console message */
686 if (cfs_time_after(cfs_time_current(), t)) {
687 /* last timeout was a long time ago */
688 cdls->cdls_delay /= 8;
690 cdls->cdls_delay *= 2;
692 if (cdls->cdls_delay < CFS_TICK)
693 cdls->cdls_delay = CFS_TICK;
694 else if (cdls->cdls_delay > dmax)
695 cdls->cdls_delay = dmax;
698 /* ensure cdls_next is never zero after it's been seen */
699 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
702 if (cdls != NULL && cdls->cdls_count != 0) {
705 nob = snprintf(buf2, sizeof(buf2),
706 "Skipped %d previous similar message%s\n",
707 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
709 catamount_printline(buf2, nob);
710 cdls->cdls_count = 0;
713 catamount_printline(buf, nob);
716 /* return on toconsole > 1, as we don't want the user getting
717 * spammed by the debug data */
721 if (debug_file_fd == NULL)
724 gettimeofday(&tv, NULL);
726 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
727 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
728 file, line, fn, buf);
734 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
737 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
738 "ASSERTION(%s) failed\n", expr);
742 #endif /* __KERNEL__ */