Whamcloud - gitweb
Branch b_head_quota
[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 (1 << 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_LQUOTA:
166                 return "lquota";
167         case S_LMV:
168                 return "lmv";
169         case S_SEC:
170                 return "sec";
171         case S_GSS:
172                 return "gss";
173         case S_MGC:
174                 return "mgc";
175         case S_MGS:
176                 return "mgs";
177         case S_FID:
178                 return "fid";
179         case S_FLD:
180                 return "fld";
181         }
182 }
183
184 /* libcfs_debug_token2mask() expects the returned
185  * string in lower-case */
186 const char *
187 libcfs_debug_dbg2str(int debug)
188 {
189         switch (1 << debug) {
190         default:
191                 return NULL;
192         case D_TRACE:
193                 return "trace";
194         case D_INODE:
195                 return "inode";
196         case D_SUPER:
197                 return "super";
198         case D_EXT2:
199                 return "ext2";
200         case D_MALLOC:
201                 return "malloc";
202         case D_CACHE:
203                 return "cache";
204         case D_INFO:
205                 return "info";
206         case D_IOCTL:
207                 return "ioctl";
208         case D_NETERROR:
209                 return "neterror";
210         case D_NET:
211                 return "net";
212         case D_WARNING:
213                 return "warning";
214         case D_BUFFS:
215                 return "buffs";
216         case D_OTHER:
217                 return "other";
218         case D_DENTRY:
219                 return "dentry";
220         case D_NETTRACE:
221                 return "nettrace";
222         case D_PAGE:
223                 return "page";
224         case D_DLMTRACE:
225                 return "dlmtrace";
226         case D_ERROR:
227                 return "error";
228         case D_EMERG:
229                 return "emerg";
230         case D_HA:
231                 return "ha";
232         case D_RPCTRACE:
233                 return "rpctrace";
234         case D_VFSTRACE:
235                 return "vfstrace";
236         case D_READA:
237                 return "reada";
238         case D_MMAP:
239                 return "mmap";
240         case D_CONFIG:
241                 return "config";
242         case D_CONSOLE:
243                 return "console";
244         case D_QUOTA:
245                 return "quota";
246         case D_SEC:
247                 return "sec";
248         }
249 }
250
251 int
252 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
253 {
254         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
255                                                  libcfs_debug_dbg2str;
256         int           len = 0;
257         const char   *token;
258         int           i;
259
260         if (mask == 0) {                        /* "0" */
261                 if (size > 0)
262                         str[0] = '0';
263                 len = 1;
264         } else {                                /* space-separated tokens */
265                 for (i = 0; i < 32; i++) {
266                         if ((mask & (1 << i)) == 0)
267                                 continue;
268
269                         token = fn(i);
270                         if (token == NULL)              /* unused bit */
271                                 continue;
272
273                         if (len > 0) {                  /* separator? */
274                                 if (len < size)
275                                         str[len] = ' ';
276                                 len++;
277                         }
278
279                         while (*token != 0) {
280                                 if (len < size)
281                                         str[len] = *token;
282                                 token++;
283                                 len++;
284                         }
285                 }
286         }
287
288         /* terminate 'str' */
289         if (len < size)
290                 str[len] = 0;
291         else
292                 str[size - 1] = 0;
293
294         return len;
295 }
296
297 int
298 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
299 {
300         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
301                                                  libcfs_debug_dbg2str;
302         int         m = 0;
303         int         matched;
304         int         n;
305         int         t;
306
307         /* Allow a number for backwards compatibility */
308
309         for (n = strlen(str); n > 0; n--)
310                 if (!isspace(str[n-1]))
311                         break;
312         matched = n;
313
314         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
315             matched == n) {
316                 *mask = m;
317                 return 0;
318         }
319
320         return libcfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
321                                0xffffffff);
322 }
323
324 /**
325  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
326  */
327 void libcfs_debug_dumplog_internal(void *arg)
328 {
329         CFS_DECL_JOURNAL_DATA;
330
331         CFS_PUSH_JOURNAL;
332
333         if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
334                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
335                          "%s.%ld." LPLD, debug_file_path_arr,
336                          cfs_time_current_sec(), (long_ptr_t)arg);
337                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
338                        debug_file_name);
339                 tracefile_dump_all_pages(debug_file_name);
340                 libcfs_run_debug_log_upcall(debug_file_name);
341         }
342         CFS_POP_JOURNAL;
343 }
344
345 int libcfs_debug_dumplog_thread(void *arg)
346 {
347         cfs_daemonize("");
348         libcfs_debug_dumplog_internal(arg);
349         cfs_waitq_signal(&debug_ctlwq);
350         return 0;
351 }
352
353 void libcfs_debug_dumplog(void)
354 {
355         int            rc;
356         cfs_waitlink_t wait;
357         ENTRY;
358
359         /* we're being careful to ensure that the kernel thread is
360          * able to set our state to running as it exits before we
361          * get to schedule() */
362         cfs_waitlink_init(&wait);
363         set_current_state(TASK_INTERRUPTIBLE);
364         cfs_waitq_add(&debug_ctlwq, &wait);
365
366         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
367                                (void *)(long_ptr_t)cfs_curproc_pid(),
368                                CLONE_VM | CLONE_FS | CLONE_FILES);
369         if (rc < 0)
370                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
371                        "%d\n", rc);
372         else
373                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
374
375         /* be sure to teardown if kernel_thread() failed */
376         cfs_waitq_del(&debug_ctlwq, &wait);
377         set_current_state(TASK_RUNNING);
378 }
379
380 int libcfs_debug_init(unsigned long bufsize)
381 {
382         int    rc = 0;
383         int    max = libcfs_debug_mb;
384
385         cfs_waitq_init(&debug_ctlwq);
386         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
387         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
388         /* If libcfs_debug_mb is set to an invalid value or uninitialized
389          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
390         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
391                 max = TCD_MAX_PAGES;
392         } else {
393                 max = (max / num_possible_cpus());
394                 max = (max << (20 - CFS_PAGE_SHIFT));
395         }
396         rc = tracefile_init(max);
397
398         if (rc == 0)
399                 libcfs_register_panic_notifier();
400
401         return rc;
402 }
403
404 int libcfs_debug_cleanup(void)
405 {
406         libcfs_unregister_panic_notifier();
407         tracefile_exit();
408         return 0;
409 }
410
411 int libcfs_debug_clear_buffer(void)
412 {
413         trace_flush_pages();
414         return 0;
415 }
416
417 /* Debug markers, although printed by S_LNET
418  * should not be be marked as such. */
419 #undef DEBUG_SUBSYSTEM
420 #define DEBUG_SUBSYSTEM S_UNDEFINED
421 int libcfs_debug_mark_buffer(const char *text)
422 {
423         CDEBUG(D_TRACE,"***************************************************\n");
424         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
425         CDEBUG(D_TRACE,"***************************************************\n");
426
427         return 0;
428 }
429 #undef DEBUG_SUBSYSTEM
430 #define DEBUG_SUBSYSTEM S_LNET
431
432 void libcfs_debug_set_level(unsigned int debug_level)
433 {
434         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
435                debug_level);
436         libcfs_debug = debug_level;
437 }
438
439 EXPORT_SYMBOL(libcfs_debug_dumplog);
440 EXPORT_SYMBOL(libcfs_debug_set_level);