Whamcloud - gitweb
Branch HEAD
[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  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26
27 #ifndef AUTOCONF_INCLUDED
28 #include <linux/config.h>
29 #endif
30 #include <linux/module.h>
31 #include <linux/kmod.h>
32 #include <linux/notifier.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/string.h>
36 #include <linux/stat.h>
37 #include <linux/errno.h>
38 #include <linux/smp_lock.h>
39 #include <linux/unistd.h>
40 #include <linux/interrupt.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <linux/completion.h>
44
45 #include <linux/fs.h>
46 #include <linux/stat.h>
47 #include <asm/uaccess.h>
48 #include <linux/miscdevice.h>
49 #include <linux/version.h>
50
51 # define DEBUG_SUBSYSTEM S_LNET
52
53 #include <libcfs/libcfs.h>
54 #include <libcfs/linux/portals_compat25.h>
55
56 #include "tracefile.h"
57
58 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
59 #include <linux/kallsyms.h>
60 #endif
61
62 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
63
64 void libcfs_run_upcall(char **argv)
65 {
66         int   rc;
67         int   argc;
68         char *envp[] = {
69                 "HOME=/",
70                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
71                 NULL};
72         ENTRY;
73
74         argv[0] = lnet_upcall;
75         argc = 1;
76         while (argv[argc] != NULL)
77                 argc++;
78
79         LASSERT(argc >= 2);
80
81         rc = USERMODEHELPER(argv[0], argv, envp);
82         if (rc < 0 && rc != -ENOENT) {
83                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
84                        "check /proc/sys/lnet/upcall\n",
85                        rc, argv[0], argv[1],
86                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
87                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
88                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
89                        argc < 6 ? "" : ",...");
90         } else {
91                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
92                        argv[0], argv[1],
93                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
94                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
95                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
96                        argc < 6 ? "" : ",...");
97         }
98 }
99
100 void libcfs_run_lbug_upcall(char *file, const char *fn, const int line)
101 {
102         char *argv[6];
103         char buf[32];
104
105         ENTRY;
106         snprintf (buf, sizeof buf, "%d", line);
107
108         argv[1] = "LBUG";
109         argv[2] = file;
110         argv[3] = (char *)fn;
111         argv[4] = buf;
112         argv[5] = NULL;
113
114         libcfs_run_upcall (argv);
115 }
116
117 #ifdef __arch_um__
118 void lbug_with_loc(char *file, const char *func, const int line)
119 {
120         libcfs_catastrophe = 1;
121         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
122                          "LBUG - trying to dump log to %s\n", debug_file_path);
123         libcfs_debug_dumplog();
124         libcfs_run_lbug_upcall(file, func, line);
125         asm("int $3");
126         panic("LBUG");
127 }
128 #else
129 /* coverity[+kill] */
130 void lbug_with_loc(char *file, const char *func, const int line)
131 {
132         libcfs_catastrophe = 1;
133         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
134
135         if (in_interrupt()) {
136                 panic("LBUG in interrupt.\n");
137                 /* not reached */
138         }
139
140         libcfs_debug_dumpstack(NULL);
141         libcfs_debug_dumplog();
142         libcfs_run_lbug_upcall(file, func, line);
143         if (libcfs_panic_on_lbug)
144                 panic("LBUG");
145         set_task_state(current, TASK_UNINTERRUPTIBLE);
146         while (1)
147                 schedule();
148 }
149 #endif /* __arch_um__ */
150
151 #ifdef __KERNEL__
152
153 void libcfs_debug_dumpstack(struct task_struct *tsk)
154 {
155 #if defined(__arch_um__)
156         if (tsk != NULL)
157                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
158                       tsk->pid, UML_PID(tsk));
159         //asm("int $3");
160 #elif defined(HAVE_SHOW_TASK)
161         /* this is exported by lustre kernel version 42 */
162         extern void show_task(struct task_struct *);
163
164         if (tsk == NULL)
165                 tsk = current;
166         CWARN("showing stack for process %d\n", tsk->pid);
167         show_task(tsk);
168 #else
169         if ((tsk == NULL) || (tsk == current))
170                 dump_stack();
171         else
172                 CWARN("can't show stack: kernel doesn't export show_task\n");
173 #endif
174 }
175
176 cfs_task_t *libcfs_current(void)
177 {
178         CWARN("current task struct is %p\n", current);
179         return current;
180 }
181
182 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
183                          void *unused2)
184 {
185         if (libcfs_panic_in_progress)
186                 return 0;
187
188         libcfs_panic_in_progress = 1;
189         mb();
190
191 #ifdef LNET_DUMP_ON_PANIC
192         /* This is currently disabled because it spews far too much to the
193          * console on the rare cases it is ever triggered. */
194
195         if (in_interrupt()) {
196                 trace_debug_print();
197         } else {
198                 while (current->lock_depth >= 0)
199                         unlock_kernel();
200
201                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
202         }
203 #endif
204         return 0;
205 }
206
207 static struct notifier_block libcfs_panic_notifier = {
208         notifier_call :     panic_notifier,
209         next :              NULL,
210         priority :          10000
211 };
212
213 void libcfs_register_panic_notifier(void)
214 {
215 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
216         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
217 #else
218         notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
219 #endif
220 }
221
222 void libcfs_unregister_panic_notifier(void)
223 {
224 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
225         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
226 #else
227         notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
228 #endif
229 }
230
231 EXPORT_SYMBOL(libcfs_debug_dumpstack);
232 EXPORT_SYMBOL(libcfs_current);
233
234 #endif /* __KERNEL__ */
235
236 EXPORT_SYMBOL(libcfs_run_upcall);
237 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
238 EXPORT_SYMBOL(lbug_with_loc);