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