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) 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/linux/linux-debug.c
38 * Author: Phil Schwan <phil@clusterfs.com>
41 #ifndef AUTOCONF_INCLUDED
42 #include <linux/config.h>
44 #include <linux/module.h>
45 #include <linux/kmod.h>
46 #include <linux/notifier.h>
47 #include <linux/kernel.h>
49 #include <linux/string.h>
50 #include <linux/stat.h>
51 #include <linux/errno.h>
52 #ifdef HAVE_KERNEL_LOCKED
53 #include <linux/smp_lock.h>
55 #include <linux/unistd.h>
56 #include <linux/interrupt.h>
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
59 #include <linux/completion.h>
62 #include <linux/stat.h>
63 #include <asm/uaccess.h>
64 #include <linux/miscdevice.h>
65 #include <linux/version.h>
67 # define DEBUG_SUBSYSTEM S_LNET
69 #include <libcfs/libcfs.h>
70 #include <libcfs/linux/portals_compat25.h>
72 #include "tracefile.h"
74 #include <linux/kallsyms.h>
76 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
77 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
80 * Upcall function once a Lustre log has been dumped.
82 * \param file path of the dumped log
84 void libcfs_run_debug_log_upcall(char *file)
90 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
94 argv[0] = lnet_debug_log_upcall;
96 LASSERTF(file != NULL, "called on a null filename\n");
97 argv[1] = file; //only need to pass the path of the file
101 rc = USERMODEHELPER(argv[0], argv, envp);
102 if (rc < 0 && rc != -ENOENT) {
103 CERROR("Error %d invoking LNET debug log upcall %s %s; "
104 "check /proc/sys/lnet/debug_log_upcall\n",
105 rc, argv[0], argv[1]);
107 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
114 void libcfs_run_upcall(char **argv)
120 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
124 argv[0] = lnet_upcall;
126 while (argv[argc] != NULL)
131 rc = USERMODEHELPER(argv[0], argv, envp);
132 if (rc < 0 && rc != -ENOENT) {
133 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
134 "check /proc/sys/lnet/upcall\n",
135 rc, argv[0], argv[1],
136 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
137 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
138 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
139 argc < 6 ? "" : ",...");
141 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
143 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
144 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
145 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
146 argc < 6 ? "" : ",...");
150 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
156 snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
159 argv[2] = (char *)msgdata->msg_file;
160 argv[3] = (char *)msgdata->msg_fn;
164 libcfs_run_upcall (argv);
168 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
170 libcfs_catastrophe = 1;
171 libcfs_debug_msg(msgdata, "LBUG - trying to dump log to %s\n",
172 libcfs_debug_file_path);
173 libcfs_debug_dumplog();
174 libcfs_run_lbug_upcall(msgdata);
179 /* coverity[+kill] */
180 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
182 libcfs_catastrophe = 1;
183 libcfs_debug_msg(msgdata, "LBUG\n");
185 if (in_interrupt()) {
186 panic("LBUG in interrupt.\n");
190 libcfs_debug_dumpstack(NULL);
191 if (!libcfs_panic_on_lbug)
192 libcfs_debug_dumplog();
193 libcfs_run_lbug_upcall(msgdata);
194 if (libcfs_panic_on_lbug)
196 set_task_state(current, TASK_UNINTERRUPTIBLE);
200 #endif /* __arch_um__ */
204 #ifdef HAVE_DUMP_TRACE
205 #include <linux/nmi.h>
206 #include <asm/stacktrace.h>
208 #ifdef HAVE_STACKTRACE_WARNING
210 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
212 printk("%s", (char *)data);
213 print_symbol(msg, symbol);
217 static void print_trace_warning(void *data, char *msg)
219 printk("%s%s\n", (char *)data, msg);
223 static int print_trace_stack(void *data, char *name)
225 printk(" <%s> ", name);
229 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
230 # define RELIABLE reliable
231 # define DUMP_TRACE_CONST const
232 static void print_trace_address(void *data, unsigned long addr, int reliable)
234 /* before 2.6.24 there was no reliable arg */
236 # define DUMP_TRACE_CONST
237 static void print_trace_address(void *data, unsigned long addr)
241 touch_nmi_watchdog();
242 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
243 __print_symbol(fmt, addr);
246 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
247 #ifdef HAVE_STACKTRACE_WARNING
248 .warning = print_trace_warning,
249 .warning_symbol = print_trace_warning_symbol,
251 .stack = print_trace_stack,
252 .address = print_trace_address,
253 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
254 .walk_stack = print_context_stack,
259 void libcfs_debug_dumpstack(struct task_struct *tsk)
261 #if defined(__arch_um__)
263 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
264 tsk->pid, UML_PID(tsk));
266 #elif defined(HAVE_DUMP_TRACE)
271 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
272 /* show_trace_log_lvl() */
273 printk("\nCall Trace:\n");
274 dump_trace(tsk, NULL, NULL,
275 #ifdef HAVE_DUMP_TRACE_ADDRESS
277 #endif /* HAVE_DUMP_TRACE_ADDRESS */
278 &print_trace_ops, NULL);
280 #elif defined(HAVE_SHOW_TASK)
281 /* this is exported by lustre kernel version 42 */
282 extern void show_task(struct task_struct *);
286 CWARN("showing stack for process %d\n", tsk->pid);
289 if ((tsk == NULL) || (tsk == current))
292 CWARN("can't show stack: kernel doesn't export show_task\n");
296 cfs_task_t *libcfs_current(void)
298 CWARN("current task struct is %p\n", current);
302 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
305 if (libcfs_panic_in_progress)
308 libcfs_panic_in_progress = 1;
311 #ifdef LNET_DUMP_ON_PANIC
312 /* This is currently disabled because it spews far too much to the
313 * console on the rare cases it is ever triggered. */
315 if (in_interrupt()) {
316 cfs_trace_debug_print();
318 # ifdef HAVE_KERNEL_LOCKED
319 while (kernel_locked())
322 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
328 static struct notifier_block libcfs_panic_notifier = {
329 notifier_call : panic_notifier,
334 void libcfs_register_panic_notifier(void)
336 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
339 void libcfs_unregister_panic_notifier(void)
341 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
344 EXPORT_SYMBOL(libcfs_debug_dumpstack);
345 EXPORT_SYMBOL(libcfs_current);
347 #endif /* __KERNEL__ */
349 EXPORT_SYMBOL(libcfs_run_upcall);
350 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
351 EXPORT_SYMBOL(lbug_with_loc);