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