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