Whamcloud - gitweb
LU-812 kernel: AUTOCONF_INCLUDED removed
[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, Whamcloud, Inc.
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/system.h>
55 #include <asm/uaccess.h>
56 #include <linux/completion.h>
57
58 #include <linux/fs.h>
59 #include <linux/stat.h>
60 #include <asm/uaccess.h>
61 #include <linux/miscdevice.h>
62 #include <linux/version.h>
63
64 # define DEBUG_SUBSYSTEM S_LNET
65
66 #include <libcfs/libcfs.h>
67 #include <libcfs/linux/portals_compat25.h>
68
69 #include "tracefile.h"
70
71 #include <linux/kallsyms.h>
72
73 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
74 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
75
76 /**
77  * Upcall function once a Lustre log has been dumped.
78  *
79  * \param file  path of the dumped log
80  */
81 void libcfs_run_debug_log_upcall(char *file)
82 {
83         char *argv[3];
84         int   rc;
85         char *envp[] = {
86                 "HOME=/",
87                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
88                 NULL};
89         ENTRY;
90
91         argv[0] = lnet_debug_log_upcall;
92
93         LASSERTF(file != NULL, "called on a null filename\n");
94         argv[1] = file; //only need to pass the path of the file
95
96         argv[2] = NULL;
97
98         rc = USERMODEHELPER(argv[0], argv, envp);
99         if (rc < 0 && rc != -ENOENT) {
100                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
101                        "check /proc/sys/lnet/debug_log_upcall\n",
102                        rc, argv[0], argv[1]);
103         } else {
104                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
105                        argv[0], argv[1]);
106         }
107
108         EXIT;
109 }
110
111 void libcfs_run_upcall(char **argv)
112 {
113         int   rc;
114         int   argc;
115         char *envp[] = {
116                 "HOME=/",
117                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
118                 NULL};
119         ENTRY;
120
121         argv[0] = lnet_upcall;
122         argc = 1;
123         while (argv[argc] != NULL)
124                 argc++;
125
126         LASSERT(argc >= 2);
127
128         rc = USERMODEHELPER(argv[0], argv, envp);
129         if (rc < 0 && rc != -ENOENT) {
130                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
131                        "check /proc/sys/lnet/upcall\n",
132                        rc, argv[0], argv[1],
133                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
134                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
135                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
136                        argc < 6 ? "" : ",...");
137         } else {
138                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
139                        argv[0], argv[1],
140                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
141                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
142                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
143                        argc < 6 ? "" : ",...");
144         }
145 }
146
147 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
148 {
149         char *argv[6];
150         char buf[32];
151
152         ENTRY;
153         snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
154
155         argv[1] = "LBUG";
156         argv[2] = (char *)msgdata->msg_file;
157         argv[3] = (char *)msgdata->msg_fn;
158         argv[4] = buf;
159         argv[5] = NULL;
160
161         libcfs_run_upcall (argv);
162 }
163
164 #ifdef __arch_um__
165 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
166 {
167         libcfs_catastrophe = 1;
168         libcfs_debug_msg(msgdata, "LBUG - trying to dump log to %s\n",
169                          libcfs_debug_file_path);
170         libcfs_debug_dumplog();
171         libcfs_run_lbug_upcall(msgdata);
172         asm("int $3");
173         panic("LBUG");
174 }
175 #else
176 /* coverity[+kill] */
177 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
178 {
179         libcfs_catastrophe = 1;
180         libcfs_debug_msg(msgdata, "LBUG\n");
181
182         if (in_interrupt()) {
183                 panic("LBUG in interrupt.\n");
184                 /* not reached */
185         }
186
187         libcfs_debug_dumpstack(NULL);
188         if (!libcfs_panic_on_lbug)
189                 libcfs_debug_dumplog();
190         libcfs_run_lbug_upcall(msgdata);
191         if (libcfs_panic_on_lbug)
192                 panic("LBUG");
193         set_task_state(current, TASK_UNINTERRUPTIBLE);
194         while (1)
195                 schedule();
196 }
197 #endif /* __arch_um__ */
198
199 #ifdef __KERNEL__
200
201 #ifdef HAVE_DUMP_TRACE
202 #include <linux/nmi.h>
203 #include <asm/stacktrace.h>
204
205 #ifdef HAVE_STACKTRACE_WARNING
206 static void
207 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
208 {
209         printk("%s", (char *)data);
210         print_symbol(msg, symbol);
211         printk("\n");
212 }
213
214 static void print_trace_warning(void *data, char *msg)
215 {
216         printk("%s%s\n", (char *)data, msg);
217 }
218 #endif
219
220 static int print_trace_stack(void *data, char *name)
221 {
222         printk(" <%s> ", name);
223         return 0;
224 }
225
226 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
227 # define RELIABLE reliable
228 # define DUMP_TRACE_CONST const
229 static void print_trace_address(void *data, unsigned long addr, int reliable)
230 #else
231 /* before 2.6.24 there was no reliable arg */
232 # define RELIABLE 1
233 # define DUMP_TRACE_CONST
234 static void print_trace_address(void *data, unsigned long addr)
235 #endif
236 {
237         char fmt[32];
238         touch_nmi_watchdog();
239         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
240         __print_symbol(fmt, addr);
241 }
242
243 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
244 #ifdef HAVE_STACKTRACE_WARNING
245         .warning = print_trace_warning,
246         .warning_symbol = print_trace_warning_symbol,
247 #endif
248         .stack = print_trace_stack,
249         .address = print_trace_address,
250 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
251         .walk_stack = print_context_stack,
252 #endif
253 };
254 #endif
255
256 void libcfs_debug_dumpstack(struct task_struct *tsk)
257 {
258 #if defined(__arch_um__)
259         if (tsk != NULL)
260                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
261                       tsk->pid, UML_PID(tsk));
262         //asm("int $3");
263 #elif defined(HAVE_DUMP_TRACE)
264         /* dump_stack() */
265         /* show_trace() */
266         if (tsk == NULL)
267                 tsk = current;
268         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
269         /* show_trace_log_lvl() */
270         printk("\nCall Trace:\n");
271         dump_trace(tsk, NULL, NULL,
272 #ifdef HAVE_DUMP_TRACE_ADDRESS
273                    0,
274 #endif /* HAVE_DUMP_TRACE_ADDRESS */
275                    &print_trace_ops, NULL);
276         printk("\n");
277 #elif defined(HAVE_SHOW_TASK)
278         /* this is exported by lustre kernel version 42 */
279         extern void show_task(struct task_struct *);
280
281         if (tsk == NULL)
282                 tsk = current;
283         CWARN("showing stack for process %d\n", tsk->pid);
284         show_task(tsk);
285 #else
286         if ((tsk == NULL) || (tsk == current))
287                 dump_stack();
288         else
289                 CWARN("can't show stack: kernel doesn't export show_task\n");
290 #endif
291 }
292
293 cfs_task_t *libcfs_current(void)
294 {
295         CWARN("current task struct is %p\n", current);
296         return current;
297 }
298
299 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
300                          void *unused2)
301 {
302         if (libcfs_panic_in_progress)
303                 return 0;
304
305         libcfs_panic_in_progress = 1;
306         mb();
307
308 #ifdef LNET_DUMP_ON_PANIC
309         /* This is currently disabled because it spews far too much to the
310          * console on the rare cases it is ever triggered. */
311
312         if (in_interrupt()) {
313                 cfs_trace_debug_print();
314         } else {
315 # ifdef HAVE_KERNEL_LOCKED
316                 while (kernel_locked())
317                         unlock_kernel();
318 # endif
319                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
320         }
321 #endif
322         return 0;
323 }
324
325 static struct notifier_block libcfs_panic_notifier = {
326         notifier_call :     panic_notifier,
327         next :              NULL,
328         priority :          10000
329 };
330
331 void libcfs_register_panic_notifier(void)
332 {
333         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
334 }
335
336 void libcfs_unregister_panic_notifier(void)
337 {
338         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
339 }
340
341 EXPORT_SYMBOL(libcfs_debug_dumpstack);
342 EXPORT_SYMBOL(libcfs_current);
343
344 #endif /* __KERNEL__ */
345
346 EXPORT_SYMBOL(libcfs_run_upcall);
347 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
348 EXPORT_SYMBOL(lbug_with_loc);