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 unsigned int libcfs_debug_mb;
61 module_param(libcfs_debug_mb, uint, 0644);
62 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
63 EXPORT_SYMBOL(libcfs_debug_mb);
65 unsigned int libcfs_printk = D_CANTMASK;
66 module_param(libcfs_printk, uint, 0644);
67 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
68 EXPORT_SYMBOL(libcfs_printk);
70 unsigned int libcfs_console_ratelimit = 1;
71 module_param(libcfs_console_ratelimit, uint, 0644);
72 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
73 EXPORT_SYMBOL(libcfs_console_ratelimit);
75 unsigned int libcfs_console_max_delay;
76 module_param(libcfs_console_max_delay, uint, 0644);
77 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
78 EXPORT_SYMBOL(libcfs_console_max_delay);
80 unsigned int libcfs_console_min_delay;
81 module_param(libcfs_console_min_delay, uint, 0644);
82 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
83 EXPORT_SYMBOL(libcfs_console_min_delay);
85 static int param_set_uint_minmax(const char *val,
86 const struct kernel_param *kp,
87 unsigned int min, unsigned int max)
94 ret = kstrtouint(val, 0, &num);
95 if (ret == -EINVAL || num < min || num > max)
97 *((unsigned int *)kp->arg) = num;
101 static int param_set_uintpos(const char *val, const struct kernel_param *kp)
103 return param_set_uint_minmax(val, kp, 1, -1);
106 static struct kernel_param_ops param_ops_uintpos = {
107 .set = param_set_uintpos,
108 .get = param_get_uint,
111 #define param_check_uintpos(name, p) \
112 __param_check(name, p, unsigned int)
114 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
115 module_param(libcfs_console_backoff, uintpos, 0644);
116 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
117 EXPORT_SYMBOL(libcfs_console_backoff);
119 unsigned int libcfs_debug_binary = 1;
120 EXPORT_SYMBOL(libcfs_debug_binary);
122 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
123 EXPORT_SYMBOL(libcfs_stack);
125 static unsigned int portal_enter_debugger;
126 EXPORT_SYMBOL(portal_enter_debugger);
128 unsigned int libcfs_catastrophe;
129 EXPORT_SYMBOL(libcfs_catastrophe);
131 unsigned int libcfs_watchdog_ratelimit = 300;
132 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
134 unsigned int libcfs_panic_on_lbug = 1;
135 module_param(libcfs_panic_on_lbug, uint, 0644);
136 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
137 EXPORT_SYMBOL(libcfs_panic_on_lbug);
139 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
140 EXPORT_SYMBOL(libcfs_kmemory);
142 static wait_queue_head_t debug_ctlwq;
144 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
146 /* We need to pass a pointer here, but elsewhere this must be a const */
147 static char *libcfs_debug_file_path;
148 module_param(libcfs_debug_file_path, charp, 0644);
149 MODULE_PARM_DESC(libcfs_debug_file_path,
150 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
152 int libcfs_panic_in_progress;
154 /* libcfs_debug_token2mask() expects the returned
155 * string in lower-case */
157 libcfs_debug_subsys2str(int subsys)
159 switch (1 << subsys) {
215 /* libcfs_debug_token2mask() expects the returned
216 * string in lower-case */
218 libcfs_debug_dbg2str(int debug)
220 switch (1 << debug) {
285 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
287 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
288 libcfs_debug_dbg2str;
293 if (mask == 0) { /* "0" */
297 } else { /* space-separated tokens */
298 for (i = 0; i < 32; i++) {
299 if ((mask & (1 << i)) == 0)
303 if (token == NULL) /* unused bit */
306 if (len > 0) { /* separator? */
312 while (*token != 0) {
321 /* terminate 'str' */
331 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
333 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
334 libcfs_debug_dbg2str;
340 /* Allow a number for backwards compatibility */
342 for (n = strlen(str); n > 0; n--)
343 if (!isspace(str[n-1]))
346 t = sscanf(str, "%i%n", &m, &matched);
347 if (t >= 1 && matched == n) {
348 /* don't print warning for lctl set_param debug=0 or -1 */
349 if (m != 0 && m != -1)
350 CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
355 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
360 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
362 void libcfs_debug_dumplog_internal(void *arg)
366 journal_info = current->journal_info;
367 current->journal_info = NULL;
369 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
370 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
371 "%s.%ld.%ld", libcfs_debug_file_path_arr,
372 get_seconds(), (long_ptr_t)arg);
373 pr_alert("LustreError: dumping log to %s\n",
375 cfs_tracefile_dump_all_pages(debug_file_name);
376 libcfs_run_debug_log_upcall(debug_file_name);
379 current->journal_info = journal_info;
382 static int libcfs_debug_dumplog_thread(void *arg)
384 libcfs_debug_dumplog_internal(arg);
385 wake_up(&debug_ctlwq);
389 void libcfs_debug_dumplog(void)
392 struct task_struct *dumper;
394 /* we're being careful to ensure that the kernel thread is
395 * able to set our state to running as it exits before we
396 * get to schedule() */
397 init_waitqueue_entry(&wait, current);
398 set_current_state(TASK_INTERRUPTIBLE);
399 add_wait_queue(&debug_ctlwq, &wait);
401 dumper = kthread_run(libcfs_debug_dumplog_thread,
402 (void *)(long)current_pid(),
403 "libcfs_debug_dumper");
405 pr_err("LustreError: cannot start log dump thread: %ld\n",
410 /* be sure to teardown if cfs_create_thread() failed */
411 remove_wait_queue(&debug_ctlwq, &wait);
412 set_current_state(TASK_RUNNING);
414 EXPORT_SYMBOL(libcfs_debug_dumplog);
416 int libcfs_debug_init(unsigned long bufsize)
419 unsigned int max = libcfs_debug_mb;
421 init_waitqueue_head(&debug_ctlwq);
423 if (libcfs_console_max_delay <= 0 || /* not set by user or */
424 libcfs_console_min_delay <= 0 || /* set to invalid values */
425 libcfs_console_min_delay >= libcfs_console_max_delay) {
426 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
427 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
430 if (libcfs_debug_file_path != NULL) {
431 strncpy(libcfs_debug_file_path_arr,
432 libcfs_debug_file_path, PATH_MAX-1);
433 libcfs_debug_file_path_arr[PATH_MAX - 1] = '\0';
436 /* If libcfs_debug_mb is set to an invalid value or uninitialized
437 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
438 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
441 max = max / num_possible_cpus();
442 max <<= (20 - PAGE_CACHE_SHIFT);
444 rc = cfs_tracefile_init(max);
447 libcfs_register_panic_notifier();
452 int libcfs_debug_cleanup(void)
454 libcfs_unregister_panic_notifier();
455 cfs_tracefile_exit();
459 int libcfs_debug_clear_buffer(void)
461 cfs_trace_flush_pages();
465 /* Debug markers, although printed by S_LNET
466 * should not be be marked as such. */
467 #undef DEBUG_SUBSYSTEM
468 #define DEBUG_SUBSYSTEM S_UNDEFINED
469 int libcfs_debug_mark_buffer(const char *text)
472 "***************************************************\n");
473 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
475 "***************************************************\n");
479 #undef DEBUG_SUBSYSTEM
480 #define DEBUG_SUBSYSTEM S_LNET
482 void libcfs_debug_set_level(unsigned int debug_level)
484 pr_warn("Lustre: Setting portals debug level to %08x\n",
486 libcfs_debug = debug_level;
489 EXPORT_SYMBOL(libcfs_debug_set_level);