Whamcloud - gitweb
Land b_head_libcfs onto HEAD (20080805_1722)
[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 [sun.com URL with a
20  * copy of GPLv2].
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;
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_panic_on_lbug = 0;
106 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
107                 "Lustre kernel panic on LBUG");
108 EXPORT_SYMBOL(libcfs_panic_on_lbug);
109
110 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
111 EXPORT_SYMBOL(libcfs_kmemory);
112
113 static cfs_waitq_t debug_ctlwq;
114
115 #ifdef __arch_um__
116 char debug_file_path[1024] = "/r/tmp/lustre-log";
117 #else
118 char debug_file_path[1024] = "/tmp/lustre-log";
119 #endif
120 char *debug_file_path_p = &debug_file_path[0];
121
122 CFS_MODULE_PARM(debug_file_path_p, "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 (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_LMV:
169                 return "lmv";
170         case S_SEC:
171                 return "sec";
172         case S_GSS:
173                 return "gss";
174         case S_MGC:
175                 return "mgc";
176         case S_MGS:
177                 return "mgs";
178         case S_FID:
179                 return "fid";
180         case S_FLD:
181                 return "fld";
182         }
183 }
184
185 /* libcfs_debug_token2mask() expects the returned
186  * string in lower-case */
187 const char *
188 libcfs_debug_dbg2str(int debug)
189 {
190         switch (debug) {
191         default:
192                 return NULL;
193         case D_TRACE:
194                 return "trace";
195         case D_INODE:
196                 return "inode";
197         case D_SUPER:
198                 return "super";
199         case D_EXT2:
200                 return "ext2";
201         case D_MALLOC:
202                 return "malloc";
203         case D_CACHE:
204                 return "cache";
205         case D_INFO:
206                 return "info";
207         case D_IOCTL:
208                 return "ioctl";
209         case D_NETERROR:
210                 return "neterror";
211         case D_NET:
212                 return "net";
213         case D_WARNING:
214                 return "warning";
215         case D_BUFFS:
216                 return "buffs";
217         case D_OTHER:
218                 return "other";
219         case D_DENTRY:
220                 return "dentry";
221         case D_NETTRACE:
222                 return "nettrace";
223         case D_PAGE:
224                 return "page";
225         case D_DLMTRACE:
226                 return "dlmtrace";
227         case D_ERROR:
228                 return "error";
229         case D_EMERG:
230                 return "emerg";
231         case D_HA:
232                 return "ha";
233         case D_RPCTRACE:
234                 return "rpctrace";
235         case D_VFSTRACE:
236                 return "vfstrace";
237         case D_READA:
238                 return "reada";
239         case D_MMAP:
240                 return "mmap";
241         case D_CONFIG:
242                 return "config";
243         case D_CONSOLE:
244                 return "console";
245         case D_QUOTA:
246                 return "quota";
247         case D_SEC:
248                 return "sec";
249         }
250 }
251
252 int
253 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
254 {
255         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
256                                                  libcfs_debug_dbg2str;
257         int           len = 0;
258         const char   *token;
259         int           bit;
260         int           i;
261
262         if (mask == 0) {                        /* "0" */
263                 if (size > 0)
264                         str[0] = '0';
265                 len = 1;
266         } else {                                /* space-separated tokens */
267                 for (i = 0; i < 32; i++) {
268                         bit = 1 << i;
269
270                         if ((mask & bit) == 0)
271                                 continue;
272
273                         token = fn(bit);
274                         if (token == NULL)              /* unused bit */
275                                 continue;
276
277                         if (len > 0) {                  /* separator? */
278                                 if (len < size)
279                                         str[len] = ' ';
280                                 len++;
281                         }
282
283                         while (*token != 0) {
284                                 if (len < size)
285                                         str[len] = *token;
286                                 token++;
287                                 len++;
288                         }
289                 }
290         }
291
292         /* terminate 'str' */
293         if (len < size)
294                 str[len] = 0;
295         else
296                 str[size - 1] = 0;
297
298         return len;
299 }
300
301 int
302 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
303 {
304         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
305                                                  libcfs_debug_dbg2str;
306         int           i;
307         int           j;
308         int           bit;
309         const char   *token;
310
311         /* match against known tokens */
312         for (i = 0; i < 32; i++) {
313                 bit = 1 << i;
314
315                 token = fn(bit);
316                 if (token == NULL)              /* unused? */
317                         continue;
318
319                 /* strcasecmp */
320                 for (j = 0; ; j++) {
321                         if (j == len) {         /* end of token */
322                                 if (token[j] == 0) {
323                                         *mask = bit;
324                                         return 0;
325                                 }
326                                 break;
327                         }
328
329                         if (token[j] == 0)
330                                 break;
331
332                         if (str[j] == token[j])
333                                 continue;
334
335                         if (str[j] < 'A' || 'Z' < str[j])
336                                 break;
337
338                         if (str[j] - 'A' + 'a' != token[j])
339                                 break;
340                 }
341         }
342
343         return -EINVAL;                         /* no match */
344 }
345
346 int
347 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
348 {
349         int         m = 0;
350         char        op = 0;
351         int         matched;
352         int         n;
353         int         t;
354
355         /* Allow a number for backwards compatibility */
356
357         for (n = strlen(str); n > 0; n--)
358                 if (!isspace(str[n-1]))
359                         break;
360         matched = n;
361
362         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
363             matched == n) {
364                 *mask = m;
365                 return 0;
366         }
367
368         /* <str> must be a list of debug tokens or numbers separated by
369          * whitespace and optionally an operator ('+' or '-').  If an operator
370          * appears first in <str>, '*mask' is used as the starting point
371          * (relative), otherwise 0 is used (absolute).  An operator applies to
372          * all following tokens up to the next operator. */
373
374         matched = 0;
375         while (*str != 0) {
376                 while (isspace(*str)) /* skip whitespace */
377                         str++;
378
379                 if (*str == 0)
380                         break;
381
382                 if (*str == '+' || *str == '-') {
383                         op = *str++;
384
385                         /* op on first token == relative */
386                         if (!matched)
387                                 m = *mask;
388
389                         while (isspace(*str)) /* skip whitespace */
390                                 str++;
391
392                         if (*str == 0)          /* trailing op */
393                                 return -EINVAL;
394                 }
395
396                 /* find token length */
397                 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
398
399                 /* match token */
400                 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
401                         return -EINVAL;
402
403                 matched = 1;
404                 if (op == '-')
405                         m &= ~t;
406                 else
407                         m |= t;
408
409                 str += n;
410         }
411
412         if (!matched)
413                 return -EINVAL;
414
415         *mask = m;
416         return 0;
417 }
418
419 void libcfs_debug_dumplog_internal(void *arg)
420 {
421         CFS_DECL_JOURNAL_DATA;
422
423         CFS_PUSH_JOURNAL;
424
425         if (strncmp(debug_file_path, "NONE", 4) != 0) {
426                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
427                          "%s.%ld.%ld", debug_file_path, cfs_time_current_sec(),
428                          (long)arg);
429                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
430                        debug_file_name);
431                 tracefile_dump_all_pages(debug_file_name);
432         }
433         CFS_POP_JOURNAL;
434 }
435
436 int libcfs_debug_dumplog_thread(void *arg)
437 {
438         cfs_daemonize("");
439         libcfs_debug_dumplog_internal(arg);
440         cfs_waitq_signal(&debug_ctlwq);
441         return 0;
442 }
443
444 void libcfs_debug_dumplog(void)
445 {
446         int            rc;
447         cfs_waitlink_t wait;
448         ENTRY;
449
450         /* we're being careful to ensure that the kernel thread is
451          * able to set our state to running as it exits before we
452          * get to schedule() */
453         cfs_waitlink_init(&wait);
454         set_current_state(TASK_INTERRUPTIBLE);
455         cfs_waitq_add(&debug_ctlwq, &wait);
456
457         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
458                                (void *)(long)cfs_curproc_pid(),
459                                CLONE_VM | CLONE_FS | CLONE_FILES);
460         if (rc < 0)
461                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
462                        "%d\n", rc);
463         else
464                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
465
466         /* be sure to teardown if kernel_thread() failed */
467         cfs_waitq_del(&debug_ctlwq, &wait);
468         set_current_state(TASK_RUNNING);
469 }
470
471 int libcfs_debug_init(unsigned long bufsize)
472 {
473         int    rc = 0;
474         int    max = libcfs_debug_mb;
475
476         cfs_waitq_init(&debug_ctlwq);
477         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
478         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
479         /* If libcfs_debug_mb is set to an invalid value or uninitialized
480          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
481         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
482                 max = TCD_MAX_PAGES;
483         } else {
484                 max = (max / num_possible_cpus());
485                 max = (max << (20 - CFS_PAGE_SHIFT));
486         }
487         rc = tracefile_init(max);
488
489         if (rc == 0)
490                 libcfs_register_panic_notifier();
491
492         return rc;
493 }
494
495 int libcfs_debug_cleanup(void)
496 {
497         libcfs_unregister_panic_notifier();
498         tracefile_exit();
499         return 0;
500 }
501
502 int libcfs_debug_clear_buffer(void)
503 {
504         trace_flush_pages();
505         return 0;
506 }
507
508 /* Debug markers, although printed by S_LNET
509  * should not be be marked as such. */
510 #undef DEBUG_SUBSYSTEM
511 #define DEBUG_SUBSYSTEM S_UNDEFINED
512 int libcfs_debug_mark_buffer(char *text)
513 {
514         CDEBUG(D_TRACE,"***************************************************\n");
515         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
516         CDEBUG(D_TRACE,"***************************************************\n");
517
518         return 0;
519 }
520 #undef DEBUG_SUBSYSTEM
521 #define DEBUG_SUBSYSTEM S_LNET
522
523 void libcfs_debug_set_level(unsigned int debug_level)
524 {
525         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
526                debug_level);
527         libcfs_debug = debug_level;
528 }
529
530 EXPORT_SYMBOL(libcfs_debug_dumplog);
531 EXPORT_SYMBOL(libcfs_debug_set_level);