Whamcloud - gitweb
e52f6145c7b44ab5282d65bbce9f2bd0168af030
[fs/lustre-release.git] / lnet / 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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/libcfs/debug.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44
45 # define DEBUG_SUBSYSTEM S_LNET
46
47 #include <stdarg.h>
48 #include <libcfs/kp30.h>
49 #include <libcfs/libcfs.h>
50 #include "tracefile.h"
51
52 static char debug_file_name[1024];
53
54 #ifdef __KERNEL__
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 int libcfs_debug_mb = -1;
67 CFS_MODULE_PARM(libcfs_debug_mb, "i", int, 0644,
68                 "Total debug buffer size.");
69 EXPORT_SYMBOL(libcfs_debug_mb);
70
71 unsigned int libcfs_printk = (D_CANTMASK | D_NETERROR);
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 = 0;
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 atomic_t libcfs_kmemory = ATOMIC_INIT(0);
117 EXPORT_SYMBOL(libcfs_kmemory);
118
119 static cfs_waitq_t debug_ctlwq;
120
121 #ifdef HAVE_BGL_SUPPORT
122 char debug_file_path_arr[1024] = "/bgl/ion/tmp/lustre-log";
123 #elif defined(__arch_um__)
124 char debug_file_path_arr[1024] = "/r/tmp/lustre-log";
125 #else
126 char debug_file_path_arr[1024] = "/tmp/lustre-log";
127 #endif
128 /* We need to pass a pointer here, but elsewhere this must be a const */
129 char *debug_file_path = &debug_file_path_arr[0];
130 CFS_MODULE_PARM(debug_file_path, "s", charp, 0644,
131                 "Path for dumping debug logs, "
132                 "set 'NONE' to prevent log dumping");
133
134 int libcfs_panic_in_progress;
135
136 /* libcfs_debug_token2mask() expects the returned
137  * string in lower-case */
138 const char *
139 libcfs_debug_subsys2str(int subsys)
140 {
141         switch (subsys) {
142         default:
143                 return NULL;
144         case S_UNDEFINED:
145                 return "undefined";
146         case S_MDC:
147                 return "mdc";
148         case S_MDS:
149                 return "mds";
150         case S_OSC:
151                 return "osc";
152         case S_OST:
153                 return "ost";
154         case S_CLASS:
155                 return "class";
156         case S_LOG:
157                 return "log";
158         case S_LLITE:
159                 return "llite";
160         case S_RPC:
161                 return "rpc";
162         case S_LNET:
163                 return "lnet";
164         case S_LND:
165                 return "lnd";
166         case S_PINGER:
167                 return "pinger";
168         case S_FILTER:
169                 return "filter";
170         case S_ECHO:
171                 return "echo";
172         case S_LDLM:
173                 return "ldlm";
174         case S_LOV:
175                 return "lov";
176         case S_LQUOTA:
177                 return "lquota";
178         case S_LMV:
179                 return "lmv";
180         case S_SEC:
181                 return "sec";
182         case S_GSS:
183                 return "gss";
184         case S_MGC:
185                 return "mgc";
186         case S_MGS:
187                 return "mgs";
188         case S_FID:
189                 return "fid";
190         case S_FLD:
191                 return "fld";
192         }
193 }
194
195 /* libcfs_debug_token2mask() expects the returned
196  * string in lower-case */
197 const char *
198 libcfs_debug_dbg2str(int debug)
199 {
200         switch (debug) {
201         default:
202                 return NULL;
203         case D_TRACE:
204                 return "trace";
205         case D_INODE:
206                 return "inode";
207         case D_SUPER:
208                 return "super";
209         case D_EXT2:
210                 return "ext2";
211         case D_MALLOC:
212                 return "malloc";
213         case D_CACHE:
214                 return "cache";
215         case D_INFO:
216                 return "info";
217         case D_IOCTL:
218                 return "ioctl";
219         case D_NETERROR:
220                 return "neterror";
221         case D_NET:
222                 return "net";
223         case D_WARNING:
224                 return "warning";
225         case D_BUFFS:
226                 return "buffs";
227         case D_OTHER:
228                 return "other";
229         case D_DENTRY:
230                 return "dentry";
231         case D_NETTRACE:
232                 return "nettrace";
233         case D_PAGE:
234                 return "page";
235         case D_DLMTRACE:
236                 return "dlmtrace";
237         case D_ERROR:
238                 return "error";
239         case D_EMERG:
240                 return "emerg";
241         case D_HA:
242                 return "ha";
243         case D_RPCTRACE:
244                 return "rpctrace";
245         case D_VFSTRACE:
246                 return "vfstrace";
247         case D_READA:
248                 return "reada";
249         case D_MMAP:
250                 return "mmap";
251         case D_CONFIG:
252                 return "config";
253         case D_CONSOLE:
254                 return "console";
255         case D_QUOTA:
256                 return "quota";
257         case D_SEC:
258                 return "sec";
259         }
260 }
261
262 int
263 libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys)
264 {
265         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
266                                                  libcfs_debug_dbg2str;
267         int           len = 0;
268         const char   *token;
269         int           bit;
270         int           i;
271
272         if (mask == 0) {                        /* "0" */
273                 if (size > 0)
274                         str[0] = '0';
275                 len = 1;
276         } else {                                /* space-separated tokens */
277                 for (i = 0; i < 32; i++) {
278                         bit = 1 << i;
279
280                         if ((mask & bit) == 0)
281                                 continue;
282
283                         token = fn(bit);
284                         if (token == NULL)              /* unused bit */
285                                 continue;
286
287                         if (len > 0) {                  /* separator? */
288                                 if (len < size)
289                                         str[len] = ' ';
290                                 len++;
291                         }
292
293                         while (*token != 0) {
294                                 if (len < size)
295                                         str[len] = *token;
296                                 token++;
297                                 len++;
298                         }
299                 }
300         }
301
302         /* terminate 'str' */
303         if (len < size)
304                 str[len] = 0;
305         else
306                 str[size - 1] = 0;
307
308         return len;
309 }
310
311 int
312 libcfs_debug_token2mask(int *mask, const char *str, int len, int is_subsys)
313 {
314         const char *(*fn)(int bit) = is_subsys ? libcfs_debug_subsys2str :
315                                                  libcfs_debug_dbg2str;
316         int           i;
317         int           j;
318         int           bit;
319         const char   *token;
320
321         /* match against known tokens */
322         for (i = 0; i < 32; i++) {
323                 bit = 1 << i;
324
325                 token = fn(bit);
326                 if (token == NULL)              /* unused? */
327                         continue;
328
329                 /* strcasecmp */
330                 for (j = 0; ; j++) {
331                         if (j == len) {         /* end of token */
332                                 if (token[j] == 0) {
333                                         *mask = bit;
334                                         return 0;
335                                 }
336                                 break;
337                         }
338
339                         if (token[j] == 0)
340                                 break;
341
342                         if (str[j] == token[j])
343                                 continue;
344
345                         if (str[j] < 'A' || 'Z' < str[j])
346                                 break;
347
348                         if (str[j] - 'A' + 'a' != token[j])
349                                 break;
350                 }
351         }
352
353         return -EINVAL;                         /* no match */
354 }
355
356 int
357 libcfs_debug_str2mask(int *mask, const char *str, int is_subsys)
358 {
359         int         m = 0;
360         char        op = 0;
361         int         matched;
362         int         n;
363         int         t;
364
365         /* Allow a number for backwards compatibility */
366
367         for (n = strlen(str); n > 0; n--)
368                 if (!isspace(str[n-1]))
369                         break;
370         matched = n;
371
372         if ((t = sscanf(str, "%i%n", &m, &matched)) >= 1 &&
373             matched == n) {
374                 *mask = m;
375                 return 0;
376         }
377
378         /* <str> must be a list of debug tokens or numbers separated by
379          * whitespace and optionally an operator ('+' or '-').  If an operator
380          * appears first in <str>, '*mask' is used as the starting point
381          * (relative), otherwise 0 is used (absolute).  An operator applies to
382          * all following tokens up to the next operator. */
383
384         matched = 0;
385         while (*str != 0) {
386                 while (isspace(*str)) /* skip whitespace */
387                         str++;
388
389                 if (*str == 0)
390                         break;
391
392                 if (*str == '+' || *str == '-') {
393                         op = *str++;
394
395                         /* op on first token == relative */
396                         if (!matched)
397                                 m = *mask;
398
399                         while (isspace(*str)) /* skip whitespace */
400                                 str++;
401
402                         if (*str == 0)          /* trailing op */
403                                 return -EINVAL;
404                 }
405
406                 /* find token length */
407                 for (n = 0; str[n] != 0 && !isspace(str[n]); n++);
408
409                 /* match token */
410                 if (libcfs_debug_token2mask(&t, str, n, is_subsys) != 0)
411                         return -EINVAL;
412
413                 matched = 1;
414                 if (op == '-')
415                         m &= ~t;
416                 else
417                         m |= t;
418
419                 str += n;
420         }
421
422         if (!matched)
423                 return -EINVAL;
424
425         *mask = m;
426         return 0;
427 }
428
429 /**
430  * Dump Lustre log to ::debug_file_path by calling tracefile_dump_all_pages()
431  */
432 void libcfs_debug_dumplog_internal(void *arg)
433 {
434         CFS_DECL_JOURNAL_DATA;
435
436         CFS_PUSH_JOURNAL;
437
438         if (strncmp(debug_file_path_arr, "NONE", 4) != 0) {
439                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
440                          "%s.%ld.%ld", debug_file_path_arr,
441                          cfs_time_current_sec(), (long)arg);
442                 printk(KERN_ALERT "LustreError: dumping log to %s\n",
443                        debug_file_name);
444
445                 tracefile_dump_all_pages(debug_file_name);
446                 libcfs_run_debug_log_upcall(debug_file_name);
447         }
448         CFS_POP_JOURNAL;
449 }
450
451 int libcfs_debug_dumplog_thread(void *arg)
452 {
453         cfs_daemonize("");
454         libcfs_debug_dumplog_internal(arg);
455         cfs_waitq_signal(&debug_ctlwq);
456         return 0;
457 }
458
459 void libcfs_debug_dumplog(void)
460 {
461         int            rc;
462         cfs_waitlink_t wait;
463         ENTRY;
464
465         /* we're being careful to ensure that the kernel thread is
466          * able to set our state to running as it exits before we
467          * get to schedule() */
468         cfs_waitlink_init(&wait);
469         set_current_state(TASK_INTERRUPTIBLE);
470         cfs_waitq_add(&debug_ctlwq, &wait);
471
472         rc = cfs_kernel_thread(libcfs_debug_dumplog_thread,
473                                (void *)(long)cfs_curproc_pid(),
474                                CLONE_VM | CLONE_FS | CLONE_FILES);
475         if (rc < 0)
476                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
477                        "%d\n", rc);
478         else
479                 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
480
481         /* be sure to teardown if kernel_thread() failed */
482         cfs_waitq_del(&debug_ctlwq, &wait);
483         set_current_state(TASK_RUNNING);
484 }
485
486 int libcfs_debug_init(unsigned long bufsize)
487 {
488         int    rc = 0;
489         int    max = libcfs_debug_mb;
490
491         cfs_waitq_init(&debug_ctlwq);
492
493         if (libcfs_console_max_delay <= 0 || /* not set by user or */
494             libcfs_console_min_delay <= 0 || /* set to invalid values */
495             libcfs_console_min_delay >= libcfs_console_max_delay) {
496                 libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
497                 libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
498         }
499
500         /* If libcfs_debug_mb is set to an invalid value or uninitialized
501          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
502         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
503                 max = TCD_MAX_PAGES;
504         } else {
505                 max = (max / num_possible_cpus());
506                 max = (max << (20 - CFS_PAGE_SHIFT));
507         }
508         rc = tracefile_init(max);
509
510         if (rc == 0)
511                 libcfs_register_panic_notifier();
512
513         return rc;
514 }
515
516 int libcfs_debug_cleanup(void)
517 {
518         libcfs_unregister_panic_notifier();
519         tracefile_exit();
520         return 0;
521 }
522
523 int libcfs_debug_clear_buffer(void)
524 {
525         trace_flush_pages();
526         return 0;
527 }
528
529 /* Debug markers, although printed by S_LNET
530  * should not be be marked as such. */
531 #undef DEBUG_SUBSYSTEM
532 #define DEBUG_SUBSYSTEM S_UNDEFINED
533 int libcfs_debug_mark_buffer(const char *text)
534 {
535         CDEBUG(D_TRACE,"***************************************************\n");
536         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
537         CDEBUG(D_TRACE,"***************************************************\n");
538
539         return 0;
540 }
541 #undef DEBUG_SUBSYSTEM
542 #define DEBUG_SUBSYSTEM S_LNET
543
544 void libcfs_debug_set_level(unsigned int debug_level)
545 {
546         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
547                debug_level);
548         libcfs_debug = debug_level;
549 }
550
551 EXPORT_SYMBOL(libcfs_debug_dumplog);
552 EXPORT_SYMBOL(libcfs_debug_set_level);
553
554
555 #else /* !__KERNEL__ */
556
557 #include <libcfs/libcfs.h>
558
559 #ifdef HAVE_CATAMOUNT_DATA_H
560 #include <catamount/data.h>
561 #include <catamount/lputs.h>
562
563 static char source_nid[16];
564 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
565 static int toconsole = 1;
566 unsigned int libcfs_console_ratelimit = 1;
567 cfs_duration_t libcfs_console_max_delay;
568 cfs_duration_t libcfs_console_min_delay;
569 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
570 #else /* !HAVE_CATAMOUNT_DATA_H */
571 #ifdef HAVE_NETDB_H
572 #include <sys/utsname.h>
573 #endif /* HAVE_NETDB_H */
574 struct utsname *tmp_utsname;
575 static char source_nid[sizeof(tmp_utsname->nodename)];
576 #endif /* HAVE_CATAMOUNT_DATA_H */
577
578 static int source_pid;
579 int smp_processor_id = 1;
580 char debug_file_path[1024];
581 FILE *debug_file_fd;
582
583 int portals_do_debug_dumplog(void *arg)
584 {
585         printf("Look in %s\n", debug_file_name);
586         return 0;
587 }
588
589
590 void portals_debug_print(void)
591 {
592         return;
593 }
594
595
596 void libcfs_debug_dumplog(void)
597 {
598         printf("Look in %s\n", debug_file_name);
599         return;
600 }
601
602 int libcfs_debug_init(unsigned long bufsize)
603 {
604         char *debug_mask = NULL;
605         char *debug_subsys = NULL;
606         char *debug_filename;
607
608 #ifdef HAVE_CATAMOUNT_DATA_H
609         char *debug_console = NULL;
610         char *debug_ratelimit = NULL;
611         char *debug_max_delay = NULL;
612         char *debug_min_delay = NULL;
613         char *debug_backoff = NULL;
614
615         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
616         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
617
618         snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
619         source_pid = _my_pid;
620
621         debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
622         if (debug_console != NULL) {
623                 toconsole = strtoul(debug_console, NULL, 0);
624                 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
625         }
626         debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
627         if (debug_ratelimit != NULL) {
628                 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
629                 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
630                                 libcfs_console_ratelimit);
631         }
632         debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
633         if (debug_max_delay != NULL)
634                 libcfs_console_max_delay =
635                             cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
636         debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
637         if (debug_min_delay != NULL)
638                 libcfs_console_min_delay =
639                             cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
640         if (debug_min_delay || debug_max_delay) {
641                 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
642                     libcfs_console_max_delay < libcfs_console_min_delay) {
643                         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
644                         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
645                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
646                                        "should be greater than "
647                                        "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
648                                        "and both parameters should be non-null"
649                                        ": restore default values\n");
650                 } else {
651                         CDEBUG(D_INFO, "set liblustre console max delay to %lus"
652                                        " and min delay to %lus\n",
653                                (cfs_duration_t)
654                                      cfs_duration_sec(libcfs_console_max_delay),
655                                (cfs_duration_t)
656                                     cfs_duration_sec(libcfs_console_min_delay));
657                 }
658         }
659         debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
660         if (debug_backoff != NULL) {
661                 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
662                 if (libcfs_console_backoff <= 0) {
663                         libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
664                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
665                                        "restore default value\n");
666                 } else {
667                         CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
668                                libcfs_console_backoff);
669                 }
670         }
671 #else
672         struct utsname myname;
673
674         if (uname(&myname) == 0)
675                 strcpy(source_nid, myname.nodename);
676         source_pid = getpid();
677 #endif
678         /* debug masks */
679         debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
680         if (debug_mask)
681                 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
682
683         debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
684         if (debug_subsys)
685                 libcfs_subsystem_debug =
686                                 (unsigned int) strtol(debug_subsys, NULL, 0);
687
688         debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
689         if (debug_filename)
690                 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
691
692         debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
693         if (debug_filename)
694                 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
695
696         if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
697                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
698                          "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
699
700         if (strcmp(debug_file_name, "stdout") == 0 ||
701             strcmp(debug_file_name, "-") == 0) {
702                 debug_file_fd = stdout;
703         } else if (strcmp(debug_file_name, "stderr") == 0) {
704                 debug_file_fd = stderr;
705         } else if (debug_file_name[0] != '\0') {
706                 debug_file_fd = fopen(debug_file_name, "w");
707                 if (debug_file_fd == NULL)
708                         fprintf(stderr, "%s: unable to open '%s': %s\n",
709                                 source_nid, debug_file_name, strerror(errno));
710         }
711
712         if (debug_file_fd == NULL)
713                 debug_file_fd = stdout;
714
715         return 0;
716 }
717
718 int libcfs_debug_cleanup(void)
719 {
720         if (debug_file_fd != stdout && debug_file_fd != stderr)
721                 fclose(debug_file_fd);
722         return 0;
723 }
724
725 int libcfs_debug_clear_buffer(void)
726 {
727         return 0;
728 }
729
730 int libcfs_debug_mark_buffer(char *text)
731 {
732
733         fprintf(debug_file_fd, "*******************************************************************************\n");
734         fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
735         fprintf(debug_file_fd, "*******************************************************************************\n");
736
737         return 0;
738 }
739
740 #ifdef HAVE_CATAMOUNT_DATA_H
741 #define CATAMOUNT_MAXLINE (256-4)
742 void catamount_printline(char *buf, size_t size)
743 {
744     char *pos = buf;
745     int prsize = size;
746
747     while (prsize > 0){
748         lputs(pos);
749         pos += CATAMOUNT_MAXLINE;
750         prsize -= CATAMOUNT_MAXLINE;
751     }
752 }
753 #endif
754
755 int
756 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
757                    int subsys, int mask,
758                    const char *file, const char *fn, const int line,
759                    const char *format1, va_list args,
760                    const char *format2, ...)
761 {
762         struct timeval tv;
763         int            nob;
764         int            remain;
765         va_list        ap;
766         char           buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
767                                             * with linux, where message can`t
768                                             * be exceed PAGE_SIZE */
769         int            console = 0;
770         char *prefix = "Lustre";
771
772 #ifdef HAVE_CATAMOUNT_DATA_H
773         /* toconsole == 0 - all messages to debug_file_fd
774          * toconsole == 1 - warnings to console, all to debug_file_fd
775          * toconsole >  1 - all debug to console */
776         if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
777                 console = 1;
778 #endif
779
780         if ((!console) && (!debug_file_fd)) {
781                 return 0;
782         }
783
784         if (mask & (D_EMERG | D_ERROR))
785                prefix = "LustreError";
786
787         nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
788                        source_pid, source_nid, file, line, fn);
789
790         remain = sizeof(buf) - nob;
791         if (format1) {
792                 nob += vsnprintf(&buf[nob], remain, format1, args);
793         }
794
795         remain = sizeof(buf) - nob;
796         if ((format2) && (remain > 0)) {
797                 va_start(ap, format2);
798                 nob += vsnprintf(&buf[nob], remain, format2, ap);
799                 va_end(ap);
800         }
801
802 #ifdef HAVE_CATAMOUNT_DATA_H
803         if (console) {
804                 /* check rate limit for console */
805                 if (cdls != NULL) {
806                         if (libcfs_console_ratelimit &&
807                                 cdls->cdls_next != 0 &&     /* not first time ever */
808                                 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
809
810                                 /* skipping a console message */
811                                 cdls->cdls_count++;
812                                 goto out_file;
813                         }
814
815                         if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
816                                            libcfs_console_max_delay +
817                                            cfs_time_seconds(10))) {
818                                 /* last timeout was a long time ago */
819                                 cdls->cdls_delay /= libcfs_console_backoff * 4;
820                         } else {
821                                 cdls->cdls_delay *= libcfs_console_backoff;
822
823                                 if (cdls->cdls_delay <
824                                                 libcfs_console_min_delay)
825                                         cdls->cdls_delay =
826                                                 libcfs_console_min_delay;
827                                 else if (cdls->cdls_delay >
828                                                 libcfs_console_max_delay)
829                                         cdls->cdls_delay =
830                                                 libcfs_console_max_delay;
831                         }
832
833                         /* ensure cdls_next is never zero after it's been seen */
834                         cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
835                 }
836
837                 if (cdls != NULL && cdls->cdls_count != 0) {
838                         char buf2[100];
839
840                         nob = snprintf(buf2, sizeof(buf2),
841                                        "Skipped %d previous similar message%s\n",
842                                        cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
843
844                         catamount_printline(buf2, nob);
845                         cdls->cdls_count = 0;
846                         goto out_file;
847                 }
848                 catamount_printline(buf, nob);
849        }
850 out_file:
851         /* return on toconsole > 1, as we don't want the user getting
852         * spammed by the debug data */
853         if (toconsole > 1)
854                 return 0;
855 #endif
856         if (debug_file_fd == NULL)
857                 return 0;
858
859         gettimeofday(&tv, NULL);
860
861         fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
862                 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
863                 file, line, fn, buf);
864
865         return 0;
866 }
867
868 void
869 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
870                         const int line)
871 {
872         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
873                          "ASSERTION(%s) failed\n", expr);
874         abort();
875 }
876
877 #endif /* __KERNEL__ */