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