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 EXPORT_SYMBOL(libcfs_debug);
43 unsigned int libcfs_printk;
44 EXPORT_SYMBOL(libcfs_printk);
46 unsigned int libcfs_console_ratelimit = 1;
47 EXPORT_SYMBOL(libcfs_console_ratelimit);
49 unsigned int libcfs_debug_binary = 1;
50 EXPORT_SYMBOL(libcfs_debug_binary);
52 unsigned int libcfs_stack;
53 EXPORT_SYMBOL(libcfs_stack);
55 unsigned int portal_enter_debugger;
56 EXPORT_SYMBOL(portal_enter_debugger);
58 unsigned int libcfs_catastrophe;
59 EXPORT_SYMBOL(libcfs_catastrophe);
61 unsigned int libcfs_panic_on_lbug = 0;
62 EXPORT_SYMBOL(libcfs_panic_on_lbug);
64 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
65 EXPORT_SYMBOL(libcfs_kmemory);
67 static cfs_waitq_t debug_ctlwq;
70 char debug_file_path[1024] = "/r/tmp/lustre-log";
72 char debug_file_path[1024] = "/tmp/lustre-log";
75 int libcfs_panic_in_progress;
77 /* libcfs_debug_token2mask() expects the returned
78 * string in lower-case */
80 libcfs_debug_subsys2str(int subsys)
134 /* libcfs_debug_token2mask() expects the returned
135 * string in lower-case */
137 libcfs_debug_dbg2str(int debug)
202 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
204 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
205 libcfs_debug_dbg2str;
211 if (mask == 0) { /* "0" */
215 } else { /* space-separated tokens */
216 for (i = 0; i < 32; i++) {
219 if ((mask & bit) == 0)
223 if (token == NULL) /* unused bit */
226 if (len > 0) { /* separator? */
232 while (*token != 0) {
241 /* terminate 'str' */
251 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
253 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
254 libcfs_debug_dbg2str;
260 /* match against known tokens */
261 for (i = 0; i < 32; i++) {
265 if (token == NULL) /* unused? */
270 if (j == len) { /* end of token */
281 if (str[j] == token[j])
284 if (str[j] < 'A' || 'Z' < str[j])
287 if (str[j] - 'A' + 'a' != token[j])
292 return -EINVAL; /* no match */
296 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
304 /* <str> must be a list of debug tokens or numbers separated by
305 * whitespace and optionally an operator ('+' or '-'). If an operator
306 * appears first in <str>, '*mask' is used as the starting point
307 * (relative), otherwise 0 is used (absolute). An operator applies to
308 * all following tokens up to the next operator. */
311 while (isspace(*str)) /* skip whitespace */
317 if (*str == '+' || *str == '-') {
320 /* op on first token == relative */
324 while (isspace(*str)) /* skip whitespace */
327 if (*str == 0) /* trailing op */
331 /* find token length */
332 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
335 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
354 void libcfs_debug_dumplog_internal(void *arg)
356 CFS_DECL_JOURNAL_DATA;
360 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
361 debug_file_path, cfs_time_current_sec(), (long)arg);
362 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
363 tracefile_dump_all_pages(debug_file_name);
368 int libcfs_debug_dumplog_thread(void *arg)
371 libcfs_debug_dumplog_internal(arg);
372 cfs_waitq_signal(&debug_ctlwq);
376 void libcfs_debug_dumplog(void)
382 /* we're being careful to ensure that the kernel thread is
383 * able to set our state to running as it exits before we
384 * get to schedule() */
385 cfs_waitlink_init(&wait);
386 set_current_state(TASK_INTERRUPTIBLE);
387 cfs_waitq_add(&debug_ctlwq, &wait);
389 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
390 (void *)(long)cfs_curproc_pid(),
391 CLONE_VM | CLONE_FS | CLONE_FILES);
393 printk(KERN_ERR "LustreError: cannot start log dump thread: "
396 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
398 /* be sure to teardown if kernel_thread() failed */
399 cfs_waitq_del(&debug_ctlwq, &wait);
400 set_current_state(TASK_RUNNING);
403 int libcfs_debug_init(unsigned long bufsize)
407 cfs_waitq_init(&debug_ctlwq);
408 rc = tracefile_init();
411 libcfs_register_panic_notifier();
416 int libcfs_debug_cleanup(void)
418 libcfs_unregister_panic_notifier();
423 int libcfs_debug_clear_buffer(void)
429 /* Debug markers, although printed by S_LNET
430 * should not be be marked as such. */
431 #undef DEBUG_SUBSYSTEM
432 #define DEBUG_SUBSYSTEM S_UNDEFINED
433 int libcfs_debug_mark_buffer(char *text)
435 CDEBUG(D_TRACE,"***************************************************\n");
436 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
437 CDEBUG(D_TRACE,"***************************************************\n");
441 #undef DEBUG_SUBSYSTEM
442 #define DEBUG_SUBSYSTEM S_LNET
444 void libcfs_debug_set_level(unsigned int debug_level)
446 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
448 libcfs_debug = debug_level;
451 EXPORT_SYMBOL(libcfs_debug_dumplog);
452 EXPORT_SYMBOL(libcfs_debug_set_level);
455 #else /* !__KERNEL__ */
457 #include <libcfs/libcfs.h>
459 #ifdef HAVE_SYS_USER_H
460 # include <sys/user.h>
463 #ifdef HAVE_CATAMOUNT_DATA_H
464 #include <catamount/data.h>
465 #include <catamount/lputs.h>
467 static char source_nid[16];
468 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
469 static int toconsole = 1;
470 unsigned int libcfs_console_ratelimit = 1;
471 #else /* !HAVE_CATAMOUNT_DATA_H */
473 #include <sys/utsname.h>
474 #endif /* HAVE_NETDB_H */
475 struct utsname *tmp_utsname;
476 static char source_nid[sizeof(tmp_utsname->nodename)];
477 #endif /* HAVE_CATAMOUNT_DATA_H */
479 static int source_pid;
480 int smp_processor_id = 1;
481 char debug_file_path[1024];
484 int portals_do_debug_dumplog(void *arg)
486 printf("Look in %s\n", debug_file_name);
491 void portals_debug_print(void)
497 void libcfs_debug_dumplog(void)
499 printf("Look in %s\n", debug_file_name);
503 int libcfs_debug_init(unsigned long bufsize)
505 char *debug_mask = NULL;
506 char *debug_subsys = NULL;
507 char *debug_filename;
509 #ifdef HAVE_CATAMOUNT_DATA_H
510 char *debug_console = NULL;
511 char *debug_ratelimit = NULL;
513 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
514 source_pid = _my_pid;
516 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
517 if (debug_console != NULL) {
518 toconsole = strtoul(debug_console, NULL, 0);
519 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
521 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
522 if (debug_ratelimit != NULL) {
523 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
524 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n", libcfs_console_ratelimit);
527 struct utsname myname;
529 if (uname(&myname) == 0)
530 strcpy(source_nid, myname.nodename);
531 source_pid = getpid();
534 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
536 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
538 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
540 libcfs_subsystem_debug =
541 (unsigned int) strtol(debug_subsys, NULL, 0);
543 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
545 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
547 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
549 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
551 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
552 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
553 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
555 if (strcmp(debug_file_name, "stdout") == 0 ||
556 strcmp(debug_file_name, "-") == 0) {
557 debug_file_fd = stdout;
558 } else if (strcmp(debug_file_name, "stderr") == 0) {
559 debug_file_fd = stderr;
560 } else if (debug_file_name[0] != '\0') {
561 debug_file_fd = fopen(debug_file_name, "w");
562 if (debug_file_fd == NULL)
563 fprintf(stderr, "%s: unable to open '%s': %s\n",
564 source_nid, debug_file_name, strerror(errno));
567 if (debug_file_fd == NULL)
568 debug_file_fd = stdout;
573 int libcfs_debug_cleanup(void)
575 if (debug_file_fd != stdout && debug_file_fd != stderr)
576 fclose(debug_file_fd);
580 int libcfs_debug_clear_buffer(void)
585 int libcfs_debug_mark_buffer(char *text)
588 fprintf(debug_file_fd, "*******************************************************************************\n");
589 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
590 fprintf(debug_file_fd, "*******************************************************************************\n");
595 #ifdef HAVE_CATAMOUNT_DATA_H
596 #define CATAMOUNT_MAXLINE (256-4)
597 void catamount_printline(char *buf, size_t size)
604 pos += CATAMOUNT_MAXLINE;
605 prsize -= CATAMOUNT_MAXLINE;
611 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
612 int subsys, int mask,
613 const char *file, const char *fn, const int line,
614 const char *format1, va_list args,
615 const char *format2, ...)
621 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
622 * where message can`t be exceed PAGE_SIZE */
624 char *prefix = "Lustre";
626 #ifdef HAVE_CATAMOUNT_DATA_H
627 /* toconsole == 0 - all messages to debug_file_fd
628 * toconsole == 1 - warnings to console, all to debug_file_fd
629 * toconsole > 1 - all debug to console */
630 if ( ((mask & D_CANTMASK) &&
631 (toconsole == 1)) || (toconsole > 1)) {
636 if ((!console) && (!debug_file_fd)) {
640 if (mask & (D_EMERG | D_ERROR))
641 prefix = "LustreError";
643 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
644 source_pid, source_nid, file, line, fn);
646 remain = sizeof(buf) - nob;
648 nob += vsnprintf(&buf[nob], remain, format1, args);
651 remain = sizeof(buf) - nob;
652 if ((format2) && (remain > 0)) {
653 va_start(ap, format2);
654 nob += vsnprintf(&buf[nob], remain, format2, ap);
658 #ifdef HAVE_CATAMOUNT_DATA_H
660 /* check rate limit for console */
662 cfs_time_t t = cdls->cdls_next +
663 cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
664 cfs_duration_t dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
666 if (libcfs_console_ratelimit &&
667 cdls->cdls_next != 0 && /* not first time ever */
668 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
670 /* skipping a console message */
675 if (cfs_time_after(cfs_time_current(), t)) {
676 /* last timeout was a long time ago */
677 cdls->cdls_delay /= 8;
679 cdls->cdls_delay *= 2;
681 if (cdls->cdls_delay < CFS_TICK)
682 cdls->cdls_delay = CFS_TICK;
683 else if (cdls->cdls_delay > dmax)
684 cdls->cdls_delay = dmax;
687 /* ensure cdls_next is never zero after it's been seen */
688 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
691 if (cdls != NULL && cdls->cdls_count != 0) {
694 nob = snprintf(buf2, sizeof(buf2),
695 "Skipped %d previous similar message%s\n",
696 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
698 catamount_printline(buf2, nob);
699 cdls->cdls_count = 0;
702 catamount_printline(buf, nob);
705 /* return on toconsole > 1, as we don't want the user getting
706 * spammed by the debug data */
710 if (debug_file_fd == NULL)
713 gettimeofday(&tv, NULL);
715 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
716 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
717 file, line, fn, buf);
723 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
726 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
727 "ASSERTION(%s) failed\n", expr);
731 #endif /* __KERNEL__ */