4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/debug.c
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define DEBUG_SUBSYSTEM S_LNET
44 #include "../../include/linux/libcfs/libcfs.h"
45 #include "tracefile.h"
47 static char debug_file_name[1024];
49 unsigned int libcfs_subsystem_debug = ~0;
50 module_param(libcfs_subsystem_debug, int, 0644);
51 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
52 EXPORT_SYMBOL(libcfs_subsystem_debug);
54 unsigned int libcfs_debug = (D_CANTMASK |
55 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
56 module_param(libcfs_debug, int, 0644);
57 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
58 EXPORT_SYMBOL(libcfs_debug);
60 static int libcfs_param_debug_mb_set(const char *val,
61 const struct kernel_param *kp)
66 rc = kstrtouint(val, 0, &num);
70 if (!*((unsigned int *)kp->arg)) {
71 *((unsigned int *)kp->arg) = num;
75 rc = cfs_trace_set_debug_mb(num);
78 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
83 /* While debug_mb setting look like unsigned int, in fact
84 * it needs quite a bunch of extra processing, so we define special
85 * debugmb parameter type with corresponding methods to handle this case */
86 static struct kernel_param_ops param_ops_debugmb = {
87 .set = libcfs_param_debug_mb_set,
88 .get = param_get_uint,
91 #define param_check_debugmb(name, p) \
92 __param_check(name, p, unsigned int)
94 static unsigned int libcfs_debug_mb;
95 module_param(libcfs_debug_mb, debugmb, 0644);
96 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
97 EXPORT_SYMBOL(libcfs_debug_mb);
99 unsigned int libcfs_printk = D_CANTMASK;
100 module_param(libcfs_printk, uint, 0644);
101 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
102 EXPORT_SYMBOL(libcfs_printk);
104 unsigned int libcfs_console_ratelimit = 1;
105 module_param(libcfs_console_ratelimit, uint, 0644);
106 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
107 EXPORT_SYMBOL(libcfs_console_ratelimit);
109 static int param_set_delay_minmax(const char *val,
110 const struct kernel_param *kp,
117 rc = kstrtoint(val, 0, &sec);
121 d = cfs_time_seconds(sec) / 100;
122 if (d < min || d > max)
125 *((unsigned int *)kp->arg) = d;
130 static int param_get_delay(char *buffer, const struct kernel_param *kp)
132 unsigned int d = *(unsigned int *)kp->arg;
134 return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
137 unsigned int libcfs_console_max_delay;
138 EXPORT_SYMBOL(libcfs_console_max_delay);
139 unsigned int libcfs_console_min_delay;
140 EXPORT_SYMBOL(libcfs_console_min_delay);
142 static int param_set_console_max_delay(const char *val,
143 const struct kernel_param *kp)
145 return param_set_delay_minmax(val, kp,
146 libcfs_console_min_delay, INT_MAX);
149 static struct kernel_param_ops param_ops_console_max_delay = {
150 .set = param_set_console_max_delay,
151 .get = param_get_delay,
154 #define param_check_console_max_delay(name, p) \
155 __param_check(name, p, unsigned int)
157 module_param(libcfs_console_max_delay, console_max_delay, 0644);
158 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
160 static int param_set_console_min_delay(const char *val,
161 const struct kernel_param *kp)
163 return param_set_delay_minmax(val, kp,
164 1, libcfs_console_max_delay);
167 static struct kernel_param_ops param_ops_console_min_delay = {
168 .set = param_set_console_min_delay,
169 .get = param_get_delay,
172 #define param_check_console_min_delay(name, p) \
173 __param_check(name, p, unsigned int)
175 module_param(libcfs_console_min_delay, console_min_delay, 0644);
176 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
178 static int param_set_uint_minmax(const char *val,
179 const struct kernel_param *kp,
180 unsigned int min, unsigned int max)
187 ret = kstrtouint(val, 0, &num);
188 if (ret < 0 || num < min || num > max)
190 *((unsigned int *)kp->arg) = num;
194 static int param_set_uintpos(const char *val, const struct kernel_param *kp)
196 return param_set_uint_minmax(val, kp, 1, -1);
199 static struct kernel_param_ops param_ops_uintpos = {
200 .set = param_set_uintpos,
201 .get = param_get_uint,
204 #define param_check_uintpos(name, p) \
205 __param_check(name, p, unsigned int)
207 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
208 module_param(libcfs_console_backoff, uintpos, 0644);
209 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
210 EXPORT_SYMBOL(libcfs_console_backoff);
212 unsigned int libcfs_debug_binary = 1;
213 EXPORT_SYMBOL(libcfs_debug_binary);
215 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
216 EXPORT_SYMBOL(libcfs_stack);
218 unsigned int libcfs_catastrophe;
219 EXPORT_SYMBOL(libcfs_catastrophe);
221 unsigned int libcfs_panic_on_lbug = 1;
222 module_param(libcfs_panic_on_lbug, uint, 0644);
223 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
224 EXPORT_SYMBOL(libcfs_panic_on_lbug);
226 static wait_queue_head_t debug_ctlwq;
228 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
230 /* We need to pass a pointer here, but elsewhere this must be a const */
231 static char *libcfs_debug_file_path;
232 module_param(libcfs_debug_file_path, charp, 0644);
233 MODULE_PARM_DESC(libcfs_debug_file_path,
234 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
236 int libcfs_panic_in_progress;
238 /* libcfs_debug_token2mask() expects the returned
239 * string in lower-case */
241 libcfs_debug_subsys2str(int subsys)
243 switch (1 << subsys) {
299 /* libcfs_debug_token2mask() expects the returned
300 * string in lower-case */
302 libcfs_debug_dbg2str(int debug)
304 switch (1 << debug) {
369 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
371 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
372 libcfs_debug_dbg2str;
377 if (mask == 0) { /* "0" */
381 } else { /* space-separated tokens */
382 for (i = 0; i < 32; i++) {
383 if ((mask & (1 << i)) == 0)
387 if (token == NULL) /* unused bit */
390 if (len > 0) { /* separator? */
396 while (*token != 0) {
405 /* terminate 'str' */
415 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
417 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
418 libcfs_debug_dbg2str;
424 /* Allow a number for backwards compatibility */
426 for (n = strlen(str); n > 0; n--)
427 if (!isspace(str[n-1]))
430 t = sscanf(str, "%i%n", &m, &matched);
431 if (t >= 1 && matched == n) {
432 /* don't print warning for lctl set_param debug=0 or -1 */
433 if (m != 0 && m != -1)
434 CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
439 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
444 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
446 void libcfs_debug_dumplog_internal(void *arg)
450 journal_info = current->journal_info;
451 current->journal_info = NULL;
453 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
454 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
455 "%s.%lld.%ld", libcfs_debug_file_path_arr,
456 (s64)ktime_get_real_seconds(), (long_ptr_t)arg);
457 pr_alert("LustreError: dumping log to %s\n",
459 cfs_tracefile_dump_all_pages(debug_file_name);
460 libcfs_run_debug_log_upcall(debug_file_name);
463 current->journal_info = journal_info;
466 static int libcfs_debug_dumplog_thread(void *arg)
468 libcfs_debug_dumplog_internal(arg);
469 wake_up(&debug_ctlwq);
473 void libcfs_debug_dumplog(void)
476 struct task_struct *dumper;
478 /* we're being careful to ensure that the kernel thread is
479 * able to set our state to running as it exits before we
480 * get to schedule() */
481 init_waitqueue_entry(&wait, current);
482 set_current_state(TASK_INTERRUPTIBLE);
483 add_wait_queue(&debug_ctlwq, &wait);
485 dumper = kthread_run(libcfs_debug_dumplog_thread,
486 (void *)(long)current_pid(),
487 "libcfs_debug_dumper");
489 pr_err("LustreError: cannot start log dump thread: %ld\n",
494 /* be sure to teardown if cfs_create_thread() failed */
495 remove_wait_queue(&debug_ctlwq, &wait);
496 set_current_state(TASK_RUNNING);
498 EXPORT_SYMBOL(libcfs_debug_dumplog);
500 int libcfs_debug_init(unsigned long bufsize)
503 unsigned int max = libcfs_debug_mb;
505 init_waitqueue_head(&debug_ctlwq);
507 if (libcfs_console_max_delay <= 0 || /* not set by user or */
508 libcfs_console_min_delay <= 0 || /* set to invalid values */
509 libcfs_console_min_delay >= libcfs_console_max_delay) {
510 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
511 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
514 if (libcfs_debug_file_path != NULL) {
515 strncpy(libcfs_debug_file_path_arr,
516 libcfs_debug_file_path, PATH_MAX-1);
517 libcfs_debug_file_path_arr[PATH_MAX - 1] = '\0';
520 /* If libcfs_debug_mb is set to an invalid value or uninitialized
521 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
522 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
525 max = max / num_possible_cpus();
526 max <<= (20 - PAGE_CACHE_SHIFT);
528 rc = cfs_tracefile_init(max);
531 libcfs_register_panic_notifier();
532 libcfs_debug_mb = cfs_trace_get_debug_mb();
538 int libcfs_debug_cleanup(void)
540 libcfs_unregister_panic_notifier();
541 cfs_tracefile_exit();
545 int libcfs_debug_clear_buffer(void)
547 cfs_trace_flush_pages();
551 /* Debug markers, although printed by S_LNET
552 * should not be be marked as such. */
553 #undef DEBUG_SUBSYSTEM
554 #define DEBUG_SUBSYSTEM S_UNDEFINED
555 int libcfs_debug_mark_buffer(const char *text)
558 "***************************************************\n");
559 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
561 "***************************************************\n");
566 #undef DEBUG_SUBSYSTEM
567 #define DEBUG_SUBSYSTEM S_LNET
569 void libcfs_debug_set_level(unsigned int debug_level)
571 pr_warn("Lustre: Setting portals debug level to %08x\n",
573 libcfs_debug = debug_level;
576 EXPORT_SYMBOL(libcfs_debug_set_level);