Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[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_QSWNAL | S_SOCKNAL |
63                                             S_GMNAL | S_OPENIBNAL);
64 EXPORT_SYMBOL(portal_subsystem_debug);
65
66 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
67                              D_RPCTRACE | D_VFSTRACE);
68 EXPORT_SYMBOL(portal_debug);
69
70 unsigned int portal_printk;
71 EXPORT_SYMBOL(portal_printk);
72
73 unsigned int portal_stack;
74 EXPORT_SYMBOL(portal_stack);
75
76 #ifdef __KERNEL__
77 atomic_t portal_kmemory = ATOMIC_INIT(0);
78 EXPORT_SYMBOL(portal_kmemory);
79 #endif
80
81 static DECLARE_WAIT_QUEUE_HEAD(debug_ctlwq);
82
83 char debug_file_path[1024] = "/tmp/lustre-log";
84 static char debug_file_name[1024];
85 static int handled_panic; /* to avoid recursive calls to notifiers */
86 char portals_upcall[1024] = "/usr/lib/lustre/portals_upcall";
87
88 int portals_do_debug_dumplog(void *arg)
89 {
90         void *journal_info;
91
92         kportal_daemonize("");
93
94         reparent_to_init();
95         journal_info = current->journal_info;
96         current->journal_info = NULL;
97
98         snprintf(debug_file_name, sizeof(debug_file_path) - 1,
99                  "%s.%ld.%ld", debug_file_path, CURRENT_SECONDS, (long)arg);
100         tracefile_dump_all_pages(debug_file_name);
101
102         current->journal_info = journal_info;
103         wake_up(&debug_ctlwq);
104         return 0;
105 }
106
107 void portals_debug_dumplog(void)
108 {
109         int rc;
110         DECLARE_WAITQUEUE(wait, current);
111         ENTRY;
112
113         /* we're being careful to ensure that the kernel thread is
114          * able to set our state to running as it exits before we
115          * get to schedule() */
116         set_current_state(TASK_INTERRUPTIBLE);
117         add_wait_queue(&debug_ctlwq, &wait);
118
119         rc = kernel_thread(portals_do_debug_dumplog, (void *)(long)current->pid,
120                            CLONE_VM | CLONE_FS | CLONE_FILES);
121         if (rc < 0)
122                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
123                        "%d\n", rc);
124         else
125                 schedule();
126
127         /* be sure to teardown if kernel_thread() failed */
128         remove_wait_queue(&debug_ctlwq, &wait);
129         set_current_state(TASK_RUNNING);
130 }
131
132 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
133                          void *unused2)
134 {
135         if (handled_panic)
136                 return 0;
137         else
138                 handled_panic = 1;
139
140         if (in_interrupt()) {
141                 trace_debug_print();
142                 return 0;
143         }
144
145         while (current->lock_depth >= 0)
146                 unlock_kernel();
147         portals_debug_dumplog();
148         return 0;
149 }
150
151 static struct notifier_block lustre_panic_notifier = {
152         notifier_call :     panic_dumplog,
153         next :              NULL,
154         priority :          10000
155 };
156
157 int portals_debug_init(unsigned long bufsize)
158 {
159         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
160         return tracefile_init();
161 }
162
163 int portals_debug_cleanup(void)
164 {
165         tracefile_exit();
166         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
167         return 0;
168 }
169
170 int portals_debug_clear_buffer(void)
171 {
172         trace_flush_pages();
173         return 0;
174 }
175
176 /* Debug markers, although printed by S_PORTALS
177  * should not be be marked as such. */
178 #undef DEBUG_SUBSYSTEM
179 #define DEBUG_SUBSYSTEM S_UNDEFINED
180 int portals_debug_mark_buffer(char *text)
181 {
182         CDEBUG(D_TRACE,"***************************************************\n");
183         CWARN("DEBUG MARKER: %s\n", text);
184         CDEBUG(D_TRACE,"***************************************************\n");
185
186         return 0;
187 }
188 #undef DEBUG_SUBSYSTEM
189 #define DEBUG_SUBSYSTEM S_PORTALS
190
191 void portals_debug_set_level(unsigned int debug_level)
192 {
193         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
194                debug_level);
195         portal_debug = debug_level;
196 }
197
198 void portals_run_upcall(char **argv)
199 {
200         int   rc;
201         int   argc;
202         char *envp[] = {
203                 "HOME=/",
204                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
205                 NULL};
206         ENTRY;
207
208         argv[0] = portals_upcall;
209         argc = 1;
210         while (argv[argc] != NULL)
211                 argc++;
212
213         LASSERT(argc >= 2);
214
215         rc = USERMODEHELPER(argv[0], argv, envp);
216         if (rc < 0) {
217                 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
218                        "check /proc/sys/portals/upcall\n",
219                        rc, argv[0], argv[1],
220                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
221                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
222                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
223                        argc < 6 ? "" : ",...");
224         } else {
225                 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
226                        argv[0], argv[1],
227                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
228                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
229                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
230                        argc < 6 ? "" : ",...");
231         }
232 }
233
234 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
235 {
236         char *argv[6];
237         char buf[32];
238
239         ENTRY;
240         snprintf (buf, sizeof buf, "%d", line);
241
242         argv[1] = "LBUG";
243         argv[2] = file;
244         argv[3] = (char *)fn;
245         argv[4] = buf;
246         argv[5] = NULL;
247
248         portals_run_upcall (argv);
249 }
250
251 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
252 {
253         if (nid == PTL_NID_ANY) {
254                 snprintf(str, PTL_NALFMT_SIZE - 1, "%s",
255                          "PTL_NID_ANY");
256                 return str;
257         }
258
259         switch(nal){
260 /* XXX this could be a nal method of some sort, 'cept it's config
261  * dependent whether (say) socknal NIDs are actually IP addresses... */
262 #ifndef CRAY_PORTALS 
263         case TCPNAL:
264                 /* userspace NAL */
265         case OPENIBNAL:
266         case SOCKNAL:
267                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u",
268                          (__u32)(nid >> 32), HIPQUAD(nid));
269                 break;
270         case QSWNAL:
271         case GMNAL:
272                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u",
273                          (__u32)(nid >> 32), (__u32)nid);
274                 break;
275 #endif
276         default:
277                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx",
278                          nal, (long long)nid);
279                 break;
280         }
281         return str;
282 }
283 /*      bug #4615       */
284 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
285 {
286         switch(nal){
287 #ifndef CRAY_PORTALS
288         case TCPNAL:
289                 /* userspace NAL */
290         case OPENIBNAL:
291         case SOCKNAL:
292                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u.%u.%u.%u,%u",
293                          (__u32)(id.nid >> 32), HIPQUAD((id.nid)) , id.pid);
294                 break;
295         case QSWNAL:
296         case GMNAL:
297                 snprintf(str, PTL_NALFMT_SIZE - 1, "%u:%u,%u",
298                          (__u32)(id.nid >> 32), (__u32)id.nid, id.pid);
299                 break;
300 #endif
301         default:
302                 snprintf(str, PTL_NALFMT_SIZE - 1, "?%d? %llx,%lx",
303                          nal, (long long)id.nid, (long)id.pid );
304                 break;
305         }
306         return str;
307 }
308
309
310 #ifdef __KERNEL__
311 char stack_backtrace[LUSTRE_TRACE_SIZE];
312 spinlock_t stack_backtrace_lock = SPIN_LOCK_UNLOCKED;
313
314 #if defined(__arch_um__)
315
316 char *portals_debug_dumpstack(void)
317 {
318         asm("int $3");
319         return "dump stack\n";
320 }
321
322 #elif defined(__i386__)
323
324 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
325 extern int lookup_symbol(unsigned long address, char *buf, int buflen);
326 const char *kallsyms_lookup(unsigned long addr,
327                             unsigned long *symbolsize,
328                             unsigned long *offset,
329                             char **modname, char *namebuf)
330 {
331         int rc = lookup_symbol(addr, namebuf, 128);
332         if (rc == -ENOSYS)
333                 return NULL;
334         return namebuf;
335 }
336 #endif
337
338 char *portals_debug_dumpstack(void)
339 {
340         unsigned long esp = current->thread.esp, addr;
341         unsigned long *stack = (unsigned long *)&esp;
342         char *buf = stack_backtrace, *pbuf = buf;
343         int size;
344
345         /* User space on another CPU? */
346         if ((esp ^ (unsigned long)current) & (PAGE_MASK << 1)){
347                 buf[0] = '\0';
348                 goto out;
349         }
350
351         size = sprintf(pbuf, " Call Trace: ");
352         pbuf += size;
353         while (((long) stack & (THREAD_SIZE - 1)) != 0) {
354                 addr = *stack++;
355                 if (kernel_text_address(addr)) {
356                         const char *sym_name;
357                         char *modname, buffer[128];
358                         unsigned long junk, offset;
359
360                         sym_name = kallsyms_lookup(addr, &junk, &offset,
361                                                    &modname, buffer);
362                         if (sym_name == NULL) {
363                                 if (buf + LUSTRE_TRACE_SIZE <= pbuf + 12)
364                                         break;
365                                 size = sprintf(pbuf, "[<%08lx>] ", addr);
366                         } else {
367                                 if (buf + LUSTRE_TRACE_SIZE
368                                             /* fix length + sizeof('\0') */
369                                     <= pbuf + strlen(buffer) + 28 + 1)
370                                         break;
371                                 size = sprintf(pbuf, "([<%08lx>] %s (0x%p)) ",
372                                                addr, buffer, stack - 1);
373                         }
374                         pbuf += size;
375                 }
376         }
377 out:
378         return buf;
379 }
380
381 #else /* !__arch_um__ && !__i386__ */
382
383 char *portals_debug_dumpstack(void)
384 {
385         char *buf = stack_backtrace;
386         buf[0] = '\0';
387         return buf;
388 }
389
390 #endif /* __arch_um__ */
391 struct task_struct *portals_current(void)
392 {
393         CWARN("current task struct is %p\n", current);
394         return current;
395 }
396
397 EXPORT_SYMBOL(stack_backtrace_lock);
398 EXPORT_SYMBOL(portals_debug_dumpstack);
399 EXPORT_SYMBOL(portals_current);
400 #endif /* __KERNEL__ */
401
402 EXPORT_SYMBOL(portals_debug_dumplog);
403 EXPORT_SYMBOL(portals_debug_set_level);
404 EXPORT_SYMBOL(portals_run_upcall);
405 EXPORT_SYMBOL(portals_run_lbug_upcall);
406 EXPORT_SYMBOL(portals_nid2str);
407 EXPORT_SYMBOL(portals_id2str);