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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * libcfs/libcfs/debug.c
33 * Author: Phil Schwan <phil@clusterfs.com>
37 # define DEBUG_SUBSYSTEM S_LNET
39 #include <linux/module.h>
40 #include <linux/ctype.h>
41 #include <libcfs/libcfs_string.h>
42 #include <linux/kthread.h>
43 #include <linux/stacktrace.h>
44 #include <linux/utsname.h>
45 #include <linux/kallsyms.h>
46 #include "tracefile.h"
48 static char debug_file_name[1024];
50 unsigned int libcfs_subsystem_debug = ~0;
51 EXPORT_SYMBOL(libcfs_subsystem_debug);
52 module_param(libcfs_subsystem_debug, int, 0644);
53 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
55 unsigned int libcfs_debug = (D_CANTMASK | D_NETERROR | D_HA | D_CONFIG |
56 D_IOCTL | D_LFSCK | D_TTY);
57 EXPORT_SYMBOL(libcfs_debug);
58 module_param(libcfs_debug, int, 0644);
59 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
61 static int libcfs_param_debug_mb_set(const char *val,
62 cfs_kernel_param_arg_t *kp)
67 rc = kstrtouint(val, 0, &num);
71 num = cfs_trace_set_debug_mb(num);
73 *((unsigned int *)kp->arg) = num;
74 num = cfs_trace_get_debug_mb();
76 /* This value is more precise */
77 *((unsigned int *)kp->arg) = num;
82 /* While debug_mb setting look like unsigned int, in fact
83 * it needs quite a bunch of extra processing, so we define special
84 * debug_mb parameter type with corresponding methods to handle this case
86 static const struct kernel_param_ops param_ops_debug_mb = {
87 .set = libcfs_param_debug_mb_set,
88 .get = param_get_uint,
91 #define param_check_debug_mb(name, p) \
92 __param_check(name, p, unsigned int)
94 static unsigned int libcfs_debug_mb;
95 #ifdef HAVE_KERNEL_PARAM_OPS
96 module_param(libcfs_debug_mb, debug_mb, 0644);
98 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
99 ¶m_ops_debug_mb, 0644);
101 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
103 unsigned int libcfs_printk = D_CANTMASK;
104 module_param(libcfs_printk, uint, 0644);
105 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
107 unsigned int libcfs_console_ratelimit = 1;
108 module_param(libcfs_console_ratelimit, uint, 0644);
109 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
111 static int param_set_delay_minmax(const char *val,
112 cfs_kernel_param_arg_t *kp,
119 rc = kstrtoint(val, 0, &sec);
123 /* The sysfs setting is in centiseconds */
124 d = cfs_time_seconds(sec) / 100;
125 if (d < min || d > max)
128 *((unsigned int *)kp->arg) = d;
133 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
135 unsigned int d = *(unsigned int *)kp->arg;
137 param_get_byte(buffer, kp);
138 return sprintf(buffer, "%lu%c", jiffies_to_msecs(d * 10) / MSEC_PER_SEC,
139 strnchr(buffer, PAGE_SIZE, '\n') ? '\n' : '\0');
142 unsigned int libcfs_console_max_delay;
143 unsigned int libcfs_console_min_delay;
145 static int param_set_console_max_delay(const char *val,
146 cfs_kernel_param_arg_t *kp)
148 return param_set_delay_minmax(val, kp,
149 libcfs_console_min_delay, INT_MAX);
152 static const struct kernel_param_ops param_ops_console_max_delay = {
153 .set = param_set_console_max_delay,
154 .get = param_get_delay,
157 #define param_check_console_max_delay(name, p) \
158 __param_check(name, p, unsigned int)
160 #ifdef HAVE_KERNEL_PARAM_OPS
161 module_param(libcfs_console_max_delay, console_max_delay, 0644);
163 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
164 param_get_delay, ¶m_ops_console_max_delay, 0644);
166 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
168 static int param_set_console_min_delay(const char *val,
169 cfs_kernel_param_arg_t *kp)
171 return param_set_delay_minmax(val, kp,
172 1, libcfs_console_max_delay);
175 static const struct kernel_param_ops param_ops_console_min_delay = {
176 .set = param_set_console_min_delay,
177 .get = param_get_delay,
180 #define param_check_console_min_delay(name, p) \
181 __param_check(name, p, unsigned int)
183 #ifdef HAVE_KERNEL_PARAM_OPS
184 module_param(libcfs_console_min_delay, console_min_delay, 0644);
186 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
187 param_get_delay, ¶m_ops_console_min_delay, 0644);
189 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
191 #ifndef HAVE_PARAM_SET_UINT_MINMAX
192 static int param_set_uint_minmax(const char *val,
193 cfs_kernel_param_arg_t *kp,
194 unsigned int min, unsigned int max)
202 ret = kstrtouint(val, 0, &num);
203 if (ret < 0 || num < min || num > max)
206 *((unsigned int *)kp->arg) = num;
211 static int param_set_uintpos(const char *val,
212 cfs_kernel_param_arg_t *kp)
214 return param_set_uint_minmax(val, kp, 1, -1);
217 static const struct kernel_param_ops param_ops_uintpos = {
218 .set = param_set_uintpos,
219 .get = param_get_uint,
222 #define param_check_uintpos(name, p) \
223 __param_check(name, p, unsigned int)
225 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
226 #ifdef HAVE_KERNEL_PARAM_OPS
227 module_param(libcfs_console_backoff, uintpos, 0644);
229 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
230 ¶m_ops_uintpos, 0644);
232 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
234 unsigned int libcfs_debug_binary = 1;
236 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
237 EXPORT_SYMBOL(libcfs_stack);
239 unsigned int libcfs_catastrophe;
240 EXPORT_SYMBOL(libcfs_catastrophe);
242 unsigned int libcfs_watchdog_ratelimit = 300;
243 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
245 unsigned int libcfs_panic_on_lbug = 1;
246 module_param(libcfs_panic_on_lbug, uint, 0644);
247 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
249 atomic64_t libcfs_kmem = ATOMIC64_INIT(0);
250 EXPORT_SYMBOL(libcfs_kmem);
252 static DECLARE_COMPLETION(debug_complete);
254 /* We need to pass a pointer here, but elsewhere this must be a const */
255 char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
256 EXPORT_SYMBOL(libcfs_debug_file_path);
257 module_param(libcfs_debug_file_path, charp, 0644);
258 MODULE_PARM_DESC(libcfs_debug_file_path,
259 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
261 int libcfs_panic_in_progress;
263 /* libcfs_debug_token2mask() expects the returned string in lower-case */
264 static const char *libcfs_debug_subsys2str(int subsys)
266 static const char * const libcfs_debug_subsystems[] =
267 LIBCFS_DEBUG_SUBSYS_NAMES;
269 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
272 return libcfs_debug_subsystems[subsys];
275 /* libcfs_debug_token2mask() expects the returned string in lower-case */
276 static const char *libcfs_debug_dbg2str(int debug)
278 static const char * const libcfs_debug_masks[] =
279 LIBCFS_DEBUG_MASKS_NAMES;
281 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
284 return libcfs_debug_masks[debug];
288 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
290 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
291 libcfs_debug_dbg2str;
296 if (mask == 0) { /* "0" */
300 } else { /* space-separated tokens */
301 for (i = 0; i < 32; i++) {
302 if ((mask & BIT(i)) == 0)
306 if (!token) /* unused bit */
309 if (len > 0) { /* separator? */
315 while (*token != 0) {
324 /* terminate 'str' */
334 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
336 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
337 libcfs_debug_dbg2str;
343 /* Allow a number for backwards compatibility */
344 for (n = strlen(str); n > 0; n--)
345 if (!isspace(str[n-1]))
348 t = sscanf(str, "%i%n", &m, &matched);
349 if (t >= 1 && matched == n) {
350 /* don't print warning for lctl set_param debug=0 or -1 */
351 if (m != 0 && m != -1)
352 CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
357 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
361 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
363 /* Upcall function once a Lustre log has been dumped.
365 * @file path of the dumped log
367 static void libcfs_run_debug_log_upcall(char *file)
371 static const char * const envp[] = {
373 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
378 argv[0] = lnet_debug_log_upcall;
380 LASSERTF(file, "called on a null filename\n");
381 argv[1] = file; /* only need to pass the path of the file */
385 rc = call_usermodehelper(argv[0], argv, (char **)envp, 1);
386 if (rc < 0 && rc != -ENOENT) {
387 CERROR("Error %d invoking LNET debug log upcall %s %s; check /sys/kernel/debug/lnet/debug_log_upcall\n",
388 rc, argv[0], argv[1]);
390 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
396 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
398 static void libcfs_debug_dumplog_internal(void *arg)
400 static time64_t last_dump_time;
401 time64_t current_time;
403 current_time = ktime_get_real_seconds();
405 if (strncmp(libcfs_debug_file_path, "NONE", 4) != 0 &&
406 current_time > last_dump_time) {
407 last_dump_time = current_time;
408 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
409 "%s.%lld.%ld", libcfs_debug_file_path,
410 (s64)current_time, (uintptr_t)arg);
411 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
412 cfs_tracefile_dump_all_pages(debug_file_name);
413 libcfs_run_debug_log_upcall(debug_file_name);
417 static int libcfs_debug_dumplog_thread(void *arg)
419 libcfs_debug_dumplog_internal(arg);
420 complete(&debug_complete);
424 static DEFINE_MUTEX(libcfs_debug_dumplog_lock);
426 void libcfs_debug_dumplog(void)
428 struct task_struct *dumper;
432 if (mutex_trylock(&libcfs_debug_dumplog_lock) == 0)
435 /* If a previous call was interrupted, debug_complete->done
436 * might be elevated, and so we won't actually wait here.
437 * So we reinit the completion to ensure we wait for
438 * one thread to complete, though it might not be the one
439 * we start if there are overlaping thread.
441 reinit_completion(&debug_complete);
442 dumper = kthread_run(libcfs_debug_dumplog_thread,
443 (void *)(long)current->pid,
444 "libcfs_debug_dumper");
446 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
449 wait_for_completion_interruptible(&debug_complete);
451 mutex_unlock(&libcfs_debug_dumplog_lock);
453 EXPORT_SYMBOL(libcfs_debug_dumplog);
455 /* coverity[+kill] */
456 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
458 libcfs_catastrophe = 1;
459 libcfs_debug_msg(msgdata, "LBUG\n");
461 if (in_interrupt()) {
462 panic("LBUG in interrupt.\n");
466 libcfs_debug_dumpstack(NULL);
467 if (libcfs_panic_on_lbug)
470 libcfs_debug_dumplog();
471 set_current_state(TASK_UNINTERRUPTIBLE);
475 EXPORT_SYMBOL(lbug_with_loc);
477 #ifdef CONFIG_STACKTRACE
479 #ifndef HAVE_SAVE_STACK_TRACE_TSK
480 #define save_stack_trace_tsk(tsk, trace) \
482 if (tsk == current) \
483 save_stack_trace(trace); \
485 pr_info("No stack, save_stack_trace_tsk() not exported\n"); \
489 static void cfs_print_stack_trace(unsigned long *entries, unsigned int nr)
493 /* Prefer %pB for backtraced symbolic names since it was added in:
494 * Linux v2.6.38-6557-g0f77a8d37825
495 * vsprintf: Introduce %pB format specifier
497 for (i = 0; i < nr; i++)
498 pr_info("[<0>] %pB\n", (void *)entries[i]);
501 #define MAX_ST_ENTRIES 100
502 static DEFINE_SPINLOCK(st_lock);
504 /* Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
505 * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
506 * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
508 #ifdef CONFIG_ARCH_STACKWALK
509 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
510 unsigned long *store,
512 unsigned int skipnr);
513 static stack_trace_save_tsk_t *task_dump_stack;
516 void __init cfs_debug_init(void)
518 #ifdef CONFIG_ARCH_STACKWALK
519 task_dump_stack = (void *)
520 cfs_kallsyms_lookup_name("stack_trace_save_tsk");
525 static void libcfs_call_trace(struct task_struct *tsk)
527 static unsigned long entries[MAX_ST_ENTRIES];
528 #ifdef CONFIG_ARCH_STACKWALK
529 unsigned int nr_entries;
532 pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
533 init_utsname()->release, init_utsname()->version);
534 pr_info("Call Trace TBD:\n");
535 if (task_dump_stack) {
536 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
537 cfs_print_stack_trace(entries, nr_entries);
539 spin_unlock(&st_lock);
541 struct stack_trace trace;
543 trace.nr_entries = 0;
544 trace.max_entries = MAX_ST_ENTRIES;
545 trace.entries = entries;
549 pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
550 init_utsname()->release, init_utsname()->version);
551 pr_info("Call Trace:\n");
552 save_stack_trace_tsk(tsk, &trace);
553 cfs_print_stack_trace(trace.entries, trace.nr_entries);
554 spin_unlock(&st_lock);
558 #else /* !CONFIG_STACKTRACE */
561 #include <linux/nmi.h>
562 #include <asm/stacktrace.h>
564 #ifdef HAVE_STACKTRACE_OPS
565 static int print_trace_stack(void *data, char *name)
567 printk(" <%s> ", name);
571 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
576 print_trace_address(void *data, unsigned long addr, int reliable)
580 touch_nmi_watchdog();
581 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "" : "? ");
582 __print_symbol(fmt, addr);
583 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
588 static const struct stacktrace_ops print_trace_ops = {
589 .stack = print_trace_stack,
590 .address = print_trace_address,
591 .walk_stack = print_context_stack,
593 #endif /* HAVE_STACKTRACE_OPS */
595 static void libcfs_call_trace(struct task_struct *tsk)
597 #ifdef HAVE_STACKTRACE_OPS
598 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
599 printk("\nCall Trace:\n");
600 dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
602 #else /* !HAVE_STACKTRACE_OPS */
606 CWARN("can't show stack: kernel doesn't export show_task\n");
607 #endif /* HAVE_STACKTRACE_OPS */
610 #else /* !CONFIG_X86 */
612 static void libcfs_call_trace(struct task_struct *tsk)
617 CWARN("can't show stack: kernel doesn't export show_task\n");
620 #endif /* CONFIG_X86 */
622 #endif /* CONFIG_STACKTRACE */
624 void libcfs_debug_dumpstack(struct task_struct *tsk)
626 libcfs_call_trace(tsk ?: current);
628 EXPORT_SYMBOL(libcfs_debug_dumpstack);
630 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
633 if (libcfs_panic_in_progress)
636 libcfs_panic_in_progress = 1;
639 #ifdef LNET_DUMP_ON_PANIC
640 /* This is currently disabled because it spews far too much to the
641 * console on the rare cases it is ever triggered. */
643 if (in_interrupt()) {
644 cfs_trace_debug_print();
646 libcfs_debug_dumplog_internal((void *)(long)current->pid);
652 static struct notifier_block libcfs_panic_notifier = {
653 .notifier_call = panic_notifier,
658 static void libcfs_register_panic_notifier(void)
660 atomic_notifier_chain_register(&panic_notifier_list,
661 &libcfs_panic_notifier);
664 static void libcfs_unregister_panic_notifier(void)
666 atomic_notifier_chain_unregister(&panic_notifier_list,
667 &libcfs_panic_notifier);
670 int libcfs_debug_init(unsigned long bufsize)
672 unsigned int max = libcfs_debug_mb;
675 if (libcfs_console_max_delay <= 0 || /* not set by user or */
676 libcfs_console_min_delay <= 0 || /* set to invalid values */
677 libcfs_console_min_delay >= libcfs_console_max_delay) {
678 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
679 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
682 /* If libcfs_debug_mb is uninitialized then just make the
683 * total buffers smp_num_cpus * TCD_MAX_PAGES
685 if (max < num_possible_cpus()) {
688 max = (max / num_possible_cpus());
689 max <<= (20 - PAGE_SHIFT);
692 rc = cfs_tracefile_init(max);
696 libcfs_register_panic_notifier();
697 kernel_param_lock(THIS_MODULE);
698 if (libcfs_debug_mb == 0)
699 libcfs_debug_mb = cfs_trace_get_debug_mb();
700 kernel_param_unlock(THIS_MODULE);
704 int libcfs_debug_cleanup(void)
706 libcfs_unregister_panic_notifier();
707 kernel_param_lock(THIS_MODULE);
708 cfs_tracefile_exit();
709 kernel_param_unlock(THIS_MODULE);
713 int libcfs_debug_clear_buffer(void)
715 cfs_trace_flush_pages();
719 /* Debug markers, although printed by S_LNET should not be be marked as such. */
720 #undef DEBUG_SUBSYSTEM
721 #define DEBUG_SUBSYSTEM S_UNDEFINED
722 int libcfs_debug_mark_buffer(const char *text)
725 "**************************************************\n");
726 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
728 "**************************************************\n");
733 #undef DEBUG_SUBSYSTEM
734 #define DEBUG_SUBSYSTEM S_LNET