Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/kmod.h>
50 #include <linux/notifier.h>
51 #include <linux/kernel.h>
52 #include <linux/mm.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>
62
63 #include <linux/fs.h>
64 #include <linux/stat.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/version.h>
68
69 # define DEBUG_SUBSYSTEM S_LNET
70
71 #include <libcfs/libcfs.h>
72 #include <libcfs/linux/portals_compat25.h>
73
74 #include "tracefile.h"
75
76 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
77 #include <linux/kallsyms.h>
78 #endif
79
80 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
81
82 void libcfs_run_upcall(char **argv)
83 {
84         int   rc;
85         int   argc;
86         char *envp[] = {
87                 "HOME=/",
88                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
89                 NULL};
90         ENTRY;
91
92         argv[0] = lnet_upcall;
93         argc = 1;
94         while (argv[argc] != NULL)
95                 argc++;
96
97         LASSERT(argc >= 2);
98
99         rc = USERMODEHELPER(argv[0], argv, envp);
100         if (rc < 0 && rc != -ENOENT) {
101                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
102                        "check /proc/sys/lnet/upcall\n",
103                        rc, argv[0], argv[1],
104                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
105                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
106                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
107                        argc < 6 ? "" : ",...");
108         } else {
109                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
110                        argv[0], argv[1],
111                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
112                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
113                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
114                        argc < 6 ? "" : ",...");
115         }
116 }
117
118 void libcfs_run_lbug_upcall(char *file, const char *fn, const int line)
119 {
120         char *argv[6];
121         char buf[32];
122
123         ENTRY;
124         snprintf (buf, sizeof buf, "%d", line);
125
126         argv[1] = "LBUG";
127         argv[2] = file;
128         argv[3] = (char *)fn;
129         argv[4] = buf;
130         argv[5] = NULL;
131
132         libcfs_run_upcall (argv);
133 }
134
135 #ifdef __arch_um__
136 void lbug_with_loc(char *file, const char *func, const int line)
137 {
138         libcfs_catastrophe = 1;
139         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
140                          "LBUG - trying to dump log to %s\n", debug_file_path);
141         libcfs_debug_dumplog();
142         libcfs_run_lbug_upcall(file, func, line);
143         asm("int $3");
144         panic("LBUG");
145 }
146 #else
147 /* coverity[+kill] */
148 void lbug_with_loc(char *file, const char *func, const int line)
149 {
150         libcfs_catastrophe = 1;
151         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
152
153         if (in_interrupt()) {
154                 panic("LBUG in interrupt.\n");
155                 /* not reached */
156         }
157
158         libcfs_debug_dumpstack(NULL);
159         libcfs_debug_dumplog();
160         libcfs_run_lbug_upcall(file, func, line);
161         if (libcfs_panic_on_lbug)
162                 panic("LBUG");
163         set_task_state(current, TASK_UNINTERRUPTIBLE);
164         while (1)
165                 schedule();
166 }
167 #endif /* __arch_um__ */
168
169 #ifdef __KERNEL__
170
171 void libcfs_debug_dumpstack(struct task_struct *tsk)
172 {
173 #if defined(__arch_um__)
174         if (tsk != NULL)
175                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
176                       tsk->pid, UML_PID(tsk));
177         //asm("int $3");
178 #elif defined(HAVE_SHOW_TASK)
179         /* this is exported by lustre kernel version 42 */
180         extern void show_task(struct task_struct *);
181
182         if (tsk == NULL)
183                 tsk = current;
184         CWARN("showing stack for process %d\n", tsk->pid);
185         show_task(tsk);
186 #else
187         if ((tsk == NULL) || (tsk == current))
188                 dump_stack();
189         else
190                 CWARN("can't show stack: kernel doesn't export show_task\n");
191 #endif
192 }
193
194 cfs_task_t *libcfs_current(void)
195 {
196         CWARN("current task struct is %p\n", current);
197         return current;
198 }
199
200 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
201                          void *unused2)
202 {
203         if (libcfs_panic_in_progress)
204                 return 0;
205
206         libcfs_panic_in_progress = 1;
207         mb();
208
209 #ifdef LNET_DUMP_ON_PANIC
210         /* This is currently disabled because it spews far too much to the
211          * console on the rare cases it is ever triggered. */
212
213         if (in_interrupt()) {
214                 trace_debug_print();
215         } else {
216                 while (current->lock_depth >= 0)
217                         unlock_kernel();
218
219                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
220         }
221 #endif
222         return 0;
223 }
224
225 static struct notifier_block libcfs_panic_notifier = {
226         notifier_call :     panic_notifier,
227         next :              NULL,
228         priority :          10000
229 };
230
231 void libcfs_register_panic_notifier(void)
232 {
233 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
234         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
235 #else
236         notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
237 #endif
238 }
239
240 void libcfs_unregister_panic_notifier(void)
241 {
242 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
243         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
244 #else
245         notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
246 #endif
247 }
248
249 EXPORT_SYMBOL(libcfs_debug_dumpstack);
250 EXPORT_SYMBOL(libcfs_current);
251
252 #endif /* __KERNEL__ */
253
254 EXPORT_SYMBOL(libcfs_run_upcall);
255 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
256 EXPORT_SYMBOL(lbug_with_loc);