Whamcloud - gitweb
cc4f3a2179db85ab4883062d5b3324fe49a9876b
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-debug.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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 #include <linux/module.h>
42 #include <linux/kmod.h>
43 #include <linux/notifier.h>
44 #include <linux/kernel.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #ifdef HAVE_KERNEL_LOCKED
50 #include <linux/smp_lock.h>
51 #endif
52 #include <linux/unistd.h>
53 #include <linux/interrupt.h>
54 #include <asm/uaccess.h>
55 #include <linux/completion.h>
56
57 #include <linux/fs.h>
58 #include <linux/stat.h>
59 #include <asm/uaccess.h>
60 #include <linux/miscdevice.h>
61 #include <linux/version.h>
62
63 # define DEBUG_SUBSYSTEM S_LNET
64
65 #include <libcfs/libcfs.h>
66 #include <libcfs/linux/portals_compat25.h>
67
68 #include "tracefile.h"
69
70 #include <linux/kallsyms.h>
71
72 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
73 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
74
75 /**
76  * Upcall function once a Lustre log has been dumped.
77  *
78  * \param file  path of the dumped log
79  */
80 void libcfs_run_debug_log_upcall(char *file)
81 {
82         char *argv[3];
83         int   rc;
84         char *envp[] = {
85                 "HOME=/",
86                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
87                 NULL};
88         ENTRY;
89
90         argv[0] = lnet_debug_log_upcall;
91
92         LASSERTF(file != NULL, "called on a null filename\n");
93         argv[1] = file; //only need to pass the path of the file
94
95         argv[2] = NULL;
96
97         rc = USERMODEHELPER(argv[0], argv, envp);
98         if (rc < 0 && rc != -ENOENT) {
99                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
100                        "check /proc/sys/lnet/debug_log_upcall\n",
101                        rc, argv[0], argv[1]);
102         } else {
103                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
104                        argv[0], argv[1]);
105         }
106
107         EXIT;
108 }
109
110 void libcfs_run_upcall(char **argv)
111 {
112         int   rc;
113         int   argc;
114         char *envp[] = {
115                 "HOME=/",
116                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
117                 NULL};
118         ENTRY;
119
120         argv[0] = lnet_upcall;
121         argc = 1;
122         while (argv[argc] != NULL)
123                 argc++;
124
125         LASSERT(argc >= 2);
126
127         rc = USERMODEHELPER(argv[0], argv, envp);
128         if (rc < 0 && rc != -ENOENT) {
129                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
130                        "check /proc/sys/lnet/upcall\n",
131                        rc, argv[0], argv[1],
132                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
133                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
134                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
135                        argc < 6 ? "" : ",...");
136         } else {
137                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
138                        argv[0], argv[1],
139                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
140                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
141                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
142                        argc < 6 ? "" : ",...");
143         }
144 }
145
146 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
147 {
148         char *argv[6];
149         char buf[32];
150
151         ENTRY;
152         snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
153
154         argv[1] = "LBUG";
155         argv[2] = (char *)msgdata->msg_file;
156         argv[3] = (char *)msgdata->msg_fn;
157         argv[4] = buf;
158         argv[5] = NULL;
159
160         libcfs_run_upcall (argv);
161 }
162
163 #ifdef __arch_um__
164 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
165 {
166         libcfs_catastrophe = 1;
167         libcfs_debug_msg(msgdata, "LBUG - trying to dump log to %s\n",
168                          libcfs_debug_file_path);
169         libcfs_debug_dumplog();
170         libcfs_run_lbug_upcall(msgdata);
171         asm("int $3");
172         panic("LBUG");
173 }
174 #else
175 /* coverity[+kill] */
176 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
177 {
178         libcfs_catastrophe = 1;
179         libcfs_debug_msg(msgdata, "LBUG\n");
180
181         if (in_interrupt()) {
182                 panic("LBUG in interrupt.\n");
183                 /* not reached */
184         }
185
186         libcfs_debug_dumpstack(NULL);
187         if (!libcfs_panic_on_lbug)
188                 libcfs_debug_dumplog();
189         libcfs_run_lbug_upcall(msgdata);
190         if (libcfs_panic_on_lbug)
191                 panic("LBUG");
192         set_task_state(current, TASK_UNINTERRUPTIBLE);
193         while (1)
194                 schedule();
195 }
196 #endif /* __arch_um__ */
197
198 #ifdef __KERNEL__
199
200 #ifdef HAVE_DUMP_TRACE
201 #include <linux/nmi.h>
202 #include <asm/stacktrace.h>
203
204 #ifdef HAVE_STACKTRACE_WARNING
205 static void
206 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
207 {
208         printk("%s", (char *)data);
209         print_symbol(msg, symbol);
210         printk("\n");
211 }
212
213 static void print_trace_warning(void *data, char *msg)
214 {
215         printk("%s%s\n", (char *)data, msg);
216 }
217 #endif
218
219 static int print_trace_stack(void *data, char *name)
220 {
221         printk(" <%s> ", name);
222         return 0;
223 }
224
225 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
226 # define RELIABLE reliable
227 # define DUMP_TRACE_CONST const
228 static void print_trace_address(void *data, unsigned long addr, int reliable)
229 #else
230 /* before 2.6.24 there was no reliable arg */
231 # define RELIABLE 1
232 # define DUMP_TRACE_CONST
233 static void print_trace_address(void *data, unsigned long addr)
234 #endif
235 {
236         char fmt[32];
237         touch_nmi_watchdog();
238         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
239         __print_symbol(fmt, addr);
240 }
241
242 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
243 #ifdef HAVE_STACKTRACE_WARNING
244         .warning = print_trace_warning,
245         .warning_symbol = print_trace_warning_symbol,
246 #endif
247         .stack = print_trace_stack,
248         .address = print_trace_address,
249 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
250         .walk_stack = print_context_stack,
251 #endif
252 };
253 #endif
254
255 void libcfs_debug_dumpstack(struct task_struct *tsk)
256 {
257 #if defined(__arch_um__)
258         if (tsk != NULL)
259                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
260                       tsk->pid, UML_PID(tsk));
261         //asm("int $3");
262 #elif defined(HAVE_DUMP_TRACE)
263         /* dump_stack() */
264         /* show_trace() */
265         if (tsk == NULL)
266                 tsk = current;
267         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
268         /* show_trace_log_lvl() */
269         printk("\nCall Trace:\n");
270         dump_trace(tsk, NULL, NULL,
271 #ifdef HAVE_DUMP_TRACE_ADDRESS
272                    0,
273 #endif /* HAVE_DUMP_TRACE_ADDRESS */
274                    &print_trace_ops, NULL);
275         printk("\n");
276 #elif defined(HAVE_SHOW_TASK)
277         /* this is exported by lustre kernel version 42 */
278         extern void show_task(struct task_struct *);
279
280         if (tsk == NULL)
281                 tsk = current;
282         CWARN("showing stack for process %d\n", tsk->pid);
283         show_task(tsk);
284 #else
285         if ((tsk == NULL) || (tsk == current))
286                 dump_stack();
287         else
288                 CWARN("can't show stack: kernel doesn't export show_task\n");
289 #endif
290 }
291
292 cfs_task_t *libcfs_current(void)
293 {
294         CWARN("current task struct is %p\n", current);
295         return current;
296 }
297
298 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
299                          void *unused2)
300 {
301         if (libcfs_panic_in_progress)
302                 return 0;
303
304         libcfs_panic_in_progress = 1;
305         mb();
306
307 #ifdef LNET_DUMP_ON_PANIC
308         /* This is currently disabled because it spews far too much to the
309          * console on the rare cases it is ever triggered. */
310
311         if (in_interrupt()) {
312                 cfs_trace_debug_print();
313         } else {
314 # ifdef HAVE_KERNEL_LOCKED
315                 while (kernel_locked())
316                         unlock_kernel();
317 # endif
318                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
319         }
320 #endif
321         return 0;
322 }
323
324 static struct notifier_block libcfs_panic_notifier = {
325         notifier_call :     panic_notifier,
326         next :              NULL,
327         priority :          10000
328 };
329
330 void libcfs_register_panic_notifier(void)
331 {
332         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
333 }
334
335 void libcfs_unregister_panic_notifier(void)
336 {
337         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
338 }
339
340 EXPORT_SYMBOL(libcfs_debug_dumpstack);
341 EXPORT_SYMBOL(libcfs_current);
342
343 #endif /* __KERNEL__ */
344
345 EXPORT_SYMBOL(libcfs_run_upcall);
346 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
347 EXPORT_SYMBOL(lbug_with_loc);