Whamcloud - gitweb
b619f9a58ff585bb6a3f43d23a4a78811d4aeba7
[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 #include <linux/string.h>
44 #include <linux/unistd.h>
45 #include <linux/stacktrace.h>
46 #include <linux/utsname.h>
47
48 # define DEBUG_SUBSYSTEM S_LNET
49
50 #include <libcfs/libcfs.h>
51
52 #include "tracefile.h"
53
54 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
55
56 /**
57  * Upcall function once a Lustre log has been dumped.
58  *
59  * \param file  path of the dumped log
60  */
61 void libcfs_run_debug_log_upcall(char *file)
62 {
63         char *argv[3];
64         int   rc;
65         char *envp[] = {
66                 "HOME=/",
67                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
68                 NULL};
69         ENTRY;
70
71         argv[0] = lnet_debug_log_upcall;
72
73         LASSERTF(file != NULL, "called on a null filename\n");
74         argv[1] = file; //only need to pass the path of the file
75
76         argv[2] = NULL;
77
78         rc = call_usermodehelper(argv[0], argv, envp, 1);
79         if (rc < 0 && rc != -ENOENT) {
80                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
81                        "check /proc/sys/lnet/debug_log_upcall\n",
82                        rc, argv[0], argv[1]);
83         } else {
84                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
85                        argv[0], argv[1]);
86         }
87
88         EXIT;
89 }
90
91 /* coverity[+kill] */
92 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
93 {
94         libcfs_catastrophe = 1;
95         libcfs_debug_msg(msgdata, "LBUG\n");
96
97         if (in_interrupt()) {
98                 panic("LBUG in interrupt.\n");
99                 /* not reached */
100         }
101
102         libcfs_debug_dumpstack(NULL);
103         if (libcfs_panic_on_lbug)
104                 panic("LBUG");
105         else
106                 libcfs_debug_dumplog();
107         set_current_state(TASK_UNINTERRUPTIBLE);
108         while (1)
109                 schedule();
110 }
111 EXPORT_SYMBOL(lbug_with_loc);
112
113 #ifdef CONFIG_STACKTRACE
114
115 #ifndef HAVE_SAVE_STACK_TRACE_TSK
116 #define save_stack_trace_tsk(tsk, trace)                                       \
117 do {                                                                           \
118         if (tsk == current)                                                    \
119                 save_stack_trace(trace);                                       \
120         else                                                                   \
121                 pr_info("No stack, save_stack_trace_tsk() not exported\n");    \
122 } while (0)
123 #endif
124
125 #define MAX_ST_ENTRIES  100
126 static DEFINE_SPINLOCK(st_lock);
127
128 /*
129  * Linux v5.1-rc5 214d8ca6ee ("stacktrace: Provide common infrastructure")
130  * CONFIG_ARCH_STACKWALK indicates that save_stack_trace_tsk symbol is not
131  * exported. Use symbol_get() to find if save_stack_trace_tsk is available.
132  */
133 #ifdef CONFIG_ARCH_STACKWALK
134 typedef unsigned int (stack_trace_save_tsk_t)(struct task_struct *task,
135                 unsigned long *store, unsigned int size,
136                 unsigned int skipnr);
137 static stack_trace_save_tsk_t *task_dump_stack;
138 #endif
139
140 static void libcfs_call_trace(struct task_struct *tsk)
141 {
142 #ifdef CONFIG_ARCH_STACKWALK
143         static unsigned long entries[MAX_ST_ENTRIES];
144         unsigned int i, nr_entries;
145
146         if (!task_dump_stack)
147                 task_dump_stack = (stack_trace_save_tsk_t *)
148                         symbol_get("stack_trace_save_tsk");
149
150         spin_lock(&st_lock);
151         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
152                init_utsname()->release, init_utsname()->version);
153         pr_info("Call Trace TBD:\n");
154         if (task_dump_stack) {
155                 nr_entries = task_dump_stack(tsk, entries, MAX_ST_ENTRIES, 0);
156                 for (i = 0; i < nr_entries; i++)
157                         pr_info("[<0>] %pB\n", (void *)entries[i]);
158         }
159         spin_unlock(&st_lock);
160 #else
161         struct stack_trace trace;
162         static unsigned long entries[MAX_ST_ENTRIES];
163
164         trace.nr_entries = 0;
165         trace.max_entries = MAX_ST_ENTRIES;
166         trace.entries = entries;
167         trace.skip = 0;
168
169         spin_lock(&st_lock);
170         pr_info("Pid: %d, comm: %.20s %s %s\n", tsk->pid, tsk->comm,
171                init_utsname()->release, init_utsname()->version);
172         pr_info("Call Trace:\n");
173         save_stack_trace_tsk(tsk, &trace);
174         print_stack_trace(&trace, 0);
175         spin_unlock(&st_lock);
176 #endif
177 }
178
179 #else /* !CONFIG_STACKTRACE */
180
181 #ifdef CONFIG_X86
182 #include <linux/nmi.h>
183 #include <asm/stacktrace.h>
184
185 #ifdef HAVE_STACKTRACE_OPS
186 #ifdef HAVE_STACKTRACE_WARNING
187 static void
188 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
189 {
190         printk("%s", (char *)data);
191         print_symbol(msg, symbol);
192         printk("\n");
193 }
194
195 static void print_trace_warning(void *data, char *msg)
196 {
197         printk("%s%s\n", (char *)data, msg);
198 }
199 #endif
200
201 static int print_trace_stack(void *data, char *name)
202 {
203         printk(" <%s> ", name);
204         return 0;
205 }
206
207 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
208 static int
209 #else
210 static void
211 #endif
212 print_trace_address(void *data, unsigned long addr, int reliable)
213 {
214         char fmt[32];
215
216         touch_nmi_watchdog();
217         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
218         __print_symbol(fmt, addr);
219 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
220         return 0;
221 #endif
222 }
223
224 static const struct stacktrace_ops print_trace_ops = {
225 #ifdef HAVE_STACKTRACE_WARNING
226         .warning = print_trace_warning,
227         .warning_symbol = print_trace_warning_symbol,
228 #endif
229         .stack = print_trace_stack,
230         .address = print_trace_address,
231 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
232         .walk_stack = print_context_stack,
233 #endif
234 };
235 #endif /* HAVE_STACKTRACE_OPS */
236
237 static void libcfs_call_trace(struct task_struct *tsk)
238 {
239 #ifdef HAVE_STACKTRACE_OPS
240         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
241         printk("\nCall Trace:\n");
242         dump_trace(tsk, NULL, NULL,
243 #ifdef HAVE_DUMP_TRACE_ADDRESS
244                    0,
245 #endif /* HAVE_DUMP_TRACE_ADDRESS */
246                    &print_trace_ops, NULL);
247         printk("\n");
248 #else /* !HAVE_STACKTRACE_OPS */
249         if (tsk == current)
250                 dump_stack();
251         else
252                 CWARN("can't show stack: kernel doesn't export show_task\n");
253 #endif /* HAVE_STACKTRACE_OPS */
254 }
255
256 #else /* !CONFIG_X86 */
257
258 static void libcfs_call_trace(struct task_struct *tsk)
259 {
260         if (tsk == current)
261                 dump_stack();
262         else
263                 CWARN("can't show stack: kernel doesn't export show_task\n");
264 }
265
266 #endif /* CONFIG_X86 */
267
268 #endif /* CONFIG_STACKTRACE */
269
270 void libcfs_debug_dumpstack(struct task_struct *tsk)
271 {
272         libcfs_call_trace(tsk ?: current);
273 }
274 EXPORT_SYMBOL(libcfs_debug_dumpstack);
275
276 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
277                          void *unused2)
278 {
279         if (libcfs_panic_in_progress)
280                 return 0;
281
282         libcfs_panic_in_progress = 1;
283         mb();
284
285 #ifdef LNET_DUMP_ON_PANIC
286         /* This is currently disabled because it spews far too much to the
287          * console on the rare cases it is ever triggered. */
288
289         if (in_interrupt()) {
290                 cfs_trace_debug_print();
291         } else {
292                 libcfs_debug_dumplog_internal((void *)(long)current_pid());
293         }
294 #endif
295         return 0;
296 }
297
298 static struct notifier_block libcfs_panic_notifier = {
299         .notifier_call  = panic_notifier,
300         .next           = NULL,
301         .priority       = 10000
302 };
303
304 void libcfs_register_panic_notifier(void)
305 {
306         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
307 }
308
309 void libcfs_unregister_panic_notifier(void)
310 {
311         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
312 }