debug.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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[256]; /* 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 = vsprintf(message, fp, ap);
  44. if (level != CE_DEBUG && message[len-1] != '\n')
  45. strcat(message, "\n");
  46. printk("%s%s", err_level[level], message);
  47. va_end(ap);
  48. spin_unlock_irqrestore(&xfs_err_lock,flags);
  49. BUG_ON(level == CE_PANIC);
  50. }
  51. void
  52. icmn_err(register int level, char *fmt, va_list ap)
  53. {
  54. ulong flags;
  55. int len;
  56. level &= XFS_ERR_MASK;
  57. if(level > XFS_MAX_ERR_LEVEL)
  58. level = XFS_MAX_ERR_LEVEL;
  59. spin_lock_irqsave(&xfs_err_lock,flags);
  60. len = vsprintf(message, fmt, ap);
  61. if (level != CE_DEBUG && message[len-1] != '\n')
  62. strcat(message, "\n");
  63. spin_unlock_irqrestore(&xfs_err_lock,flags);
  64. printk("%s%s", err_level[level], message);
  65. BUG_ON(level == CE_PANIC);
  66. }
  67. void
  68. assfail(char *expr, char *file, int line)
  69. {
  70. printk("Assertion failed: %s, file: %s, line: %d\n", expr, file, line);
  71. BUG();
  72. }
  73. #if ((defined(DEBUG) || defined(INDUCE_IO_ERRROR)) && !defined(NO_WANT_RANDOM))
  74. unsigned long random(void)
  75. {
  76. static unsigned long RandomValue = 1;
  77. /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
  78. register long rv = RandomValue;
  79. register long lo;
  80. register long hi;
  81. hi = rv / 127773;
  82. lo = rv % 127773;
  83. rv = 16807 * lo - 2836 * hi;
  84. if (rv <= 0) rv += 2147483647;
  85. return RandomValue = rv;
  86. }
  87. #endif /* DEBUG || INDUCE_IO_ERRROR || !NO_WANT_RANDOM */