4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/debug.c
34 * Author: Phil Schwan <phil@clusterfs.com>
38 # define DEBUG_SUBSYSTEM S_LNET
40 #include <linux/kthread.h>
41 #include <libcfs/libcfs.h>
42 #include "tracefile.h"
44 static char debug_file_name[1024];
46 unsigned int libcfs_subsystem_debug = ~0;
47 module_param(libcfs_subsystem_debug, int, 0644);
48 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
49 EXPORT_SYMBOL(libcfs_subsystem_debug);
51 unsigned int libcfs_debug = (D_CANTMASK |
52 D_NETERROR | D_HA | D_CONFIG | D_IOCTL | D_LFSCK);
53 module_param(libcfs_debug, int, 0644);
54 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
55 EXPORT_SYMBOL(libcfs_debug);
57 static int libcfs_param_debug_mb_set(const char *val,
58 cfs_kernel_param_arg_t *kp)
63 rc = kstrtouint(val, 0, &num);
68 * RHEL6 does not support any kind of locking so we have to provide
71 #ifndef HAVE_MODULE_PARAM_LOCKING
72 kernel_param_lock(THIS_MODULE);
74 if (!*((unsigned int *)kp->arg)) {
75 *((unsigned int *)kp->arg) = num;
79 rc = cfs_trace_set_debug_mb(num);
82 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
84 #ifndef HAVE_MODULE_PARAM_LOCKING
85 kernel_param_unlock(THIS_MODULE);
90 /* While debug_mb setting look like unsigned int, in fact
91 * it needs quite a bunch of extra processing, so we define special
92 * debug_mb parameter type with corresponding methods to handle this case
94 static struct kernel_param_ops param_ops_debug_mb = {
95 .set = libcfs_param_debug_mb_set,
96 .get = param_get_uint,
99 #define param_check_debug_mb(name, p) \
100 __param_check(name, p, unsigned int)
102 static unsigned int libcfs_debug_mb;
103 #ifdef HAVE_KERNEL_PARAM_OPS
104 module_param(libcfs_debug_mb, debug_mb, 0644);
106 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
107 ¶m_ops_debug_mb, 0644);
109 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
111 unsigned int libcfs_printk = D_CANTMASK;
112 module_param(libcfs_printk, uint, 0644);
113 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
115 unsigned int libcfs_console_ratelimit = 1;
116 module_param(libcfs_console_ratelimit, uint, 0644);
117 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
119 static int param_set_delay_minmax(const char *val,
120 cfs_kernel_param_arg_t *kp,
127 rc = kstrtoint(val, 0, &sec);
131 /* The sysfs setting is in centiseconds */
132 d = cfs_time_seconds(sec) / 100;
133 if (d < min || d > max)
136 *((unsigned int *)kp->arg) = d;
141 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
143 unsigned int d = *(unsigned int *)kp->arg;
145 return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
148 unsigned int libcfs_console_max_delay;
149 unsigned int libcfs_console_min_delay;
151 static int param_set_console_max_delay(const char *val,
152 cfs_kernel_param_arg_t *kp)
154 return param_set_delay_minmax(val, kp,
155 libcfs_console_min_delay, INT_MAX);
158 static struct kernel_param_ops param_ops_console_max_delay = {
159 .set = param_set_console_max_delay,
160 .get = param_get_delay,
163 #define param_check_console_max_delay(name, p) \
164 __param_check(name, p, unsigned int)
166 #ifdef HAVE_KERNEL_PARAM_OPS
167 module_param(libcfs_console_max_delay, console_max_delay, 0644);
169 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
170 param_get_delay, ¶m_ops_console_max_delay, 0644);
172 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
174 static int param_set_console_min_delay(const char *val,
175 cfs_kernel_param_arg_t *kp)
177 return param_set_delay_minmax(val, kp,
178 1, libcfs_console_max_delay);
181 static struct kernel_param_ops param_ops_console_min_delay = {
182 .set = param_set_console_min_delay,
183 .get = param_get_delay,
186 #define param_check_console_min_delay(name, p) \
187 __param_check(name, p, unsigned int)
189 #ifdef HAVE_KERNEL_PARAM_OPS
190 module_param(libcfs_console_min_delay, console_min_delay, 0644);
192 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
193 param_get_delay, ¶m_ops_console_min_delay, 0644);
195 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
197 static int param_set_uint_minmax(const char *val,
198 cfs_kernel_param_arg_t *kp,
199 unsigned int min, unsigned int max)
207 ret = kstrtouint(val, 0, &num);
208 if (ret < 0 || num < min || num > max)
211 *((unsigned int *)kp->arg) = num;
215 static int param_set_uintpos(const char *val,
216 cfs_kernel_param_arg_t *kp)
218 return param_set_uint_minmax(val, kp, 1, -1);
221 static struct kernel_param_ops param_ops_uintpos = {
222 .set = param_set_uintpos,
223 .get = param_get_uint,
226 #define param_check_uintpos(name, p) \
227 __param_check(name, p, unsigned int)
229 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
230 #ifdef HAVE_KERNEL_PARAM_OPS
231 module_param(libcfs_console_backoff, uintpos, 0644);
233 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
234 ¶m_ops_uintpos, 0644);
236 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
238 unsigned int libcfs_debug_binary = 1;
240 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
241 EXPORT_SYMBOL(libcfs_stack);
243 unsigned int libcfs_catastrophe;
244 EXPORT_SYMBOL(libcfs_catastrophe);
246 unsigned int libcfs_watchdog_ratelimit = 300;
248 unsigned int libcfs_panic_on_lbug = 1;
249 module_param(libcfs_panic_on_lbug, uint, 0644);
250 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
252 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
253 EXPORT_SYMBOL(libcfs_kmemory);
255 static wait_queue_head_t debug_ctlwq;
257 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
258 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
260 /* We need to pass a pointer here, but elsewhere this must be a const */
261 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
262 module_param(libcfs_debug_file_path, charp, 0644);
263 MODULE_PARM_DESC(libcfs_debug_file_path,
264 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
266 int libcfs_panic_in_progress;
268 /* libcfs_debug_token2mask() expects the returned
269 * string in lower-case */
270 static const char *libcfs_debug_subsys2str(int subsys)
272 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
274 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
277 return libcfs_debug_subsystems[subsys];
280 /* libcfs_debug_token2mask() expects the returned
281 * string in lower-case */
282 static const char *libcfs_debug_dbg2str(int debug)
284 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
286 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
289 return libcfs_debug_masks[debug];
293 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
295 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
296 libcfs_debug_dbg2str;
301 if (mask == 0) { /* "0" */
305 } else { /* space-separated tokens */
306 for (i = 0; i < 32; i++) {
307 if ((mask & (1 << i)) == 0)
311 if (token == NULL) /* unused bit */
314 if (len > 0) { /* separator? */
320 while (*token != 0) {
329 /* terminate 'str' */
339 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
341 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
342 libcfs_debug_dbg2str;
348 /* Allow a number for backwards compatibility */
350 for (n = strlen(str); n > 0; n--)
351 if (!isspace(str[n-1]))
355 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
357 /* don't print warning for lctl set_param debug=0 or -1 */
358 if (m != 0 && m != -1)
359 CWARN("You are trying to use a numerical value for the "
360 "mask - this will be deprecated in a future "
366 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
371 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
373 void libcfs_debug_dumplog_internal(void *arg)
375 static time64_t last_dump_time;
376 time64_t current_time;
379 journal_info = current->journal_info;
380 current->journal_info = NULL;
381 current_time = ktime_get_real_seconds();
383 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
384 current_time > last_dump_time) {
385 last_dump_time = current_time;
386 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
387 "%s.%lld.%ld", libcfs_debug_file_path_arr,
388 (s64)current_time, (uintptr_t)arg);
389 printk(KERN_ALERT "LustreError: dumping log to %s\n",
391 cfs_tracefile_dump_all_pages(debug_file_name);
392 libcfs_run_debug_log_upcall(debug_file_name);
394 current->journal_info = journal_info;
397 static int libcfs_debug_dumplog_thread(void *arg)
399 libcfs_debug_dumplog_internal(arg);
400 wake_up(&debug_ctlwq);
404 void libcfs_debug_dumplog(void)
407 struct task_struct *dumper;
410 /* we're being careful to ensure that the kernel thread is
411 * able to set our state to running as it exits before we
412 * get to schedule() */
413 init_waitqueue_entry(&wait, current);
414 set_current_state(TASK_INTERRUPTIBLE);
415 add_wait_queue(&debug_ctlwq, &wait);
417 dumper = kthread_run(libcfs_debug_dumplog_thread,
418 (void *)(long)current_pid(),
419 "libcfs_debug_dumper");
421 printk(KERN_ERR "LustreError: cannot start log dump thread:"
422 " %ld\n", PTR_ERR(dumper));
426 /* be sure to teardown if cfs_create_thread() failed */
427 remove_wait_queue(&debug_ctlwq, &wait);
428 set_current_state(TASK_RUNNING);
430 EXPORT_SYMBOL(libcfs_debug_dumplog);
432 int libcfs_debug_init(unsigned long bufsize)
435 unsigned int max = libcfs_debug_mb;
437 init_waitqueue_head(&debug_ctlwq);
439 if (libcfs_console_max_delay <= 0 || /* not set by user or */
440 libcfs_console_min_delay <= 0 || /* set to invalid values */
441 libcfs_console_min_delay >= libcfs_console_max_delay) {
442 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
443 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
446 if (libcfs_debug_file_path != NULL) {
447 strlcpy(libcfs_debug_file_path_arr,
448 libcfs_debug_file_path,
449 sizeof(libcfs_debug_file_path_arr));
452 /* If libcfs_debug_mb is set to an invalid value or uninitialized
453 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
454 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
457 max = (max / num_possible_cpus());
458 max = (max << (20 - PAGE_SHIFT));
461 rc = cfs_tracefile_init(max);
465 libcfs_register_panic_notifier();
466 kernel_param_lock(THIS_MODULE);
467 libcfs_debug_mb = cfs_trace_get_debug_mb();
468 kernel_param_unlock(THIS_MODULE);
472 int libcfs_debug_cleanup(void)
474 libcfs_unregister_panic_notifier();
475 kernel_param_lock(THIS_MODULE);
476 cfs_tracefile_exit();
477 kernel_param_unlock(THIS_MODULE);
481 int libcfs_debug_clear_buffer(void)
483 cfs_trace_flush_pages();
487 /* Debug markers, although printed by S_LNET
488 * should not be be marked as such. */
489 #undef DEBUG_SUBSYSTEM
490 #define DEBUG_SUBSYSTEM S_UNDEFINED
491 int libcfs_debug_mark_buffer(const char *text)
493 CDEBUG(D_TRACE,"***************************************************\n");
494 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
495 CDEBUG(D_TRACE,"***************************************************\n");
499 #undef DEBUG_SUBSYSTEM
500 #define DEBUG_SUBSYSTEM S_LNET
502 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
504 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
508 EXPORT_SYMBOL(libcfs_log_return);
510 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
513 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
514 " : %#lx)\n", label, rc, rc, rc);
516 EXPORT_SYMBOL(libcfs_log_goto);