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) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/linux/linux-debug.c
34 * Author: Phil Schwan <phil@clusterfs.com>
37 #include <linux/errno.h>
38 #include <linux/interrupt.h>
39 #include <linux/kallsyms.h>
40 #include <linux/kmod.h>
41 #include <linux/module.h>
42 #include <linux/notifier.h>
43 #include <linux/string.h>
44 #include <linux/unistd.h>
45 #include <linux/stacktrace.h>
46 #include <linux/utsname.h>
48 # define DEBUG_SUBSYSTEM S_LNET
50 #include <libcfs/libcfs.h>
52 #include "tracefile.h"
54 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
57 * Upcall function once a Lustre log has been dumped.
59 * \param file path of the dumped log
61 void libcfs_run_debug_log_upcall(char *file)
67 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
71 argv[0] = lnet_debug_log_upcall;
73 LASSERTF(file != NULL, "called on a null filename\n");
74 argv[1] = file; //only need to pass the path of the file
78 rc = call_usermodehelper(argv[0], argv, envp, 1);
79 if (rc < 0 && rc != -ENOENT) {
80 CERROR("Error %d invoking LNET debug log upcall %s %s; "
81 "check /proc/sys/lnet/debug_log_upcall\n",
82 rc, argv[0], argv[1]);
84 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
92 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
94 libcfs_catastrophe = 1;
95 libcfs_debug_msg(msgdata, "LBUG\n");
98 panic("LBUG in interrupt.\n");
102 libcfs_debug_dumpstack(NULL);
103 if (libcfs_panic_on_lbug)
106 libcfs_debug_dumplog();
107 set_current_state(TASK_UNINTERRUPTIBLE);
111 EXPORT_SYMBOL(lbug_with_loc);
113 #ifdef CONFIG_STACKTRACE
115 #ifndef HAVE_SAVE_STACK_TRACE_TSK
116 #define save_stack_trace_tsk(tsk, trace) \
118 if (tsk == current) \
119 save_stack_trace(trace); \
121 pr_info("No stack, save_stack_trace_tsk() not exported\n"); \
125 #define MAX_ST_ENTRIES 100
126 static DEFINE_SPINLOCK(st_lock);
129 * Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
130 * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
131 * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
133 #ifdef CONFIG_ARCH_STACKWALK
134 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
135 unsigned long *store, unsigned int size,
136 unsigned int skipnr);
137 static stack_trace_save_tsk_t *task_dump_stack;
140 static void libcfs_call_trace(struct task_struct *tsk)
142 #ifdef CONFIG_ARCH_STACKWALK
143 static unsigned long entries[MAX_ST_ENTRIES];
144 unsigned int i, nr_entries;
146 if (!task_dump_stack)
147 task_dump_stack = (stack_trace_save_tsk_t *)
148 symbol_get("stack_trace_save_tsk");
151 pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
152 init_utsname()->release, init_utsname()->version);
153 pr_info("Call Trace TBD:\n");
154 if (task_dump_stack) {
155 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
156 for (i = 0; i < nr_entries; i++)
157 pr_info("[<0>] %pB\n", (void *)entries[i]);
159 spin_unlock(&st_lock);
161 struct stack_trace trace;
162 static unsigned long entries[MAX_ST_ENTRIES];
164 trace.nr_entries = 0;
165 trace.max_entries = MAX_ST_ENTRIES;
166 trace.entries = entries;
170 pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
171 init_utsname()->release, init_utsname()->version);
172 pr_info("Call Trace:\n");
173 save_stack_trace_tsk(tsk, &trace);
174 print_stack_trace(&trace, 0);
175 spin_unlock(&st_lock);
179 #else /* !CONFIG_STACKTRACE */
182 #include <linux/nmi.h>
183 #include <asm/stacktrace.h>
185 #ifdef HAVE_STACKTRACE_OPS
186 static int print_trace_stack(void *data, char *name)
188 printk(" <%s> ", name);
192 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
197 print_trace_address(void *data, unsigned long addr, int reliable)
201 touch_nmi_watchdog();
202 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
203 __print_symbol(fmt, addr);
204 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
209 static const struct stacktrace_ops print_trace_ops = {
210 .stack = print_trace_stack,
211 .address = print_trace_address,
212 .walk_stack = print_context_stack,
214 #endif /* HAVE_STACKTRACE_OPS */
216 static void libcfs_call_trace(struct task_struct *tsk)
218 #ifdef HAVE_STACKTRACE_OPS
219 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
220 printk("\nCall Trace:\n");
221 dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
223 #else /* !HAVE_STACKTRACE_OPS */
227 CWARN("can't show stack: kernel doesn't export show_task\n");
228 #endif /* HAVE_STACKTRACE_OPS */
231 #else /* !CONFIG_X86 */
233 static void libcfs_call_trace(struct task_struct *tsk)
238 CWARN("can't show stack: kernel doesn't export show_task\n");
241 #endif /* CONFIG_X86 */
243 #endif /* CONFIG_STACKTRACE */
245 void libcfs_debug_dumpstack(struct task_struct *tsk)
247 libcfs_call_trace(tsk ?: current);
249 EXPORT_SYMBOL(libcfs_debug_dumpstack);
251 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
254 if (libcfs_panic_in_progress)
257 libcfs_panic_in_progress = 1;
260 #ifdef LNET_DUMP_ON_PANIC
261 /* This is currently disabled because it spews far too much to the
262 * console on the rare cases it is ever triggered. */
264 if (in_interrupt()) {
265 cfs_trace_debug_print();
267 libcfs_debug_dumplog_internal((void *)(long)current_pid());
273 static struct notifier_block libcfs_panic_notifier = {
274 .notifier_call = panic_notifier,
279 void libcfs_register_panic_notifier(void)
281 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
284 void libcfs_unregister_panic_notifier(void)
286 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);