Whamcloud - gitweb
b=20592
[fs/lustre-release.git] / libcfs / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 # define DEBUG_SUBSYSTEM S_LNET
46
47 #include <libcfs/libcfs.h>
48 #include "tracefile.h"
49
50 static char debug_file_name[1024];
51
52 unsigned int libcfs_subsystem_debug = ~0;
53 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
54                 "Lustre kernel debug subsystem mask");
55 EXPORT_SYMBOL(libcfs_subsystem_debug);
56
57 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
58                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
59 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
60                 "Lustre kernel debug mask");
61 EXPORT_SYMBOL(libcfs_debug);
62
63 int libcfs_debug_mb = -1;
64 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
65                 "Total debug buffer size.");
66 EXPORT_SYMBOL(libcfs_debug_mb);
67
68 unsigned int libcfs_printk = (D_CANTMASK | D_NETERROR);
69 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
70                 "Lustre kernel debug console mask");
71 EXPORT_SYMBOL(libcfs_printk);
72
73 unsigned int libcfs_console_ratelimit = 1;
74 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
75                 "Lustre kernel debug console ratelimit (0 to disable)");
76 EXPORT_SYMBOL(libcfs_console_ratelimit);
77
78 cfs_duration_t libcfs_console_max_delay;
79 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
80                 "Lustre kernel debug console max delay (jiffies)");
81 EXPORT_SYMBOL(libcfs_console_max_delay);
82
83 cfs_duration_t libcfs_console_min_delay;
84 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
85                 "Lustre kernel debug console min delay (jiffies)");
86 EXPORT_SYMBOL(libcfs_console_min_delay);
87
88 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
89 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
90                 "Lustre kernel debug console backoff factor");
91 EXPORT_SYMBOL(libcfs_console_backoff);
92
93 unsigned int libcfs_debug_binary = 1;
94 EXPORT_SYMBOL(libcfs_debug_binary);
95
96 unsigned int libcfs_stack;
97 EXPORT_SYMBOL(libcfs_stack);
98
99 unsigned int portal_enter_debugger;
100 EXPORT_SYMBOL(portal_enter_debugger);
101
102 unsigned int libcfs_catastrophe;
103 EXPORT_SYMBOL(libcfs_catastrophe);
104
105 unsigned int libcfs_watchdog_ratelimit = 300;
106 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
107
108 unsigned int libcfs_panic_on_lbug = 1;
109 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
110                 "Lustre kernel panic on LBUG");
111 EXPORT_SYMBOL(libcfs_panic_on_lbug);
112
113 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
114 EXPORT_SYMBOL(libcfs_kmemory);
115
116 static cfs_waitq_t debug_ctlwq;
117
118 char debug_file_path_arr[1024] = DEBUG_FILE_PATH_DEFAULT;
119
120 /* We need to pass a pointer here, but elsewhere this must be a const */
121 char *debug_file_path = &debug_file_path_arr[0];
122 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
123                 "Path for dumping debug logs, "
124                 "set 'NONE' to prevent log dumping");
125
126 int libcfs_panic_in_progress;
127
128 /* libcfs_debug_token2mask() expects the returned
129  * string in lower-case */
130 const char *
131 libcfs_debug_subsys2str(int subsys)
132 {
133         switch (1 << subsys) {
134         default:
135                 return NULL;
136         case S_UNDEFINED:
137                 return "undefined";
138         case S_MDC:
139                 return "mdc";
140         case S_MDS:
141                 return "mds";
142         case S_OSC:
143                 return "osc";
144         case S_OST:
145                 return "ost";
146         case S_CLASS:
147                 return "class";
148         case S_LOG:
149                 return "log";
150         case S_LLITE:
151                 return "llite";
152         case S_RPC:
153                 return "rpc";
154         case S_LNET:
155                 return "lnet";
156         case S_LND:
157                 return "lnd";
158         case S_PINGER:
159                 return "pinger";
160         case S_FILTER:
161                 return "filter";
162         case S_ECHO:
163                 return "echo";
164         case S_LDLM:
165                 return "ldlm";
166         case S_LOV:
167                 return "lov";
168         case S_LQUOTA:
169                 return "lquota";
170         case S_LMV:
171                 return "lmv";
172         case S_SEC:
173                 return "sec";
174         case S_GSS:
175                 return "gss";
176         case S_MGC:
177                 return "mgc";
178         case S_MGS:
179                 return "mgs";
180         case S_FID:
181                 return "fid";
182         case S_FLD:
183                 return "fld";
184         }
185 }
186
187 /* libcfs_debug_token2mask() expects the returned
188  * string in lower-case */
189 const char *
190 libcfs_debug_dbg2str(int debug)
191 {
192         switch (1 << debug) {
193         default:
194                 return NULL;
195         case D_TRACE:
196                 return "trace";
197         case D_INODE:
198                 return "inode";
199         case D_SUPER:
200                 return "super";
201         case D_EXT2:
202                 return "ext2";
203         case D_MALLOC:
204                 return "malloc";
205         case D_CACHE:
206                 return "cache";
207         case D_INFO:
208                 return "info";
209         case D_IOCTL:
210                 return "ioctl";
211         case D_NETERROR:
212                 return "neterror";
213         case D_NET:
214                 return "net";
215         case D_WARNING:
216                 return "warning";
217         case D_BUFFS:
218                 return "buffs";
219         case D_OTHER:
220                 return "other";
221         case D_DENTRY:
222                 return "dentry";
223         case D_NETTRACE:
224                 return "nettrace";
225         case D_PAGE:
226                 return "page";
227         case D_DLMTRACE:
228                 return "dlmtrace";
229         case D_ERROR:
230                 return "error";
231         case D_EMERG:
232                 return "emerg";
233         case D_HA:
234                 return "ha";
235         case D_RPCTRACE:
236                 return "rpctrace";
237         case D_VFSTRACE:
238                 return "vfstrace";
239         case D_READA:
240                 return "reada";
241         case D_MMAP:
242                 return "mmap";
243         case D_CONFIG:
244                 return "config";
245         case D_CONSOLE:
246                 return "console";
247         case D_QUOTA:
248                 return "quota";
249         case D_SEC:
250                 return "sec";
251         }
252 }
253
254 int
255 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
256 {
257         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
258                                                  libcfs_debug_dbg2str;
259         int           len = 0;
260         const char   *token;
261         int           i;
262
263         if (mask == 0) {                        /* "0" */
264                 if (size > 0)
265                         str[0] = '0';
266                 len = 1;
267         } else {                                /* space-separated tokens */
268                 for (i = 0; i < 32; i++) {
269                         if ((mask & (1 << i)) == 0)
270                                 continue;
271
272                         token = fn(i);
273                         if (token == NULL)              /* unused bit */
274                                 continue;
275
276                         if (len > 0) {                  /* separator? */
277                                 if (len < size)
278                                         str[len] = ' ';
279                                 len++;
280                         }
281
282                         while (*token != 0) {
283                                 if (len < size)
284                                         str[len] = *token;
285                                 token++;
286                                 len++;
287                         }
288                 }
289         }
290
291         /* terminate 'str' */
292         if (len < size)
293                 str[len] = 0;
294         else
295                 str[size - 1] = 0;
296
297         return len;
298 }
299
300 int
301 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
302 {
303         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
304                                                  libcfs_debug_dbg2str;
305         int         m = 0;
306         int         matched;
307         int         n;
308         int         t;
309
310         /* Allow a number for backwards compatibility */
311
312         for (n = strlen(str); n > 0; n--)
313                 if (!isspace(str[n-1]))
314                         break;
315         matched = n;
316
317         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
318             matched == n) {
319                 *mask = m;
320                 return 0;
321         }
322
323         return libcfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
324                                0xffffffff);
325 }
326
327 /**
328  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
329  */
330 void libcfs_debug_dumplog_internal(void *arg)
331 {
332         CFS_DECL_JOURNAL_DATA;
333
334         CFS_PUSH_JOURNAL;
335
336         if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
337                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
338                          "%s.%ld." LPLD, debug_file_path_arr,
339                          cfs_time_current_sec(), (long_ptr_t)arg);
340                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
341                        debug_file_name);
342                 tracefile_dump_all_pages(debug_file_name);
343                 libcfs_run_debug_log_upcall(debug_file_name);
344         }
345         CFS_POP_JOURNAL;
346 }
347
348 int libcfs_debug_dumplog_thread(void *arg)
349 {
350         libcfs_debug_dumplog_internal(arg);
351         cfs_waitq_signal(&debug_ctlwq);
352         return 0;
353 }
354
355 void libcfs_debug_dumplog(void)
356 {
357         cfs_waitlink_t wait;
358         cfs_task_t    *dumper;
359         ENTRY;
360
361         /* we're being careful to ensure that the kernel thread is
362          * able to set our state to running as it exits before we
363          * get to schedule() */
364         cfs_waitlink_init(&wait);
365         set_current_state(TASK_INTERRUPTIBLE);
366         cfs_waitq_add(&debug_ctlwq, &wait);
367
368         dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
369                                  (void*)(long)cfs_curproc_pid(),
370                                  "libcfs_debug_dumper");
371         if (IS_ERR(dumper))
372                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
373                        "%ld\n", PTR_ERR(dumper));
374         else
375                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
376
377         /* be sure to teardown if kernel_thread() failed */
378         cfs_waitq_del(&debug_ctlwq, &wait);
379         set_current_state(TASK_RUNNING);
380 }
381
382 int libcfs_debug_init(unsigned long bufsize)
383 {
384         int    rc = 0;
385         int    max = libcfs_debug_mb;
386
387         cfs_waitq_init(&debug_ctlwq);
388
389         if (libcfs_console_max_delay <= 0 || /* not set by user or */
390             libcfs_console_min_delay <= 0 || /* set to invalid values */
391             libcfs_console_min_delay >= libcfs_console_max_delay) {
392                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
393                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
394         }
395
396         /* If libcfs_debug_mb is set to an invalid value or uninitialized
397          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
398         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
399                 max = TCD_MAX_PAGES;
400         } else {
401                 max = (max / num_possible_cpus());
402                 max = (max << (20 - CFS_PAGE_SHIFT));
403         }
404         rc = tracefile_init(max);
405
406         if (rc == 0)
407                 libcfs_register_panic_notifier();
408
409         return rc;
410 }
411
412 int libcfs_debug_cleanup(void)
413 {
414         libcfs_unregister_panic_notifier();
415         tracefile_exit();
416         return 0;
417 }
418
419 int libcfs_debug_clear_buffer(void)
420 {
421         trace_flush_pages();
422         return 0;
423 }
424
425 /* Debug markers, although printed by S_LNET
426  * should not be be marked as such. */
427 #undef DEBUG_SUBSYSTEM
428 #define DEBUG_SUBSYSTEM S_UNDEFINED
429 int libcfs_debug_mark_buffer(const char *text)
430 {
431         CDEBUG(D_TRACE,"***************************************************\n");
432         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
433         CDEBUG(D_TRACE,"***************************************************\n");
434
435         return 0;
436 }
437 #undef DEBUG_SUBSYSTEM
438 #define DEBUG_SUBSYSTEM S_LNET
439
440 void libcfs_debug_set_level(unsigned int debug_level)
441 {
442         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
443                debug_level);
444         libcfs_debug = debug_level;
445 }
446
447 EXPORT_SYMBOL(libcfs_debug_dumplog);
448 EXPORT_SYMBOL(libcfs_debug_set_level);