Whamcloud - gitweb
cd77438f0ef15fbec9ad8711669737d1246bf285
[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_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 void portals_debug_dumplog_internal(void *arg)
89 {
90         void *journal_info = current->journal_info;
91         current->journal_info = NULL;
92
93         snprintf(debug_file_name, sizeof(debug_file_path) - 1,
94                  "%s.%ld.%ld", debug_file_path, CURRENT_SECONDS, (long)arg);
95         printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
96         tracefile_dump_all_pages(debug_file_name);
97
98         current->journal_info = journal_info;
99 }
100
101 int portals_debug_dumplog_thread(void *arg)
102 {
103         kportal_daemonize("");
104         reparent_to_init();
105         portals_debug_dumplog_internal(arg);
106         wake_up(&debug_ctlwq);
107         return 0;
108 }
109
110 void portals_debug_dumplog(void)
111 {
112         int rc;
113         DECLARE_WAITQUEUE(wait, current);
114         ENTRY;
115
116         /* we're being careful to ensure that the kernel thread is
117          * able to set our state to running as it exits before we
118          * get to schedule() */
119         set_current_state(TASK_INTERRUPTIBLE);
120         add_wait_queue(&debug_ctlwq, &wait);
121
122         rc = kernel_thread(portals_debug_dumplog_thread,
123                            (void *)(long)current->pid,
124                            CLONE_VM | CLONE_FS | CLONE_FILES);
125         if (rc < 0)
126                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
127                        "%d\n", rc);
128         else
129                 schedule();
130
131         /* be sure to teardown if kernel_thread() failed */
132         remove_wait_queue(&debug_ctlwq, &wait);
133         set_current_state(TASK_RUNNING);
134 }
135
136 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
137                          void *unused2)
138 {
139         if (handled_panic)
140                 return 0;
141         else
142                 handled_panic = 1;
143
144         if (in_interrupt()) {
145                 trace_debug_print();
146                 return 0;
147         }
148
149         while (current->lock_depth >= 0)
150                 unlock_kernel();
151         portals_debug_dumplog();
152         return 0;
153 }
154
155 static struct notifier_block lustre_panic_notifier = {
156         notifier_call :     panic_dumplog,
157         next :              NULL,
158         priority :          10000
159 };
160
161 int portals_debug_init(unsigned long bufsize)
162 {
163         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
164         return tracefile_init();
165 }
166
167 int portals_debug_cleanup(void)
168 {
169         tracefile_exit();
170         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
171         return 0;
172 }
173
174 int portals_debug_clear_buffer(void)
175 {
176         trace_flush_pages();
177         return 0;
178 }
179
180 /* Debug markers, although printed by S_PORTALS
181  * should not be be marked as such. */
182 #undef DEBUG_SUBSYSTEM
183 #define DEBUG_SUBSYSTEM S_UNDEFINED
184 int portals_debug_mark_buffer(char *text)
185 {
186         CDEBUG(D_TRACE,"***************************************************\n");
187         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
188         CDEBUG(D_TRACE,"***************************************************\n");
189
190         return 0;
191 }
192 #undef DEBUG_SUBSYSTEM
193 #define DEBUG_SUBSYSTEM S_PORTALS
194
195 void portals_debug_set_level(unsigned int debug_level)
196 {
197         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
198                debug_level);
199         portal_debug = debug_level;
200 }
201
202 void portals_run_upcall(char **argv)
203 {
204         int   rc;
205         int   argc;
206         char *envp[] = {
207                 "HOME=/",
208                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
209                 NULL};
210         ENTRY;
211
212         argv[0] = portals_upcall;
213         argc = 1;
214         while (argv[argc] != NULL)
215                 argc++;
216
217         LASSERT(argc >= 2);
218
219         rc = USERMODEHELPER(argv[0], argv, envp);
220         if (rc < 0) {
221                 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
222                        "check /proc/sys/portals/upcall\n",
223                        rc, argv[0], argv[1],
224                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
225                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
226                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
227                        argc < 6 ? "" : ",...");
228         } else {
229                 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
230                        argv[0], argv[1],
231                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
232                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
233                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
234                        argc < 6 ? "" : ",...");
235         }
236 }
237
238 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
239 {
240         char *argv[6];
241         char buf[32];
242
243         ENTRY;
244         snprintf (buf, sizeof buf, "%d", line);
245
246         argv[1] = "LBUG";
247         argv[2] = file;
248         argv[3] = (char *)fn;
249         argv[4] = buf;
250         argv[5] = NULL;
251
252         portals_run_upcall (argv);
253 }
254
255 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
256 {
257         if (nid == PTL_NID_ANY) {
258                 snprintf(str, PTL_NALFMT_SIZE, "%s", "PTL_NID_ANY");
259                 return str;
260         }
261
262         switch(nal){
263 /* XXX this could be a nal method of some sort, 'cept it's config
264  * dependent whether (say) socknal NIDs are actually IP addresses... */
265 #if !CRAY_PORTALS 
266         case TCPNAL:
267                 /* userspace NAL */
268         case IIBNAL:
269         case OPENIBNAL:
270         case SOCKNAL:
271                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u.%u.%u.%u",
272                          (__u32)(nid >> 32), HIPQUAD(nid));
273                 break;
274         case QSWNAL:
275         case GMNAL:
276                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u",
277                          (__u32)(nid >> 32), (__u32)nid);
278                 break;
279 #endif
280         default:
281                 snprintf(str, PTL_NALFMT_SIZE, "?%x? %llx",
282                          nal, (long long)nid);
283                 break;
284         }
285         return str;
286 }
287
288 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
289 {
290         int   len;
291         
292         portals_nid2str(nal, id.nid, str);
293         len = strlen(str);
294         snprintf(str + len, PTL_NALFMT_SIZE - len, "-%u", id.pid);
295         return str;
296 }
297
298 #ifdef __KERNEL__
299
300 void portals_debug_dumpstack(struct task_struct *tsk)
301 {
302 #if defined(__arch_um__)
303         if (tsk != NULL)
304                 CWARN("stack dump for pid %d (%d) requested; wake up gdb.\n",
305                       tsk->pid, UML_PID(tsk));
306         asm("int $3");
307 #elif defined(HAVE_SHOW_TASK)
308         /* this is exported by lustre kernel version 42 */
309         extern void show_task(struct task_struct *);
310
311         if (tsk == NULL)
312                 tsk = current;
313         CWARN("showing stack for process %d\n", tsk->pid);
314         show_task(tsk);
315 #else
316         CWARN("can't show stack: kernel doesn't export show_task\n");
317 #endif
318 }
319
320 struct task_struct *portals_current(void)
321 {
322         CWARN("current task struct is %p\n", current);
323         return current;
324 }
325
326 EXPORT_SYMBOL(portals_debug_dumpstack);
327 EXPORT_SYMBOL(portals_current);
328 #endif /* __KERNEL__ */
329
330 EXPORT_SYMBOL(portals_debug_dumplog);
331 EXPORT_SYMBOL(portals_debug_set_level);
332 EXPORT_SYMBOL(portals_run_upcall);
333 EXPORT_SYMBOL(portals_run_lbug_upcall);
334 EXPORT_SYMBOL(portals_nid2str);
335 EXPORT_SYMBOL(portals_id2str);