1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * libcfs/libcfs/linux/linux-debug.c
40 * Author: Phil Schwan <phil@clusterfs.com>
44 # define EXPORT_SYMTAB
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/kmod.h>
52 #include <linux/notifier.h>
53 #include <linux/kernel.h>
55 #include <linux/string.h>
56 #include <linux/stat.h>
57 #include <linux/errno.h>
58 #include <linux/smp_lock.h>
59 #include <linux/unistd.h>
60 #include <linux/interrupt.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <linux/completion.h>
66 #include <linux/stat.h>
67 #include <asm/uaccess.h>
68 #include <linux/miscdevice.h>
69 #include <linux/version.h>
71 # define DEBUG_SUBSYSTEM S_LNET
73 #include <libcfs/libcfs.h>
74 #include <libcfs/linux/portals_compat25.h>
76 #include "tracefile.h"
78 #include <linux/kallsyms.h>
80 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
81 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
84 * Upcall function once a Lustre log has been dumped.
86 * \param file path of the dumped log
88 void libcfs_run_debug_log_upcall(char *file)
94 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
98 argv[0] = lnet_debug_log_upcall;
100 LASSERTF(file != NULL, "called on a null filename\n");
101 argv[1] = file; //only need to pass the path of the file
105 rc = USERMODEHELPER(argv[0], argv, envp);
106 if (rc < 0 && rc != -ENOENT) {
107 CERROR("Error %d invoking LNET debug log upcall %s %s; "
108 "check /proc/sys/lnet/debug_log_upcall\n",
109 rc, argv[0], argv[1]);
111 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
118 void libcfs_run_upcall(char **argv)
124 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
128 argv[0] = lnet_upcall;
130 while (argv[argc] != NULL)
135 rc = USERMODEHELPER(argv[0], argv, envp);
136 if (rc < 0 && rc != -ENOENT) {
137 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
138 "check /proc/sys/lnet/upcall\n",
139 rc, argv[0], argv[1],
140 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
141 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
142 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
143 argc < 6 ? "" : ",...");
145 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
147 argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
148 argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
149 argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
150 argc < 6 ? "" : ",...");
154 void libcfs_run_lbug_upcall(const char *file, const char *fn, const int line)
160 snprintf (buf, sizeof buf, "%d", line);
163 argv[2] = (char *)file;
164 argv[3] = (char *)fn;
168 libcfs_run_upcall (argv);
172 void lbug_with_loc(const char *file, const char *func, const int line)
174 libcfs_catastrophe = 1;
175 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
176 "LBUG - trying to dump log to %s\n",
177 libcfs_debug_file_path);
178 libcfs_debug_dumplog();
179 libcfs_run_lbug_upcall(file, func, line);
184 /* coverity[+kill] */
185 void lbug_with_loc(const char *file, const char *func, const int line)
187 libcfs_catastrophe = 1;
188 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
190 if (in_interrupt()) {
191 panic("LBUG in interrupt.\n");
195 libcfs_debug_dumpstack(NULL);
196 if (!libcfs_panic_on_lbug)
197 libcfs_debug_dumplog();
198 libcfs_run_lbug_upcall(file, func, line);
199 if (libcfs_panic_on_lbug)
201 set_task_state(current, TASK_UNINTERRUPTIBLE);
205 #endif /* __arch_um__ */
209 #ifdef HAVE_DUMP_TRACE
210 #include <linux/nmi.h>
211 #include <asm/stacktrace.h>
213 #ifdef HAVE_STACKTRACE_WARNING
215 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
217 printk("%s", (char *)data);
218 print_symbol(msg, symbol);
222 static void print_trace_warning(void *data, char *msg)
224 printk("%s%s\n", (char *)data, msg);
228 static int print_trace_stack(void *data, char *name)
230 printk(" <%s> ", name);
234 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
235 # define RELIABLE reliable
236 # define DUMP_TRACE_CONST const
237 static void print_trace_address(void *data, unsigned long addr, int reliable)
239 /* before 2.6.24 there was no reliable arg */
241 # define DUMP_TRACE_CONST
242 static void print_trace_address(void *data, unsigned long addr)
246 touch_nmi_watchdog();
247 sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
248 __print_symbol(fmt, addr);
251 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
252 #ifdef HAVE_STACKTRACE_WARNING
253 .warning = print_trace_warning,
254 .warning_symbol = print_trace_warning_symbol,
256 .stack = print_trace_stack,
257 .address = print_trace_address,
258 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
259 .walk_stack = print_context_stack,
264 void libcfs_debug_dumpstack(struct task_struct *tsk)
266 #if defined(__arch_um__)
268 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
269 tsk->pid, UML_PID(tsk));
271 #elif defined(HAVE_DUMP_TRACE)
276 printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
277 /* show_trace_log_lvl() */
278 printk("\nCall Trace:\n");
279 dump_trace(tsk, NULL, NULL,
280 #ifdef HAVE_DUMP_TRACE_ADDRESS
282 #endif /* HAVE_DUMP_TRACE_ADDRESS */
283 &print_trace_ops, NULL);
285 #elif defined(HAVE_SHOW_TASK)
286 /* this is exported by lustre kernel version 42 */
287 extern void show_task(struct task_struct *);
291 CWARN("showing stack for process %d\n", tsk->pid);
294 if ((tsk == NULL) || (tsk == current))
297 CWARN("can't show stack: kernel doesn't export show_task\n");
301 cfs_task_t *libcfs_current(void)
303 CWARN("current task struct is %p\n", current);
307 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
310 if (libcfs_panic_in_progress)
313 libcfs_panic_in_progress = 1;
316 #ifdef LNET_DUMP_ON_PANIC
317 /* This is currently disabled because it spews far too much to the
318 * console on the rare cases it is ever triggered. */
320 if (in_interrupt()) {
321 cfs_trace_debug_print();
323 while (current->lock_depth >= 0)
326 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
332 static struct notifier_block libcfs_panic_notifier = {
333 notifier_call : panic_notifier,
338 void libcfs_register_panic_notifier(void)
340 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
341 atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
343 notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
347 void libcfs_unregister_panic_notifier(void)
349 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
350 atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
352 notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
356 EXPORT_SYMBOL(libcfs_debug_dumpstack);
357 EXPORT_SYMBOL(libcfs_current);
359 #endif /* __KERNEL__ */
361 EXPORT_SYMBOL(libcfs_run_upcall);
362 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
363 EXPORT_SYMBOL(lbug_with_loc);