1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/debug.c
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define EXPORT_SYMTAB
45 # define DEBUG_SUBSYSTEM S_LNET
47 #include <libcfs/libcfs.h>
48 #include "tracefile.h"
50 static char debug_file_name[1024];
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);
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);
63 unsigned int libcfs_debug_mb = 0;
64 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
65 "Total debug buffer size.");
66 EXPORT_SYMBOL(libcfs_debug_mb);
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);
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);
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);
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);
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);
93 unsigned int libcfs_debug_binary = 1;
94 EXPORT_SYMBOL(libcfs_debug_binary);
96 unsigned int libcfs_stack;
97 EXPORT_SYMBOL(libcfs_stack);
99 unsigned int portal_enter_debugger;
100 EXPORT_SYMBOL(portal_enter_debugger);
102 unsigned int libcfs_catastrophe;
103 EXPORT_SYMBOL(libcfs_catastrophe);
105 unsigned int libcfs_watchdog_ratelimit = 300;
106 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
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);
113 cfs_atomic_t libcfs_kmemory = CFS_ATOMIC_INIT(0);
114 EXPORT_SYMBOL(libcfs_kmemory);
116 static cfs_waitq_t debug_ctlwq;
118 char libcfs_debug_file_path_arr[1024] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
120 /* We need to pass a pointer here, but elsewhere this must be a const */
121 char *libcfs_debug_file_path = &libcfs_debug_file_path_arr[0];
122 CFS_MODULE_PARM(libcfs_debug_file_path, "s", charp, 0644,
123 "Path for dumping debug logs, "
124 "set 'NONE' to prevent log dumping");
126 int libcfs_panic_in_progress;
128 /* libcfs_debug_token2mask() expects the returned
129 * string in lower-case */
131 libcfs_debug_subsys2str(int subsys)
133 switch (1 << subsys) {
187 /* libcfs_debug_token2mask() expects the returned
188 * string in lower-case */
190 libcfs_debug_dbg2str(int debug)
192 switch (1 << debug) {
255 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
257 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
258 libcfs_debug_dbg2str;
263 if (mask == 0) { /* "0" */
267 } else { /* space-separated tokens */
268 for (i = 0; i < 32; i++) {
269 if ((mask & (1 << i)) == 0)
273 if (token == NULL) /* unused bit */
276 if (len > 0) { /* separator? */
282 while (*token != 0) {
291 /* terminate 'str' */
301 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
303 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
304 libcfs_debug_dbg2str;
310 /* Allow a number for backwards compatibility */
312 for (n = strlen(str); n > 0; n--)
313 if (!isspace(str[n-1]))
317 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
323 return libcfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
328 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
330 void libcfs_debug_dumplog_internal(void *arg)
332 CFS_DECL_JOURNAL_DATA;
336 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
337 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
338 "%s.%ld." LPLD, libcfs_debug_file_path_arr,
339 cfs_time_current_sec(), (long_ptr_t)arg);
340 printk(CFS_KERN_ALERT "LustreError: dumping log to %s\n",
342 cfs_tracefile_dump_all_pages(debug_file_name);
343 libcfs_run_debug_log_upcall(debug_file_name);
348 int libcfs_debug_dumplog_thread(void *arg)
350 libcfs_debug_dumplog_internal(arg);
351 cfs_waitq_signal(&debug_ctlwq);
355 void libcfs_debug_dumplog(void)
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 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
366 cfs_waitq_add(&debug_ctlwq, &wait);
368 dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
369 (void*)(long)cfs_curproc_pid(),
370 "libcfs_debug_dumper");
372 printk(CFS_KERN_ERR "LustreError: cannot start log dump thread:"
373 " %ld\n", PTR_ERR(dumper));
375 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
377 /* be sure to teardown if cfs_kernel_thread() failed */
378 cfs_waitq_del(&debug_ctlwq, &wait);
379 cfs_set_current_state(CFS_TASK_RUNNING);
382 int libcfs_debug_init(unsigned long bufsize)
385 unsigned int max = libcfs_debug_mb;
387 cfs_waitq_init(&debug_ctlwq);
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;
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 > cfs_trace_max_debug_mb() || max < cfs_num_possible_cpus()) {
401 max = (max / cfs_num_possible_cpus());
402 max = (max << (20 - CFS_PAGE_SHIFT));
404 rc = cfs_tracefile_init(max);
407 libcfs_register_panic_notifier();
412 int libcfs_debug_cleanup(void)
414 libcfs_unregister_panic_notifier();
415 cfs_tracefile_exit();
419 int libcfs_debug_clear_buffer(void)
421 cfs_trace_flush_pages();
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)
431 CDEBUG(D_TRACE,"***************************************************\n");
432 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
433 CDEBUG(D_TRACE,"***************************************************\n");
437 #undef DEBUG_SUBSYSTEM
438 #define DEBUG_SUBSYSTEM S_LNET
440 void libcfs_debug_set_level(unsigned int debug_level)
442 printk(CFS_KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
444 libcfs_debug = debug_level;
447 EXPORT_SYMBOL(libcfs_debug_dumplog);
448 EXPORT_SYMBOL(libcfs_debug_set_level);