Whamcloud - gitweb
40ae258b1a6b87602f474b822edfedfe8b4ff8f5
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/linux/linux-debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  */
36
37 #include <linux/module.h>
38 #include <linux/kmod.h>
39 #include <linux/notifier.h>
40 #include <linux/kernel.h>
41 #include <linux/mm.h>
42 #include <linux/string.h>
43 #include <linux/stat.h>
44 #include <linux/errno.h>
45 #ifdef HAVE_KERNEL_LOCKED
46 #include <linux/smp_lock.h>
47 #endif
48 #include <linux/unistd.h>
49 #include <linux/interrupt.h>
50 #include <asm/uaccess.h>
51 #include <linux/completion.h>
52
53 #include <linux/fs.h>
54 #include <linux/stat.h>
55 #include <asm/uaccess.h>
56 #include <linux/miscdevice.h>
57 #include <linux/version.h>
58
59 # define DEBUG_SUBSYSTEM S_LNET
60
61 #include <libcfs/libcfs.h>
62
63 #include "tracefile.h"
64
65 #include <linux/kallsyms.h>
66
67 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
68 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
69
70 /**
71  * Upcall function once a Lustre log has been dumped.
72  *
73  * \param file  path of the dumped log
74  */
75 void libcfs_run_debug_log_upcall(char *file)
76 {
77         char *argv[3];
78         int   rc;
79         char *envp[] = {
80                 "HOME=/",
81                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
82                 NULL};
83         ENTRY;
84
85         argv[0] = lnet_debug_log_upcall;
86
87         LASSERTF(file != NULL, "called on a null filename\n");
88         argv[1] = file; //only need to pass the path of the file
89
90         argv[2] = NULL;
91
92         rc = call_usermodehelper(argv[0], argv, envp, 1);
93         if (rc < 0 && rc != -ENOENT) {
94                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
95                        "check /proc/sys/lnet/debug_log_upcall\n",
96                        rc, argv[0], argv[1]);
97         } else {
98                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
99                        argv[0], argv[1]);
100         }
101
102         EXIT;
103 }
104
105 void libcfs_run_upcall(char **argv)
106 {
107         int   rc;
108         int   argc;
109         char *envp[] = {
110                 "HOME=/",
111                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
112                 NULL};
113         ENTRY;
114
115         argv[0] = lnet_upcall;
116         argc = 1;
117         while (argv[argc] != NULL)
118                 argc++;
119
120         LASSERT(argc >= 2);
121
122         rc = call_usermodehelper(argv[0], argv, envp, 1);
123         if (rc < 0 && rc != -ENOENT) {
124                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
125                        "check /proc/sys/lnet/upcall\n",
126                        rc, argv[0], argv[1],
127                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
128                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
129                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
130                        argc < 6 ? "" : ",...");
131         } else {
132                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
133                        argv[0], argv[1],
134                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
135                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
136                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
137                        argc < 6 ? "" : ",...");
138         }
139 }
140
141 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
142 {
143         char *argv[6];
144         char buf[32];
145
146         ENTRY;
147         snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
148
149         argv[1] = "LBUG";
150         argv[2] = (char *)msgdata->msg_file;
151         argv[3] = (char *)msgdata->msg_fn;
152         argv[4] = buf;
153         argv[5] = NULL;
154
155         libcfs_run_upcall (argv);
156 }
157 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
158
159 /* coverity[+kill] */
160 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
161 {
162         libcfs_catastrophe = 1;
163         libcfs_debug_msg(msgdata, "LBUG\n");
164
165         if (in_interrupt()) {
166                 panic("LBUG in interrupt.\n");
167                 /* not reached */
168         }
169
170         libcfs_debug_dumpstack(NULL);
171         if (!libcfs_panic_on_lbug)
172                 libcfs_debug_dumplog();
173         libcfs_run_lbug_upcall(msgdata);
174         if (libcfs_panic_on_lbug)
175                 panic("LBUG");
176         set_task_state(current, TASK_UNINTERRUPTIBLE);
177         while (1)
178                 schedule();
179 }
180 EXPORT_SYMBOL(lbug_with_loc);
181
182 #ifdef CONFIG_X86
183 #include <linux/nmi.h>
184 #include <asm/stacktrace.h>
185
186 #ifdef HAVE_STACKTRACE_WARNING
187 static void
188 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
189 {
190         printk("%s", (char *)data);
191         print_symbol(msg, symbol);
192         printk("\n");
193 }
194
195 static void print_trace_warning(void *data, char *msg)
196 {
197         printk("%s%s\n", (char *)data, msg);
198 }
199 #endif
200
201 static int print_trace_stack(void *data, char *name)
202 {
203         printk(" <%s> ", name);
204         return 0;
205 }
206
207 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
208 static int
209 #else
210 static void
211 #endif
212 print_trace_address(void *data, unsigned long addr, int reliable)
213 {
214         char fmt[32];
215
216         touch_nmi_watchdog();
217         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
218         __print_symbol(fmt, addr);
219 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
220         return 0;
221 #endif
222 }
223
224 static const struct stacktrace_ops print_trace_ops = {
225 #ifdef HAVE_STACKTRACE_WARNING
226         .warning = print_trace_warning,
227         .warning_symbol = print_trace_warning_symbol,
228 #endif
229         .stack = print_trace_stack,
230         .address = print_trace_address,
231 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
232         .walk_stack = print_context_stack,
233 #endif
234 };
235
236 static void libcfs_call_trace(struct task_struct *tsk)
237 {
238         dump_trace(tsk, NULL, NULL,
239 #ifdef HAVE_DUMP_TRACE_ADDRESS
240                    0,
241 #endif /* HAVE_DUMP_TRACE_ADDRESS */
242                    &print_trace_ops, NULL);
243         printk("\n");
244 }
245
246 #else /* !CONFIG_X86 */
247
248 static void libcfs_call_trace(struct task_struct *tsk)
249 {
250         if ((tsk == NULL) || (tsk == current))
251                 dump_stack();
252         else
253                 CWARN("can't show stack: kernel doesn't export show_task\n");
254 }
255
256 #endif /* CONFIG_X86 */
257
258 void libcfs_debug_dumpstack(struct task_struct *tsk)
259 {
260         /* dump_stack() */
261         /* show_trace() */
262         if (tsk == NULL)
263                 tsk = current;
264         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
265         /* show_trace_log_lvl() */
266         printk("\nCall Trace:\n");
267         libcfs_call_trace(tsk);
268 }
269 EXPORT_SYMBOL(libcfs_debug_dumpstack);
270
271 struct task_struct *libcfs_current(void)
272 {
273         CWARN("current task struct is %p\n", current);
274         return current;
275 }
276
277 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
278                          void *unused2)
279 {
280         if (libcfs_panic_in_progress)
281                 return 0;
282
283         libcfs_panic_in_progress = 1;
284         mb();
285
286 #ifdef LNET_DUMP_ON_PANIC
287         /* This is currently disabled because it spews far too much to the
288          * console on the rare cases it is ever triggered. */
289
290         if (in_interrupt()) {
291                 cfs_trace_debug_print();
292         } else {
293 #ifdef HAVE_KERNEL_LOCKED
294                 while (kernel_locked())
295                         unlock_kernel();
296 #endif
297                 libcfs_debug_dumplog_internal((void *)(long)current_pid());
298         }
299 #endif
300         return 0;
301 }
302
303 static struct notifier_block libcfs_panic_notifier = {
304         notifier_call :     panic_notifier,
305         next :              NULL,
306         priority :          10000
307 };
308
309 void libcfs_register_panic_notifier(void)
310 {
311         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
312 }
313
314 void libcfs_unregister_panic_notifier(void)
315 {
316         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
317 }