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/ctype.h>
41 #include <linux/kthread.h>
42 #include <libcfs/linux/linux-misc.h>
43 #include <libcfs/libcfs.h>
44 #include "tracefile.h"
46 static char debug_file_name[1024];
48 unsigned int libcfs_subsystem_debug = ~0;
49 module_param(libcfs_subsystem_debug, int, 0644);
50 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
51 EXPORT_SYMBOL(libcfs_subsystem_debug);
53 unsigned int libcfs_debug = (D_CANTMASK |
54 D_NETERROR | D_HA | D_CONFIG | D_IOCTL | D_LFSCK);
55 module_param(libcfs_debug, int, 0644);
56 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
57 EXPORT_SYMBOL(libcfs_debug);
59 static int libcfs_param_debug_mb_set(const char *val,
60 cfs_kernel_param_arg_t *kp)
65 rc = kstrtouint(val, 0, &num);
70 * RHEL6 does not support any kind of locking so we have to provide
73 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
74 kernel_param_lock(THIS_MODULE);
76 if (!*((unsigned int *)kp->arg)) {
77 *((unsigned int *)kp->arg) = num;
79 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
80 kernel_param_unlock(THIS_MODULE);
85 rc = cfs_trace_set_debug_mb(num);
88 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
90 #if !defined(HAVE_MODULE_PARAM_LOCKING) && !defined(HAVE_KERNEL_PARAM_LOCK)
91 kernel_param_unlock(THIS_MODULE);
96 /* While debug_mb setting look like unsigned int, in fact
97 * it needs quite a bunch of extra processing, so we define special
98 * debug_mb parameter type with corresponding methods to handle this case
100 static struct kernel_param_ops param_ops_debug_mb = {
101 .set = libcfs_param_debug_mb_set,
102 .get = param_get_uint,
105 #define param_check_debug_mb(name, p) \
106 __param_check(name, p, unsigned int)
108 static unsigned int libcfs_debug_mb;
109 #ifdef HAVE_KERNEL_PARAM_OPS
110 module_param(libcfs_debug_mb, debug_mb, 0644);
112 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
113 ¶m_ops_debug_mb, 0644);
115 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
117 unsigned int libcfs_printk = D_CANTMASK;
118 module_param(libcfs_printk, uint, 0644);
119 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
121 unsigned int libcfs_console_ratelimit = 1;
122 module_param(libcfs_console_ratelimit, uint, 0644);
123 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
125 static int param_set_delay_minmax(const char *val,
126 cfs_kernel_param_arg_t *kp,
133 rc = kstrtoint(val, 0, &sec);
137 /* The sysfs setting is in centiseconds */
138 d = cfs_time_seconds(sec) / 100;
139 if (d < min || d > max)
142 *((unsigned int *)kp->arg) = d;
147 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
149 unsigned int d = *(unsigned int *)kp->arg;
151 return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
154 unsigned int libcfs_console_max_delay;
155 unsigned int libcfs_console_min_delay;
157 static int param_set_console_max_delay(const char *val,
158 cfs_kernel_param_arg_t *kp)
160 return param_set_delay_minmax(val, kp,
161 libcfs_console_min_delay, INT_MAX);
164 static struct kernel_param_ops param_ops_console_max_delay = {
165 .set = param_set_console_max_delay,
166 .get = param_get_delay,
169 #define param_check_console_max_delay(name, p) \
170 __param_check(name, p, unsigned int)
172 #ifdef HAVE_KERNEL_PARAM_OPS
173 module_param(libcfs_console_max_delay, console_max_delay, 0644);
175 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
176 param_get_delay, ¶m_ops_console_max_delay, 0644);
178 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
180 static int param_set_console_min_delay(const char *val,
181 cfs_kernel_param_arg_t *kp)
183 return param_set_delay_minmax(val, kp,
184 1, libcfs_console_max_delay);
187 static struct kernel_param_ops param_ops_console_min_delay = {
188 .set = param_set_console_min_delay,
189 .get = param_get_delay,
192 #define param_check_console_min_delay(name, p) \
193 __param_check(name, p, unsigned int)
195 #ifdef HAVE_KERNEL_PARAM_OPS
196 module_param(libcfs_console_min_delay, console_min_delay, 0644);
198 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
199 param_get_delay, ¶m_ops_console_min_delay, 0644);
201 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
203 static int param_set_uint_minmax(const char *val,
204 cfs_kernel_param_arg_t *kp,
205 unsigned int min, unsigned int max)
213 ret = kstrtouint(val, 0, &num);
214 if (ret < 0 || num < min || num > max)
217 *((unsigned int *)kp->arg) = num;
221 static int param_set_uintpos(const char *val,
222 cfs_kernel_param_arg_t *kp)
224 return param_set_uint_minmax(val, kp, 1, -1);
227 static struct kernel_param_ops param_ops_uintpos = {
228 .set = param_set_uintpos,
229 .get = param_get_uint,
232 #define param_check_uintpos(name, p) \
233 __param_check(name, p, unsigned int)
235 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
236 #ifdef HAVE_KERNEL_PARAM_OPS
237 module_param(libcfs_console_backoff, uintpos, 0644);
239 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
240 ¶m_ops_uintpos, 0644);
242 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
244 unsigned int libcfs_debug_binary = 1;
246 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
247 EXPORT_SYMBOL(libcfs_stack);
249 unsigned int libcfs_catastrophe;
250 EXPORT_SYMBOL(libcfs_catastrophe);
252 unsigned int libcfs_watchdog_ratelimit = 300;
254 unsigned int libcfs_panic_on_lbug = 1;
255 module_param(libcfs_panic_on_lbug, uint, 0644);
256 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
258 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
259 EXPORT_SYMBOL(libcfs_kmemory);
261 static wait_queue_head_t debug_ctlwq;
263 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
264 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
266 /* We need to pass a pointer here, but elsewhere this must be a const */
267 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
268 module_param(libcfs_debug_file_path, charp, 0644);
269 MODULE_PARM_DESC(libcfs_debug_file_path,
270 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
272 int libcfs_panic_in_progress;
274 /* libcfs_debug_token2mask() expects the returned
275 * string in lower-case */
276 static const char *libcfs_debug_subsys2str(int subsys)
278 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
280 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
283 return libcfs_debug_subsystems[subsys];
286 /* libcfs_debug_token2mask() expects the returned
287 * string in lower-case */
288 static const char *libcfs_debug_dbg2str(int debug)
290 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
292 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
295 return libcfs_debug_masks[debug];
299 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
301 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
302 libcfs_debug_dbg2str;
307 if (mask == 0) { /* "0" */
311 } else { /* space-separated tokens */
312 for (i = 0; i < 32; i++) {
313 if ((mask & (1 << i)) == 0)
317 if (token == NULL) /* unused bit */
320 if (len > 0) { /* separator? */
326 while (*token != 0) {
335 /* terminate 'str' */
345 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
347 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
348 libcfs_debug_dbg2str;
354 /* Allow a number for backwards compatibility */
356 for (n = strlen(str); n > 0; n--)
357 if (!isspace(str[n-1]))
361 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
363 /* don't print warning for lctl set_param debug=0 or -1 */
364 if (m != 0 && m != -1)
365 CWARN("You are trying to use a numerical value for the "
366 "mask - this will be deprecated in a future "
372 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
377 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
379 void libcfs_debug_dumplog_internal(void *arg)
381 static time64_t last_dump_time;
382 time64_t current_time;
385 journal_info = current->journal_info;
386 current->journal_info = NULL;
387 current_time = ktime_get_real_seconds();
389 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
390 current_time > last_dump_time) {
391 last_dump_time = current_time;
392 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
393 "%s.%lld.%ld", libcfs_debug_file_path_arr,
394 (s64)current_time, (uintptr_t)arg);
395 printk(KERN_ALERT "LustreError: dumping log to %s\n",
397 cfs_tracefile_dump_all_pages(debug_file_name);
398 libcfs_run_debug_log_upcall(debug_file_name);
400 current->journal_info = journal_info;
403 static int libcfs_debug_dumplog_thread(void *arg)
405 libcfs_debug_dumplog_internal(arg);
406 wake_up(&debug_ctlwq);
410 void libcfs_debug_dumplog(void)
412 wait_queue_entry_t wait;
413 struct task_struct *dumper;
416 /* we're being careful to ensure that the kernel thread is
417 * able to set our state to running as it exits before we
418 * get to schedule() */
419 init_waitqueue_entry(&wait, current);
420 set_current_state(TASK_INTERRUPTIBLE);
421 add_wait_queue(&debug_ctlwq, &wait);
423 dumper = kthread_run(libcfs_debug_dumplog_thread,
424 (void *)(long)current_pid(),
425 "libcfs_debug_dumper");
427 printk(KERN_ERR "LustreError: cannot start log dump thread:"
428 " %ld\n", PTR_ERR(dumper));
432 /* be sure to teardown if cfs_create_thread() failed */
433 remove_wait_queue(&debug_ctlwq, &wait);
434 set_current_state(TASK_RUNNING);
436 EXPORT_SYMBOL(libcfs_debug_dumplog);
438 int libcfs_debug_init(unsigned long bufsize)
441 unsigned int max = libcfs_debug_mb;
443 init_waitqueue_head(&debug_ctlwq);
445 if (libcfs_console_max_delay <= 0 || /* not set by user or */
446 libcfs_console_min_delay <= 0 || /* set to invalid values */
447 libcfs_console_min_delay >= libcfs_console_max_delay) {
448 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
449 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
452 if (libcfs_debug_file_path != NULL) {
453 strlcpy(libcfs_debug_file_path_arr,
454 libcfs_debug_file_path,
455 sizeof(libcfs_debug_file_path_arr));
458 /* If libcfs_debug_mb is set to an invalid value or uninitialized
459 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
460 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
463 max = (max / num_possible_cpus());
464 max = (max << (20 - PAGE_SHIFT));
467 rc = cfs_tracefile_init(max);
471 libcfs_register_panic_notifier();
472 kernel_param_lock(THIS_MODULE);
473 libcfs_debug_mb = cfs_trace_get_debug_mb();
474 kernel_param_unlock(THIS_MODULE);
478 int libcfs_debug_cleanup(void)
480 libcfs_unregister_panic_notifier();
481 kernel_param_lock(THIS_MODULE);
482 cfs_tracefile_exit();
483 kernel_param_unlock(THIS_MODULE);
487 int libcfs_debug_clear_buffer(void)
489 cfs_trace_flush_pages();
493 /* Debug markers, although printed by S_LNET
494 * should not be be marked as such. */
495 #undef DEBUG_SUBSYSTEM
496 #define DEBUG_SUBSYSTEM S_UNDEFINED
497 int libcfs_debug_mark_buffer(const char *text)
499 CDEBUG(D_TRACE,"***************************************************\n");
500 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
501 CDEBUG(D_TRACE,"***************************************************\n");
505 #undef DEBUG_SUBSYSTEM
506 #define DEBUG_SUBSYSTEM S_LNET
508 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
510 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
514 EXPORT_SYMBOL(libcfs_log_return);
516 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
519 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
520 " : %#lx)\n", label, rc, rc, rc);
522 EXPORT_SYMBOL(libcfs_log_goto);