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>
42 # define EXPORT_SYMTAB
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/kmod.h>
50 #include <linux/notifier.h>
51 #include <linux/kernel.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/smp_lock.h>
57 #include <linux/unistd.h>
58 #include <linux/interrupt.h>
59 #include <asm/system.h>
60 #include <asm/uaccess.h>
61 #include <linux/completion.h>
64 #include <linux/stat.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/version.h>
69 # define DEBUG_SUBSYSTEM S_LNET
71 #include <libcfs/libcfs.h>
72 #include <libcfs/linux/portals_compat25.h>
74 #include "tracefile.h"
76 #include <linux/kallsyms.h>
78 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
79 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
82 * Upcall function once a Lustre log has been dumped.
84 * \param file path of the dumped log
86 void libcfs_run_debug_log_upcall(char *file)
92 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
96 argv[0] = lnet_debug_log_upcall;
98 LASSERTF(file != NULL, "called on a null filename\n");
99 argv[1] = file; //only need to pass the path of the file
103 rc = USERMODEHELPER(argv[0], argv, envp);
104 if (rc < 0 && rc != -ENOENT) {
105 CERROR("Error %d invoking LNET debug log upcall %s %s; "
106 "check /proc/sys/lnet/debug_log_upcall\n",
107 rc, argv[0], argv[1]);
109 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
116 void libcfs_run_upcall(char **argv)
122 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
126 argv[0] = lnet_upcall;
128 while (argv[argc] != NULL)
133 rc = USERMODEHELPER(argv[0], argv, envp);
134 if (rc < 0 && rc != -ENOENT) {
135 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
136 "check /proc/sys/lnet/upcall\n",
137 rc, argv[0], argv[1],
138 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
139 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
140 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
141 argc < 6 ? "" : ",...");
143 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
145 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
146 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
147 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
148 argc < 6 ? "" : ",...");
152 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
158 snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
161 argv[2] = (char *)msgdata->msg_file;
162 argv[3] = (char *)msgdata->msg_fn;
166 libcfs_run_upcall (argv);
170 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
172 libcfs_catastrophe = 1;
173 libcfs_debug_msg(msgdata, "LBUG - trying to dump log to %s\n",
174 libcfs_debug_file_path);
175 libcfs_debug_dumplog();
176 libcfs_run_lbug_upcall(msgdata);
181 /* coverity[+kill] */
182 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
184 libcfs_catastrophe = 1;
185 libcfs_debug_msg(msgdata, "LBUG\n");
187 if (in_interrupt()) {
188 panic("LBUG in interrupt.\n");
192 libcfs_debug_dumpstack(NULL);
193 if (!libcfs_panic_on_lbug)
194 libcfs_debug_dumplog();
195 libcfs_run_lbug_upcall(msgdata);
196 if (libcfs_panic_on_lbug)
198 set_task_state(current, TASK_UNINTERRUPTIBLE);
202 #endif /* __arch_um__ */
206 #ifdef HAVE_DUMP_TRACE
207 #include <linux/nmi.h>
208 #include <asm/stacktrace.h>
210 #ifdef HAVE_STACKTRACE_WARNING
212 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
214 printk("%s", (char *)data);
215 print_symbol(msg, symbol);
219 static void print_trace_warning(void *data, char *msg)
221 printk("%s%s\n", (char *)data, msg);
225 static int print_trace_stack(void *data, char *name)
227 printk(" <%s> ", name);
231 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
232 # define RELIABLE reliable
233 # define DUMP_TRACE_CONST const
234 static void print_trace_address(void *data, unsigned long addr, int reliable)
236 /* before 2.6.24 there was no reliable arg */
238 # define DUMP_TRACE_CONST
239 static void print_trace_address(void *data, unsigned long addr)
243 touch_nmi_watchdog();
244 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
245 __print_symbol(fmt, addr);
248 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
249 #ifdef HAVE_STACKTRACE_WARNING
250 .warning = print_trace_warning,
251 .warning_symbol = print_trace_warning_symbol,
253 .stack = print_trace_stack,
254 .address = print_trace_address,
255 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
256 .walk_stack = print_context_stack,
261 void libcfs_debug_dumpstack(struct task_struct *tsk)
263 #if defined(__arch_um__)
265 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
266 tsk->pid, UML_PID(tsk));
268 #elif defined(HAVE_DUMP_TRACE)
273 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
274 /* show_trace_log_lvl() */
275 printk("\nCall Trace:\n");
276 dump_trace(tsk, NULL, NULL,
277 #ifdef HAVE_DUMP_TRACE_ADDRESS
279 #endif /* HAVE_DUMP_TRACE_ADDRESS */
280 &print_trace_ops, NULL);
282 #elif defined(HAVE_SHOW_TASK)
283 /* this is exported by lustre kernel version 42 */
284 extern void show_task(struct task_struct *);
288 CWARN("showing stack for process %d\n", tsk->pid);
291 if ((tsk == NULL) || (tsk == current))
294 CWARN("can't show stack: kernel doesn't export show_task\n");
298 cfs_task_t *libcfs_current(void)
300 CWARN("current task struct is %p\n", current);
304 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
307 if (libcfs_panic_in_progress)
310 libcfs_panic_in_progress = 1;
313 #ifdef LNET_DUMP_ON_PANIC
314 /* This is currently disabled because it spews far too much to the
315 * console on the rare cases it is ever triggered. */
317 if (in_interrupt()) {
318 cfs_trace_debug_print();
320 while (current->lock_depth >= 0)
323 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
329 static struct notifier_block libcfs_panic_notifier = {
330 notifier_call : panic_notifier,
335 void libcfs_register_panic_notifier(void)
337 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
340 void libcfs_unregister_panic_notifier(void)
342 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
345 EXPORT_SYMBOL(libcfs_debug_dumpstack);
346 EXPORT_SYMBOL(libcfs_current);
348 #endif /* __KERNEL__ */
350 EXPORT_SYMBOL(libcfs_run_upcall);
351 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
352 EXPORT_SYMBOL(lbug_with_loc);