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