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/libcfs.h>
43 #include "tracefile.h"
45 static char debug_file_name[1024];
47 unsigned int libcfs_subsystem_debug = ~0;
48 module_param(libcfs_subsystem_debug, int, 0644);
49 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
50 EXPORT_SYMBOL(libcfs_subsystem_debug);
52 unsigned int libcfs_debug = (D_CANTMASK | D_NETERROR | D_HA | D_CONFIG |
53 D_IOCTL | D_LFSCK | D_TTY);
54 module_param(libcfs_debug, int, 0644);
55 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
56 EXPORT_SYMBOL(libcfs_debug);
58 static int libcfs_param_debug_mb_set(const char *val,
59 cfs_kernel_param_arg_t *kp)
64 rc = kstrtouint(val, 0, &num);
69 * RHEL6 does not support any kind of locking so we have to provide
72 if (!*((unsigned int *)kp->arg)) {
73 *((unsigned int *)kp->arg) = num;
77 rc = cfs_trace_set_debug_mb(num);
80 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
86 * While debug_mb setting look like unsigned int, in fact
87 * it needs quite a bunch of extra processing, so we define special
88 * debug_mb parameter type with corresponding methods to handle this case
90 static struct kernel_param_ops param_ops_debug_mb = {
91 .set = libcfs_param_debug_mb_set,
92 .get = param_get_uint,
95 #define param_check_debug_mb(name, p) \
96 __param_check(name, p, unsigned int)
98 static unsigned int libcfs_debug_mb;
99 #ifdef HAVE_KERNEL_PARAM_OPS
100 module_param(libcfs_debug_mb, debug_mb, 0644);
102 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
103 ¶m_ops_debug_mb, 0644);
105 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
107 unsigned int libcfs_printk = D_CANTMASK;
108 module_param(libcfs_printk, uint, 0644);
109 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
111 unsigned int libcfs_console_ratelimit = 1;
112 module_param(libcfs_console_ratelimit, uint, 0644);
113 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
115 static int param_set_delay_minmax(const char *val,
116 cfs_kernel_param_arg_t *kp,
123 rc = kstrtoint(val, 0, &sec);
127 /* The sysfs setting is in centiseconds */
128 d = cfs_time_seconds(sec) / 100;
129 if (d < min || d > max)
132 *((unsigned int *)kp->arg) = d;
137 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
139 unsigned int d = *(unsigned int *)kp->arg;
141 return sprintf(buffer, "%lu", jiffies_to_msecs(d * 10) / MSEC_PER_SEC);
144 unsigned int libcfs_console_max_delay;
145 unsigned int libcfs_console_min_delay;
147 static int param_set_console_max_delay(const char *val,
148 cfs_kernel_param_arg_t *kp)
150 return param_set_delay_minmax(val, kp,
151 libcfs_console_min_delay, INT_MAX);
154 static struct kernel_param_ops param_ops_console_max_delay = {
155 .set = param_set_console_max_delay,
156 .get = param_get_delay,
159 #define param_check_console_max_delay(name, p) \
160 __param_check(name, p, unsigned int)
162 #ifdef HAVE_KERNEL_PARAM_OPS
163 module_param(libcfs_console_max_delay, console_max_delay, 0644);
165 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
166 param_get_delay, ¶m_ops_console_max_delay, 0644);
168 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
170 static int param_set_console_min_delay(const char *val,
171 cfs_kernel_param_arg_t *kp)
173 return param_set_delay_minmax(val, kp,
174 1, libcfs_console_max_delay);
177 static struct kernel_param_ops param_ops_console_min_delay = {
178 .set = param_set_console_min_delay,
179 .get = param_get_delay,
182 #define param_check_console_min_delay(name, p) \
183 __param_check(name, p, unsigned int)
185 #ifdef HAVE_KERNEL_PARAM_OPS
186 module_param(libcfs_console_min_delay, console_min_delay, 0644);
188 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
189 param_get_delay, ¶m_ops_console_min_delay, 0644);
191 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
193 static int param_set_uint_minmax(const char *val,
194 cfs_kernel_param_arg_t *kp,
195 unsigned int min, unsigned int max)
203 ret = kstrtouint(val, 0, &num);
204 if (ret < 0 || num < min || num > max)
207 *((unsigned int *)kp->arg) = num;
211 static int param_set_uintpos(const char *val,
212 cfs_kernel_param_arg_t *kp)
214 return param_set_uint_minmax(val, kp, 1, -1);
217 static struct kernel_param_ops param_ops_uintpos = {
218 .set = param_set_uintpos,
219 .get = param_get_uint,
222 #define param_check_uintpos(name, p) \
223 __param_check(name, p, unsigned int)
225 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
226 #ifdef HAVE_KERNEL_PARAM_OPS
227 module_param(libcfs_console_backoff, uintpos, 0644);
229 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
230 ¶m_ops_uintpos, 0644);
232 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
234 unsigned int libcfs_debug_binary = 1;
236 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
237 EXPORT_SYMBOL(libcfs_stack);
239 unsigned int libcfs_catastrophe;
240 EXPORT_SYMBOL(libcfs_catastrophe);
242 unsigned int libcfs_watchdog_ratelimit = 300;
243 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
245 unsigned int libcfs_panic_on_lbug = 1;
246 module_param(libcfs_panic_on_lbug, uint, 0644);
247 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
249 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
250 EXPORT_SYMBOL(libcfs_kmemory);
252 static DECLARE_COMPLETION(debug_complete);
254 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
255 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
257 /* We need to pass a pointer here, but elsewhere this must be a const */
258 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
259 module_param(libcfs_debug_file_path, charp, 0644);
260 MODULE_PARM_DESC(libcfs_debug_file_path,
261 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
263 int libcfs_panic_in_progress;
266 * libcfs_debug_token2mask() expects the returned
267 * string in lower-case
269 static const char *libcfs_debug_subsys2str(int subsys)
271 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
273 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
276 return libcfs_debug_subsystems[subsys];
280 * libcfs_debug_token2mask() expects the returned
281 * string in lower-case
283 static const char *libcfs_debug_dbg2str(int debug)
285 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
287 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
290 return libcfs_debug_masks[debug];
294 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
296 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
297 libcfs_debug_dbg2str;
302 if (mask == 0) { /* "0" */
306 } else { /* space-separated tokens */
307 for (i = 0; i < 32; i++) {
308 if ((mask & (1 << i)) == 0)
312 if (token == NULL) /* unused bit */
315 if (len > 0) { /* separator? */
321 while (*token != 0) {
330 /* terminate 'str' */
340 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
342 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
343 libcfs_debug_dbg2str;
349 /* Allow a number for backwards compatibility */
350 for (n = strlen(str); n > 0; n--)
351 if (!isspace(str[n-1]))
355 t = sscanf(str, "%i%n", &m, &matched);
356 if (t >= 1 && matched == n) {
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 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
390 cfs_tracefile_dump_all_pages(debug_file_name);
391 libcfs_run_debug_log_upcall(debug_file_name);
393 current->journal_info = journal_info;
396 static int libcfs_debug_dumplog_thread(void *arg)
398 libcfs_debug_dumplog_internal(arg);
399 complete(&debug_complete);
403 void libcfs_debug_dumplog(void)
405 struct task_struct *dumper;
408 /* If a previous call was interrupted, debug_complete->done
409 * might be elevated, and so we won't actually wait here.
410 * So we reinit the completion to ensure we wait for
411 * one thread to complete, though it might not be the one
412 * we start if there are overlaping thread.
414 reinit_completion(&debug_complete);
415 dumper = kthread_run(libcfs_debug_dumplog_thread,
416 (void *)(long)current_pid(),
417 "libcfs_debug_dumper");
419 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
422 wait_for_completion_interruptible(&debug_complete);
424 EXPORT_SYMBOL(libcfs_debug_dumplog);
426 int libcfs_debug_init(unsigned long bufsize)
429 unsigned int max = libcfs_debug_mb;
431 if (libcfs_console_max_delay <= 0 || /* not set by user or */
432 libcfs_console_min_delay <= 0 || /* set to invalid values */
433 libcfs_console_min_delay >= libcfs_console_max_delay) {
434 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
435 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
438 if (libcfs_debug_file_path != NULL) {
439 strlcpy(libcfs_debug_file_path_arr,
440 libcfs_debug_file_path,
441 sizeof(libcfs_debug_file_path_arr));
445 * If libcfs_debug_mb is set to an invalid value or uninitialized
446 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES
448 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
451 max = (max / num_possible_cpus());
452 max = (max << (20 - PAGE_SHIFT));
455 rc = cfs_tracefile_init(max);
459 libcfs_register_panic_notifier();
460 kernel_param_lock(THIS_MODULE);
461 if (libcfs_debug_mb == 0)
462 libcfs_debug_mb = cfs_trace_get_debug_mb();
463 kernel_param_unlock(THIS_MODULE);
467 int libcfs_debug_cleanup(void)
469 libcfs_unregister_panic_notifier();
470 kernel_param_lock(THIS_MODULE);
471 cfs_tracefile_exit();
472 kernel_param_unlock(THIS_MODULE);
476 int libcfs_debug_clear_buffer(void)
478 cfs_trace_flush_pages();
483 * Debug markers, although printed by S_LNET
484 * should not be be marked as such.
486 #undef DEBUG_SUBSYSTEM
487 #define DEBUG_SUBSYSTEM S_UNDEFINED
488 int libcfs_debug_mark_buffer(const char *text)
490 CDEBUG(D_TRACE, "**************************************************\n");
491 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
492 CDEBUG(D_TRACE, "**************************************************\n");
496 #undef DEBUG_SUBSYSTEM
497 #define DEBUG_SUBSYSTEM S_LNET
499 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
501 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
505 EXPORT_SYMBOL(libcfs_log_return);
507 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
510 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
511 " : %#lx)\n", label, rc, rc, rc);
513 EXPORT_SYMBOL(libcfs_log_goto);