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