Whamcloud - gitweb
8cbb117ec085a395829a1eb9c1def003d0dc21e1
[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 # define DEBUG_SUBSYSTEM S_PORTALS
28
29 #include <libcfs/kp30.h>
30 #include <libcfs/libcfs.h>
31
32 #include "tracefile.h"
33
34 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_NAL);
35 EXPORT_SYMBOL(portal_subsystem_debug);
36
37 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
38                              D_RPCTRACE | D_VFSTRACE);
39 EXPORT_SYMBOL(portal_debug);
40
41 unsigned int portal_printk;
42 EXPORT_SYMBOL(portal_printk);
43
44 unsigned int portal_stack;
45 EXPORT_SYMBOL(portal_stack);
46
47 #ifdef __KERNEL__
48 atomic_t portal_kmemory = ATOMIC_INIT(0);
49 EXPORT_SYMBOL(portal_kmemory);
50 #endif
51
52 static cfs_waitq_t debug_ctlwq;
53
54 char debug_file_path[1024] = "/tmp/lustre-log";
55 static char debug_file_name[1024];
56
57 void portals_debug_dumplog_internal(void *arg)
58 {
59         CFS_DECL_JOURNAL_DATA;
60
61         CFS_PUSH_JOURNAL;
62
63         snprintf(debug_file_name, sizeof(debug_file_path) - 1,
64                  "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(), (long)arg);
65         printk(KERN_ALERT "LustreError: dumping log to %s\n", debug_file_name);
66         tracefile_dump_all_pages(debug_file_name);
67
68         CFS_POP_JOURNAL;
69 }
70
71 int portals_debug_dumplog_thread(void *arg)
72 {
73         kportal_daemonize("");
74         reparent_to_init();
75         portals_debug_dumplog_internal(arg);
76         cfs_waitq_signal(&debug_ctlwq);
77         return 0;
78 }
79
80 void portals_debug_dumplog(void)
81 {
82         int            rc;
83         cfs_waitlink_t wait;
84         ENTRY;
85
86         /* we're being careful to ensure that the kernel thread is
87          * able to set our state to running as it exits before we
88          * get to schedule() */
89         cfs_waitlink_init(&wait);
90         set_current_state(TASK_INTERRUPTIBLE);
91         cfs_waitq_add(&debug_ctlwq, &wait);
92
93         rc = cfs_kernel_thread(portals_debug_dumplog_thread,
94                                (void *)(long)cfs_curproc_pid(),
95                                CLONE_VM | CLONE_FS | CLONE_FILES);
96         if (rc < 0)
97                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
98                        "%d\n", rc);
99         else
100                 schedule();
101
102         /* be sure to teardown if kernel_thread() failed */
103         cfs_waitq_del(&debug_ctlwq, &wait);
104         set_current_state(TASK_RUNNING);
105 }
106
107 #ifdef PORTALS_DUMP_ON_PANIC
108 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
109                          void *unused2)
110 {
111         static int handled_panic; /* to avoid recursive calls to notifiers */
112
113         if (handled_panic)
114                 return 0;
115         else
116                 handled_panic = 1;
117
118         if (in_interrupt()) {
119                 trace_debug_print();
120                 return 0;
121         }
122
123         while (current->lock_depth >= 0)
124                 unlock_kernel();
125         portals_debug_dumplog();
126         return 0;
127 }
128
129 static struct notifier_block lustre_panic_notifier = {
130         notifier_call :     panic_dumplog,
131         next :              NULL,
132         priority :          10000
133 };
134 #endif
135
136 #ifdef CRAY_PORTALS
137 extern void *lus_portals_debug;
138 #endif
139
140 int portals_debug_init(unsigned long bufsize)
141 {
142         cfs_waitq_init(&debug_ctlwq);
143 #ifdef CRAY_PORTALS
144         lus_portals_debug = &portals_debug_msg;
145 #endif
146 #ifdef PORTALS_DUMP_ON_PANIC
147         /* This is currently disabled because it spews far too much to the
148          * console on the rare cases it is ever triggered. */
149         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
150 #endif
151         return tracefile_init();
152 }
153
154 int portals_debug_cleanup(void)
155 {
156         tracefile_exit();
157 #ifdef PORTALS_DUMP_ON_PANIC
158         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
159 #endif
160 #ifdef CRAY_PORTALS
161         lus_portals_debug = NULL;
162 #endif
163         return 0;
164 }
165
166 int portals_debug_clear_buffer(void)
167 {
168         trace_flush_pages();
169         return 0;
170 }
171
172 /* Debug markers, although printed by S_PORTALS
173  * should not be be marked as such. */
174 #undef DEBUG_SUBSYSTEM
175 #define DEBUG_SUBSYSTEM S_UNDEFINED
176 int portals_debug_mark_buffer(char *text)
177 {
178         CDEBUG(D_TRACE,"***************************************************\n");
179         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
180         CDEBUG(D_TRACE,"***************************************************\n");
181
182         return 0;
183 }
184 #undef DEBUG_SUBSYSTEM
185 #define DEBUG_SUBSYSTEM S_PORTALS
186
187 void portals_debug_set_level(unsigned int debug_level)
188 {
189         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
190                debug_level);
191         portal_debug = debug_level;
192 }
193
194 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
195 {
196         if (nid == PTL_NID_ANY) {
197                 snprintf(str, PTL_NALFMT_SIZE, "%s", "PTL_NID_ANY");
198                 return str;
199         }
200
201         switch(nal){
202 /* XXX this could be a nal method of some sort, 'cept it's config
203  * dependent whether (say) socknal NIDs are actually IP addresses... */
204 #if !CRAY_PORTALS
205         case TCPNAL:
206                 /* userspace NAL */
207         case IIBNAL:
208         case VIBNAL:
209         case OPENIBNAL:
210         case RANAL:
211         case SOCKNAL:
212                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u.%u.%u.%u",
213                          (__u32)(nid >> 32), HIPQUAD(nid));
214                 break;
215         case QSWNAL:
216         case GMNAL:
217         case LONAL:
218                 snprintf(str, PTL_NALFMT_SIZE, "%u:%u",
219                          (__u32)(nid >> 32), (__u32)nid);
220                 break;
221 #endif
222         default:
223                 snprintf(str, PTL_NALFMT_SIZE, "?%x? %llx",
224                          nal, (long long)nid);
225                 break;
226         }
227         return str;
228 }
229
230 char *portals_id2str(int nal, ptl_process_id_t id, char *str)
231 {
232         int   len;
233
234         portals_nid2str(nal, id.nid, str);
235         len = strlen(str);
236         snprintf(str + len, PTL_NALFMT_SIZE - len, "-%u", id.pid);
237         return str;
238 }
239
240 EXPORT_SYMBOL(portals_debug_dumplog);
241 EXPORT_SYMBOL(portals_debug_set_level);
242 EXPORT_SYMBOL(portals_nid2str);
243 EXPORT_SYMBOL(portals_id2str);