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