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, 2012, 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 "../../include/linux/libcfs/libcfs.h"
41 #include "tracefile.h"
43 static char debug_file_name[1024];
45 unsigned int libcfs_subsystem_debug = ~0;
46 EXPORT_SYMBOL(libcfs_subsystem_debug);
47 module_param(libcfs_subsystem_debug, int, 0644);
48 MODULE_PARM_DESC(libcfs_subsystem_debug, "Lustre kernel debug subsystem mask");
50 unsigned int libcfs_debug = (D_CANTMASK |
51 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
52 EXPORT_SYMBOL(libcfs_debug);
53 module_param(libcfs_debug, int, 0644);
54 MODULE_PARM_DESC(libcfs_debug, "Lustre kernel debug mask");
56 static int libcfs_param_debug_mb_set(const char *val,
57 const struct kernel_param *kp)
62 rc = kstrtouint(val, 0, &num);
66 if (!*((unsigned int *)kp->arg)) {
67 *((unsigned int *)kp->arg) = num;
71 rc = cfs_trace_set_debug_mb(num);
74 *((unsigned int *)kp->arg) = cfs_trace_get_debug_mb();
79 /* While debug_mb setting look like unsigned int, in fact
80 * it needs quite a bunch of extra processing, so we define special
81 * debugmb parameter type with corresponding methods to handle this case
83 static struct kernel_param_ops param_ops_debugmb = {
84 .set = libcfs_param_debug_mb_set,
85 .get = param_get_uint,
88 #define param_check_debugmb(name, p) \
89 __param_check(name, p, unsigned int)
91 static unsigned int libcfs_debug_mb;
92 module_param(libcfs_debug_mb, debugmb, 0644);
93 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
95 unsigned int libcfs_printk = D_CANTMASK;
96 module_param(libcfs_printk, uint, 0644);
97 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
99 unsigned int libcfs_console_ratelimit = 1;
100 module_param(libcfs_console_ratelimit, uint, 0644);
101 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
103 static int param_set_delay_minmax(const char *val,
104 const struct kernel_param *kp,
111 rc = kstrtoint(val, 0, &sec);
115 d = cfs_time_seconds(sec) / 100;
116 if (d < min || d > max)
119 *((unsigned int *)kp->arg) = d;
124 static int param_get_delay(char *buffer, const struct kernel_param *kp)
126 unsigned int d = *(unsigned int *)kp->arg;
128 return sprintf(buffer, "%u", (unsigned int)cfs_duration_sec(d * 100));
131 unsigned int libcfs_console_max_delay;
132 unsigned int libcfs_console_min_delay;
134 static int param_set_console_max_delay(const char *val,
135 const struct kernel_param *kp)
137 return param_set_delay_minmax(val, kp,
138 libcfs_console_min_delay, INT_MAX);
141 static struct kernel_param_ops param_ops_console_max_delay = {
142 .set = param_set_console_max_delay,
143 .get = param_get_delay,
146 #define param_check_console_max_delay(name, p) \
147 __param_check(name, p, unsigned int)
149 module_param(libcfs_console_max_delay, console_max_delay, 0644);
150 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
152 static int param_set_console_min_delay(const char *val,
153 const struct kernel_param *kp)
155 return param_set_delay_minmax(val, kp,
156 1, libcfs_console_max_delay);
159 static struct kernel_param_ops param_ops_console_min_delay = {
160 .set = param_set_console_min_delay,
161 .get = param_get_delay,
164 #define param_check_console_min_delay(name, p) \
165 __param_check(name, p, unsigned int)
167 module_param(libcfs_console_min_delay, console_min_delay, 0644);
168 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
170 static int param_set_uint_minmax(const char *val,
171 const struct kernel_param *kp,
172 unsigned int min, unsigned int max)
179 ret = kstrtouint(val, 0, &num);
180 if (ret < 0 || num < min || num > max)
182 *((unsigned int *)kp->arg) = num;
186 static int param_set_uintpos(const char *val, const struct kernel_param *kp)
188 return param_set_uint_minmax(val, kp, 1, -1);
191 static struct kernel_param_ops param_ops_uintpos = {
192 .set = param_set_uintpos,
193 .get = param_get_uint,
196 #define param_check_uintpos(name, p) \
197 __param_check(name, p, unsigned int)
199 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
200 module_param(libcfs_console_backoff, uintpos, 0644);
201 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
203 unsigned int libcfs_debug_binary = 1;
205 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
206 EXPORT_SYMBOL(libcfs_stack);
208 unsigned int libcfs_catastrophe;
209 EXPORT_SYMBOL(libcfs_catastrophe);
211 unsigned int libcfs_panic_on_lbug = 1;
212 module_param(libcfs_panic_on_lbug, uint, 0644);
213 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
215 static wait_queue_head_t debug_ctlwq;
217 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
219 /* We need to pass a pointer here, but elsewhere this must be a const */
220 static char *libcfs_debug_file_path;
221 module_param(libcfs_debug_file_path, charp, 0644);
222 MODULE_PARM_DESC(libcfs_debug_file_path,
223 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
225 int libcfs_panic_in_progress;
227 /* libcfs_debug_token2mask() expects the returned string in lower-case */
229 libcfs_debug_subsys2str(int subsys)
231 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
233 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
236 return libcfs_debug_subsystems[subsys];
239 /* libcfs_debug_token2mask() expects the returned string in lower-case */
241 libcfs_debug_dbg2str(int debug)
243 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
245 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
248 return libcfs_debug_masks[debug];
252 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
254 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
255 libcfs_debug_dbg2str;
260 if (mask == 0) { /* "0" */
264 } else { /* space-separated tokens */
265 for (i = 0; i < 32; i++) {
266 if ((mask & (1 << i)) == 0)
270 if (!token) /* unused bit */
273 if (len > 0) { /* separator? */
279 while (*token != 0) {
288 /* terminate 'str' */
298 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
300 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
301 libcfs_debug_dbg2str;
307 /* Allow a number for backwards compatibility */
309 for (n = strlen(str); n > 0; n--)
310 if (!isspace(str[n - 1]))
313 t = sscanf(str, "%i%n", &m, &matched);
314 if (t >= 1 && matched == n) {
315 /* don't print warning for lctl set_param debug=0 or -1 */
316 if (m != 0 && m != -1)
317 CWARN("You are trying to use a numerical value for the mask - this will be deprecated in a future release.\n");
322 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
327 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
329 void libcfs_debug_dumplog_internal(void *arg)
331 static time64_t last_dump_time;
332 time64_t current_time;
335 journal_info = current->journal_info;
336 current->journal_info = NULL;
337 current_time = ktime_get_real_seconds();
339 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) &&
340 current_time > last_dump_time) {
341 last_dump_time = current_time;
342 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
343 "%s.%lld.%ld", libcfs_debug_file_path_arr,
344 (s64)current_time, (long_ptr_t)arg);
345 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
346 cfs_tracefile_dump_all_pages(debug_file_name);
347 libcfs_run_debug_log_upcall(debug_file_name);
350 current->journal_info = journal_info;
353 static int libcfs_debug_dumplog_thread(void *arg)
355 libcfs_debug_dumplog_internal(arg);
356 wake_up(&debug_ctlwq);
360 void libcfs_debug_dumplog(void)
363 struct task_struct *dumper;
365 /* we're being careful to ensure that the kernel thread is
366 * able to set our state to running as it exits before we
369 init_waitqueue_entry(&wait, current);
370 add_wait_queue(&debug_ctlwq, &wait);
372 dumper = kthread_run(libcfs_debug_dumplog_thread,
373 (void *)(long)current_pid(),
374 "libcfs_debug_dumper");
375 set_current_state(TASK_INTERRUPTIBLE);
377 pr_err("LustreError: cannot start log dump thread: %ld\n",
382 /* be sure to teardown if cfs_create_thread() failed */
383 remove_wait_queue(&debug_ctlwq, &wait);
384 set_current_state(TASK_RUNNING);
386 EXPORT_SYMBOL(libcfs_debug_dumplog);
388 int libcfs_debug_init(unsigned long bufsize)
391 unsigned int max = libcfs_debug_mb;
393 init_waitqueue_head(&debug_ctlwq);
395 if (libcfs_console_max_delay <= 0 || /* not set by user or */
396 libcfs_console_min_delay <= 0 || /* set to invalid values */
397 libcfs_console_min_delay >= libcfs_console_max_delay) {
398 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
399 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
402 if (libcfs_debug_file_path) {
403 strlcpy(libcfs_debug_file_path_arr,
404 libcfs_debug_file_path,
405 sizeof(libcfs_debug_file_path_arr));
408 /* If libcfs_debug_mb is set to an invalid value or uninitialized
409 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES
411 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
414 max = max / num_possible_cpus();
415 max <<= (20 - PAGE_SHIFT);
417 rc = cfs_tracefile_init(max);
420 libcfs_register_panic_notifier();
421 libcfs_debug_mb = cfs_trace_get_debug_mb();
427 int libcfs_debug_cleanup(void)
429 libcfs_unregister_panic_notifier();
430 cfs_tracefile_exit();
434 int libcfs_debug_clear_buffer(void)
436 cfs_trace_flush_pages();
440 /* Debug markers, although printed by S_LNET should not be be marked as such. */
441 #undef DEBUG_SUBSYSTEM
442 #define DEBUG_SUBSYSTEM S_UNDEFINED
443 int libcfs_debug_mark_buffer(const char *text)
446 "***************************************************\n");
447 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
449 "***************************************************\n");
454 #undef DEBUG_SUBSYSTEM
455 #define DEBUG_SUBSYSTEM S_LNET