Whamcloud - gitweb
53f15d20208dc031a24380c41957e921c807594e
[fs/lustre-release.git] / lustre / portals / 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_IBNAL);
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         printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
101         tracefile_dump_all_pages(debug_file_name);
102
103         current->journal_info = journal_info;
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_do_debug_dumplog, (void *)(long)current->pid,
121                            CLONE_VM | CLONE_FS | CLONE_FILES);
122         if (rc < 0)
123                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
124                        "%d\n", rc);
125         else
126                 schedule();
127
128         /* be sure to teardown if kernel_thread() failed */
129         remove_wait_queue(&debug_ctlwq, &wait);
130         set_current_state(TASK_RUNNING);
131 }
132
133 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
134                          void *unused2)
135 {
136         if (handled_panic)
137                 return 0;
138         else
139                 handled_panic = 1;
140
141         if (in_interrupt()) {
142                 trace_debug_print();
143                 return 0;
144         }
145
146         while (current->lock_depth >= 0)
147                 unlock_kernel();
148         portals_debug_dumplog();
149         return 0;
150 }
151
152 static struct notifier_block lustre_panic_notifier = {
153         notifier_call :     panic_dumplog,
154         next :              NULL,
155         priority :          10000
156 };
157
158 int portals_debug_init(unsigned long bufsize)
159 {
160         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
161         return tracefile_init();
162 }
163
164 int portals_debug_cleanup(void)
165 {
166         tracefile_exit();
167         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
168         return 0;
169 }
170
171 int portals_debug_clear_buffer(void)
172 {
173         trace_flush_pages();
174         return 0;
175 }
176
177 /* Debug markers, although printed by S_PORTALS
178  * should not be be marked as such. */
179 #undef DEBUG_SUBSYSTEM
180 #define DEBUG_SUBSYSTEM S_UNDEFINED
181 int portals_debug_mark_buffer(char *text)
182 {
183         CDEBUG(D_TRACE,"***************************************************\n");
184         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
185         CDEBUG(D_TRACE,"***************************************************\n");
186
187         return 0;
188 }
189 #undef DEBUG_SUBSYSTEM
190 #define DEBUG_SUBSYSTEM S_PORTALS
191
192 void portals_debug_set_level(unsigned int debug_level)
193 {
194         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
195                debug_level);
196         portal_debug = debug_level;
197 }
198
199 void portals_run_upcall(char **argv)
200 {
201         int   rc;
202         int   argc;
203         char *envp[] = {
204                 "HOME=/",
205                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
206                 NULL};
207         ENTRY;
208
209         argv[0] = portals_upcall;
210         argc = 1;
211         while (argv[argc] != NULL)
212                 argc++;
213
214         LASSERT(argc >= 2);
215
216         rc = USERMODEHELPER(argv[0], argv, envp);
217         if (rc < 0) {
218                 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
219                        "check /proc/sys/portals/upcall\n",
220                        rc, argv[0], argv[1],
221                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
222                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
223                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
224                        argc < 6 ? "" : ",...");
225         } else {
226                 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
227                        argv[0], argv[1],
228                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
229                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
230                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
231                        argc < 6 ? "" : ",...");
232         }
233 }
234
235 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
236 {
237         char *argv[6];
238         char buf[32];
239
240         ENTRY;
241         snprintf (buf, sizeof buf, "%d", line);
242
243         argv[1] = "LBUG";
244         argv[2] = file;
245         argv[3] = (char *)fn;
246         argv[4] = buf;
247         argv[5] = NULL;
248
249         portals_run_upcall (argv);
250 }
251
252 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
253 {
254         if (nid == PTL_NID_ANY) {
255                 snprintf(str, PTL_NALFMT_SIZE, "%s", "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 #if !CRAY_PORTALS 
263         case TCPNAL:
264                 /* userspace NAL */
265         case IIBNAL:
266         case OPENIBNAL:
267         case SOCKNAL:
268                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u.%u.%u.%u",
269                          (__u32)(nid >> 32), HIPQUAD(nid));
270                 break;
271         case QSWNAL:
272         case GMNAL:
273                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u",
274                          (__u32)(nid >> 32), (__u32)nid);
275                 break;
276 #endif
277         default:
278                 snprintf(str, PTL_NALFMT_SIZE, "?%x? %llx",
279                          nal, (long long)nid);
280                 break;
281         }
282         return str;
283 }
284
285 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
286 {
287         int   len;
288         
289         portals_nid2str(nal, id.nid, str);
290         len = strlen(str);
291         snprintf(str + len, PTL_NALFMT_SIZE - len, ",%u", id.pid);
292         return str;
293 }
294
295 #ifdef __KERNEL__
296 char stack_backtrace[LUSTRE_TRACE_SIZE];
297 spinlock_t stack_backtrace_lock = SPIN_LOCK_UNLOCKED;
298
299 #if defined(__arch_um__)
300
301 char *portals_debug_dumpstack(void)
302 {
303         asm("int $3");
304         return "dump stack\n";
305 }
306
307 #elif defined(__i386__)
308
309 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
310 extern int lookup_symbol(unsigned long address, char *buf, int buflen);
311 const char *kallsyms_lookup(unsigned long addr,
312                             unsigned long *symbolsize,
313                             unsigned long *offset,
314                             char **modname, char *namebuf)
315 {
316         int rc = lookup_symbol(addr, namebuf, 128);
317         if (rc == -ENOSYS)
318                 return NULL;
319         return namebuf;
320 }
321 #endif
322
323 char *portals_debug_dumpstack(void)
324 {
325         unsigned long esp = current->thread.esp, addr;
326         unsigned long *stack = (unsigned long *)&esp;
327         char *buf = stack_backtrace, *pbuf = buf;
328         int size;
329
330         /* User space on another CPU? */
331         if ((esp ^ (unsigned long)current) & (PAGE_MASK << 1)){
332                 buf[0] = '\0';
333                 goto out;
334         }
335
336         size = sprintf(pbuf, " Call Trace: ");
337         pbuf += size;
338         while (((long) stack & (THREAD_SIZE - 1)) != 0) {
339                 addr = *stack++;
340                 if (kernel_text_address(addr)) {
341                         const char *sym_name;
342                         char *modname, buffer[128];
343                         unsigned long junk, offset;
344
345                         sym_name = kallsyms_lookup(addr, &junk, &offset,
346                                                    &modname, buffer);
347                         if (sym_name == NULL) {
348                                 if (buf + LUSTRE_TRACE_SIZE <= pbuf + 12)
349                                         break;
350                                 size = sprintf(pbuf, "[<%08lx>] ", addr);
351                         } else {
352                                 if (buf + LUSTRE_TRACE_SIZE
353                                             /* fix length + sizeof('\0') */
354                                     <= pbuf + strlen(buffer) + 28 + 1)
355                                         break;
356                                 size = sprintf(pbuf, "([<%08lx>] %s (0x%p)) ",
357                                                addr, buffer, stack - 1);
358                         }
359                         pbuf += size;
360                 }
361         }
362 out:
363         return buf;
364 }
365
366 #else /* !__arch_um__ && !__i386__ */
367
368 char *portals_debug_dumpstack(void)
369 {
370         char *buf = stack_backtrace;
371         buf[0] = '\0';
372         return buf;
373 }
374
375 #endif /* __arch_um__ */
376 struct task_struct *portals_current(void)
377 {
378         CWARN("current task struct is %p\n", current);
379         return current;
380 }
381
382 EXPORT_SYMBOL(stack_backtrace_lock);
383 EXPORT_SYMBOL(portals_debug_dumpstack);
384 EXPORT_SYMBOL(portals_current);
385 #endif /* __KERNEL__ */
386
387 EXPORT_SYMBOL(portals_debug_dumplog);
388 EXPORT_SYMBOL(portals_debug_set_level);
389 EXPORT_SYMBOL(portals_run_upcall);
390 EXPORT_SYMBOL(portals_run_lbug_upcall);
391 EXPORT_SYMBOL(portals_nid2str);
392 EXPORT_SYMBOL(portals_id2str);