Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[fs/lustre-release.git] / libcfs / libcfs / debug.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/debug.c
33  *
34  * Author: Phil Schwan <phil@clusterfs.com>
35  *
36  */
37
38 # define DEBUG_SUBSYSTEM S_LNET
39
40 #include <linux/kthread.h>
41 #include <libcfs/libcfs.h>
42 #include "tracefile.h"
43
44 static char debug_file_name[1024];
45
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);
50
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);
56
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.");
60
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");
64
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)");
68
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)");
72
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)");
76
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");
80
81 unsigned int libcfs_debug_binary = 1;
82
83 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
84 EXPORT_SYMBOL(libcfs_stack);
85
86 unsigned int libcfs_catastrophe;
87 EXPORT_SYMBOL(libcfs_catastrophe);
88
89 unsigned int libcfs_watchdog_ratelimit = 300;
90
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");
94
95 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
96 EXPORT_SYMBOL(libcfs_kmemory);
97
98 static wait_queue_head_t debug_ctlwq;
99
100 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
101
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");
107
108 int libcfs_panic_in_progress;
109
110 /* libcfs_debug_token2mask() expects the returned
111  * string in lower-case */
112 static const char *libcfs_debug_subsys2str(int subsys)
113 {
114         static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
115
116         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
117                 return NULL;
118
119         return libcfs_debug_subsystems[subsys];
120 }
121
122 /* libcfs_debug_token2mask() expects the returned
123  * string in lower-case */
124 static const char *libcfs_debug_dbg2str(int debug)
125 {
126         static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
127
128         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
129                 return NULL;
130
131         return libcfs_debug_masks[debug];
132 }
133
134 int
135 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
136 {
137         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
138                                                  libcfs_debug_dbg2str;
139         int           len = 0;
140         const char   *token;
141         int           i;
142
143         if (mask == 0) {                        /* "0" */
144                 if (size > 0)
145                         str[0] = '0';
146                 len = 1;
147         } else {                                /* space-separated tokens */
148                 for (i = 0; i < 32; i++) {
149                         if ((mask & (1 << i)) == 0)
150                                 continue;
151
152                         token = fn(i);
153                         if (token == NULL)              /* unused bit */
154                                 continue;
155
156                         if (len > 0) {                  /* separator? */
157                                 if (len < size)
158                                         str[len] = ' ';
159                                 len++;
160                         }
161
162                         while (*token != 0) {
163                                 if (len < size)
164                                         str[len] = *token;
165                                 token++;
166                                 len++;
167                         }
168                 }
169         }
170
171         /* terminate 'str' */
172         if (len < size)
173                 str[len] = 0;
174         else
175                 str[size - 1] = 0;
176
177         return len;
178 }
179
180 int
181 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
182 {
183         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
184                                                  libcfs_debug_dbg2str;
185         int         m = 0;
186         int         matched;
187         int         n;
188         int         t;
189
190         /* Allow a number for backwards compatibility */
191
192         for (n = strlen(str); n > 0; n--)
193                 if (!isspace(str[n-1]))
194                         break;
195         matched = n;
196
197         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
198             matched == n) {
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 "
203                               "release.\n");
204                 *mask = m;
205                 return 0;
206         }
207
208         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
209                             0xffffffff);
210 }
211
212 /**
213  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
214  */
215 void libcfs_debug_dumplog_internal(void *arg)
216 {
217         static time_t last_dump_time;
218         time_t current_time;
219         void *journal_info;
220
221         journal_info = current->journal_info;
222         current->journal_info = NULL;
223
224         current_time = cfs_time_current_sec();
225
226         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
227             current_time > last_dump_time) {
228                 last_dump_time = current_time;
229                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
230                          "%s.%ld.%ld", libcfs_debug_file_path_arr,
231                          current_time, (uintptr_t)arg);
232                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
233                        debug_file_name);
234                 cfs_tracefile_dump_all_pages(debug_file_name);
235                 libcfs_run_debug_log_upcall(debug_file_name);
236         }
237         current->journal_info = journal_info;
238 }
239
240 static int libcfs_debug_dumplog_thread(void *arg)
241 {
242         libcfs_debug_dumplog_internal(arg);
243         wake_up(&debug_ctlwq);
244         return 0;
245 }
246
247 void libcfs_debug_dumplog(void)
248 {
249         wait_queue_t wait;
250         struct task_struct    *dumper;
251         ENTRY;
252
253         /* we're being careful to ensure that the kernel thread is
254          * able to set our state to running as it exits before we
255          * get to schedule() */
256         init_waitqueue_entry(&wait, current);
257         set_current_state(TASK_INTERRUPTIBLE);
258         add_wait_queue(&debug_ctlwq, &wait);
259
260         dumper = kthread_run(libcfs_debug_dumplog_thread,
261                              (void *)(long)current_pid(),
262                              "libcfs_debug_dumper");
263         if (IS_ERR(dumper))
264                 printk(KERN_ERR "LustreError: cannot start log dump thread:"
265                        " %ld\n", PTR_ERR(dumper));
266         else
267                 schedule();
268
269         /* be sure to teardown if cfs_create_thread() failed */
270         remove_wait_queue(&debug_ctlwq, &wait);
271         set_current_state(TASK_RUNNING);
272 }
273 EXPORT_SYMBOL(libcfs_debug_dumplog);
274
275 int libcfs_debug_init(unsigned long bufsize)
276 {
277         int    rc = 0;
278         unsigned int max = libcfs_debug_mb;
279
280         init_waitqueue_head(&debug_ctlwq);
281
282         if (libcfs_console_max_delay <= 0 || /* not set by user or */
283             libcfs_console_min_delay <= 0 || /* set to invalid values */
284             libcfs_console_min_delay >= libcfs_console_max_delay) {
285                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
286                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
287         }
288
289         if (libcfs_debug_file_path != NULL) {
290                 strlcpy(libcfs_debug_file_path_arr,
291                         libcfs_debug_file_path,
292                         sizeof(libcfs_debug_file_path_arr));
293         }
294
295         /* If libcfs_debug_mb is set to an invalid value or uninitialized
296          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
297         if (max > cfs_trace_max_debug_mb() || max < num_possible_cpus()) {
298                 max = TCD_MAX_PAGES;
299         } else {
300                 max = (max / num_possible_cpus());
301                 max = (max << (20 - PAGE_SHIFT));
302         }
303         rc = cfs_tracefile_init(max);
304
305         if (rc == 0)
306                 libcfs_register_panic_notifier();
307
308         return rc;
309 }
310
311 int libcfs_debug_cleanup(void)
312 {
313         libcfs_unregister_panic_notifier();
314         cfs_tracefile_exit();
315         return 0;
316 }
317
318 int libcfs_debug_clear_buffer(void)
319 {
320         cfs_trace_flush_pages();
321         return 0;
322 }
323
324 /* Debug markers, although printed by S_LNET
325  * should not be be marked as such. */
326 #undef DEBUG_SUBSYSTEM
327 #define DEBUG_SUBSYSTEM S_UNDEFINED
328 int libcfs_debug_mark_buffer(const char *text)
329 {
330         CDEBUG(D_TRACE,"***************************************************\n");
331         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
332         CDEBUG(D_TRACE,"***************************************************\n");
333
334         return 0;
335 }
336 #undef DEBUG_SUBSYSTEM
337 #define DEBUG_SUBSYSTEM S_LNET
338
339 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
340 {
341         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
342                          rc, rc, rc);
343         return rc;
344 }
345 EXPORT_SYMBOL(libcfs_log_return);
346
347 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
348                      long rc)
349 {
350         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
351                          " : %#lx)\n", label, rc, rc, rc);
352 }
353 EXPORT_SYMBOL(libcfs_log_goto);