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 #ifdef HAVE_STACKTRACE_WARNING
188 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
190 printk("%s", (char *)data);
191 print_symbol(msg, symbol);
195 static void print_trace_warning(void *data, char *msg)
197 printk("%s%s\n", (char *)data, msg);
201 static int print_trace_stack(void *data, char *name)
203 printk(" <%s> ", name);
207 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
212 print_trace_address(void *data, unsigned long addr, int reliable)
216 touch_nmi_watchdog();
217 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
218 __print_symbol(fmt, addr);
219 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
224 static const struct stacktrace_ops print_trace_ops = {
225 #ifdef HAVE_STACKTRACE_WARNING
226 .warning = print_trace_warning,
227 .warning_symbol = print_trace_warning_symbol,
229 .stack = print_trace_stack,
230 .address = print_trace_address,
231 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
232 .walk_stack = print_context_stack,
235 #endif /* HAVE_STACKTRACE_OPS */
237 static void libcfs_call_trace(struct task_struct *tsk)
239 #ifdef HAVE_STACKTRACE_OPS
240 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
241 printk("\nCall Trace:\n");
242 dump_trace(tsk, NULL, NULL,
243 #ifdef HAVE_DUMP_TRACE_ADDRESS
245 #endif /* HAVE_DUMP_TRACE_ADDRESS */
246 &print_trace_ops, NULL);
248 #else /* !HAVE_STACKTRACE_OPS */
252 CWARN("can't show stack: kernel doesn't export show_task\n");
253 #endif /* HAVE_STACKTRACE_OPS */
256 #else /* !CONFIG_X86 */
258 static void libcfs_call_trace(struct task_struct *tsk)
263 CWARN("can't show stack: kernel doesn't export show_task\n");
266 #endif /* CONFIG_X86 */
268 #endif /* CONFIG_STACKTRACE */
270 void libcfs_debug_dumpstack(struct task_struct *tsk)
272 libcfs_call_trace(tsk ?: current);
274 EXPORT_SYMBOL(libcfs_debug_dumpstack);
276 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
279 if (libcfs_panic_in_progress)
282 libcfs_panic_in_progress = 1;
285 #ifdef LNET_DUMP_ON_PANIC
286 /* This is currently disabled because it spews far too much to the
287 * console on the rare cases it is ever triggered. */
289 if (in_interrupt()) {
290 cfs_trace_debug_print();
292 libcfs_debug_dumplog_internal((void *)(long)current_pid());
298 static struct notifier_block libcfs_panic_notifier = {
299 .notifier_call = panic_notifier,
304 void libcfs_register_panic_notifier(void)
306 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
309 void libcfs_unregister_panic_notifier(void)
311 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);