Whamcloud - gitweb
b=17166 remove <2.6.9 compatibility code from HEAD
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/kmod.h>
50 #include <linux/notifier.h>
51 #include <linux/kernel.h>
52 #include <linux/mm.h>
53 #include <linux/string.h>
54 #include <linux/stat.h>
55 #include <linux/errno.h>
56 #include <linux/smp_lock.h>
57 #include <linux/unistd.h>
58 #include <linux/interrupt.h>
59 #include <asm/system.h>
60 #include <asm/uaccess.h>
61 #include <linux/completion.h>
62
63 #include <linux/fs.h>
64 #include <linux/stat.h>
65 #include <asm/uaccess.h>
66 #include <linux/miscdevice.h>
67 #include <linux/version.h>
68
69 # define DEBUG_SUBSYSTEM S_LNET
70
71 #include <libcfs/libcfs.h>
72 #include <libcfs/linux/portals_compat25.h>
73
74 #include "tracefile.h"
75
76 #include <linux/kallsyms.h>
77
78 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
79 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
80
81 /**
82  * Upcall function once a Lustre log has been dumped.
83  *
84  * \param file  path of the dumped log
85  */
86 void libcfs_run_debug_log_upcall(char *file)
87 {
88         char *argv[3];
89         int   rc;
90         char *envp[] = {
91                 "HOME=/",
92                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
93                 NULL};
94         ENTRY;
95
96         argv[0] = lnet_debug_log_upcall;
97
98         LASSERTF(file != NULL, "called on a null filename\n");
99         argv[1] = file; //only need to pass the path of the file
100
101         argv[2] = NULL;
102
103         rc = USERMODEHELPER(argv[0], argv, envp);
104         if (rc < 0 && rc != -ENOENT) {
105                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
106                        "check /proc/sys/lnet/debug_log_upcall\n",
107                        rc, argv[0], argv[1]);
108         } else {
109                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
110                        argv[0], argv[1]);
111         }
112
113         EXIT;
114 }
115
116 void libcfs_run_upcall(char **argv)
117 {
118         int   rc;
119         int   argc;
120         char *envp[] = {
121                 "HOME=/",
122                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
123                 NULL};
124         ENTRY;
125
126         argv[0] = lnet_upcall;
127         argc = 1;
128         while (argv[argc] != NULL)
129                 argc++;
130
131         LASSERT(argc >= 2);
132
133         rc = USERMODEHELPER(argv[0], argv, envp);
134         if (rc < 0 && rc != -ENOENT) {
135                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
136                        "check /proc/sys/lnet/upcall\n",
137                        rc, 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         } else {
143                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
144                        argv[0], argv[1],
145                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
146                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
147                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
148                        argc < 6 ? "" : ",...");
149         }
150 }
151
152 void libcfs_run_lbug_upcall(const char *file, const char *fn, const int line)
153 {
154         char *argv[6];
155         char buf[32];
156
157         ENTRY;
158         snprintf (buf, sizeof buf, "%d", line);
159
160         argv[1] = "LBUG";
161         argv[2] = (char *)file;
162         argv[3] = (char *)fn;
163         argv[4] = buf;
164         argv[5] = NULL;
165
166         libcfs_run_upcall (argv);
167 }
168
169 #ifdef __arch_um__
170 void lbug_with_loc(const char *file, const char *func, const int line)
171 {
172         libcfs_catastrophe = 1;
173         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
174                          "LBUG - trying to dump log to %s\n",
175                          libcfs_debug_file_path);
176         libcfs_debug_dumplog();
177         libcfs_run_lbug_upcall(file, func, line);
178         asm("int $3");
179         panic("LBUG");
180 }
181 #else
182 /* coverity[+kill] */
183 void lbug_with_loc(const char *file, const char *func, const int line)
184 {
185         libcfs_catastrophe = 1;
186         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
187
188         if (in_interrupt()) {
189                 panic("LBUG in interrupt.\n");
190                 /* not reached */
191         }
192
193         libcfs_debug_dumpstack(NULL);
194         if (!libcfs_panic_on_lbug)
195                 libcfs_debug_dumplog();
196         libcfs_run_lbug_upcall(file, func, line);
197         if (libcfs_panic_on_lbug)
198                 panic("LBUG");
199         set_task_state(current, TASK_UNINTERRUPTIBLE);
200         while (1)
201                 schedule();
202 }
203 #endif /* __arch_um__ */
204
205 #ifdef __KERNEL__
206
207 #ifdef HAVE_DUMP_TRACE
208 #include <linux/nmi.h>
209 #include <asm/stacktrace.h>
210
211 static void
212 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
213 {
214         printk("%s", (char *)data);
215         print_symbol(msg, symbol);
216         printk("\n");
217 }
218
219 static void print_trace_warning(void *data, char *msg)
220 {
221         printk("%s%s\n", (char *)data, msg);
222 }
223
224 static int print_trace_stack(void *data, char *name)
225 {
226         printk(" <%s> ", name);
227         return 0;
228 }
229
230 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
231 # define RELIABLE reliable
232 # define DUMP_TRACE_CONST const
233 static void print_trace_address(void *data, unsigned long addr, int reliable)
234 #else
235 /* before 2.6.24 there was no reliable arg */
236 # define RELIABLE 1
237 # define DUMP_TRACE_CONST
238 static void print_trace_address(void *data, unsigned long addr)
239 #endif
240 {
241         char fmt[32];
242         touch_nmi_watchdog();
243         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
244         __print_symbol(fmt, addr);
245 }
246
247 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
248         .warning = print_trace_warning,
249         .warning_symbol = print_trace_warning_symbol,
250         .stack = print_trace_stack,
251         .address = print_trace_address,
252 };
253 #endif
254
255 void libcfs_debug_dumpstack(struct task_struct *tsk)
256 {
257 #if defined(__arch_um__)
258         if (tsk != NULL)
259                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
260                       tsk->pid, UML_PID(tsk));
261         //asm("int $3");
262 #elif defined(HAVE_DUMP_TRACE)
263         /* dump_stack() */
264         /* show_trace() */
265         if (tsk == NULL)
266                 tsk = current;
267         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
268         /* show_trace_log_lvl() */
269         printk("\nCall Trace:\n");
270         dump_trace(tsk, NULL, NULL,
271 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
272                    0,
273 #endif /* HAVE_TRACE_ADDRESS_RELIABLE */
274                    &print_trace_ops, NULL);
275         printk("\n");
276 #elif defined(HAVE_SHOW_TASK)
277         /* this is exported by lustre kernel version 42 */
278         extern void show_task(struct task_struct *);
279
280         if (tsk == NULL)
281                 tsk = current;
282         CWARN("showing stack for process %d\n", tsk->pid);
283         show_task(tsk);
284 #else
285         if ((tsk == NULL) || (tsk == current))
286                 dump_stack();
287         else
288                 CWARN("can't show stack: kernel doesn't export show_task\n");
289 #endif
290 }
291
292 cfs_task_t *libcfs_current(void)
293 {
294         CWARN("current task struct is %p\n", current);
295         return current;
296 }
297
298 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
299                          void *unused2)
300 {
301         if (libcfs_panic_in_progress)
302                 return 0;
303
304         libcfs_panic_in_progress = 1;
305         mb();
306
307 #ifdef LNET_DUMP_ON_PANIC
308         /* This is currently disabled because it spews far too much to the
309          * console on the rare cases it is ever triggered. */
310
311         if (in_interrupt()) {
312                 cfs_trace_debug_print();
313         } else {
314                 while (current->lock_depth >= 0)
315                         unlock_kernel();
316
317                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
318         }
319 #endif
320         return 0;
321 }
322
323 static struct notifier_block libcfs_panic_notifier = {
324         notifier_call :     panic_notifier,
325         next :              NULL,
326         priority :          10000
327 };
328
329 void libcfs_register_panic_notifier(void)
330 {
331 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
332         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
333 #else
334         notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
335 #endif
336 }
337
338 void libcfs_unregister_panic_notifier(void)
339 {
340 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
341         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
342 #else
343         notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
344 #endif
345 }
346
347 EXPORT_SYMBOL(libcfs_debug_dumpstack);
348 EXPORT_SYMBOL(libcfs_current);
349
350 #endif /* __KERNEL__ */
351
352 EXPORT_SYMBOL(libcfs_run_upcall);
353 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
354 EXPORT_SYMBOL(lbug_with_loc);