Whamcloud - gitweb
2778c62c10886a52566aa3e9045114a9ff79dd1e
[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, 2017, 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/errno.h>
38 #include <linux/interrupt.h>
39 #include <linux/kallsyms.h>
40 #include <linux/kmod.h>
41 #include <linux/module.h>
42 #include <linux/notifier.h>
43 #ifdef HAVE_KERNEL_LOCKED
44 #include <linux/smp_lock.h>
45 #endif
46 #include <linux/string.h>
47 #include <linux/unistd.h>
48
49 # define DEBUG_SUBSYSTEM S_LNET
50
51 #include <libcfs/libcfs.h>
52
53 #include "tracefile.h"
54
55 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
56
57 /**
58  * Upcall function once a Lustre log has been dumped.
59  *
60  * \param file  path of the dumped log
61  */
62 void libcfs_run_debug_log_upcall(char *file)
63 {
64         char *argv[3];
65         int   rc;
66         char *envp[] = {
67                 "HOME=/",
68                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
69                 NULL};
70         ENTRY;
71
72         argv[0] = lnet_debug_log_upcall;
73
74         LASSERTF(file != NULL, "called on a null filename\n");
75         argv[1] = file; //only need to pass the path of the file
76
77         argv[2] = NULL;
78
79         rc = call_usermodehelper(argv[0], argv, envp, 1);
80         if (rc < 0 && rc != -ENOENT) {
81                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
82                        "check /proc/sys/lnet/debug_log_upcall\n",
83                        rc, argv[0], argv[1]);
84         } else {
85                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
86                        argv[0], argv[1]);
87         }
88
89         EXIT;
90 }
91
92 /* coverity[+kill] */
93 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
94 {
95         libcfs_catastrophe = 1;
96         libcfs_debug_msg(msgdata, "LBUG\n");
97
98         if (in_interrupt()) {
99                 panic("LBUG in interrupt.\n");
100                 /* not reached */
101         }
102
103         libcfs_debug_dumpstack(NULL);
104         if (!libcfs_panic_on_lbug)
105                 libcfs_debug_dumplog();
106         if (libcfs_panic_on_lbug)
107                 panic("LBUG");
108         set_current_state(TASK_UNINTERRUPTIBLE);
109         while (1)
110                 schedule();
111 }
112 EXPORT_SYMBOL(lbug_with_loc);
113
114 #ifdef CONFIG_X86
115 #include <linux/nmi.h>
116 #include <asm/stacktrace.h>
117
118 #ifdef HAVE_STACKTRACE_OPS
119 #ifdef HAVE_STACKTRACE_WARNING
120 static void
121 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
122 {
123         printk("%s", (char *)data);
124         print_symbol(msg, symbol);
125         printk("\n");
126 }
127
128 static void print_trace_warning(void *data, char *msg)
129 {
130         printk("%s%s\n", (char *)data, msg);
131 }
132 #endif
133
134 static int print_trace_stack(void *data, char *name)
135 {
136         printk(" <%s> ", name);
137         return 0;
138 }
139
140 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
141 static int
142 #else
143 static void
144 #endif
145 print_trace_address(void *data, unsigned long addr, int reliable)
146 {
147         char fmt[32];
148
149         touch_nmi_watchdog();
150         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
151         __print_symbol(fmt, addr);
152 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
153         return 0;
154 #endif
155 }
156
157 static const struct stacktrace_ops print_trace_ops = {
158 #ifdef HAVE_STACKTRACE_WARNING
159         .warning = print_trace_warning,
160         .warning_symbol = print_trace_warning_symbol,
161 #endif
162         .stack = print_trace_stack,
163         .address = print_trace_address,
164 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
165         .walk_stack = print_context_stack,
166 #endif
167 };
168 #endif /* HAVE_STACKTRACE_OPS */
169
170 static void libcfs_call_trace(struct task_struct *tsk)
171 {
172 #ifdef HAVE_STACKTRACE_OPS
173         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
174         printk("\nCall Trace:\n");
175         dump_trace(tsk, NULL, NULL,
176 #ifdef HAVE_DUMP_TRACE_ADDRESS
177                    0,
178 #endif /* HAVE_DUMP_TRACE_ADDRESS */
179                    &print_trace_ops, NULL);
180         printk("\n");
181 #else /* !HAVE_STACKTRACE_OPS */
182         if (tsk == current)
183                 dump_stack();
184         else
185                 CWARN("can't show stack: kernel doesn't export show_task\n");
186 #endif /* HAVE_STACKTRACE_OPS */
187 }
188
189 #else /* !CONFIG_X86 */
190
191 static void libcfs_call_trace(struct task_struct *tsk)
192 {
193         if (tsk == current)
194                 dump_stack();
195         else
196                 CWARN("can't show stack: kernel doesn't export show_task\n");
197 }
198
199 #endif /* CONFIG_X86 */
200
201 void libcfs_debug_dumpstack(struct task_struct *tsk)
202 {
203         libcfs_call_trace(tsk ?: current);
204 }
205 EXPORT_SYMBOL(libcfs_debug_dumpstack);
206
207 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
208                          void *unused2)
209 {
210         if (libcfs_panic_in_progress)
211                 return 0;
212
213         libcfs_panic_in_progress = 1;
214         mb();
215
216 #ifdef LNET_DUMP_ON_PANIC
217         /* This is currently disabled because it spews far too much to the
218          * console on the rare cases it is ever triggered. */
219
220         if (in_interrupt()) {
221                 cfs_trace_debug_print();
222         } else {
223 #ifdef HAVE_KERNEL_LOCKED
224                 while (kernel_locked())
225                         unlock_kernel();
226 #endif
227                 libcfs_debug_dumplog_internal((void *)(long)current_pid());
228         }
229 #endif
230         return 0;
231 }
232
233 static struct notifier_block libcfs_panic_notifier = {
234         .notifier_call  = panic_notifier,
235         .next           = NULL,
236         .priority       = 10000
237 };
238
239 void libcfs_register_panic_notifier(void)
240 {
241         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
242 }
243
244 void libcfs_unregister_panic_notifier(void)
245 {
246         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
247 }