Whamcloud - gitweb
b=18536
[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         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
493         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
494         /* If libcfs_debug_mb is set to an invalid value or uninitialized
495          * then just make the total buffers smp_num_cpus * TCD_MAX_PAGES */
496         if (max > trace_max_debug_mb() || max < num_possible_cpus()) {
497                 max = TCD_MAX_PAGES;
498         } else {
499                 max = (max / num_possible_cpus());
500                 max = (max << (20 - CFS_PAGE_SHIFT));
501         }
502         rc = tracefile_init(max);
503
504         if (rc == 0)
505                 libcfs_register_panic_notifier();
506
507         return rc;
508 }
509
510 int libcfs_debug_cleanup(void)
511 {
512         libcfs_unregister_panic_notifier();
513         tracefile_exit();
514         return 0;
515 }
516
517 int libcfs_debug_clear_buffer(void)
518 {
519         trace_flush_pages();
520         return 0;
521 }
522
523 /* Debug markers, although printed by S_LNET
524  * should not be be marked as such. */
525 #undef DEBUG_SUBSYSTEM
526 #define DEBUG_SUBSYSTEM S_UNDEFINED
527 int libcfs_debug_mark_buffer(const char *text)
528 {
529         CDEBUG(D_TRACE,"***************************************************\n");
530         LCONSOLE(D_WARNING, "DEBUG MARKER: %s\n", text);
531         CDEBUG(D_TRACE,"***************************************************\n");
532
533         return 0;
534 }
535 #undef DEBUG_SUBSYSTEM
536 #define DEBUG_SUBSYSTEM S_LNET
537
538 void libcfs_debug_set_level(unsigned int debug_level)
539 {
540         printk(KERN_WARNING "Lustre: Setting portals debug level to %08x\n",
541                debug_level);
542         libcfs_debug = debug_level;
543 }
544
545 EXPORT_SYMBOL(libcfs_debug_dumplog);
546 EXPORT_SYMBOL(libcfs_debug_set_level);
547
548
549 #else /* !__KERNEL__ */
550
551 #include <libcfs/libcfs.h>
552
553 #ifdef HAVE_CATAMOUNT_DATA_H
554 #include <catamount/data.h>
555 #include <catamount/lputs.h>
556
557 static char source_nid[16];
558 /* 0 indicates no messages to console, 1 is errors, > 1 is all debug messages */
559 static int toconsole = 1;
560 unsigned int libcfs_console_ratelimit = 1;
561 cfs_duration_t libcfs_console_max_delay;
562 cfs_duration_t libcfs_console_min_delay;
563 unsigned int libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
564 #else /* !HAVE_CATAMOUNT_DATA_H */
565 #ifdef HAVE_NETDB_H
566 #include <sys/utsname.h>
567 #endif /* HAVE_NETDB_H */
568 struct utsname *tmp_utsname;
569 static char source_nid[sizeof(tmp_utsname->nodename)];
570 #endif /* HAVE_CATAMOUNT_DATA_H */
571
572 static int source_pid;
573 int smp_processor_id = 1;
574 char debug_file_path[1024];
575 FILE *debug_file_fd;
576
577 int portals_do_debug_dumplog(void *arg)
578 {
579         printf("Look in %s\n", debug_file_name);
580         return 0;
581 }
582
583
584 void portals_debug_print(void)
585 {
586         return;
587 }
588
589
590 void libcfs_debug_dumplog(void)
591 {
592         printf("Look in %s\n", debug_file_name);
593         return;
594 }
595
596 int libcfs_debug_init(unsigned long bufsize)
597 {
598         char *debug_mask = NULL;
599         char *debug_subsys = NULL;
600         char *debug_filename;
601
602 #ifdef HAVE_CATAMOUNT_DATA_H
603         char *debug_console = NULL;
604         char *debug_ratelimit = NULL;
605         char *debug_max_delay = NULL;
606         char *debug_min_delay = NULL;
607         char *debug_backoff = NULL;
608
609         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
610         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
611
612         snprintf(source_nid, sizeof(source_nid) - 1, "%u", _my_pnid);
613         source_pid = _my_pid;
614
615         debug_console = getenv("LIBLUSTRE_DEBUG_CONSOLE");
616         if (debug_console != NULL) {
617                 toconsole = strtoul(debug_console, NULL, 0);
618                 CDEBUG(D_INFO, "set liblustre toconsole to %u\n", toconsole);
619         }
620         debug_ratelimit = getenv("LIBLUSTRE_DEBUG_CONSOLE_RATELIMIT");
621         if (debug_ratelimit != NULL) {
622                 libcfs_console_ratelimit = strtoul(debug_ratelimit, NULL, 0);
623                 CDEBUG(D_INFO, "set liblustre console ratelimit to %u\n",
624                                 libcfs_console_ratelimit);
625         }
626         debug_max_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY");
627         if (debug_max_delay != NULL)
628                 libcfs_console_max_delay =
629                             cfs_time_seconds(strtoul(debug_max_delay, NULL, 0));
630         debug_min_delay = getenv("LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY");
631         if (debug_min_delay != NULL)
632                 libcfs_console_min_delay =
633                             cfs_time_seconds(strtoul(debug_min_delay, NULL, 0));
634         if (debug_min_delay || debug_max_delay) {
635                 if (!libcfs_console_max_delay || !libcfs_console_min_delay ||
636                     libcfs_console_max_delay < libcfs_console_min_delay) {
637                         libcfs_console_max_delay = CDEBUG_DEFAULT_MAX_DELAY;
638                         libcfs_console_min_delay = CDEBUG_DEFAULT_MIN_DELAY;
639                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_MAX_DELAY "
640                                        "should be greater than "
641                                        "LIBLUSTRE_DEBUG_CONSOLE_MIN_DELAY "
642                                        "and both parameters should be non-null"
643                                        ": restore default values\n");
644                 } else {
645                         CDEBUG(D_INFO, "set liblustre console max delay to %lus"
646                                        " and min delay to %lus\n",
647                                (cfs_duration_t)
648                                      cfs_duration_sec(libcfs_console_max_delay),
649                                (cfs_duration_t)
650                                     cfs_duration_sec(libcfs_console_min_delay));
651                 }
652         }
653         debug_backoff = getenv("LIBLUSTRE_DEBUG_CONSOLE_BACKOFF");
654         if (debug_backoff != NULL) {
655                 libcfs_console_backoff = strtoul(debug_backoff, NULL, 0);
656                 if (libcfs_console_backoff <= 0) {
657                         libcfs_console_backoff = CDEBUG_DEFAULT_BACKOFF;
658                         CDEBUG(D_INFO, "LIBLUSTRE_DEBUG_CONSOLE_BACKOFF <= 0: "
659                                        "restore default value\n");
660                 } else {
661                         CDEBUG(D_INFO, "set liblustre console backoff to %u\n",
662                                libcfs_console_backoff);
663                 }
664         }
665 #else
666         struct utsname myname;
667
668         if (uname(&myname) == 0)
669                 strcpy(source_nid, myname.nodename);
670         source_pid = getpid();
671 #endif
672         /* debug masks */
673         debug_mask = getenv("LIBLUSTRE_DEBUG_MASK");
674         if (debug_mask)
675                 libcfs_debug = (unsigned int) strtol(debug_mask, NULL, 0);
676
677         debug_subsys = getenv("LIBLUSTRE_DEBUG_SUBSYS");
678         if (debug_subsys)
679                 libcfs_subsystem_debug =
680                                 (unsigned int) strtol(debug_subsys, NULL, 0);
681
682         debug_filename = getenv("LIBLUSTRE_DEBUG_BASE");
683         if (debug_filename)
684                 strncpy(debug_file_path,debug_filename,sizeof(debug_file_path));
685
686         debug_filename = getenv("LIBLUSTRE_DEBUG_FILE");
687         if (debug_filename)
688                 strncpy(debug_file_name,debug_filename,sizeof(debug_file_name));
689
690         if (debug_file_name[0] == '\0' && debug_file_path[0] != '\0')
691                 snprintf(debug_file_name, sizeof(debug_file_name) - 1,
692                          "%s-%s-"CFS_TIME_T".log", debug_file_path, source_nid, time(0));
693
694         if (strcmp(debug_file_name, "stdout") == 0 ||
695             strcmp(debug_file_name, "-") == 0) {
696                 debug_file_fd = stdout;
697         } else if (strcmp(debug_file_name, "stderr") == 0) {
698                 debug_file_fd = stderr;
699         } else if (debug_file_name[0] != '\0') {
700                 debug_file_fd = fopen(debug_file_name, "w");
701                 if (debug_file_fd == NULL)
702                         fprintf(stderr, "%s: unable to open '%s': %s\n",
703                                 source_nid, debug_file_name, strerror(errno));
704         }
705
706         if (debug_file_fd == NULL)
707                 debug_file_fd = stdout;
708
709         return 0;
710 }
711
712 int libcfs_debug_cleanup(void)
713 {
714         if (debug_file_fd != stdout && debug_file_fd != stderr)
715                 fclose(debug_file_fd);
716         return 0;
717 }
718
719 int libcfs_debug_clear_buffer(void)
720 {
721         return 0;
722 }
723
724 int libcfs_debug_mark_buffer(char *text)
725 {
726
727         fprintf(debug_file_fd, "*******************************************************************************\n");
728         fprintf(debug_file_fd, "DEBUG MARKER: %s\n", text);
729         fprintf(debug_file_fd, "*******************************************************************************\n");
730
731         return 0;
732 }
733
734 #ifdef HAVE_CATAMOUNT_DATA_H
735 #define CATAMOUNT_MAXLINE (256-4)
736 void catamount_printline(char *buf, size_t size)
737 {
738     char *pos = buf;
739     int prsize = size;
740
741     while (prsize > 0){
742         lputs(pos);
743         pos += CATAMOUNT_MAXLINE;
744         prsize -= CATAMOUNT_MAXLINE;
745     }
746 }
747 #endif
748
749 int
750 libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
751                    int subsys, int mask,
752                    const char *file, const char *fn, const int line,
753                    const char *format1, va_list args,
754                    const char *format2, ...)
755 {
756         struct timeval tv;
757         int            nob;
758         int            remain;
759         va_list        ap;
760         char           buf[CFS_PAGE_SIZE]; /* size 4096 used for compatimble
761                                             * with linux, where message can`t
762                                             * be exceed PAGE_SIZE */
763         int            console = 0;
764         char *prefix = "Lustre";
765
766 #ifdef HAVE_CATAMOUNT_DATA_H
767         /* toconsole == 0 - all messages to debug_file_fd
768          * toconsole == 1 - warnings to console, all to debug_file_fd
769          * toconsole >  1 - all debug to console */
770         if (((mask & libcfs_printk) && toconsole == 1) || toconsole > 1)
771                 console = 1;
772 #endif
773
774         if ((!console) && (!debug_file_fd)) {
775                 return 0;
776         }
777
778         if (mask & (D_EMERG | D_ERROR))
779                prefix = "LustreError";
780
781         nob = snprintf(buf, sizeof(buf), "%s: %u-%s:(%s:%d:%s()): ", prefix,
782                        source_pid, source_nid, file, line, fn);
783
784         remain = sizeof(buf) - nob;
785         if (format1) {
786                 nob += vsnprintf(&buf[nob], remain, format1, args);
787         }
788
789         remain = sizeof(buf) - nob;
790         if ((format2) && (remain > 0)) {
791                 va_start(ap, format2);
792                 nob += vsnprintf(&buf[nob], remain, format2, ap);
793                 va_end(ap);
794         }
795
796 #ifdef HAVE_CATAMOUNT_DATA_H
797         if (console) {
798                 /* check rate limit for console */
799                 if (cdls != NULL) {
800                         if (libcfs_console_ratelimit &&
801                                 cdls->cdls_next != 0 &&     /* not first time ever */
802                                 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
803
804                                 /* skipping a console message */
805                                 cdls->cdls_count++;
806                                 goto out_file;
807                         }
808
809                         if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
810                                            libcfs_console_max_delay +
811                                            cfs_time_seconds(10))) {
812                                 /* last timeout was a long time ago */
813                                 cdls->cdls_delay /= libcfs_console_backoff * 4;
814                         } else {
815                                 cdls->cdls_delay *= libcfs_console_backoff;
816
817                                 if (cdls->cdls_delay <
818                                                 libcfs_console_min_delay)
819                                         cdls->cdls_delay =
820                                                 libcfs_console_min_delay;
821                                 else if (cdls->cdls_delay >
822                                                 libcfs_console_max_delay)
823                                         cdls->cdls_delay =
824                                                 libcfs_console_max_delay;
825                         }
826
827                         /* ensure cdls_next is never zero after it's been seen */
828                         cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
829                 }
830
831                 if (cdls != NULL && cdls->cdls_count != 0) {
832                         char buf2[100];
833
834                         nob = snprintf(buf2, sizeof(buf2),
835                                        "Skipped %d previous similar message%s\n",
836                                        cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
837
838                         catamount_printline(buf2, nob);
839                         cdls->cdls_count = 0;
840                         goto out_file;
841                 }
842                 catamount_printline(buf, nob);
843        }
844 out_file:
845         /* return on toconsole > 1, as we don't want the user getting
846         * spammed by the debug data */
847         if (toconsole > 1)
848                 return 0;
849 #endif
850         if (debug_file_fd == NULL)
851                 return 0;
852
853         gettimeofday(&tv, NULL);
854
855         fprintf(debug_file_fd, CFS_TIME_T".%06lu:%u:%s:(%s:%d:%s()): %s",
856                 tv.tv_sec, tv.tv_usec, source_pid, source_nid,
857                 file, line, fn, buf);
858
859         return 0;
860 }
861
862 void
863 libcfs_assertion_failed(const char *expr, const char *file, const char *func,
864                         const int line)
865 {
866         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
867                          "ASSERTION(%s) failed\n", expr);
868         abort();
869 }
870
871 #endif /* __KERNEL__ */