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 #ifdef HAVE_KERNEL_LOCKED
44 #include <linux/smp_lock.h>
46 #include <linux/string.h>
47 #include <linux/unistd.h>
48 #include <linux/stacktrace.h>
49 #include <linux/utsname.h>
51 # define DEBUG_SUBSYSTEM S_LNET
53 #include <libcfs/libcfs.h>
55 #include "tracefile.h"
57 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
60 * Upcall function once a Lustre log has been dumped.
62 * \param file path of the dumped log
64 void libcfs_run_debug_log_upcall(char *file)
70 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
74 argv[0] = lnet_debug_log_upcall;
76 LASSERTF(file != NULL, "called on a null filename\n");
77 argv[1] = file; //only need to pass the path of the file
81 rc = call_usermodehelper(argv[0], argv, envp, 1);
82 if (rc < 0 && rc != -ENOENT) {
83 CERROR("Error %d invoking LNET debug log upcall %s %s; "
84 "check /proc/sys/lnet/debug_log_upcall\n",
85 rc, argv[0], argv[1]);
87 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
95 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
97 libcfs_catastrophe = 1;
98 libcfs_debug_msg(msgdata, "LBUG\n");
100 if (in_interrupt()) {
101 panic("LBUG in interrupt.\n");
105 libcfs_debug_dumpstack(NULL);
106 if (libcfs_panic_on_lbug)
109 libcfs_debug_dumplog();
110 set_current_state(TASK_UNINTERRUPTIBLE);
114 EXPORT_SYMBOL(lbug_with_loc);
116 #ifdef CONFIG_STACKTRACE
118 #ifndef HAVE_SAVE_STACK_TRACE_TSK
119 #define save_stack_trace_tsk(tsk, trace) \
121 if (tsk == current) \
122 save_stack_trace(trace); \
124 pr_info("No stack, save_stack_trace_tsk() not exported\n"); \
128 #define MAX_ST_ENTRIES 100
129 static DEFINE_SPINLOCK(st_lock);
131 static void libcfs_call_trace(struct task_struct *tsk)
133 struct stack_trace trace;
134 static unsigned long entries[MAX_ST_ENTRIES];
136 trace.nr_entries = 0;
137 trace.max_entries = MAX_ST_ENTRIES;
138 trace.entries = entries;
142 pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
143 init_utsname()->release, init_utsname()->version);
144 pr_info("Call Trace:\n");
145 save_stack_trace_tsk(tsk, &trace);
146 print_stack_trace(&trace, 0);
147 spin_unlock(&st_lock);
150 #else /* !CONFIG_STACKTRACE */
153 #include <linux/nmi.h>
154 #include <asm/stacktrace.h>
156 #ifdef HAVE_STACKTRACE_OPS
157 #ifdef HAVE_STACKTRACE_WARNING
159 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
161 printk("%s", (char *)data);
162 print_symbol(msg, symbol);
166 static void print_trace_warning(void *data, char *msg)
168 printk("%s%s\n", (char *)data, msg);
172 static int print_trace_stack(void *data, char *name)
174 printk(" <%s> ", name);
178 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
183 print_trace_address(void *data, unsigned long addr, int reliable)
187 touch_nmi_watchdog();
188 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
189 __print_symbol(fmt, addr);
190 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
195 static const struct stacktrace_ops print_trace_ops = {
196 #ifdef HAVE_STACKTRACE_WARNING
197 .warning = print_trace_warning,
198 .warning_symbol = print_trace_warning_symbol,
200 .stack = print_trace_stack,
201 .address = print_trace_address,
202 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
203 .walk_stack = print_context_stack,
206 #endif /* HAVE_STACKTRACE_OPS */
208 static void libcfs_call_trace(struct task_struct *tsk)
210 #ifdef HAVE_STACKTRACE_OPS
211 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
212 printk("\nCall Trace:\n");
213 dump_trace(tsk, NULL, NULL,
214 #ifdef HAVE_DUMP_TRACE_ADDRESS
216 #endif /* HAVE_DUMP_TRACE_ADDRESS */
217 &print_trace_ops, NULL);
219 #else /* !HAVE_STACKTRACE_OPS */
223 CWARN("can't show stack: kernel doesn't export show_task\n");
224 #endif /* HAVE_STACKTRACE_OPS */
227 #else /* !CONFIG_X86 */
229 static void libcfs_call_trace(struct task_struct *tsk)
234 CWARN("can't show stack: kernel doesn't export show_task\n");
237 #endif /* CONFIG_X86 */
239 #endif /* CONFIG_STACKTRACE */
241 void libcfs_debug_dumpstack(struct task_struct *tsk)
243 libcfs_call_trace(tsk ?: current);
245 EXPORT_SYMBOL(libcfs_debug_dumpstack);
247 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
250 if (libcfs_panic_in_progress)
253 libcfs_panic_in_progress = 1;
256 #ifdef LNET_DUMP_ON_PANIC
257 /* This is currently disabled because it spews far too much to the
258 * console on the rare cases it is ever triggered. */
260 if (in_interrupt()) {
261 cfs_trace_debug_print();
263 #ifdef HAVE_KERNEL_LOCKED
264 while (kernel_locked())
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);