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 /* Allow a number for backwards compatibility */
306 for (n = strlen(str); n > 0; n--)
307 if (!isspace(str[n-1]))
311 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
317 /* <str> must be a list of debug tokens or numbers separated by
318 * whitespace and optionally an operator ('+' or '-'). If an operator
319 * appears first in <str>, '*mask' is used as the starting point
320 * (relative), otherwise 0 is used (absolute). An operator applies to
321 * all following tokens up to the next operator. */
325 while (isspace(*str)) /* skip whitespace */
331 if (*str == '+' || *str == '-') {
334 /* op on first token == relative */
338 while (isspace(*str)) /* skip whitespace */
341 if (*str == 0) /* trailing op */
345 /* find token length */
346 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
349 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
368 void libcfs_debug_dumplog_internal(void *arg)
370 CFS_DECL_JOURNAL_DATA;
374 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
375 debug_file_path, cfs_time_current_sec(), (long)arg);
376 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
377 tracefile_dump_all_pages(debug_file_name);
382 int libcfs_debug_dumplog_thread(void *arg)
385 libcfs_debug_dumplog_internal(arg);
386 cfs_waitq_signal(&debug_ctlwq);
390 void libcfs_debug_dumplog(void)
396 /* we're being careful to ensure that the kernel thread is
397 * able to set our state to running as it exits before we
398 * get to schedule() */
399 cfs_waitlink_init(&wait);
400 set_current_state(TASK_INTERRUPTIBLE);
401 cfs_waitq_add(&debug_ctlwq, &wait);
403 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
404 (void *)(long)cfs_curproc_pid(),
405 CLONE_VM | CLONE_FS | CLONE_FILES);
407 printk(KERN_ERR "LustreError: cannot start log dump thread: "
410 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
412 /* be sure to teardown if kernel_thread() failed */
413 cfs_waitq_del(&debug_ctlwq, &wait);
414 set_current_state(TASK_RUNNING);
417 int libcfs_debug_init(unsigned long bufsize)
421 cfs_waitq_init(&debug_ctlwq);
422 rc = tracefile_init();
425 libcfs_register_panic_notifier();
430 int libcfs_debug_cleanup(void)
432 libcfs_unregister_panic_notifier();
437 int libcfs_debug_clear_buffer(void)
443 /* Debug markers, although printed by S_LNET
444 * should not be be marked as such. */
445 #undef DEBUG_SUBSYSTEM
446 #define DEBUG_SUBSYSTEM S_UNDEFINED
447 int libcfs_debug_mark_buffer(char *text)
449 CDEBUG(D_TRACE,"***************************************************\n");
450 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
451 CDEBUG(D_TRACE,"***************************************************\n");
455 #undef DEBUG_SUBSYSTEM
456 #define DEBUG_SUBSYSTEM S_LNET
458 void libcfs_debug_set_level(unsigned int debug_level)
460 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
462 libcfs_debug = debug_level;
465 EXPORT_SYMBOL(libcfs_debug_dumplog);
466 EXPORT_SYMBOL(libcfs_debug_set_level);
469 #else /* !__KERNEL__ */
471 #include <libcfs/libcfs.h>
473 #ifdef HAVE_SYS_USER_H
474 # include <sys/user.h>
477 #ifdef HAVE_CATAMOUNT_DATA_H
478 #include <catamount/data.h>
479 #include <catamount/lputs.h>
481 static char source_nid[16];
482 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
483 static int toconsole = 1;
484 unsigned int libcfs_console_ratelimit = 1;
485 #else /* !HAVE_CATAMOUNT_DATA_H */
487 #include <sys/utsname.h>
488 #endif /* HAVE_NETDB_H */
489 struct utsname *tmp_utsname;
490 static char source_nid[sizeof(tmp_utsname->nodename)];
491 #endif /* HAVE_CATAMOUNT_DATA_H */
493 static int source_pid;
494 int smp_processor_id = 1;
495 char debug_file_path[1024];
498 int portals_do_debug_dumplog(void *arg)
500 printf("Look in %s\n", debug_file_name);
505 void portals_debug_print(void)
511 void libcfs_debug_dumplog(void)
513 printf("Look in %s\n", debug_file_name);
517 int libcfs_debug_init(unsigned long bufsize)
519 char *debug_mask = NULL;
520 char *debug_subsys = NULL;
521 char *debug_filename;
523 #ifdef HAVE_CATAMOUNT_DATA_H
524 char *debug_console = NULL;
525 char *debug_ratelimit = NULL;
527 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
528 source_pid = _my_pid;
530 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
531 if (debug_console != NULL) {
532 toconsole = strtoul(debug_console, NULL, 0);
533 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
535 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
536 if (debug_ratelimit != NULL) {
537 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
538 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n", libcfs_console_ratelimit);
541 struct utsname myname;
543 if (uname(&myname) == 0)
544 strcpy(source_nid, myname.nodename);
545 source_pid = getpid();
548 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
550 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
552 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
554 libcfs_subsystem_debug =
555 (unsigned int) strtol(debug_subsys, NULL, 0);
557 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
559 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
561 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
563 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
565 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
566 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
567 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
569 if (strcmp(debug_file_name, "stdout") == 0 ||
570 strcmp(debug_file_name, "-") == 0) {
571 debug_file_fd = stdout;
572 } else if (strcmp(debug_file_name, "stderr") == 0) {
573 debug_file_fd = stderr;
574 } else if (debug_file_name[0] != '\0') {
575 debug_file_fd = fopen(debug_file_name, "w");
576 if (debug_file_fd == NULL)
577 fprintf(stderr, "%s: unable to open '%s': %s\n",
578 source_nid, debug_file_name, strerror(errno));
581 if (debug_file_fd == NULL)
582 debug_file_fd = stdout;
587 int libcfs_debug_cleanup(void)
589 if (debug_file_fd != stdout && debug_file_fd != stderr)
590 fclose(debug_file_fd);
594 int libcfs_debug_clear_buffer(void)
599 int libcfs_debug_mark_buffer(char *text)
602 fprintf(debug_file_fd, "*******************************************************************************\n");
603 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
604 fprintf(debug_file_fd, "*******************************************************************************\n");
609 #ifdef HAVE_CATAMOUNT_DATA_H
610 #define CATAMOUNT_MAXLINE (256-4)
611 void catamount_printline(char *buf, size_t size)
618 pos += CATAMOUNT_MAXLINE;
619 prsize -= CATAMOUNT_MAXLINE;
625 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
626 int subsys, int mask,
627 const char *file, const char *fn, const int line,
628 const char *format1, va_list args,
629 const char *format2, ...)
635 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
636 * where message can`t be exceed PAGE_SIZE */
638 char *prefix = "Lustre";
640 #ifdef HAVE_CATAMOUNT_DATA_H
641 /* toconsole == 0 - all messages to debug_file_fd
642 * toconsole == 1 - warnings to console, all to debug_file_fd
643 * toconsole > 1 - all debug to console */
644 if ( ((mask & D_CANTMASK) &&
645 (toconsole == 1)) || (toconsole > 1)) {
650 if ((!console) && (!debug_file_fd)) {
654 if (mask & (D_EMERG | D_ERROR))
655 prefix = "LustreError";
657 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
658 source_pid, source_nid, file, line, fn);
660 remain = sizeof(buf) - nob;
662 nob += vsnprintf(&buf[nob], remain, format1, args);
665 remain = sizeof(buf) - nob;
666 if ((format2) && (remain > 0)) {
667 va_start(ap, format2);
668 nob += vsnprintf(&buf[nob], remain, format2, ap);
672 #ifdef HAVE_CATAMOUNT_DATA_H
674 /* check rate limit for console */
676 cfs_time_t t = cdls->cdls_next +
677 cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
678 cfs_duration_t dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
680 if (libcfs_console_ratelimit &&
681 cdls->cdls_next != 0 && /* not first time ever */
682 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
684 /* skipping a console message */
689 if (cfs_time_after(cfs_time_current(), t)) {
690 /* last timeout was a long time ago */
691 cdls->cdls_delay /= 8;
693 cdls->cdls_delay *= 2;
695 if (cdls->cdls_delay < CFS_TICK)
696 cdls->cdls_delay = CFS_TICK;
697 else if (cdls->cdls_delay > dmax)
698 cdls->cdls_delay = dmax;
701 /* ensure cdls_next is never zero after it's been seen */
702 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
705 if (cdls != NULL && cdls->cdls_count != 0) {
708 nob = snprintf(buf2, sizeof(buf2),
709 "Skipped %d previous similar message%s\n",
710 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
712 catamount_printline(buf2, nob);
713 cdls->cdls_count = 0;
716 catamount_printline(buf, nob);
719 /* return on toconsole > 1, as we don't want the user getting
720 * spammed by the debug data */
724 if (debug_file_fd == NULL)
727 gettimeofday(&tv, NULL);
729 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
730 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
731 file, line, fn, buf);
737 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
740 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
741 "ASSERTION(%s) failed\n", expr);
745 #endif /* __KERNEL__ */