Whamcloud - gitweb
d1db71a19b38822653f97d4a450e3a17770633c2
[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 #ifdef HAVE_KERNEL_CONFIG_H
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 <asm/segment.h>
49 #include <linux/miscdevice.h>
50 #include <linux/version.h>
51
52 # define DEBUG_SUBSYSTEM S_LNET
53
54 #include <libcfs/kp30.h>
55 #include <libcfs/linux/portals_compat25.h>
56 #include <libcfs/libcfs.h>
57
58 #include "tracefile.h"
59
60 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
61 #include <linux/kallsyms.h>
62 #endif
63
64 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
65
66 void libcfs_run_upcall(char **argv)
67 {
68         int   rc;
69         int   argc;
70         char *envp[] = {
71                 "HOME=/",
72                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
73                 NULL};
74         ENTRY;
75
76         argv[0] = lnet_upcall;
77         argc = 1;
78         while (argv[argc] != NULL)
79                 argc++;
80
81         LASSERT(argc >= 2);
82
83         rc = USERMODEHELPER(argv[0], argv, envp);
84         if (rc < 0 && rc != -ENOENT) {
85                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
86                        "check /proc/sys/lnet/upcall\n",
87                        rc, argv[0], argv[1],
88                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
89                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
90                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
91                        argc < 6 ? "" : ",...");
92         } else {
93                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
94                        argv[0], argv[1],
95                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
96                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
97                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
98                        argc < 6 ? "" : ",...");
99         }
100 }
101
102 void libcfs_run_lbug_upcall(char *file, const char *fn, const int line)
103 {
104         char *argv[6];
105         char buf[32];
106
107         ENTRY;
108         snprintf (buf, sizeof buf, "%d", line);
109
110         argv[1] = "LBUG";
111         argv[2] = file;
112         argv[3] = (char *)fn;
113         argv[4] = buf;
114         argv[5] = NULL;
115
116         libcfs_run_upcall (argv);
117 }
118
119 #ifdef __arch_um__
120 void lbug_with_loc(char *file, const char *func, const int line)
121 {
122         libcfs_catastrophe = 1;
123         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
124                          "LBUG - trying to dump log to %s\n", debug_file_path);
125         libcfs_debug_dumplog();
126         libcfs_run_lbug_upcall(file, func, line);
127         asm("int $3");
128         panic("LBUG");
129 }
130 #else
131 /* coverity[+kill] */
132 void lbug_with_loc(char *file, const char *func, const int line)
133 {
134         libcfs_catastrophe = 1;
135         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
136
137         if (in_interrupt()) {
138                 panic("LBUG in interrupt.\n");
139                 /* not reached */
140         }
141
142         libcfs_debug_dumpstack(NULL);
143         libcfs_debug_dumplog();
144         libcfs_run_lbug_upcall(file, func, line);
145         if (libcfs_panic_on_lbug)
146                 panic("LBUG");
147         set_task_state(current, TASK_UNINTERRUPTIBLE);
148         while (1)
149                 schedule();
150 }
151 #endif /* __arch_um__ */
152
153 #ifdef __KERNEL__
154
155 void libcfs_debug_dumpstack(struct task_struct *tsk)
156 {
157 #if defined(__arch_um__)
158         if (tsk != NULL)
159                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
160                       tsk->pid, UML_PID(tsk));
161         //asm("int $3");
162 #elif defined(HAVE_SHOW_TASK)
163         /* this is exported by lustre kernel version 42 */
164         extern void show_task(struct task_struct *);
165
166         if (tsk == NULL)
167                 tsk = current;
168         CWARN("showing stack for process %d\n", tsk->pid);
169         show_task(tsk);
170 #else
171         CWARN("can't show stack: kernel doesn't export show_task\n");
172         if ((tsk == NULL) || (tsk == current))
173                 dump_stack();
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);