Whamcloud - gitweb
Branch: b_cray
[fs/lustre-release.git] / lnet / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/notifier.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/interrupt.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <linux/completion.h>
42
43 #include <linux/fs.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <asm/segment.h>
47 #include <linux/miscdevice.h>
48 #include <linux/version.h>
49
50 # define DEBUG_SUBSYSTEM S_PORTALS
51
52 #include <linux/kp30.h>
53 #include <linux/portals_compat25.h>
54 #include <linux/libcfs.h>
55
56 #include "tracefile.h"
57
58 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
59 #include <linux/kallsyms.h>
60 #endif
61
62 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_NAL);
63 EXPORT_SYMBOL(portal_subsystem_debug);
64
65 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
66                              D_RPCTRACE | D_VFSTRACE);
67 EXPORT_SYMBOL(portal_debug);
68
69 unsigned int portal_printk;
70 EXPORT_SYMBOL(portal_printk);
71
72 unsigned int portal_stack;
73 EXPORT_SYMBOL(portal_stack);
74
75 #ifdef __KERNEL__
76 atomic_t portal_kmemory = ATOMIC_INIT(0);
77 EXPORT_SYMBOL(portal_kmemory);
78 #endif
79
80 static DECLARE_WAIT_QUEUE_HEAD(debug_ctlwq);
81
82 char debug_file_path[1024] = "/tmp/lustre-log";
83 static char debug_file_name[1024];
84 char portals_upcall[1024] = "/usr/lib/lustre/portals_upcall";
85
86 void portals_debug_dumplog_internal(void *arg)
87 {
88         void *journal_info = current->journal_info;
89         current->journal_info = NULL;
90
91         snprintf(debug_file_name, sizeof(debug_file_path) - 1,
92                  "%s.%ld.%ld", debug_file_path, CURRENT_SECONDS, (long)arg);
93         printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
94         tracefile_dump_all_pages(debug_file_name);
95
96         current->journal_info = journal_info;
97 }
98
99 int portals_debug_dumplog_thread(void *arg)
100 {
101         kportal_daemonize("");
102         reparent_to_init();
103         portals_debug_dumplog_internal(arg);
104         wake_up(&debug_ctlwq);
105         return 0;
106 }
107
108 void portals_debug_dumplog(void)
109 {
110         int rc;
111         DECLARE_WAITQUEUE(wait, current);
112         ENTRY;
113
114         /* we're being careful to ensure that the kernel thread is
115          * able to set our state to running as it exits before we
116          * get to schedule() */
117         set_current_state(TASK_INTERRUPTIBLE);
118         add_wait_queue(&debug_ctlwq, &wait);
119
120         rc = kernel_thread(portals_debug_dumplog_thread,
121                            (void *)(long)current->pid,
122                            CLONE_VM | CLONE_FS | CLONE_FILES);
123         if (rc < 0)
124                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
125                        "%d\n", rc);
126         else
127                 schedule();
128
129         /* be sure to teardown if kernel_thread() failed */
130         remove_wait_queue(&debug_ctlwq, &wait);
131         set_current_state(TASK_RUNNING);
132 }
133
134 #ifdef PORTALS_DUMP_ON_PANIC
135 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
136                          void *unused2)
137 {
138         static int handled_panic; /* to avoid recursive calls to notifiers */
139
140         if (handled_panic)
141                 return 0;
142         else
143                 handled_panic = 1;
144
145         if (in_interrupt()) {
146                 trace_debug_print();
147                 return 0;
148         }
149
150         while (current->lock_depth >= 0)
151                 unlock_kernel();
152         portals_debug_dumplog();
153         return 0;
154 }
155
156 static struct notifier_block lustre_panic_notifier = {
157         notifier_call :     panic_dumplog,
158         next :              NULL,
159         priority :          10000
160 };
161 #endif
162
163 #ifdef CRAY_PORTALS
164 extern void *lus_portals_debug;
165 #endif
166
167 int portals_debug_init(unsigned long bufsize)
168 {
169 #ifdef CRAY_PORTALS
170         lus_portals_debug = &portals_debug_msg;
171 #endif
172 #ifdef PORTALS_DUMP_ON_PANIC
173         /* This is currently disabled because it spews far too much to the
174          * console on the rare cases it is ever triggered. */
175         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
176 #endif
177         return tracefile_init();
178 }
179
180 int portals_debug_cleanup(void)
181 {
182         tracefile_exit();
183 #ifdef PORTALS_DUMP_ON_PANIC
184         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
185 #endif
186 #ifdef CRAY_PORTALS
187         lus_portals_debug = NULL;
188 #endif
189         return 0;
190 }
191
192 int portals_debug_clear_buffer(void)
193 {
194         trace_flush_pages();
195         return 0;
196 }
197
198 /* Debug markers, although printed by S_PORTALS
199  * should not be be marked as such. */
200 #undef DEBUG_SUBSYSTEM
201 #define DEBUG_SUBSYSTEM S_UNDEFINED
202 int portals_debug_mark_buffer(char *text)
203 {
204         CDEBUG(D_TRACE,"***************************************************\n");
205         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
206         CDEBUG(D_TRACE,"***************************************************\n");
207
208         return 0;
209 }
210 #undef DEBUG_SUBSYSTEM
211 #define DEBUG_SUBSYSTEM S_PORTALS
212
213 void portals_debug_set_level(unsigned int debug_level)
214 {
215         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
216                debug_level);
217         portal_debug = debug_level;
218 }
219
220 void portals_run_upcall(char **argv)
221 {
222         int   rc;
223         int   argc;
224         char *envp[] = {
225                 "HOME=/",
226                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
227                 NULL};
228         ENTRY;
229
230         argv[0] = portals_upcall;
231         argc = 1;
232         while (argv[argc] != NULL)
233                 argc++;
234
235         LASSERT(argc >= 2);
236
237         rc = USERMODEHELPER(argv[0], argv, envp);
238         if (rc < 0) {
239                 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
240                        "check /proc/sys/portals/upcall\n",
241                        rc, argv[0], argv[1],
242                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
243                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
244                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
245                        argc < 6 ? "" : ",...");
246         } else {
247                 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
248                        argv[0], argv[1],
249                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
250                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
251                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
252                        argc < 6 ? "" : ",...");
253         }
254 }
255
256 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
257 {
258         char *argv[6];
259         char buf[32];
260
261         ENTRY;
262         snprintf (buf, sizeof buf, "%d", line);
263
264         argv[1] = "LBUG";
265         argv[2] = file;
266         argv[3] = (char *)fn;
267         argv[4] = buf;
268         argv[5] = NULL;
269
270         portals_run_upcall (argv);
271 }
272
273 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
274 {
275         if (nid == PTL_NID_ANY) {
276                 snprintf(str, PTL_NALFMT_SIZE, "%s", "PTL_NID_ANY");
277                 return str;
278         }
279
280         switch(nal){
281 /* XXX this could be a nal method of some sort, 'cept it's config
282  * dependent whether (say) socknal NIDs are actually IP addresses... */
283 #if !CRAY_PORTALS 
284         case TCPNAL:
285                 /* userspace NAL */
286         case IIBNAL:
287         case VIBNAL:
288         case OPENIBNAL:
289         case RANAL:
290         case SOCKNAL:
291                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u.%u.%u.%u",
292                          (__u32)(nid >> 32), HIPQUAD(nid));
293                 break;
294         case QSWNAL:
295         case GMNAL:
296         case LONAL:
297                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u",
298                          (__u32)(nid >> 32), (__u32)nid);
299                 break;
300 #endif
301         default:
302                 snprintf(str, PTL_NALFMT_SIZE, "?%x? %llx",
303                          nal, (long long)nid);
304                 break;
305         }
306         return str;
307 }
308
309 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
310 {
311         int   len;
312         
313         portals_nid2str(nal, id.nid, str);
314         len = strlen(str);
315         snprintf(str + len, PTL_NALFMT_SIZE - len, "-%u", id.pid);
316         return str;
317 }
318
319 #ifdef __KERNEL__
320
321 void portals_debug_dumpstack(struct task_struct *tsk)
322 {
323 #if defined(__arch_um__)
324         if (tsk != NULL)
325                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
326                       tsk->pid, UML_PID(tsk));
327         asm("int $3");
328 #elif defined(HAVE_SHOW_TASK)
329         /* this is exported by lustre kernel version 42 */
330         extern void show_task(struct task_struct *);
331
332         if (tsk == NULL)
333                 tsk = current;
334         CWARN("showing stack for process %d\n", tsk->pid);
335         show_task(tsk);
336 #else
337         CWARN("can't show stack: kernel doesn't export show_task\n");
338 #endif
339 }
340
341 struct task_struct *portals_current(void)
342 {
343         CWARN("current task struct is %p\n", current);
344         return current;
345 }
346
347 EXPORT_SYMBOL(portals_debug_dumpstack);
348 EXPORT_SYMBOL(portals_current);
349 #endif /* __KERNEL__ */
350
351 EXPORT_SYMBOL(portals_debug_dumplog);
352 EXPORT_SYMBOL(portals_debug_set_level);
353 EXPORT_SYMBOL(portals_run_upcall);
354 EXPORT_SYMBOL(portals_run_lbug_upcall);
355 EXPORT_SYMBOL(portals_nid2str);
356 EXPORT_SYMBOL(portals_id2str);