Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[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  2008 Sun Microsystems, Inc. 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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
77 #include <linux/kallsyms.h>
78 #endif
79
80 /* We need to pass a pointer here, but elsewhere this must be a const */
81 static char *debug_file_path = &libcfs_debug_file_path_arr[0];
82 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
83                 "Path for dumping debug logs (deprecated)");
84
85 char lnet_upcall[1024] = "/usr/lib/lustre/lnet_upcall";
86 char lnet_debug_log_upcall[1024] = "/usr/lib/lustre/lnet_debug_log_upcall";
87
88 /**
89  * Upcall function once a Lustre log has been dumped.
90  *
91  * \param file  path of the dumped log
92  */
93 void libcfs_run_debug_log_upcall(char *file)
94 {
95         char *argv[3];
96         int   rc;
97         char *envp[] = {
98                 "HOME=/",
99                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
100                 NULL};
101         ENTRY;
102
103         argv[0] = lnet_debug_log_upcall;
104
105         LASSERTF(file != NULL, "called on a null filename\n");
106         argv[1] = file; //only need to pass the path of the file
107
108         argv[2] = NULL;
109
110         rc = USERMODEHELPER(argv[0], argv, envp);
111         if (rc < 0 && rc != -ENOENT) {
112                 CERROR("Error %d invoking LNET debug log upcall %s %s; "
113                        "check /proc/sys/lnet/debug_log_upcall\n",
114                        rc, argv[0], argv[1]);
115         } else {
116                 CDEBUG(D_HA, "Invoked LNET debug log upcall %s %s\n",
117                        argv[0], argv[1]);
118         }
119
120         EXIT;
121 }
122
123 void libcfs_run_upcall(char **argv)
124 {
125         int   rc;
126         int   argc;
127         char *envp[] = {
128                 "HOME=/",
129                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
130                 NULL};
131         ENTRY;
132
133         argv[0] = lnet_upcall;
134         argc = 1;
135         while (argv[argc] != NULL)
136                 argc++;
137
138         LASSERT(argc >= 2);
139
140         rc = USERMODEHELPER(argv[0], argv, envp);
141         if (rc < 0 && rc != -ENOENT) {
142                 CERROR("Error %d invoking LNET upcall %s %s%s%s%s%s%s%s%s; "
143                        "check /proc/sys/lnet/upcall\n",
144                        rc, 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         } else {
150                 CDEBUG(D_HA, "Invoked LNET upcall %s %s%s%s%s%s%s%s%s\n",
151                        argv[0], argv[1],
152                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
153                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
154                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
155                        argc < 6 ? "" : ",...");
156         }
157 }
158
159 void libcfs_run_lbug_upcall(const char *file, const char *fn, const int line)
160 {
161         char *argv[6];
162         char buf[32];
163
164         ENTRY;
165         snprintf (buf, sizeof buf, "%d", line);
166
167         argv[1] = "LBUG";
168         argv[2] = (char *)file;
169         argv[3] = (char *)fn;
170         argv[4] = buf;
171         argv[5] = NULL;
172
173         libcfs_run_upcall (argv);
174 }
175
176 #ifdef __arch_um__
177 void lbug_with_loc(const char *file, const char *func, const int line)
178 {
179         libcfs_catastrophe = 1;
180         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
181                          "LBUG - trying to dump log to %s\n",
182                          libcfs_debug_file_path);
183         libcfs_debug_dumplog();
184         libcfs_run_lbug_upcall(file, func, line);
185         asm("int $3");
186         panic("LBUG");
187 }
188 #else
189 /* coverity[+kill] */
190 void lbug_with_loc(const char *file, const char *func, const int line)
191 {
192         libcfs_catastrophe = 1;
193         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
194
195         if (in_interrupt()) {
196                 panic("LBUG in interrupt.\n");
197                 /* not reached */
198         }
199
200         libcfs_debug_dumpstack(NULL);
201         if (!libcfs_panic_on_lbug)
202                 libcfs_debug_dumplog();
203         libcfs_run_lbug_upcall(file, func, line);
204         if (libcfs_panic_on_lbug)
205                 panic("LBUG");
206         set_task_state(current, TASK_UNINTERRUPTIBLE);
207         while (1)
208                 schedule();
209 }
210 #endif /* __arch_um__ */
211
212 #ifdef __KERNEL__
213
214 #ifdef HAVE_DUMP_TRACE
215 #include <linux/nmi.h>
216 #include <asm/stacktrace.h>
217
218 static void
219 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
220 {
221         printk("%s", (char *)data);
222         print_symbol(msg, symbol);
223         printk("\n");
224 }
225
226 static void print_trace_warning(void *data, char *msg)
227 {
228         printk("%s%s\n", (char *)data, msg);
229 }
230
231 static int print_trace_stack(void *data, char *name)
232 {
233         printk(" <%s> ", name);
234         return 0;
235 }
236
237 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
238 # define RELIABLE reliable
239 # define DUMP_TRACE_CONST const
240 static void print_trace_address(void *data, unsigned long addr, int reliable)
241 #else
242 /* before 2.6.24 there was no reliable arg */
243 # define RELIABLE 1
244 # define DUMP_TRACE_CONST
245 static void print_trace_address(void *data, unsigned long addr)
246 #endif
247 {
248         char fmt[32];
249         touch_nmi_watchdog();
250         sprintf(fmt, " [<%016lx>] %s%%s\n", addr, RELIABLE ? "": "? ");
251         __print_symbol(fmt, addr);
252 }
253
254 static DUMP_TRACE_CONST struct stacktrace_ops print_trace_ops = {
255         .warning = print_trace_warning,
256         .warning_symbol = print_trace_warning_symbol,
257         .stack = print_trace_stack,
258         .address = print_trace_address,
259 };
260 #endif
261
262 void libcfs_debug_dumpstack(struct task_struct *tsk)
263 {
264 #if defined(__arch_um__)
265         if (tsk != NULL)
266                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
267                       tsk->pid, UML_PID(tsk));
268         //asm("int $3");
269 #elif defined(HAVE_DUMP_TRACE)
270         /* dump_stack() */
271         /* show_trace() */
272         if (tsk == NULL)
273                 tsk = current;
274         printk("Pid: %d, comm: %.20s\n", tsk->pid, tsk->comm);
275         /* show_trace_log_lvl() */
276         printk("\nCall Trace:\n");
277         dump_trace(tsk, NULL, NULL,
278 #ifdef HAVE_TRACE_ADDRESS_RELIABLE
279                    0,
280 #endif /* HAVE_TRACE_ADDRESS_RELIABLE */
281                    &print_trace_ops, NULL);
282         printk("\n");
283 #elif defined(HAVE_SHOW_TASK)
284         /* this is exported by lustre kernel version 42 */
285         extern void show_task(struct task_struct *);
286
287         if (tsk == NULL)
288                 tsk = current;
289         CWARN("showing stack for process %d\n", tsk->pid);
290         show_task(tsk);
291 #else
292         if ((tsk == NULL) || (tsk == current))
293                 dump_stack();
294         else
295                 CWARN("can't show stack: kernel doesn't export show_task\n");
296 #endif
297 }
298
299 cfs_task_t *libcfs_current(void)
300 {
301         CWARN("current task struct is %p\n", current);
302         return current;
303 }
304
305 static int panic_notifier(struct notifier_block *self, unsigned long unused1,
306                          void *unused2)
307 {
308         if (libcfs_panic_in_progress)
309                 return 0;
310
311         libcfs_panic_in_progress = 1;
312         mb();
313
314 #ifdef LNET_DUMP_ON_PANIC
315         /* This is currently disabled because it spews far too much to the
316          * console on the rare cases it is ever triggered. */
317
318         if (in_interrupt()) {
319                 cfs_trace_debug_print();
320         } else {
321                 while (current->lock_depth >= 0)
322                         unlock_kernel();
323
324                 libcfs_debug_dumplog_internal((void *)(long)cfs_curproc_pid());
325         }
326 #endif
327         return 0;
328 }
329
330 static struct notifier_block libcfs_panic_notifier = {
331         notifier_call :     panic_notifier,
332         next :              NULL,
333         priority :          10000
334 };
335
336 void libcfs_register_panic_notifier(void)
337 {
338 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
339         atomic_notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
340 #else
341         notifier_chain_register(&panic_notifier_list, &libcfs_panic_notifier);
342 #endif
343 }
344
345 void libcfs_unregister_panic_notifier(void)
346 {
347 #ifdef HAVE_ATOMIC_PANIC_NOTIFIER
348         atomic_notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
349 #else
350         notifier_chain_unregister(&panic_notifier_list, &libcfs_panic_notifier);
351 #endif
352 }
353
354 EXPORT_SYMBOL(libcfs_debug_dumpstack);
355 EXPORT_SYMBOL(libcfs_current);
356
357 #endif /* __KERNEL__ */
358
359 EXPORT_SYMBOL(libcfs_run_upcall);
360 EXPORT_SYMBOL(libcfs_run_lbug_upcall);
361 EXPORT_SYMBOL(lbug_with_loc);