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