Whamcloud - gitweb
dd40428822844643db43db89745fad283b7f9f1d
[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_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 (1 << 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           i;
257
258         if (mask == 0) {                        /* "0" */
259                 if (size > 0)
260                         str[0] = '0';
261                 len = 1;
262         } else {                                /* space-separated tokens */
263                 for (i = 0; i < 32; i++) {
264                         if ((mask & (1 << i)) == 0)
265                                 continue;
266
267                         token = fn(i);
268                         if (token == NULL)              /* unused bit */
269                                 continue;
270
271                         if (len > 0) {                  /* separator? */
272                                 if (len < size)
273                                         str[len] = ' ';
274                                 len++;
275                         }
276
277                         while (*token != 0) {
278                                 if (len < size)
279                                         str[len] = *token;
280                                 token++;
281                                 len++;
282                         }
283                 }
284         }
285
286         /* terminate 'str' */
287         if (len < size)
288                 str[len] = 0;
289         else
290                 str[size - 1] = 0;
291
292         return len;
293 }
294
295 int
296 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
297 {
298         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
299                                                  libcfs_debug_dbg2str;
300         int         m = 0;
301         int         matched;
302         int         n;
303         int         t;
304
305         /* Allow a number for backwards compatibility */
306
307         for (n = strlen(str); n > 0; n--)
308                 if (!isspace(str[n-1]))
309                         break;
310         matched = n;
311
312         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
313             matched == n) {
314                 *mask = m;
315                 return 0;
316         }
317
318         return libcfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
319                                0xffffffff);
320 }
321
322 /**
323  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
324  */
325 void libcfs_debug_dumplog_internal(void *arg)
326 {
327         CFS_DECL_JOURNAL_DATA;
328
329         CFS_PUSH_JOURNAL;
330
331         if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
332                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
333                          "%s.%ld." LPLD, debug_file_path_arr,
334                          cfs_time_current_sec(), (long_ptr_t)arg);
335                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
336                        debug_file_name);
337                 tracefile_dump_all_pages(debug_file_name);
338                 libcfs_run_debug_log_upcall(debug_file_name);
339         }
340         CFS_POP_JOURNAL;
341 }
342
343 int libcfs_debug_dumplog_thread(void *arg)
344 {
345         cfs_daemonize("");
346         libcfs_debug_dumplog_internal(arg);
347         cfs_waitq_signal(&debug_ctlwq);
348         return 0;
349 }
350
351 void libcfs_debug_dumplog(void)
352 {
353         int            rc;
354         cfs_waitlink_t wait;
355         ENTRY;
356
357         /* we're being careful to ensure that the kernel thread is
358          * able to set our state to running as it exits before we
359          * get to schedule() */
360         cfs_waitlink_init(&wait);
361         set_current_state(TASK_INTERRUPTIBLE);
362         cfs_waitq_add(&debug_ctlwq, &wait);
363
364         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
365                                (void *)(long_ptr_t)cfs_curproc_pid(),
366                                CLONE_VM | CLONE_FS | CLONE_FILES);
367         if (rc < 0)
368                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
369                        "%d\n", rc);
370         else
371                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
372
373         /* be sure to teardown if kernel_thread() failed */
374         cfs_waitq_del(&debug_ctlwq, &wait);
375         set_current_state(TASK_RUNNING);
376 }
377
378 int libcfs_debug_init(unsigned long bufsize)
379 {
380         int    rc = 0;
381         int    max = libcfs_debug_mb;
382
383         cfs_waitq_init(&debug_ctlwq);
384         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
385         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
386         /* If libcfs_debug_mb is set to an invalid value or uninitialized
387          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
388         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
389                 max = TCD_MAX_PAGES;
390         } else {
391                 max = (max / num_possible_cpus());
392                 max = (max << (20 - CFS_PAGE_SHIFT));
393         }
394         rc = tracefile_init(max);
395
396         if (rc == 0)
397                 libcfs_register_panic_notifier();
398
399         return rc;
400 }
401
402 int libcfs_debug_cleanup(void)
403 {
404         libcfs_unregister_panic_notifier();
405         tracefile_exit();
406         return 0;
407 }
408
409 int libcfs_debug_clear_buffer(void)
410 {
411         trace_flush_pages();
412         return 0;
413 }
414
415 /* Debug markers, although printed by S_LNET
416  * should not be be marked as such. */
417 #undef DEBUG_SUBSYSTEM
418 #define DEBUG_SUBSYSTEM S_UNDEFINED
419 int libcfs_debug_mark_buffer(const char *text)
420 {
421         CDEBUG(D_TRACE,"***************************************************\n");
422         CDEBUG(D_WARNING, "DEBUG MARKER: %s\n", text);
423         CDEBUG(D_TRACE,"***************************************************\n");
424
425         return 0;
426 }
427 #undef DEBUG_SUBSYSTEM
428 #define DEBUG_SUBSYSTEM S_LNET
429
430 void libcfs_debug_set_level(unsigned int debug_level)
431 {
432         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
433                debug_level);
434         libcfs_debug = debug_level;
435 }
436
437 EXPORT_SYMBOL(libcfs_debug_dumplog);
438 EXPORT_SYMBOL(libcfs_debug_set_level);