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, 2017, 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 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
72 kernel_param_lock(THIS_MODULE);
74 if (!*((unsigned int *)kp->arg)) {
75 *((unsigned int *)kp->arg) = num;
77 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
78 kernel_param_unlock(THIS_MODULE);
83 rc = cfs_trace_set_debug_mb(num);
86 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
88 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
89 kernel_param_unlock(THIS_MODULE);
94 /* While debug_mb setting look like unsigned int, in fact
95 * it needs quite a bunch of extra processing, so we define special
96 * debug_mb parameter type with corresponding methods to handle this case
98 static struct kernel_param_ops param_ops_debug_mb = {
99 .set = libcfs_param_debug_mb_set,
100 .get = param_get_uint,
103 #define param_check_debug_mb(name, p) \
104 __param_check(name, p, unsigned int)
106 static unsigned int libcfs_debug_mb;
107 #ifdef HAVE_KERNEL_PARAM_OPS
108 module_param(libcfs_debug_mb, debug_mb, 0644);
110 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
111 ¶m_ops_debug_mb, 0644);
113 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
115 unsigned int libcfs_printk = D_CANTMASK;
116 module_param(libcfs_printk, uint, 0644);
117 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
119 unsigned int libcfs_console_ratelimit = 1;
120 module_param(libcfs_console_ratelimit, uint, 0644);
121 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
123 static int param_set_delay_minmax(const char *val,
124 cfs_kernel_param_arg_t *kp,
131 rc = kstrtoint(val, 0, &sec);
135 /* The sysfs setting is in centiseconds */
136 d = cfs_time_seconds(sec) / 100;
137 if (d < min || d > max)
140 *((unsigned int *)kp->arg) = d;
145 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
147 unsigned int d = *(unsigned int *)kp->arg;
149 return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
152 unsigned int libcfs_console_max_delay;
153 unsigned int libcfs_console_min_delay;
155 static int param_set_console_max_delay(const char *val,
156 cfs_kernel_param_arg_t *kp)
158 return param_set_delay_minmax(val, kp,
159 libcfs_console_min_delay, INT_MAX);
162 static struct kernel_param_ops param_ops_console_max_delay = {
163 .set = param_set_console_max_delay,
164 .get = param_get_delay,
167 #define param_check_console_max_delay(name, p) \
168 __param_check(name, p, unsigned int)
170 #ifdef HAVE_KERNEL_PARAM_OPS
171 module_param(libcfs_console_max_delay, console_max_delay, 0644);
173 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
174 param_get_delay, ¶m_ops_console_max_delay, 0644);
176 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
178 static int param_set_console_min_delay(const char *val,
179 cfs_kernel_param_arg_t *kp)
181 return param_set_delay_minmax(val, kp,
182 1, libcfs_console_max_delay);
185 static struct kernel_param_ops param_ops_console_min_delay = {
186 .set = param_set_console_min_delay,
187 .get = param_get_delay,
190 #define param_check_console_min_delay(name, p) \
191 __param_check(name, p, unsigned int)
193 #ifdef HAVE_KERNEL_PARAM_OPS
194 module_param(libcfs_console_min_delay, console_min_delay, 0644);
196 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
197 param_get_delay, ¶m_ops_console_min_delay, 0644);
199 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
201 static int param_set_uint_minmax(const char *val,
202 cfs_kernel_param_arg_t *kp,
203 unsigned int min, unsigned int max)
211 ret = kstrtouint(val, 0, &num);
212 if (ret < 0 || num < min || num > max)
215 *((unsigned int *)kp->arg) = num;
219 static int param_set_uintpos(const char *val,
220 cfs_kernel_param_arg_t *kp)
222 return param_set_uint_minmax(val, kp, 1, -1);
225 static struct kernel_param_ops param_ops_uintpos = {
226 .set = param_set_uintpos,
227 .get = param_get_uint,
230 #define param_check_uintpos(name, p) \
231 __param_check(name, p, unsigned int)
233 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
234 #ifdef HAVE_KERNEL_PARAM_OPS
235 module_param(libcfs_console_backoff, uintpos, 0644);
237 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
238 ¶m_ops_uintpos, 0644);
240 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
242 unsigned int libcfs_debug_binary = 1;
244 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
245 EXPORT_SYMBOL(libcfs_stack);
247 unsigned int libcfs_catastrophe;
248 EXPORT_SYMBOL(libcfs_catastrophe);
250 unsigned int libcfs_watchdog_ratelimit = 300;
252 unsigned int libcfs_panic_on_lbug = 1;
253 module_param(libcfs_panic_on_lbug, uint, 0644);
254 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
256 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
257 EXPORT_SYMBOL(libcfs_kmemory);
259 static wait_queue_head_t debug_ctlwq;
261 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
262 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
264 /* We need to pass a pointer here, but elsewhere this must be a const */
265 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
266 module_param(libcfs_debug_file_path, charp, 0644);
267 MODULE_PARM_DESC(libcfs_debug_file_path,
268 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
270 int libcfs_panic_in_progress;
272 /* libcfs_debug_token2mask() expects the returned
273 * string in lower-case */
274 static const char *libcfs_debug_subsys2str(int subsys)
276 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
278 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
281 return libcfs_debug_subsystems[subsys];
284 /* libcfs_debug_token2mask() expects the returned
285 * string in lower-case */
286 static const char *libcfs_debug_dbg2str(int debug)
288 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
290 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
293 return libcfs_debug_masks[debug];
297 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
299 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
300 libcfs_debug_dbg2str;
305 if (mask == 0) { /* "0" */
309 } else { /* space-separated tokens */
310 for (i = 0; i < 32; i++) {
311 if ((mask & (1 << i)) == 0)
315 if (token == NULL) /* unused bit */
318 if (len > 0) { /* separator? */
324 while (*token != 0) {
333 /* terminate 'str' */
343 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
345 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
346 libcfs_debug_dbg2str;
352 /* Allow a number for backwards compatibility */
354 for (n = strlen(str); n > 0; n--)
355 if (!isspace(str[n-1]))
359 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
361 /* don't print warning for lctl set_param debug=0 or -1 */
362 if (m != 0 && m != -1)
363 CWARN("You are trying to use a numerical value for the "
364 "mask - this will be deprecated in a future "
370 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
375 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
377 void libcfs_debug_dumplog_internal(void *arg)
379 static time64_t last_dump_time;
380 time64_t current_time;
383 journal_info = current->journal_info;
384 current->journal_info = NULL;
385 current_time = ktime_get_real_seconds();
387 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
388 current_time > last_dump_time) {
389 last_dump_time = current_time;
390 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
391 "%s.%lld.%ld", libcfs_debug_file_path_arr,
392 (s64)current_time, (uintptr_t)arg);
393 printk(KERN_ALERT "LustreError: dumping log to %s\n",
395 cfs_tracefile_dump_all_pages(debug_file_name);
396 libcfs_run_debug_log_upcall(debug_file_name);
398 current->journal_info = journal_info;
401 static int libcfs_debug_dumplog_thread(void *arg)
403 libcfs_debug_dumplog_internal(arg);
404 wake_up(&debug_ctlwq);
408 void libcfs_debug_dumplog(void)
411 struct task_struct *dumper;
414 /* we're being careful to ensure that the kernel thread is
415 * able to set our state to running as it exits before we
416 * get to schedule() */
417 init_waitqueue_entry(&wait, current);
418 set_current_state(TASK_INTERRUPTIBLE);
419 add_wait_queue(&debug_ctlwq, &wait);
421 dumper = kthread_run(libcfs_debug_dumplog_thread,
422 (void *)(long)current_pid(),
423 "libcfs_debug_dumper");
425 printk(KERN_ERR "LustreError: cannot start log dump thread:"
426 " %ld\n", PTR_ERR(dumper));
430 /* be sure to teardown if cfs_create_thread() failed */
431 remove_wait_queue(&debug_ctlwq, &wait);
432 set_current_state(TASK_RUNNING);
434 EXPORT_SYMBOL(libcfs_debug_dumplog);
436 int libcfs_debug_init(unsigned long bufsize)
439 unsigned int max = libcfs_debug_mb;
441 init_waitqueue_head(&debug_ctlwq);
443 if (libcfs_console_max_delay <= 0 || /* not set by user or */
444 libcfs_console_min_delay <= 0 || /* set to invalid values */
445 libcfs_console_min_delay >= libcfs_console_max_delay) {
446 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
447 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
450 if (libcfs_debug_file_path != NULL) {
451 strlcpy(libcfs_debug_file_path_arr,
452 libcfs_debug_file_path,
453 sizeof(libcfs_debug_file_path_arr));
456 /* If libcfs_debug_mb is set to an invalid value or uninitialized
457 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
458 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
461 max = (max / num_possible_cpus());
462 max = (max << (20 - PAGE_SHIFT));
465 rc = cfs_tracefile_init(max);
469 libcfs_register_panic_notifier();
470 kernel_param_lock(THIS_MODULE);
471 libcfs_debug_mb = cfs_trace_get_debug_mb();
472 kernel_param_unlock(THIS_MODULE);
476 int libcfs_debug_cleanup(void)
478 libcfs_unregister_panic_notifier();
479 kernel_param_lock(THIS_MODULE);
480 cfs_tracefile_exit();
481 kernel_param_unlock(THIS_MODULE);
485 int libcfs_debug_clear_buffer(void)
487 cfs_trace_flush_pages();
491 /* Debug markers, although printed by S_LNET
492 * should not be be marked as such. */
493 #undef DEBUG_SUBSYSTEM
494 #define DEBUG_SUBSYSTEM S_UNDEFINED
495 int libcfs_debug_mark_buffer(const char *text)
497 CDEBUG(D_TRACE,"***************************************************\n");
498 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
499 CDEBUG(D_TRACE,"***************************************************\n");
503 #undef DEBUG_SUBSYSTEM
504 #define DEBUG_SUBSYSTEM S_LNET
506 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
508 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
512 EXPORT_SYMBOL(libcfs_log_return);
514 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
517 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
518 " : %#lx)\n", label, rc, rc, rc);
520 EXPORT_SYMBOL(libcfs_log_goto);