Whamcloud - gitweb
LU-5829 libcfs: remove unnecessary EXPORT_SYMBOL
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #include <linux/module.h>
42 #include <linux/kmod.h>
43 #include <linux/notifier.h>
44 #include <linux/kernel.h>
45 #include <linux/mm.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #ifdef HAVE_KERNEL_LOCKED
50 #include <linux/smp_lock.h>
51 #endif
52 #include <linux/unistd.h>
53 #include <linux/interrupt.h>
54 #include <asm/uaccess.h>
55 #include <linux/completion.h>
56
57 #include <linux/fs.h>
58 #include <linux/stat.h>
59 #include <asm/uaccess.h>
60 #include <linux/miscdevice.h>
61 #include <linux/version.h>
62
63 # define DEBUG_SUBSYSTEM S_LNET
64
65 #include <libcfs/libcfs.h>
66
67 #include "tracefile.h"
68
69 #include <linux/kallsyms.h>
70
71 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
72 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
73
74 /**
75  * Upcall function once a Lustre log has been dumped.
76  *
77  * \param file  path of the dumped log
78  */
79 void libcfs_run_debug_log_upcall(char *file)
80 {
81         char *argv[3];
82         int   rc;
83         char *envp[] = {
84                 "HOME=/",
85                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
86                 NULL};
87         ENTRY;
88
89         argv[0] = lnet_debug_log_upcall;
90
91         LASSERTF(file != NULL, "called on a null filename\n");
92         argv[1] = file; //only need to pass the path of the file
93
94         argv[2] = NULL;
95
96         rc = call_usermodehelper(argv[0], argv, envp, 1);
97         if (rc < 0 && rc != -ENOENT) {
98                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
99                        "check /proc/sys/lnet/debug_log_upcall\n",
100                        rc, argv[0], argv[1]);
101         } else {
102                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
103                        argv[0], argv[1]);
104         }
105
106         EXIT;
107 }
108
109 void libcfs_run_upcall(char **argv)
110 {
111         int   rc;
112         int   argc;
113         char *envp[] = {
114                 "HOME=/",
115                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
116                 NULL};
117         ENTRY;
118
119         argv[0] = lnet_upcall;
120         argc = 1;
121         while (argv[argc] != NULL)
122                 argc++;
123
124         LASSERT(argc >= 2);
125
126         rc = call_usermodehelper(argv[0], argv, envp, 1);
127         if (rc < 0 && rc != -ENOENT) {
128                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
129                        "check /proc/sys/lnet/upcall\n",
130                        rc, argv[0], argv[1],
131                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
132                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
133                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
134                        argc < 6 ? "" : ",...");
135         } else {
136                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
137                        argv[0], argv[1],
138                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
139                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
140                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
141                        argc < 6 ? "" : ",...");
142         }
143 }
144
145 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *msgdata)
146 {
147         char *argv[6];
148         char buf[32];
149
150         ENTRY;
151         snprintf (buf, sizeof buf, "%d", msgdata->msg_line);
152
153         argv[1] = "LBUG";
154         argv[2] = (char *)msgdata->msg_file;
155         argv[3] = (char *)msgdata->msg_fn;
156         argv[4] = buf;
157         argv[5] = NULL;
158
159         libcfs_run_upcall (argv);
160 }
161
162 /* coverity[+kill] */
163 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
164 {
165         libcfs_catastrophe = 1;
166         libcfs_debug_msg(msgdata, "LBUG\n");
167
168         if (in_interrupt()) {
169                 panic("LBUG in interrupt.\n");
170                 /* not reached */
171         }
172
173         libcfs_debug_dumpstack(NULL);
174         if (!libcfs_panic_on_lbug)
175                 libcfs_debug_dumplog();
176         libcfs_run_lbug_upcall(msgdata);
177         if (libcfs_panic_on_lbug)
178                 panic("LBUG");
179         set_task_state(current, TASK_UNINTERRUPTIBLE);
180         while (1)
181                 schedule();
182 }
183
184 #ifdef __KERNEL__
185
186 #ifdef CONFIG_X86
187 #include <linux/nmi.h>
188 #include <asm/stacktrace.h>
189
190 #ifdef HAVE_STACKTRACE_WARNING
191 static void
192 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
193 {
194         printk("%s", (char *)data);
195         print_symbol(msg, symbol);
196         printk("\n");
197 }
198
199 static void print_trace_warning(void *data, char *msg)
200 {
201         printk("%s%s\n", (char *)data, msg);
202 }
203 #endif
204
205 static int print_trace_stack(void *data, char *name)
206 {
207         printk(" <%s> ", name);
208         return 0;
209 }
210
211 static void print_trace_address(void *data, unsigned long addr, int reliable)
212 {
213         char fmt[32];
214
215         touch_nmi_watchdog();
216         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, reliable ? "": "? ");
217         __print_symbol(fmt, addr);
218 }
219
220 static const struct stacktrace_ops print_trace_ops = {
221 #ifdef HAVE_STACKTRACE_WARNING
222         .warning = print_trace_warning,
223         .warning_symbol = print_trace_warning_symbol,
224 #endif
225         .stack = print_trace_stack,
226         .address = print_trace_address,
227 #ifdef STACKTRACE_OPS_HAVE_WALK_STACK
228         .walk_stack = print_context_stack,
229 #endif
230 };
231
232 static void libcfs_call_trace(struct task_struct *tsk)
233 {
234         dump_trace(tsk, NULL, NULL,
235 #ifdef HAVE_DUMP_TRACE_ADDRESS
236                    0,
237 #endif /* HAVE_DUMP_TRACE_ADDRESS */
238                    &print_trace_ops, NULL);
239         printk("\n");
240 }
241
242 #else /* !CONFIG_X86 */
243
244 static void libcfs_call_trace(struct task_struct *tsk)
245 {
246         if ((tsk == NULL) || (tsk == current))
247                 dump_stack();
248         else
249                 CWARN("can't show stack: kernel doesn't export show_task\n");
250 }
251
252 #endif /* CONFIG_X86 */
253
254 void libcfs_debug_dumpstack(struct task_struct *tsk)
255 {
256         /* dump_stack() */
257         /* show_trace() */
258         if (tsk == NULL)
259                 tsk = current;
260         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
261         /* show_trace_log_lvl() */
262         printk("\nCall Trace:\n");
263         libcfs_call_trace(tsk);
264 }
265
266 struct task_struct *libcfs_current(void)
267 {
268         CWARN("current task struct is %p\n", current);
269         return current;
270 }
271
272 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
273                          void *unused2)
274 {
275         if (libcfs_panic_in_progress)
276                 return 0;
277
278         libcfs_panic_in_progress = 1;
279         mb();
280
281 #ifdef LNET_DUMP_ON_PANIC
282         /* This is currently disabled because it spews far too much to the
283          * console on the rare cases it is ever triggered. */
284
285         if (in_interrupt()) {
286                 cfs_trace_debug_print();
287         } else {
288 #ifdef HAVE_KERNEL_LOCKED
289                 while (kernel_locked())
290                         unlock_kernel();
291 #endif
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 }
313
314 EXPORT_SYMBOL(libcfs_debug_dumpstack);
315
316 #endif /* __KERNEL__ */
317
318 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
319 EXPORT_SYMBOL(lbug_with_loc);