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 unsigned int libcfs_debug_mb;
58 module_param(libcfs_debug_mb, uint, 0644);
59 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
61 unsigned int libcfs_printk = D_CANTMASK;
62 module_param(libcfs_printk, uint, 0644);
63 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
65 unsigned int libcfs_console_ratelimit = 1;
66 module_param(libcfs_console_ratelimit, uint, 0644);
67 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
69 unsigned int libcfs_console_max_delay;
70 module_param(libcfs_console_max_delay, uint, 0644);
71 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
73 unsigned int libcfs_console_min_delay;
74 module_param(libcfs_console_min_delay, uint, 0644);
75 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
77 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
78 module_param(libcfs_console_backoff, uint, 0644);
79 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
81 unsigned int libcfs_debug_binary = 1;
83 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
84 EXPORT_SYMBOL(libcfs_stack);
86 unsigned int libcfs_catastrophe;
87 EXPORT_SYMBOL(libcfs_catastrophe);
89 unsigned int libcfs_watchdog_ratelimit = 300;
91 unsigned int libcfs_panic_on_lbug = 1;
92 module_param(libcfs_panic_on_lbug, uint, 0644);
93 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
95 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
96 EXPORT_SYMBOL(libcfs_kmemory);
98 static wait_queue_head_t debug_ctlwq;
100 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
102 /* We need to pass a pointer here, but elsewhere this must be a const */
103 static char *libcfs_debug_file_path;
104 module_param(libcfs_debug_file_path, charp, 0644);
105 MODULE_PARM_DESC(libcfs_debug_file_path,
106 "Path for dumping debug logs, set 'NONE' to prevent log dumping");
108 int libcfs_panic_in_progress;
110 /* libcfs_debug_token2mask() expects the returned
111 * string in lower-case */
112 static const char *libcfs_debug_subsys2str(int subsys)
114 static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
116 if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
119 return libcfs_debug_subsystems[subsys];
122 /* libcfs_debug_token2mask() expects the returned
123 * string in lower-case */
124 static const char *libcfs_debug_dbg2str(int debug)
126 static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
128 if (debug >= ARRAY_SIZE(libcfs_debug_masks))
131 return libcfs_debug_masks[debug];
135 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
137 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
138 libcfs_debug_dbg2str;
143 if (mask == 0) { /* "0" */
147 } else { /* space-separated tokens */
148 for (i = 0; i < 32; i++) {
149 if ((mask & (1 << i)) == 0)
153 if (token == NULL) /* unused bit */
156 if (len > 0) { /* separator? */
162 while (*token != 0) {
171 /* terminate 'str' */
181 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
183 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
184 libcfs_debug_dbg2str;
190 /* Allow a number for backwards compatibility */
192 for (n = strlen(str); n > 0; n--)
193 if (!isspace(str[n-1]))
197 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
199 /* don't print warning for lctl set_param debug=0 or -1 */
200 if (m != 0 && m != -1)
201 CWARN("You are trying to use a numerical value for the "
202 "mask - this will be deprecated in a future "
208 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
213 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
215 void libcfs_debug_dumplog_internal(void *arg)
217 static time64_t last_dump_time;
218 time64_t current_time;
221 journal_info = current->journal_info;
222 current->journal_info = NULL;
223 current_time = ktime_get_real_seconds();
225 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
226 current_time > last_dump_time) {
227 last_dump_time = current_time;
228 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
229 "%s.%lld.%ld", libcfs_debug_file_path_arr,
230 (s64)current_time, (uintptr_t)arg);
231 printk(KERN_ALERT "LustreError: dumping log to %s\n",
233 cfs_tracefile_dump_all_pages(debug_file_name);
234 libcfs_run_debug_log_upcall(debug_file_name);
236 current->journal_info = journal_info;
239 static int libcfs_debug_dumplog_thread(void *arg)
241 libcfs_debug_dumplog_internal(arg);
242 wake_up(&debug_ctlwq);
246 void libcfs_debug_dumplog(void)
249 struct task_struct *dumper;
252 /* we're being careful to ensure that the kernel thread is
253 * able to set our state to running as it exits before we
254 * get to schedule() */
255 init_waitqueue_entry(&wait, current);
256 set_current_state(TASK_INTERRUPTIBLE);
257 add_wait_queue(&debug_ctlwq, &wait);
259 dumper = kthread_run(libcfs_debug_dumplog_thread,
260 (void *)(long)current_pid(),
261 "libcfs_debug_dumper");
263 printk(KERN_ERR "LustreError: cannot start log dump thread:"
264 " %ld\n", PTR_ERR(dumper));
268 /* be sure to teardown if cfs_create_thread() failed */
269 remove_wait_queue(&debug_ctlwq, &wait);
270 set_current_state(TASK_RUNNING);
272 EXPORT_SYMBOL(libcfs_debug_dumplog);
274 int libcfs_debug_init(unsigned long bufsize)
277 unsigned int max = libcfs_debug_mb;
279 init_waitqueue_head(&debug_ctlwq);
281 if (libcfs_console_max_delay <= 0 || /* not set by user or */
282 libcfs_console_min_delay <= 0 || /* set to invalid values */
283 libcfs_console_min_delay >= libcfs_console_max_delay) {
284 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
285 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
288 if (libcfs_debug_file_path != NULL) {
289 strlcpy(libcfs_debug_file_path_arr,
290 libcfs_debug_file_path,
291 sizeof(libcfs_debug_file_path_arr));
294 /* If libcfs_debug_mb is set to an invalid value or uninitialized
295 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
296 if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
299 max = (max / num_possible_cpus());
300 max = (max << (20 - PAGE_SHIFT));
302 rc = cfs_tracefile_init(max);
305 libcfs_register_panic_notifier();
310 int libcfs_debug_cleanup(void)
312 libcfs_unregister_panic_notifier();
313 cfs_tracefile_exit();
317 int libcfs_debug_clear_buffer(void)
319 cfs_trace_flush_pages();
323 /* Debug markers, although printed by S_LNET
324 * should not be be marked as such. */
325 #undef DEBUG_SUBSYSTEM
326 #define DEBUG_SUBSYSTEM S_UNDEFINED
327 int libcfs_debug_mark_buffer(const char *text)
329 CDEBUG(D_TRACE,"***************************************************\n");
330 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
331 CDEBUG(D_TRACE,"***************************************************\n");
335 #undef DEBUG_SUBSYSTEM
336 #define DEBUG_SUBSYSTEM S_LNET
338 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
340 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
344 EXPORT_SYMBOL(libcfs_log_return);
346 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
349 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
350 " : %#lx)\n", label, rc, rc, rc);
352 EXPORT_SYMBOL(libcfs_log_goto);