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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/debug.c
38 * Author: Phil Schwan <phil@clusterfs.com>
42 # define DEBUG_SUBSYSTEM S_LNET
44 #include <libcfs/libcfs.h>
45 #include "tracefile.h"
47 static char debug_file_name[1024];
49 unsigned int libcfs_subsystem_debug = ~0;
50 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
51 "Lustre kernel debug subsystem mask");
52 EXPORT_SYMBOL(libcfs_subsystem_debug);
54 unsigned int libcfs_debug = (D_CANTMASK |
55 D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
56 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
57 "Lustre kernel debug mask");
58 EXPORT_SYMBOL(libcfs_debug);
60 unsigned int libcfs_debug_mb = 0;
61 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
62 "Total debug buffer size.");
63 EXPORT_SYMBOL(libcfs_debug_mb);
65 unsigned int libcfs_printk = D_CANTMASK;
66 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
67 "Lustre kernel debug console mask");
68 EXPORT_SYMBOL(libcfs_printk);
70 unsigned int libcfs_console_ratelimit = 1;
71 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
72 "Lustre kernel debug console ratelimit (0 to disable)");
73 EXPORT_SYMBOL(libcfs_console_ratelimit);
75 unsigned int libcfs_console_max_delay;
76 CFS_MODULE_PARM(libcfs_console_max_delay, "l", uint, 0644,
77 "Lustre kernel debug console max delay (jiffies)");
78 EXPORT_SYMBOL(libcfs_console_max_delay);
80 unsigned int libcfs_console_min_delay;
81 CFS_MODULE_PARM(libcfs_console_min_delay, "l", uint, 0644,
82 "Lustre kernel debug console min delay (jiffies)");
83 EXPORT_SYMBOL(libcfs_console_min_delay);
85 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
86 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
87 "Lustre kernel debug console backoff factor");
88 EXPORT_SYMBOL(libcfs_console_backoff);
90 unsigned int libcfs_debug_binary = 1;
91 EXPORT_SYMBOL(libcfs_debug_binary);
93 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
94 EXPORT_SYMBOL(libcfs_stack);
96 unsigned int portal_enter_debugger;
97 EXPORT_SYMBOL(portal_enter_debugger);
99 unsigned int libcfs_catastrophe;
100 EXPORT_SYMBOL(libcfs_catastrophe);
102 unsigned int libcfs_watchdog_ratelimit = 300;
103 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
105 unsigned int libcfs_panic_on_lbug = 1;
106 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
107 "Lustre kernel panic on LBUG");
108 EXPORT_SYMBOL(libcfs_panic_on_lbug);
110 cfs_atomic_t libcfs_kmemory = CFS_ATOMIC_INIT(0);
111 EXPORT_SYMBOL(libcfs_kmemory);
113 static cfs_waitq_t debug_ctlwq;
115 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
117 /* We need to pass a pointer here, but elsewhere this must be a const */
118 char *libcfs_debug_file_path;
119 CFS_MODULE_PARM(libcfs_debug_file_path, "s", charp, 0644,
120 "Path for dumping debug logs, "
121 "set 'NONE' to prevent log dumping");
123 int libcfs_panic_in_progress;
125 /* libcfs_debug_token2mask() expects the returned
126 * string in lower-case */
128 libcfs_debug_subsys2str(int subsys)
130 switch (1 << subsys) {
184 /* libcfs_debug_token2mask() expects the returned
185 * string in lower-case */
187 libcfs_debug_dbg2str(int debug)
189 switch (1 << debug) {
254 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
256 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
257 libcfs_debug_dbg2str;
262 if (mask == 0) { /* "0" */
266 } else { /* space-separated tokens */
267 for (i = 0; i < 32; i++) {
268 if ((mask & (1 << i)) == 0)
272 if (token == NULL) /* unused bit */
275 if (len > 0) { /* separator? */
281 while (*token != 0) {
290 /* terminate 'str' */
300 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
302 const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
303 libcfs_debug_dbg2str;
309 /* Allow a number for backwards compatibility */
311 for (n = strlen(str); n > 0; n--)
312 if (!isspace(str[n-1]))
316 if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
318 /* don't print warning for lctl set_param debug=0 or -1 */
319 if (m != 0 && m != -1)
320 CWARN("You are trying to use a numerical value for the "
321 "mask - this will be deprecated in a future "
327 return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
332 * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
334 void libcfs_debug_dumplog_internal(void *arg)
336 CFS_DECL_JOURNAL_DATA;
340 if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
341 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
342 "%s.%ld." LPLD, libcfs_debug_file_path_arr,
343 cfs_time_current_sec(), (long_ptr_t)arg);
344 printk(CFS_KERN_ALERT "LustreError: dumping log to %s\n",
346 cfs_tracefile_dump_all_pages(debug_file_name);
347 libcfs_run_debug_log_upcall(debug_file_name);
352 int libcfs_debug_dumplog_thread(void *arg)
354 libcfs_debug_dumplog_internal(arg);
355 cfs_waitq_signal(&debug_ctlwq);
359 void libcfs_debug_dumplog(void)
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
367 * get to schedule() */
368 cfs_waitlink_init(&wait);
369 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
370 cfs_waitq_add(&debug_ctlwq, &wait);
372 dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
373 (void*)(long)cfs_curproc_pid(),
374 "libcfs_debug_dumper");
376 printk(CFS_KERN_ERR "LustreError: cannot start log dump thread:"
377 " %ld\n", PTR_ERR(dumper));
379 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
381 /* be sure to teardown if cfs_create_thread() failed */
382 cfs_waitq_del(&debug_ctlwq, &wait);
383 cfs_set_current_state(CFS_TASK_RUNNING);
385 EXPORT_SYMBOL(libcfs_debug_dumplog);
387 int libcfs_debug_init(unsigned long bufsize)
390 unsigned int max = libcfs_debug_mb;
392 cfs_waitq_init(&debug_ctlwq);
394 if (libcfs_console_max_delay <= 0 || /* not set by user or */
395 libcfs_console_min_delay <= 0 || /* set to invalid values */
396 libcfs_console_min_delay >= libcfs_console_max_delay) {
397 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
398 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
401 if (libcfs_debug_file_path != NULL) {
402 memset(libcfs_debug_file_path_arr, 0, PATH_MAX);
403 strncpy(libcfs_debug_file_path_arr,
404 libcfs_debug_file_path, PATH_MAX-1);
407 /* If libcfs_debug_mb is set to an invalid value or uninitialized
408 * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
409 if (max > cfs_trace_max_debug_mb() || max < cfs_num_possible_cpus()) {
412 max = (max / cfs_num_possible_cpus());
413 max = (max << (20 - CFS_PAGE_SHIFT));
415 rc = cfs_tracefile_init(max);
418 libcfs_register_panic_notifier();
423 int libcfs_debug_cleanup(void)
425 libcfs_unregister_panic_notifier();
426 cfs_tracefile_exit();
430 int libcfs_debug_clear_buffer(void)
432 cfs_trace_flush_pages();
436 /* Debug markers, although printed by S_LNET
437 * should not be be marked as such. */
438 #undef DEBUG_SUBSYSTEM
439 #define DEBUG_SUBSYSTEM S_UNDEFINED
440 int libcfs_debug_mark_buffer(const char *text)
442 CDEBUG(D_TRACE,"***************************************************\n");
443 LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
444 CDEBUG(D_TRACE,"***************************************************\n");
448 #undef DEBUG_SUBSYSTEM
449 #define DEBUG_SUBSYSTEM S_LNET
451 void libcfs_debug_set_level(unsigned int debug_level)
453 printk(CFS_KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
455 libcfs_debug = debug_level;
458 EXPORT_SYMBOL(libcfs_debug_set_level);
460 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
462 libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
466 EXPORT_SYMBOL(libcfs_log_return);
468 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
471 libcfs_debug_msg(msgdata, "Process leaving via %s (rc=" LPLU " : " LPLD
472 " : " LPLX ")\n", label, (ulong_ptr_t)rc, rc, rc);
474 EXPORT_SYMBOL(libcfs_log_goto);