Whamcloud - gitweb
remove unneed include.
[fs/lustre-release.git] / lnet / 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/kp30.h>
54 #include <libcfs/linux/portals_compat25.h>
55 #include <libcfs/libcfs.h>
56
57 #include "tracefile.h"
58
59 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
60 #include <linux/kallsyms.h>
61 #endif
62
63 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
64
65 void libcfs_run_upcall(char **argv)
66 {
67         int   rc;
68         int   argc;
69         char *envp[] = {
70                 "HOME=/",
71                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
72                 NULL};
73         ENTRY;
74
75         argv[0] = lnet_upcall;
76         argc = 1;
77         while (argv[argc] != NULL)
78                 argc++;
79
80         LASSERT(argc >= 2);
81
82         rc = USERMODEHELPER(argv[0], argv, envp);
83         if (rc < 0 && rc != -ENOENT) {
84                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
85                        "check /proc/sys/lnet/upcall\n",
86                        rc, argv[0], argv[1],
87                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
88                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
89                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
90                        argc < 6 ? "" : ",...");
91         } else {
92                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
93                        argv[0], argv[1],
94                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
95                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
96                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
97                        argc < 6 ? "" : ",...");
98         }
99 }
100
101 void libcfs_run_lbug_upcall(char *file, const char *fn, const int line)
102 {
103         char *argv[6];
104         char buf[32];
105
106         ENTRY;
107         snprintf (buf, sizeof buf, "%d", line);
108
109         argv[1] = "LBUG";
110         argv[2] = file;
111         argv[3] = (char *)fn;
112         argv[4] = buf;
113         argv[5] = NULL;
114
115         libcfs_run_upcall (argv);
116 }
117
118 #ifdef __arch_um__
119 void lbug_with_loc(char *file, const char *func, const int line)
120 {
121         libcfs_catastrophe = 1;
122         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
123                          "LBUG - trying to dump log to %s\n", debug_file_path);
124         libcfs_debug_dumplog();
125         libcfs_run_lbug_upcall(file, func, line);
126         asm("int $3");
127         panic("LBUG");
128 }
129 #else
130 /* coverity[+kill] */
131 void lbug_with_loc(char *file, const char *func, const int line)
132 {
133         libcfs_catastrophe = 1;
134         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
135
136         if (in_interrupt()) {
137                 panic("LBUG in interrupt.\n");
138                 /* not reached */
139         }
140
141         libcfs_debug_dumpstack(NULL);
142         libcfs_debug_dumplog();
143         libcfs_run_lbug_upcall(file, func, line);
144         if (libcfs_panic_on_lbug)
145                 panic("LBUG");
146         set_task_state(current, TASK_UNINTERRUPTIBLE);
147         while (1)
148                 schedule();
149 }
150 #endif /* __arch_um__ */
151
152 #ifdef __KERNEL__
153
154 void libcfs_debug_dumpstack(struct task_struct *tsk)
155 {
156 #if defined(__arch_um__)
157         if (tsk != NULL)
158                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
159                       tsk->pid, UML_PID(tsk));
160         //asm("int $3");
161 #elif defined(HAVE_SHOW_TASK)
162         /* this is exported by lustre kernel version 42 */
163         extern void show_task(struct task_struct *);
164
165         if (tsk == NULL)
166                 tsk = current;
167         CWARN("showing stack for process %d\n", tsk->pid);
168         show_task(tsk);
169 #else
170         if ((tsk == NULL) || (tsk == current))
171                 dump_stack();
172         else
173                 CWARN("can't show stack: kernel doesn't export show_task\n");
174 #endif
175 }
176
177 cfs_task_t *libcfs_current(void)
178 {
179         CWARN("current task struct is %p\n", current);
180         return current;
181 }
182
183 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
184                          void *unused2)
185 {
186         if (libcfs_panic_in_progress)
187                 return 0;
188
189         libcfs_panic_in_progress = 1;
190         mb();
191
192 #ifdef LNET_DUMP_ON_PANIC
193         /* This is currently disabled because it spews far too much to the
194          * console on the rare cases it is ever triggered. */
195
196         if (in_interrupt()) {
197                 trace_debug_print();
198         } else {
199                 while (current->lock_depth >= 0)
200                         unlock_kernel();
201
202                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
203         }
204 #endif
205         return 0;
206 }
207
208 static struct notifier_block libcfs_panic_notifier = {
209         notifier_call :     panic_notifier,
210         next :              NULL,
211         priority :          10000
212 };
213
214 void libcfs_register_panic_notifier(void)
215 {
216 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
217         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
218 #else
219         notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
220 #endif
221 }
222
223 void libcfs_unregister_panic_notifier(void)
224 {
225 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
226         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
227 #else
228         notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
229 #endif
230 }
231
232 EXPORT_SYMBOL(libcfs_debug_dumpstack);
233 EXPORT_SYMBOL(libcfs_current);
234
235 #endif /* __KERNEL__ */
236
237 EXPORT_SYMBOL(libcfs_run_upcall);
238 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
239 EXPORT_SYMBOL(lbug_with_loc);