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