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 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
63 EXPORT_SYMBOL(libcfs_kmemory);
65 static cfs_waitq_t debug_ctlwq;
67 char debug_file_path[1024] = "/tmp/lustre-log";
69 int libcfs_panic_in_progress;
71 /* libcfs_debug_token2mask() expects the returned
72 * string in lower-case */
74 libcfs_debug_subsys2str(int subsys)
128 /* libcfs_debug_token2mask() expects the returned
129 * string in lower-case */
131 libcfs_debug_dbg2str(int debug)
196 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
198 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
199 libcfs_debug_dbg2str;
205 if (mask == 0) { /* "0" */
209 } else { /* space-separated tokens */
210 for (i = 0; i < 32; i++) {
213 if ((mask & bit) == 0)
217 if (token == NULL) /* unused bit */
220 if (len > 0) { /* separator? */
226 while (*token != 0) {
235 /* terminate 'str' */
245 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
247 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
248 libcfs_debug_dbg2str;
254 /* match against known tokens */
255 for (i = 0; i < 32; i++) {
259 if (token == NULL) /* unused? */
264 if (j == len) { /* end of token */
275 if (str[j] == token[j])
278 if (str[j] < 'A' || 'Z' < str[j])
281 if (str[j] - 'A' + 'a' != token[j])
286 return -EINVAL; /* no match */
290 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
298 /* <str> must be a list of debug tokens or numbers separated by
299 * whitespace and optionally an operator ('+' or '-'). If an operator
300 * appears first in <str>, '*mask' is used as the starting point
301 * (relative), otherwise 0 is used (absolute). An operator applies to
302 * all following tokens up to the next operator. */
305 while (isspace(*str)) /* skip whitespace */
311 if (*str == '+' || *str == '-') {
314 /* op on first token == relative */
318 while (isspace(*str)) /* skip whitespace */
321 if (*str == 0) /* trailing op */
325 /* find token length */
326 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
329 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
348 void libcfs_debug_dumplog_internal(void *arg)
350 CFS_DECL_JOURNAL_DATA;
354 snprintf(debug_file_name, sizeof(debug_file_path) - 1, "%s.%ld.%ld",
355 debug_file_path, cfs_time_current_sec(), (long)arg);
356 printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
357 tracefile_dump_all_pages(debug_file_name);
362 int libcfs_debug_dumplog_thread(void *arg)
365 libcfs_debug_dumplog_internal(arg);
366 cfs_waitq_signal(&debug_ctlwq);
370 void libcfs_debug_dumplog(void)
376 /* we're being careful to ensure that the kernel thread is
377 * able to set our state to running as it exits before we
378 * get to schedule() */
379 cfs_waitlink_init(&wait);
380 set_current_state(TASK_INTERRUPTIBLE);
381 cfs_waitq_add(&debug_ctlwq, &wait);
383 rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
384 (void *)(long)cfs_curproc_pid(),
385 CLONE_VM | CLONE_FS | CLONE_FILES);
387 printk(KERN_ERR "LustreError: cannot start log dump thread: "
390 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
392 /* be sure to teardown if kernel_thread() failed */
393 cfs_waitq_del(&debug_ctlwq, &wait);
394 set_current_state(TASK_RUNNING);
397 int libcfs_debug_init(unsigned long bufsize)
401 cfs_waitq_init(&debug_ctlwq);
402 rc = tracefile_init();
405 libcfs_register_panic_notifier();
410 int libcfs_debug_cleanup(void)
412 libcfs_unregister_panic_notifier();
417 int libcfs_debug_clear_buffer(void)
423 /* Debug markers, although printed by S_LNET
424 * should not be be marked as such. */
425 #undef DEBUG_SUBSYSTEM
426 #define DEBUG_SUBSYSTEM S_UNDEFINED
427 int libcfs_debug_mark_buffer(char *text)
429 CDEBUG(D_TRACE,"***************************************************\n");
430 CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
431 CDEBUG(D_TRACE,"***************************************************\n");
435 #undef DEBUG_SUBSYSTEM
436 #define DEBUG_SUBSYSTEM S_LNET
438 void libcfs_debug_set_level(unsigned int debug_level)
440 printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
442 libcfs_debug = debug_level;
445 EXPORT_SYMBOL(libcfs_debug_dumplog);
446 EXPORT_SYMBOL(libcfs_debug_set_level);
449 #else /* !__KERNEL__ */
451 #include <libcfs/libcfs.h>
453 #ifdef HAVE_SYS_USER_H
454 # include <sys/user.h>
457 #ifdef HAVE_CATAMOUNT_DATA_H
458 #include <catamount/data.h>
459 #include <catamount/lputs.h>
461 static char source_nid[16];
462 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
463 static int toconsole = 1;
464 unsigned int libcfs_console_ratelimit = 1;
465 #else /* !HAVE_CATAMOUNT_DATA_H */
467 #include <sys/utsname.h>
468 #endif /* HAVE_CATAMOUNT_DATA_H */
469 struct utsname *tmp_utsname;
470 static char source_nid[sizeof(tmp_utsname->nodename)];
471 #endif /* __KERNEL__ */
473 static int source_pid;
474 int smp_processor_id = 1;
475 char debug_file_path[1024];
478 int portals_do_debug_dumplog(void *arg)
480 printf("Look in %s\n", debug_file_name);
485 void portals_debug_print(void)
491 void libcfs_debug_dumplog(void)
493 printf("Look in %s\n", debug_file_name);
497 int libcfs_debug_init(unsigned long bufsize)
499 char *debug_mask = NULL;
500 char *debug_subsys = NULL;
501 char *debug_filename;
503 #ifdef HAVE_CATAMOUNT_DATA_H
504 char *debug_console = NULL;
505 char *debug_ratelimit = NULL;
507 snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
508 source_pid = _my_pid;
510 debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
511 if (debug_console != NULL) {
512 toconsole = strtoul(debug_console, NULL, 0);
513 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
515 debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
516 if (debug_ratelimit != NULL) {
517 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
518 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n", libcfs_console_ratelimit);
521 struct utsname myname;
523 if (uname(&myname) == 0)
524 strcpy(source_nid, myname.nodename);
525 source_pid = getpid();
528 debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
530 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
532 debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
534 libcfs_subsystem_debug =
535 (unsigned int) strtol(debug_subsys, NULL, 0);
537 debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
539 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
541 debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
543 strncpy(debug_file_name,debug_filename,sizeof(debug_file_path));
545 if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
546 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
547 "%s-%s-%lu.log", debug_file_path, source_nid, time(0));
549 if (strcmp(debug_file_name, "stdout") == 0 ||
550 strcmp(debug_file_name, "-") == 0) {
551 debug_file_fd = stdout;
552 } else if (strcmp(debug_file_name, "stderr") == 0) {
553 debug_file_fd = stderr;
554 } else if (debug_file_name[0] != '\0') {
555 debug_file_fd = fopen(debug_file_name, "w");
556 if (debug_file_fd == NULL)
557 fprintf(stderr, "%s: unable to open '%s': %s\n",
558 source_nid, debug_file_name, strerror(errno));
561 if (debug_file_fd == NULL)
562 debug_file_fd = stdout;
567 int libcfs_debug_cleanup(void)
569 if (debug_file_fd != stdout && debug_file_fd != stderr)
570 fclose(debug_file_fd);
574 int libcfs_debug_clear_buffer(void)
579 int libcfs_debug_mark_buffer(char *text)
582 fprintf(debug_file_fd, "*******************************************************************************\n");
583 fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
584 fprintf(debug_file_fd, "*******************************************************************************\n");
589 #ifdef HAVE_CATAMOUNT_DATA_H
590 #define CATAMOUNT_MAXLINE (256-4)
591 void catamount_printline(char *buf, size_t size)
598 pos += CATAMOUNT_MAXLINE;
599 prsize -= CATAMOUNT_MAXLINE;
605 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
606 int subsys, int mask,
607 const char *file, const char *fn, const int line,
608 const char *format1, va_list args,
609 const char *format2, ...)
615 char buf[PAGE_SIZE]; /* size 4096 used for compatimble with linux,
616 * where message can`t be exceed PAGE_SIZE */
618 char *prefix = "Lustre";
620 #ifdef HAVE_CATAMOUNT_DATA_H
621 /* toconsole == 0 - all messages to debug_file_fd
622 * toconsole == 1 - warnings to console, all to debug_file_fd
623 * toconsole > 1 - all debug to console */
624 if ( ((mask & D_CANTMASK) &&
625 (toconsole == 1)) || (toconsole > 1)) {
630 if ((!console) && (!debug_file_fd)) {
634 if (mask & (D_EMERG | D_ERROR))
635 prefix = "LustreError";
637 nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
638 source_pid, source_nid, file, line, fn);
640 remain = sizeof(buf) - nob;
642 nob += vsnprintf(&buf[nob], remain, format1, args);
645 remain = sizeof(buf) - nob;
646 if ((format2) && (remain > 0)) {
647 va_start(ap, format2);
648 nob += vsnprintf(&buf[nob], remain, format2, ap);
652 #ifdef HAVE_CATAMOUNT_DATA_H
654 /* check rate limit for console */
656 cfs_time_t t = cdls->cdls_next +
657 cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
658 cfs_duration_t dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
660 if (libcfs_console_ratelimit &&
661 cdls->cdls_next != 0 && /* not first time ever */
662 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
664 /* skipping a console message */
669 if (cfs_time_after(cfs_time_current(), t)) {
670 /* last timeout was a long time ago */
671 cdls->cdls_delay /= 8;
673 cdls->cdls_delay *= 2;
675 if (cdls->cdls_delay < CFS_TICK)
676 cdls->cdls_delay = CFS_TICK;
677 else if (cdls->cdls_delay > dmax)
678 cdls->cdls_delay = dmax;
681 /* ensure cdls_next is never zero after it's been seen */
682 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
685 if (cdls != NULL && cdls->cdls_count != 0) {
688 nob = snprintf(buf2, sizeof(buf2),
689 "Skipped %d previous similar message%s\n",
690 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
692 catamount_printline(buf2, nob);
693 cdls->cdls_count = 0;
696 catamount_printline(buf, nob);
699 /* return on toconsole > 1, as we don't want the user getting
700 * spammed by the debug data */
704 if (debug_file_fd == NULL)
707 gettimeofday(&tv, NULL);
709 fprintf(debug_file_fd, "%lu.%06lu:%u:%s:(%s:%d:%s()): %s",
710 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
711 file, line, fn, buf);
717 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
720 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
721 "ASSERTION(%s) failed\n", expr);
725 #endif /* __KERNEL__ */