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
29 #include <libcfs/kp30.h>
30 #include <libcfs/libcfs.h>
31 #include "tracefile.h"
33 static char debug_file_name[1024];
36 unsigned int libcfs_subsystem_debug = ~0;
37 EXPORT_SYMBOL(libcfs_subsystem_debug);
39 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
40 D_NETERROR | D_HA | D_CONFIG | D_IOCTL |
41 D_DLMTRACE | D_RPCTRACE | D_VFSTRACE);
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;
70 char debug_file_path[1024] = "/tmp/lustre-log";
72 int libcfs_panic_in_progress;
74 /* libcfs_debug_token2mask() expects the returned
75 * string in lower-case */
77 libcfs_debug_subsys2str(int subsys)
131 /* libcfs_debug_token2mask() expects the returned
132 * string in lower-case */
134 libcfs_debug_dbg2str(int debug)
199 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
201 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
202 libcfs_debug_dbg2str;
208 if (mask == 0) { /* "0" */
212 } else { /* space-separated tokens */
213 for (i = 0; i < 32; i++) {
216 if ((mask & bit) == 0)
220 if (token == NULL) /* unused bit */
223 if (len > 0) { /* separator? */
229 while (*token != 0) {
238 /* terminate 'str' */
248 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
250 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
251 libcfs_debug_dbg2str;
257 /* match against known tokens */
258 for (i = 0; i < 32; i++) {
262 if (token == NULL) /* unused? */
267 if (j == len) { /* end of token */
278 if (str[j] == token[j])
281 if (str[j] < 'A' || 'Z' < str[j])
284 if (str[j] - 'A' + 'a' != token[j])
289 return -EINVAL; /* no match */
293 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
301 /* <str> must be a list of debug tokens or numbers separated by
302 * whitespace and optionally an operator ('+' or '-'). If an operator
303 * appears first in <str>, '*mask' is used as the starting point
304 * (relative), otherwise 0 is used (absolute). An operator applies to
305 * all following tokens up to the next operator. */
308 while (isspace(*str)) /* skip whitespace */
314 if (*str == '+' || *str == '-') {
317 /* op on first token == relative */
321 while (isspace(*str)) /* skip whitespace */
324 if (*str == 0) /* trailing op */
328 /* find token length */
329 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
332 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
351 void libcfs_debug_dumplog_internal(void *arg)
353 CFS_DECL_JOURNAL_DATA;
357 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
358 debug_file_path, cfs_time_current_sec(), (long)arg);
359 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
360 tracefile_dump_all_pages(debug_file_name);
365 int libcfs_debug_dumplog_thread(void *arg)
368 libcfs_debug_dumplog_internal(arg);
369 cfs_waitq_signal(&debug_ctlwq);
373 void libcfs_debug_dumplog(void)
379 /* we're being careful to ensure that the kernel thread is
380 * able to set our state to running as it exits before we
381 * get to schedule() */
382 cfs_waitlink_init(&wait);
383 set_current_state(TASK_INTERRUPTIBLE);
384 cfs_waitq_add(&debug_ctlwq, &wait);
386 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
387 (void *)(long)cfs_curproc_pid(),
388 CLONE_VM | CLONE_FS | CLONE_FILES);
390 printk(KERN_ERR "LustreError: cannot start log dump thread: "
393 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
395 /* be sure to teardown if kernel_thread() failed */
396 cfs_waitq_del(&debug_ctlwq, &wait);
397 set_current_state(TASK_RUNNING);
400 int libcfs_debug_init(unsigned long bufsize)
404 cfs_waitq_init(&debug_ctlwq);
405 rc = tracefile_init();
408 libcfs_register_panic_notifier();
413 int libcfs_debug_cleanup(void)
415 libcfs_unregister_panic_notifier();
420 int libcfs_debug_clear_buffer(void)
426 /* Debug markers, although printed by S_LNET
427 * should not be be marked as such. */
428 #undef DEBUG_SUBSYSTEM
429 #define DEBUG_SUBSYSTEM S_UNDEFINED
430 int libcfs_debug_mark_buffer(char *text)
432 CDEBUG(D_TRACE,"***************************************************\n");
433 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
434 CDEBUG(D_TRACE,"***************************************************\n");
438 #undef DEBUG_SUBSYSTEM
439 #define DEBUG_SUBSYSTEM S_LNET
441 void libcfs_debug_set_level(unsigned int debug_level)
443 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
445 libcfs_debug = debug_level;
448 EXPORT_SYMBOL(libcfs_debug_dumplog);
449 EXPORT_SYMBOL(libcfs_debug_set_level);
452 #else /* !__KERNEL__ */
454 #include <libcfs/libcfs.h>
456 #ifdef HAVE_SYS_USER_H
457 # include <sys/user.h>
460 #ifdef HAVE_CATAMOUNT_DATA_H
461 #include <catamount/data.h>
462 #include <catamount/lputs.h>
464 static char source_nid[16];
465 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
466 static int toconsole = 1;
467 unsigned int libcfs_console_ratelimit = 1;
468 #else /* !HAVE_CATAMOUNT_DATA_H */
470 #include <sys/utsname.h>
471 #endif /* HAVE_CATAMOUNT_DATA_H */
472 struct utsname *tmp_utsname;
473 static char source_nid[sizeof(tmp_utsname->nodename)];
474 #endif /* __KERNEL__ */
476 static int source_pid;
477 int smp_processor_id = 1;
478 char debug_file_path[1024];
481 int portals_do_debug_dumplog(void *arg)
483 printf("Look in %s\n", debug_file_name);
488 void portals_debug_print(void)
494 void libcfs_debug_dumplog(void)
496 printf("Look in %s\n", debug_file_name);
500 int libcfs_debug_init(unsigned long bufsize)
502 char *debug_mask = NULL;
503 char *debug_subsys = NULL;
504 char *debug_filename;
506 #ifdef HAVE_CATAMOUNT_DATA_H
507 char *debug_console = NULL;
508 char *debug_ratelimit = NULL;
510 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
511 source_pid = _my_pid;
513 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
514 if (debug_console != NULL) {
515 toconsole = strtoul(debug_console, NULL, 0);
516 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
518 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
519 if (debug_ratelimit != NULL) {
520 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
521 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n", libcfs_console_ratelimit);
524 struct utsname myname;
526 if (uname(&myname) == 0)
527 strcpy(source_nid, myname.nodename);
528 source_pid = getpid();
531 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
533 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
535 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
537 libcfs_subsystem_debug =
538 (unsigned int) strtol(debug_subsys, NULL, 0);
540 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
542 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
544 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
546 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
548 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
549 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
550 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
552 if (strcmp(debug_file_name, "stdout") == 0 ||
553 strcmp(debug_file_name, "-") == 0) {
554 debug_file_fd = stdout;
555 } else if (strcmp(debug_file_name, "stderr") == 0) {
556 debug_file_fd = stderr;
557 } else if (debug_file_name[0] != '\0') {
558 debug_file_fd = fopen(debug_file_name, "w");
559 if (debug_file_fd == NULL)
560 fprintf(stderr, "%s: unable to open '%s': %s\n",
561 source_nid, debug_file_name, strerror(errno));
564 if (debug_file_fd == NULL)
565 debug_file_fd = stdout;
570 int libcfs_debug_cleanup(void)
572 if (debug_file_fd != stdout && debug_file_fd != stderr)
573 fclose(debug_file_fd);
577 int libcfs_debug_clear_buffer(void)
582 int libcfs_debug_mark_buffer(char *text)
585 fprintf(debug_file_fd, "*******************************************************************************\n");
586 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
587 fprintf(debug_file_fd, "*******************************************************************************\n");
592 #ifdef HAVE_CATAMOUNT_DATA_H
593 #define CATAMOUNT_MAXLINE (256-4)
594 void catamount_printline(char *buf, size_t size)
601 pos += CATAMOUNT_MAXLINE;
602 prsize -= CATAMOUNT_MAXLINE;
608 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
609 int subsys, int mask,
610 const char *file, const char *fn, const int line,
611 const char *format1, va_list args,
612 const char *format2, ...)
618 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
619 * where message can`t be exceed PAGE_SIZE */
621 char *prefix = "Lustre";
623 #ifdef HAVE_CATAMOUNT_DATA_H
624 /* toconsole == 0 - all messages to debug_file_fd
625 * toconsole == 1 - warnings to console, all to debug_file_fd
626 * toconsole > 1 - all debug to console */
627 if ( ((mask & D_CANTMASK) &&
628 (toconsole == 1)) || (toconsole > 1)) {
633 if ((!console) && (!debug_file_fd)) {
637 if (mask & (D_EMERG | D_ERROR))
638 prefix = "LustreError";
640 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
641 source_pid, source_nid, file, line, fn);
643 remain = sizeof(buf) - nob;
645 nob += vsnprintf(&buf[nob], remain, format1, args);
648 remain = sizeof(buf) - nob;
649 if ((format2) && (remain > 0)) {
650 va_start(ap, format2);
651 nob += vsnprintf(&buf[nob], remain, format2, ap);
655 #ifdef HAVE_CATAMOUNT_DATA_H
657 /* check rate limit for console */
659 cfs_time_t t = cdls->cdls_next +
660 cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
661 cfs_duration_t dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
663 if (libcfs_console_ratelimit &&
664 cdls->cdls_next != 0 && /* not first time ever */
665 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
667 /* skipping a console message */
672 if (cfs_time_after(cfs_time_current(), t)) {
673 /* last timeout was a long time ago */
674 cdls->cdls_delay /= 8;
676 cdls->cdls_delay *= 2;
678 if (cdls->cdls_delay < CFS_TICK)
679 cdls->cdls_delay = CFS_TICK;
680 else if (cdls->cdls_delay > dmax)
681 cdls->cdls_delay = dmax;
684 /* ensure cdls_next is never zero after it's been seen */
685 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
688 if (cdls != NULL && cdls->cdls_count != 0) {
691 nob = snprintf(buf2, sizeof(buf2),
692 "Skipped %d previous similar message%s\n",
693 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
695 catamount_printline(buf2, nob);
696 cdls->cdls_count = 0;
699 catamount_printline(buf, nob);
702 /* return on toconsole > 1, as we don't want the user getting
703 * spammed by the debug data */
707 if (debug_file_fd == NULL)
710 gettimeofday(&tv, NULL);
712 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
713 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
714 file, line, fn, buf);
720 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
723 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
724 "ASSERTION(%s) failed\n", expr);
728 #endif /* __KERNEL__ */