Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / libcfs / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * libcfs/libcfs/debug.c
39  *
40  * Author: Phil Schwan <phil@clusterfs.com>
41  *
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47
48 # define DEBUG_SUBSYSTEM S_LNET
49
50 #include <libcfs/libcfs.h>
51 #include "tracefile.h"
52
53 static char debug_file_name[1024];
54
55 unsigned int libcfs_subsystem_debug = ~0;
56 CFS_MODULE_PARM(libcfs_subsystem_debug, "i", int, 0644,
57                 "Lustre kernel debug subsystem mask");
58 EXPORT_SYMBOL(libcfs_subsystem_debug);
59
60 unsigned int libcfs_debug = (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE |
61                              D_NETERROR | D_HA | D_CONFIG | D_IOCTL);
62 CFS_MODULE_PARM(libcfs_debug, "i", int, 0644,
63                 "Lustre kernel debug mask");
64 EXPORT_SYMBOL(libcfs_debug);
65
66 unsigned int libcfs_debug_mb = 0;
67 CFS_MODULE_PARM(libcfs_debug_mb, "i", uint, 0644,
68                 "Total debug buffer size.");
69 EXPORT_SYMBOL(libcfs_debug_mb);
70
71 unsigned int libcfs_printk = D_CANTMASK;
72 CFS_MODULE_PARM(libcfs_printk, "i", uint, 0644,
73                 "Lustre kernel debug console mask");
74 EXPORT_SYMBOL(libcfs_printk);
75
76 unsigned int libcfs_console_ratelimit = 1;
77 CFS_MODULE_PARM(libcfs_console_ratelimit, "i", uint, 0644,
78                 "Lustre kernel debug console ratelimit (0 to disable)");
79 EXPORT_SYMBOL(libcfs_console_ratelimit);
80
81 cfs_duration_t libcfs_console_max_delay;
82 CFS_MODULE_PARM(libcfs_console_max_delay, "l", ulong, 0644,
83                 "Lustre kernel debug console max delay (jiffies)");
84 EXPORT_SYMBOL(libcfs_console_max_delay);
85
86 cfs_duration_t libcfs_console_min_delay;
87 CFS_MODULE_PARM(libcfs_console_min_delay, "l", ulong, 0644,
88                 "Lustre kernel debug console min delay (jiffies)");
89 EXPORT_SYMBOL(libcfs_console_min_delay);
90
91 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
92 CFS_MODULE_PARM(libcfs_console_backoff, "i", uint, 0644,
93                 "Lustre kernel debug console backoff factor");
94 EXPORT_SYMBOL(libcfs_console_backoff);
95
96 unsigned int libcfs_debug_binary = 1;
97 EXPORT_SYMBOL(libcfs_debug_binary);
98
99 unsigned int libcfs_stack;
100 EXPORT_SYMBOL(libcfs_stack);
101
102 unsigned int portal_enter_debugger;
103 EXPORT_SYMBOL(portal_enter_debugger);
104
105 unsigned int libcfs_catastrophe;
106 EXPORT_SYMBOL(libcfs_catastrophe);
107
108 unsigned int libcfs_watchdog_ratelimit = 300;
109 EXPORT_SYMBOL(libcfs_watchdog_ratelimit);
110
111 unsigned int libcfs_panic_on_lbug = 1;
112 CFS_MODULE_PARM(libcfs_panic_on_lbug, "i", uint, 0644,
113                 "Lustre kernel panic on LBUG");
114 EXPORT_SYMBOL(libcfs_panic_on_lbug);
115
116 cfs_atomic_t libcfs_kmemory = CFS_ATOMIC_INIT(0);
117 EXPORT_SYMBOL(libcfs_kmemory);
118
119 static cfs_waitq_t debug_ctlwq;
120
121 char libcfs_debug_file_path_arr[PATH_MAX] = LIBCFS_DEBUG_FILE_PATH_DEFAULT;
122
123 /* We need to pass a pointer here, but elsewhere this must be a const */
124 char *libcfs_debug_file_path;
125 CFS_MODULE_PARM(libcfs_debug_file_path, "s", charp, 0644,
126                 "Path for dumping debug logs, "
127                 "set 'NONE' to prevent log dumping");
128
129 int libcfs_panic_in_progress;
130
131 /* libcfs_debug_token2mask() expects the returned
132  * string in lower-case */
133 const char *
134 libcfs_debug_subsys2str(int subsys)
135 {
136         switch (1 << subsys) {
137         default:
138                 return NULL;
139         case S_UNDEFINED:
140                 return "undefined";
141         case S_MDC:
142                 return "mdc";
143         case S_MDS:
144                 return "mds";
145         case S_OSC:
146                 return "osc";
147         case S_OST:
148                 return "ost";
149         case S_CLASS:
150                 return "class";
151         case S_LOG:
152                 return "log";
153         case S_LLITE:
154                 return "llite";
155         case S_RPC:
156                 return "rpc";
157         case S_LNET:
158                 return "lnet";
159         case S_LND:
160                 return "lnd";
161         case S_PINGER:
162                 return "pinger";
163         case S_FILTER:
164                 return "filter";
165         case S_ECHO:
166                 return "echo";
167         case S_LDLM:
168                 return "ldlm";
169         case S_LOV:
170                 return "lov";
171         case S_LQUOTA:
172                 return "lquota";
173         case S_LMV:
174                 return "lmv";
175         case S_SEC:
176                 return "sec";
177         case S_GSS:
178                 return "gss";
179         case S_MGC:
180                 return "mgc";
181         case S_MGS:
182                 return "mgs";
183         case S_FID:
184                 return "fid";
185         case S_FLD:
186                 return "fld";
187         }
188 }
189
190 /* libcfs_debug_token2mask() expects the returned
191  * string in lower-case */
192 const char *
193 libcfs_debug_dbg2str(int debug)
194 {
195         switch (1 << debug) {
196         default:
197                 return NULL;
198         case D_TRACE:
199                 return "trace";
200         case D_INODE:
201                 return "inode";
202         case D_SUPER:
203                 return "super";
204         case D_EXT2:
205                 return "ext2";
206         case D_MALLOC:
207                 return "malloc";
208         case D_CACHE:
209                 return "cache";
210         case D_INFO:
211                 return "info";
212         case D_IOCTL:
213                 return "ioctl";
214         case D_NETERROR:
215                 return "neterror";
216         case D_NET:
217                 return "net";
218         case D_WARNING:
219                 return "warning";
220         case D_BUFFS:
221                 return "buffs";
222         case D_OTHER:
223                 return "other";
224         case D_DENTRY:
225                 return "dentry";
226         case D_NETTRACE:
227                 return "nettrace";
228         case D_PAGE:
229                 return "page";
230         case D_DLMTRACE:
231                 return "dlmtrace";
232         case D_ERROR:
233                 return "error";
234         case D_EMERG:
235                 return "emerg";
236         case D_HA:
237                 return "ha";
238         case D_RPCTRACE:
239                 return "rpctrace";
240         case D_VFSTRACE:
241                 return "vfstrace";
242         case D_READA:
243                 return "reada";
244         case D_MMAP:
245                 return "mmap";
246         case D_CONFIG:
247                 return "config";
248         case D_CONSOLE:
249                 return "console";
250         case D_QUOTA:
251                 return "quota";
252         case D_SEC:
253                 return "sec";
254         }
255 }
256
257 int
258 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
259 {
260         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
261                                                  libcfs_debug_dbg2str;
262         int           len = 0;
263         const char   *token;
264         int           i;
265
266         if (mask == 0) {                        /* "0" */
267                 if (size > 0)
268                         str[0] = '0';
269                 len = 1;
270         } else {                                /* space-separated tokens */
271                 for (i = 0; i < 32; i++) {
272                         if ((mask & (1 << i)) == 0)
273                                 continue;
274
275                         token = fn(i);
276                         if (token == NULL)              /* unused bit */
277                                 continue;
278
279                         if (len > 0) {                  /* separator? */
280                                 if (len < size)
281                                         str[len] = ' ';
282                                 len++;
283                         }
284
285                         while (*token != 0) {
286                                 if (len < size)
287                                         str[len] = *token;
288                                 token++;
289                                 len++;
290                         }
291                 }
292         }
293
294         /* terminate 'str' */
295         if (len < size)
296                 str[len] = 0;
297         else
298                 str[size - 1] = 0;
299
300         return len;
301 }
302
303 int
304 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
305 {
306         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
307                                                  libcfs_debug_dbg2str;
308         int         m = 0;
309         int         matched;
310         int         n;
311         int         t;
312
313         /* Allow a number for backwards compatibility */
314
315         for (n = strlen(str); n > 0; n--)
316                 if (!isspace(str[n-1]))
317                         break;
318         matched = n;
319
320         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
321             matched == n) {
322                 /* don't print warning for lctl set_param debug=0 or -1 */
323                 if (m != 0 && m != -1)
324                         CWARN("You are trying to use a numerical value for the "
325                               "mask - this will be deprecated in a future "
326                               "release.\n");
327                 *mask = m;
328                 return 0;
329         }
330
331         return cfs_str2mask(str, fn, mask, is_subsys ? 0 : D_CANTMASK,
332                             0xffffffff);
333 }
334
335 /**
336  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
337  */
338 void libcfs_debug_dumplog_internal(void *arg)
339 {
340         CFS_DECL_JOURNAL_DATA;
341
342         CFS_PUSH_JOURNAL;
343
344         if (strncmp(libcfs_debug_file_path_arr, "NONE", 4) != 0) {
345                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
346                          "%s.%ld." LPLD, libcfs_debug_file_path_arr,
347                          cfs_time_current_sec(), (long_ptr_t)arg);
348                 printk(CFS_KERN_ALERT "LustreError: dumping log to %s\n",
349                        debug_file_name);
350                 cfs_tracefile_dump_all_pages(debug_file_name);
351                 libcfs_run_debug_log_upcall(debug_file_name);
352         }
353         CFS_POP_JOURNAL;
354 }
355
356 int libcfs_debug_dumplog_thread(void *arg)
357 {
358         libcfs_debug_dumplog_internal(arg);
359         cfs_waitq_signal(&debug_ctlwq);
360         return 0;
361 }
362
363 void libcfs_debug_dumplog(void)
364 {
365         cfs_waitlink_t wait;
366         cfs_task_t    *dumper;
367         ENTRY;
368
369         /* we're being careful to ensure that the kernel thread is
370          * able to set our state to running as it exits before we
371          * get to schedule() */
372         cfs_waitlink_init(&wait);
373         cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
374         cfs_waitq_add(&debug_ctlwq, &wait);
375
376         dumper = cfs_kthread_run(libcfs_debug_dumplog_thread,
377                                  (void*)(long)cfs_curproc_pid(),
378                                  "libcfs_debug_dumper");
379         if (IS_ERR(dumper))
380                 printk(CFS_KERN_ERR "LustreError: cannot start log dump thread:"
381                        " %ld\n", PTR_ERR(dumper));
382         else
383                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
384
385         /* be sure to teardown if cfs_create_thread() failed */
386         cfs_waitq_del(&debug_ctlwq, &wait);
387         cfs_set_current_state(CFS_TASK_RUNNING);
388 }
389
390 int libcfs_debug_init(unsigned long bufsize)
391 {
392         int    rc = 0;
393         unsigned int max = libcfs_debug_mb;
394
395         cfs_waitq_init(&debug_ctlwq);
396
397         if (libcfs_console_max_delay <= 0 || /* not set by user or */
398             libcfs_console_min_delay <= 0 || /* set to invalid values */
399             libcfs_console_min_delay >= libcfs_console_max_delay) {
400                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
401                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
402         }
403
404         if (libcfs_debug_file_path != NULL) {
405                 memset(libcfs_debug_file_path_arr, 0, PATH_MAX);
406                 strncpy(libcfs_debug_file_path_arr, 
407                         libcfs_debug_file_path, PATH_MAX-1);
408         }
409
410         /* If libcfs_debug_mb is set to an invalid value or uninitialized
411          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
412         if (max > cfs_trace_max_debug_mb() || max < cfs_num_possible_cpus()) {
413                 max = TCD_MAX_PAGES;
414         } else {
415                 max = (max / cfs_num_possible_cpus());
416                 max = (max << (20 - CFS_PAGE_SHIFT));
417         }
418         rc = cfs_tracefile_init(max);
419
420         if (rc == 0)
421                 libcfs_register_panic_notifier();
422
423         return rc;
424 }
425
426 int libcfs_debug_cleanup(void)
427 {
428         libcfs_unregister_panic_notifier();
429         cfs_tracefile_exit();
430         return 0;
431 }
432
433 int libcfs_debug_clear_buffer(void)
434 {
435         cfs_trace_flush_pages();
436         return 0;
437 }
438
439 /* Debug markers, although printed by S_LNET
440  * 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)
444 {
445         CDEBUG(D_TRACE,"***************************************************\n");
446         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
447         CDEBUG(D_TRACE,"***************************************************\n");
448
449         return 0;
450 }
451 #undef DEBUG_SUBSYSTEM
452 #define DEBUG_SUBSYSTEM S_LNET
453
454 void libcfs_debug_set_level(unsigned int debug_level)
455 {
456         printk(CFS_KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
457                debug_level);
458         libcfs_debug = debug_level;
459 }
460
461 EXPORT_SYMBOL(libcfs_debug_dumplog);
462 EXPORT_SYMBOL(libcfs_debug_set_level);