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