Whamcloud - gitweb
280ada3238a110158706338d0ec590da771eff7e
[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, 2017, 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/ctype.h>
41 #include <linux/kthread.h>
42 #include <libcfs/libcfs.h>
43 #include "tracefile.h"
44
45 static char debug_file_name[1024];
46
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);
51
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);
57
58 static int libcfs_param_debug_mb_set(const char *val,
59                                      cfs_kernel_param_arg_t *kp)
60 {
61         int rc;
62         unsigned int num;
63
64         rc = kstrtouint(val, 0, &num);
65         if (rc < 0)
66                 return rc;
67
68         num = cfs_trace_set_debug_mb(num);
69
70         *((unsigned int *)kp->arg) = num;
71         num = cfs_trace_get_debug_mb();
72         if (num)
73                 /* This value is more precise */
74                 *((unsigned int *)kp->arg) = num;
75
76         return 0;
77 }
78
79 /*
80  * While debug_mb setting look like unsigned int, in fact
81  * it needs quite a bunch of extra processing, so we define special
82  * debug_mb parameter type with corresponding methods to handle this case
83  */
84 static struct kernel_param_ops param_ops_debug_mb = {
85         .set = libcfs_param_debug_mb_set,
86         .get = param_get_uint,
87 };
88
89 #define param_check_debug_mb(name, p) \
90                 __param_check(name, p, unsigned int)
91
92 static unsigned int libcfs_debug_mb;
93 #ifdef HAVE_KERNEL_PARAM_OPS
94 module_param(libcfs_debug_mb, debug_mb, 0644);
95 #else
96 module_param_call(libcfs_debug_mb, libcfs_param_debug_mb_set, param_get_uint,
97                   &param_ops_debug_mb, 0644);
98 #endif
99 MODULE_PARM_DESC(libcfs_debug_mb, "Total debug buffer size.");
100
101 unsigned int libcfs_printk = D_CANTMASK;
102 module_param(libcfs_printk, uint, 0644);
103 MODULE_PARM_DESC(libcfs_printk, "Lustre kernel debug console mask");
104
105 unsigned int libcfs_console_ratelimit = 1;
106 module_param(libcfs_console_ratelimit, uint, 0644);
107 MODULE_PARM_DESC(libcfs_console_ratelimit, "Lustre kernel debug console ratelimit (0 to disable)");
108
109 static int param_set_delay_minmax(const char *val,
110                                   cfs_kernel_param_arg_t *kp,
111                                   long min, long max)
112 {
113         long d;
114         int sec;
115         int rc;
116
117         rc = kstrtoint(val, 0, &sec);
118         if (rc)
119                 return -EINVAL;
120
121         /* The sysfs setting is in centiseconds */
122         d = cfs_time_seconds(sec) / 100;
123         if (d < min || d > max)
124                 return -EINVAL;
125
126         *((unsigned int *)kp->arg) = d;
127
128         return 0;
129 }
130
131 static int param_get_delay(char *buffer, cfs_kernel_param_arg_t *kp)
132 {
133         unsigned int d = *(unsigned int *)kp->arg;
134
135         return sprintf(buffer, "%lu", jiffies_to_msecs(d * 10) / MSEC_PER_SEC);
136 }
137
138 unsigned int libcfs_console_max_delay;
139 unsigned int libcfs_console_min_delay;
140
141 static int param_set_console_max_delay(const char *val,
142                                        cfs_kernel_param_arg_t *kp)
143 {
144         return param_set_delay_minmax(val, kp,
145                                       libcfs_console_min_delay, INT_MAX);
146 }
147
148 static struct kernel_param_ops param_ops_console_max_delay = {
149         .set = param_set_console_max_delay,
150         .get = param_get_delay,
151 };
152
153 #define param_check_console_max_delay(name, p) \
154                 __param_check(name, p, unsigned int)
155
156 #ifdef HAVE_KERNEL_PARAM_OPS
157 module_param(libcfs_console_max_delay, console_max_delay, 0644);
158 #else
159 module_param_call(libcfs_console_max_delay, param_set_console_max_delay,
160                   param_get_delay, &param_ops_console_max_delay, 0644);
161 #endif
162 MODULE_PARM_DESC(libcfs_console_max_delay, "Lustre kernel debug console max delay (jiffies)");
163
164 static int param_set_console_min_delay(const char *val,
165                                        cfs_kernel_param_arg_t *kp)
166 {
167         return param_set_delay_minmax(val, kp,
168                                       1, libcfs_console_max_delay);
169 }
170
171 static struct kernel_param_ops param_ops_console_min_delay = {
172         .set = param_set_console_min_delay,
173         .get = param_get_delay,
174 };
175
176 #define param_check_console_min_delay(name, p) \
177                 __param_check(name, p, unsigned int)
178
179 #ifdef HAVE_KERNEL_PARAM_OPS
180 module_param(libcfs_console_min_delay, console_min_delay, 0644);
181 #else
182 module_param_call(libcfs_console_min_delay, param_set_console_min_delay,
183                   param_get_delay, &param_ops_console_min_delay, 0644);
184 #endif
185 MODULE_PARM_DESC(libcfs_console_min_delay, "Lustre kernel debug console min delay (jiffies)");
186
187 static int param_set_uint_minmax(const char *val,
188                                  cfs_kernel_param_arg_t *kp,
189                                  unsigned int min, unsigned int max)
190 {
191         unsigned int num;
192         int ret;
193
194         if (!val)
195                 return -EINVAL;
196
197         ret = kstrtouint(val, 0, &num);
198         if (ret < 0 || num < min || num > max)
199                 return -EINVAL;
200
201         *((unsigned int *)kp->arg) = num;
202         return 0;
203 }
204
205 static int param_set_uintpos(const char *val,
206                              cfs_kernel_param_arg_t *kp)
207 {
208         return param_set_uint_minmax(val, kp, 1, -1);
209 }
210
211 static struct kernel_param_ops param_ops_uintpos = {
212         .set = param_set_uintpos,
213         .get = param_get_uint,
214 };
215
216 #define param_check_uintpos(name, p) \
217                 __param_check(name, p, unsigned int)
218
219 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
220 #ifdef HAVE_KERNEL_PARAM_OPS
221 module_param(libcfs_console_backoff, uintpos, 0644);
222 #else
223 module_param_call(libcfs_console_backoff, param_set_uintpos, param_get_uint,
224                   &param_ops_uintpos, 0644);
225 #endif
226 MODULE_PARM_DESC(libcfs_console_backoff, "Lustre kernel debug console backoff factor");
227
228 unsigned int libcfs_debug_binary = 1;
229
230 unsigned int libcfs_stack = 3 * THREAD_SIZE / 4;
231 EXPORT_SYMBOL(libcfs_stack);
232
233 unsigned int libcfs_catastrophe;
234 EXPORT_SYMBOL(libcfs_catastrophe);
235
236 unsigned int libcfs_watchdog_ratelimit = 300;
237 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
238
239 unsigned int libcfs_panic_on_lbug = 1;
240 module_param(libcfs_panic_on_lbug, uint, 0644);
241 MODULE_PARM_DESC(libcfs_panic_on_lbug, "Lustre kernel panic on LBUG");
242
243 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
244 EXPORT_SYMBOL(libcfs_kmemory);
245
246 static DECLARE_COMPLETION(debug_complete);
247
248 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
249 EXPORT_SYMBOL(libcfs_debug_file_path_arr);
250
251 /* We need to pass a pointer here, but elsewhere this must be a const */
252 static char *libcfs_debug_file_path = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
253 module_param(libcfs_debug_file_path, charp, 0644);
254 MODULE_PARM_DESC(libcfs_debug_file_path,
255                  "Path for dumping debug logs, set 'NONE' to prevent log dumping");
256
257 int libcfs_panic_in_progress;
258
259 /*
260  * libcfs_debug_token2mask() expects the returned
261  * string in lower-case
262  */
263 static const char *libcfs_debug_subsys2str(int subsys)
264 {
265         static const char *libcfs_debug_subsystems[] = LIBCFS_DEBUG_SUBSYS_NAMES;
266
267         if (subsys >= ARRAY_SIZE(libcfs_debug_subsystems))
268                 return NULL;
269
270         return libcfs_debug_subsystems[subsys];
271 }
272
273 /*
274  * libcfs_debug_token2mask() expects the returned
275  * string in lower-case
276  */
277 static const char *libcfs_debug_dbg2str(int debug)
278 {
279         static const char *libcfs_debug_masks[] = LIBCFS_DEBUG_MASKS_NAMES;
280
281         if (debug >= ARRAY_SIZE(libcfs_debug_masks))
282                 return NULL;
283
284         return libcfs_debug_masks[debug];
285 }
286
287 int
288 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
289 {
290         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
291                                                  libcfs_debug_dbg2str;
292         int len = 0;
293         const char *token;
294         int i;
295
296         if (mask == 0) {                        /* "0" */
297                 if (size > 0)
298                         str[0] = '0';
299                 len = 1;
300         } else {                                /* space-separated tokens */
301                 for (i = 0; i < 32; i++) {
302                         if ((mask & BIT(i)) == 0)
303                                 continue;
304
305                         token = fn(i);
306                         if (token == NULL)      /* unused bit */
307                                 continue;
308
309                         if (len > 0) {          /* separator? */
310                                 if (len < size)
311                                         str[len] = ' ';
312                                 len++;
313                         }
314
315                         while (*token != 0) {
316                                 if (len < size)
317                                         str[len] = *token;
318                                 token++;
319                                 len++;
320                         }
321                 }
322         }
323
324         /* terminate 'str' */
325         if (len < size)
326                 str[len] = 0;
327         else
328                 str[size - 1] = 0;
329
330         return len;
331 }
332
333 int
334 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
335 {
336         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
337                                                  libcfs_debug_dbg2str;
338         int m = 0;
339         int matched;
340         int n;
341         int t;
342
343         /* Allow a number for backwards compatibility */
344         for (n = strlen(str); n > 0; n--)
345                 if (!isspace(str[n-1]))
346                         break;
347         matched = n;
348
349         t = sscanf(str, "%i%n", &m, &matched);
350         if (t >= 1 && matched == n) {
351                 /* don't print warning for lctl set_param debug=0 or -1 */
352                 if (m != 0 && m != -1)
353                         CWARN("You are trying to use a numerical value for the "
354                               "mask - this will be deprecated in a future "
355                               "release.\n");
356                 *mask = m;
357                 return 0;
358         }
359
360         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
361                             0xffffffff);
362 }
363
364 /**
365  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
366  */
367 void libcfs_debug_dumplog_internal(void *arg)
368 {
369         static time64_t last_dump_time;
370         time64_t current_time;
371         void *journal_info;
372
373         journal_info = current->journal_info;
374         current->journal_info = NULL;
375         current_time = ktime_get_real_seconds();
376
377         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0 &&
378             current_time > last_dump_time) {
379                 last_dump_time = current_time;
380                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
381                          "%s.%lld.%ld", libcfs_debug_file_path_arr,
382                          (s64)current_time, (uintptr_t)arg);
383                 pr_alert("LustreError: dumping log to %s\n", debug_file_name);
384                 cfs_tracefile_dump_all_pages(debug_file_name);
385                 libcfs_run_debug_log_upcall(debug_file_name);
386         }
387         current->journal_info = journal_info;
388 }
389
390 static int libcfs_debug_dumplog_thread(void *arg)
391 {
392         libcfs_debug_dumplog_internal(arg);
393         complete(&debug_complete);
394         return 0;
395 }
396
397 static DEFINE_MUTEX(libcfs_debug_dumplog_lock);
398
399 void libcfs_debug_dumplog(void)
400 {
401         struct task_struct *dumper;
402
403         ENTRY;
404
405         if (mutex_trylock(&libcfs_debug_dumplog_lock) == 0)
406                 return;
407
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.
413          */
414         reinit_completion(&debug_complete);
415         dumper = kthread_run(libcfs_debug_dumplog_thread,
416                              (void *)(long)current->pid,
417                              "libcfs_debug_dumper");
418         if (IS_ERR(dumper))
419                 pr_err("LustreError: cannot start log dump thread: rc = %ld\n",
420                        PTR_ERR(dumper));
421         else
422                 wait_for_completion_interruptible(&debug_complete);
423
424         mutex_unlock(&libcfs_debug_dumplog_lock);
425 }
426 EXPORT_SYMBOL(libcfs_debug_dumplog);
427
428 int libcfs_debug_init(unsigned long bufsize)
429 {
430         int rc = 0;
431         unsigned int max = libcfs_debug_mb;
432
433         if (libcfs_console_max_delay <= 0 || /* not set by user or */
434             libcfs_console_min_delay <= 0 || /* set to invalid values */
435             libcfs_console_min_delay >= libcfs_console_max_delay) {
436                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
437                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
438         }
439
440         if (libcfs_debug_file_path != NULL) {
441                 strlcpy(libcfs_debug_file_path_arr,
442                         libcfs_debug_file_path,
443                         sizeof(libcfs_debug_file_path_arr));
444         }
445
446         /* If libcfs_debug_mb is uninitialized then just make the
447          * total buffers smp_num_cpus * TCD_MAX_PAGES
448          */
449         if (max < num_possible_cpus()) {
450                 max = TCD_MAX_PAGES;
451         } else {
452                 max = (max / num_possible_cpus());
453                 max = (max << (20 - PAGE_SHIFT));
454         }
455
456         rc = cfs_tracefile_init(max);
457         if (rc)
458                 return rc;
459
460         libcfs_register_panic_notifier();
461         kernel_param_lock(THIS_MODULE);
462         if (libcfs_debug_mb == 0)
463                 libcfs_debug_mb = cfs_trace_get_debug_mb();
464         kernel_param_unlock(THIS_MODULE);
465         return rc;
466 }
467
468 int libcfs_debug_cleanup(void)
469 {
470         libcfs_unregister_panic_notifier();
471         kernel_param_lock(THIS_MODULE);
472         cfs_tracefile_exit();
473         kernel_param_unlock(THIS_MODULE);
474         return 0;
475 }
476
477 int libcfs_debug_clear_buffer(void)
478 {
479         cfs_trace_flush_pages();
480         return 0;
481 }
482
483 /*
484  * Debug markers, although printed by S_LNET
485  * should not be be marked as such.
486  */
487 #undef DEBUG_SUBSYSTEM
488 #define DEBUG_SUBSYSTEM S_UNDEFINED
489 int libcfs_debug_mark_buffer(const char *text)
490 {
491         CDEBUG(D_TRACE, "**************************************************\n");
492         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
493         CDEBUG(D_TRACE, "**************************************************\n");
494
495         return 0;
496 }
497 #undef DEBUG_SUBSYSTEM
498 #define DEBUG_SUBSYSTEM S_LNET
499
500 long libcfs_log_return(struct libcfs_debug_msg_data *msgdata, long rc)
501 {
502         libcfs_debug_msg(msgdata, "Process leaving (rc=%lu : %ld : %lx)\n",
503                          rc, rc, rc);
504         return rc;
505 }
506 EXPORT_SYMBOL(libcfs_log_return);
507
508 void libcfs_log_goto(struct libcfs_debug_msg_data *msgdata, const char *label,
509                      long rc)
510 {
511         libcfs_debug_msg(msgdata, "Process leaving via %s (rc=%lu : %ld"
512                          " : %#lx)\n", label, rc, rc, rc);
513 }
514 EXPORT_SYMBOL(libcfs_log_goto);