Whamcloud - gitweb
LU-12977 ldiskfs: properly take inode_lock() for truncates
[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 static int print_trace_stack(void *data, char *name)
187 {
188         printk(" <%s> ", name);
189         return 0;
190 }
191
192 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
193 static int
194 #else
195 static void
196 #endif
197 print_trace_address(void *data, unsigned long addr, int reliable)
198 {
199         char fmt[32];
200
201         touch_nmi_watchdog();
202         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
203         __print_symbol(fmt, addr);
204 #ifdef STACKTRACE_OPS_ADDRESS_RETURN_INT
205         return 0;
206 #endif
207 }
208
209 static const struct stacktrace_ops print_trace_ops = {
210         .stack = print_trace_stack,
211         .address = print_trace_address,
212         .walk_stack = print_context_stack,
213 };
214 #endif /* HAVE_STACKTRACE_OPS */
215
216 static void libcfs_call_trace(struct task_struct *tsk)
217 {
218 #ifdef HAVE_STACKTRACE_OPS
219         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
220         printk("\nCall Trace:\n");
221         dump_trace(tsk, NULL, NULL, 0, &print_trace_ops, NULL);
222         printk("\n");
223 #else /* !HAVE_STACKTRACE_OPS */
224         if (tsk == current)
225                 dump_stack();
226         else
227                 CWARN("can't show stack: kernel doesn't export show_task\n");
228 #endif /* HAVE_STACKTRACE_OPS */
229 }
230
231 #else /* !CONFIG_X86 */
232
233 static void libcfs_call_trace(struct task_struct *tsk)
234 {
235         if (tsk == current)
236                 dump_stack();
237         else
238                 CWARN("can't show stack: kernel doesn't export show_task\n");
239 }
240
241 #endif /* CONFIG_X86 */
242
243 #endif /* CONFIG_STACKTRACE */
244
245 void libcfs_debug_dumpstack(struct task_struct *tsk)
246 {
247         libcfs_call_trace(tsk ?: current);
248 }
249 EXPORT_SYMBOL(libcfs_debug_dumpstack);
250
251 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
252                          void *unused2)
253 {
254         if (libcfs_panic_in_progress)
255                 return 0;
256
257         libcfs_panic_in_progress = 1;
258         mb();
259
260 #ifdef LNET_DUMP_ON_PANIC
261         /* This is currently disabled because it spews far too much to the
262          * console on the rare cases it is ever triggered. */
263
264         if (in_interrupt()) {
265                 cfs_trace_debug_print();
266         } else {
267                 libcfs_debug_dumplog_internal((void *)(long)current_pid());
268         }
269 #endif
270         return 0;
271 }
272
273 static struct notifier_block libcfs_panic_notifier = {
274         .notifier_call  = panic_notifier,
275         .next           = NULL,
276         .priority       = 10000
277 };
278
279 void libcfs_register_panic_notifier(void)
280 {
281         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
282 }
283
284 void libcfs_unregister_panic_notifier(void)
285 {
286         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
287 }