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 #include <linux/smp_lock.h>
53 #include <linux/unistd.h>
54 #include <linux/interrupt.h>
55 #include <asm/system.h>
56 #include <asm/uaccess.h>
57 #include <linux/completion.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/miscdevice.h>
63 #include <linux/version.h>
65 # define DEBUG_SUBSYSTEM S_LNET
67 #include <libcfs/libcfs.h>
68 #include <libcfs/linux/portals_compat25.h>
70 #include "tracefile.h"
72 #include <linux/kallsyms.h>
74 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
75 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
78 * Upcall function once a Lustre log has been dumped.
80 * \param file path of the dumped log
82 void libcfs_run_debug_log_upcall(char *file)
88 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
92 argv[0] = lnet_debug_log_upcall;
94 LASSERTF(file != NULL, "called on a null filename\n");
95 argv[1] = file; //only need to pass the path of the file
99 rc = USERMODEHELPER(argv[0], argv, envp);
100 if (rc < 0 && rc != -ENOENT) {
101 CERROR("Error %d invoking LNET debug log upcall %s %s; "
102 "check /proc/sys/lnet/debug_log_upcall\n",
103 rc, argv[0], argv[1]);
105 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
112 void libcfs_run_upcall(char **argv)
118 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
122 argv[0] = lnet_upcall;
124 while (argv[argc] != NULL)
129 rc = USERMODEHELPER(argv[0], argv, envp);
130 if (rc < 0 && rc != -ENOENT) {
131 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
132 "check /proc/sys/lnet/upcall\n",
133 rc, argv[0], argv[1],
134 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
135 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
136 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
137 argc < 6 ? "" : ",...");
139 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
141 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
142 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
143 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
144 argc < 6 ? "" : ",...");
148 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
154 snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
157 argv[2] = (char *)msgdata->msg_file;
158 argv[3] = (char *)msgdata->msg_fn;
162 libcfs_run_upcall (argv);
166 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
168 libcfs_catastrophe = 1;
169 libcfs_debug_msg(msgdata, "LBUG - trying to dump log to %s\n",
170 libcfs_debug_file_path);
171 libcfs_debug_dumplog();
172 libcfs_run_lbug_upcall(msgdata);
177 /* coverity[+kill] */
178 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
180 libcfs_catastrophe = 1;
181 libcfs_debug_msg(msgdata, "LBUG\n");
183 if (in_interrupt()) {
184 panic("LBUG in interrupt.\n");
188 libcfs_debug_dumpstack(NULL);
189 if (!libcfs_panic_on_lbug)
190 libcfs_debug_dumplog();
191 libcfs_run_lbug_upcall(msgdata);
192 if (libcfs_panic_on_lbug)
194 set_task_state(current, TASK_UNINTERRUPTIBLE);
198 #endif /* __arch_um__ */
202 #ifdef HAVE_DUMP_TRACE
203 #include <linux/nmi.h>
204 #include <asm/stacktrace.h>
206 #ifdef HAVE_STACKTRACE_WARNING
208 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
210 printk("%s", (char *)data);
211 print_symbol(msg, symbol);
215 static void print_trace_warning(void *data, char *msg)
217 printk("%s%s\n", (char *)data, msg);
221 static int print_trace_stack(void *data, char *name)
223 printk(" <%s> ", name);
227 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
228 # define RELIABLE reliable
229 # define DUMP_TRACE_CONST const
230 static void print_trace_address(void *data, unsigned long addr, int reliable)
232 /* before 2.6.24 there was no reliable arg */
234 # define DUMP_TRACE_CONST
235 static void print_trace_address(void *data, unsigned long addr)
239 touch_nmi_watchdog();
240 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
241 __print_symbol(fmt, addr);
244 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
245 #ifdef HAVE_STACKTRACE_WARNING
246 .warning = print_trace_warning,
247 .warning_symbol = print_trace_warning_symbol,
249 .stack = print_trace_stack,
250 .address = print_trace_address,
251 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
252 .walk_stack = print_context_stack,
257 void libcfs_debug_dumpstack(struct task_struct *tsk)
259 #if defined(__arch_um__)
261 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
262 tsk->pid, UML_PID(tsk));
264 #elif defined(HAVE_DUMP_TRACE)
269 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
270 /* show_trace_log_lvl() */
271 printk("\nCall Trace:\n");
272 dump_trace(tsk, NULL, NULL,
273 #ifdef HAVE_DUMP_TRACE_ADDRESS
275 #endif /* HAVE_DUMP_TRACE_ADDRESS */
276 &print_trace_ops, NULL);
278 #elif defined(HAVE_SHOW_TASK)
279 /* this is exported by lustre kernel version 42 */
280 extern void show_task(struct task_struct *);
284 CWARN("showing stack for process %d\n", tsk->pid);
287 if ((tsk == NULL) || (tsk == current))
290 CWARN("can't show stack: kernel doesn't export show_task\n");
294 cfs_task_t *libcfs_current(void)
296 CWARN("current task struct is %p\n", current);
300 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
303 if (libcfs_panic_in_progress)
306 libcfs_panic_in_progress = 1;
309 #ifdef LNET_DUMP_ON_PANIC
310 /* This is currently disabled because it spews far too much to the
311 * console on the rare cases it is ever triggered. */
313 if (in_interrupt()) {
314 cfs_trace_debug_print();
316 # ifdef HAVE_KERNEL_LOCKED
317 while (kernel_locked())
320 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
326 static struct notifier_block libcfs_panic_notifier = {
327 notifier_call : panic_notifier,
332 void libcfs_register_panic_notifier(void)
334 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
337 void libcfs_unregister_panic_notifier(void)
339 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
342 EXPORT_SYMBOL(libcfs_debug_dumpstack);
343 EXPORT_SYMBOL(libcfs_current);
345 #endif /* __KERNEL__ */
347 EXPORT_SYMBOL(libcfs_run_upcall);
348 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
349 EXPORT_SYMBOL(lbug_with_loc);