debug.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <xfs.h>
  19. #include "debug.h"
  20. #include "spin.h"
  21. static char message[1024]; /* keep it off the stack */
  22. static DEFINE_SPINLOCK(xfs_err_lock);
  23. /* Translate from CE_FOO to KERN_FOO, err_level(CE_FOO) == KERN_FOO */
  24. #define XFS_MAX_ERR_LEVEL 7
  25. #define XFS_ERR_MASK ((1 << 3) - 1)
  26. static const char * const err_level[XFS_MAX_ERR_LEVEL+1] =
  27. {KERN_EMERG, KERN_ALERT, KERN_CRIT,
  28. KERN_ERR, KERN_WARNING, KERN_NOTICE,
  29. KERN_INFO, KERN_DEBUG};
  30. void
  31. cmn_err(register int level, char *fmt, ...)
  32. {
  33. char *fp = fmt;
  34. int len;
  35. ulong flags;
  36. va_list ap;
  37. level &= XFS_ERR_MASK;
  38. if (level > XFS_MAX_ERR_LEVEL)
  39. level = XFS_MAX_ERR_LEVEL;
  40. spin_lock_irqsave(&xfs_err_lock,flags);
  41. va_start(ap, fmt);
  42. if (*fmt == '!') fp++;
  43. len = vsnprintf(message, sizeof(message), fp, ap);
  44. if (len >= sizeof(message))
  45. len = sizeof(message) - 1;
  46. if (message[len-1] == '\n')
  47. message[len-1] = 0;
  48. printk("%s%s\n", err_level[level], message);
  49. va_end(ap);
  50. spin_unlock_irqrestore(&xfs_err_lock,flags);
  51. BUG_ON(level == CE_PANIC);
  52. }
  53. void
  54. icmn_err(register int level, char *fmt, va_list ap)
  55. {
  56. ulong flags;
  57. int len;
  58. level &= XFS_ERR_MASK;
  59. if(level > XFS_MAX_ERR_LEVEL)
  60. level = XFS_MAX_ERR_LEVEL;
  61. spin_lock_irqsave(&xfs_err_lock,flags);
  62. len = vsnprintf(message, sizeof(message), fmt, ap);
  63. if (len >= sizeof(message))
  64. len = sizeof(message) - 1;
  65. if (message[len-1] == '\n')
  66. message[len-1] = 0;
  67. printk("%s%s\n", err_level[level], message);
  68. spin_unlock_irqrestore(&xfs_err_lock,flags);
  69. BUG_ON(level == CE_PANIC);
  70. }
  71. void
  72. assfail(char *expr, char *file, int line)
  73. {
  74. printk("Assertion failed: %s, file: %s, line: %d\n", expr, file, line);
  75. BUG();
  76. }
  77. #if ((defined(DEBUG) || defined(INDUCE_IO_ERRROR)) && !defined(NO_WANT_RANDOM))
  78. unsigned long random(void)
  79. {
  80. static unsigned long RandomValue = 1;
  81. /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
  82. register long rv = RandomValue;
  83. register long lo;
  84. register long hi;
  85. hi = rv / 127773;
  86. lo = rv % 127773;
  87. rv = 16807 * lo - 2836 * hi;
  88. if (rv <= 0) rv += 2147483647;
  89. return RandomValue = rv;
  90. }
  91. #endif /* DEBUG || INDUCE_IO_ERRROR || !NO_WANT_RANDOM */