malloc_debug.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * libc/stdlib/malloc/malloc_debug.c -- malloc debugging support
  3. *
  4. * Copyright (C) 2002 NEC Corporation
  5. * Copyright (C) 2002 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU Lesser
  8. * General Public License. See the file COPYING.LIB in the main
  9. * directory of this archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include "malloc.h"
  18. #include "heap.h"
  19. #ifdef MALLOC_DEBUGGING
  20. int __malloc_debug = 0, __malloc_check = 0;
  21. #endif
  22. #ifdef MALLOC_MMB_DEBUGGING
  23. int __malloc_mmb_debug = 0;
  24. #endif
  25. /* Debugging output is indented this may levels. */
  26. int __malloc_debug_cur_indent = 0;
  27. /* Print FMT and args indented at the current debug print level, followed
  28. by a newline, and change the level by INDENT. */
  29. void
  30. __malloc_debug_printf (int indent, const char *fmt, ...)
  31. {
  32. int i;
  33. va_list val;
  34. for (i = 0; i < __malloc_debug_cur_indent; i++)
  35. fputs (" ", stderr);
  36. va_start (val, fmt);
  37. vfprintf (stderr, fmt, val);
  38. va_end (val);
  39. putc ('\n', stderr);
  40. __malloc_debug_indent (indent);
  41. }
  42. void
  43. __malloc_debug_init (void)
  44. {
  45. char *ev = getenv ("MALLOC_DEBUG");
  46. if (ev)
  47. {
  48. int val = atoi (ev);
  49. if (val & 1)
  50. __malloc_check = 1;
  51. #ifdef MALLOC_DEBUGGING
  52. if (val & 2)
  53. __malloc_debug = 1;
  54. #endif
  55. #ifdef MALLOC_MMB_DEBUGGING
  56. if (val & 4)
  57. __malloc_mmb_debug = 1;
  58. #endif
  59. #ifdef HEAP_DEBUGGING
  60. if (val & 8)
  61. __heap_debug = 1;
  62. #endif
  63. if (val)
  64. __malloc_debug_printf
  65. (0, "malloc_debug: initialized to %d (check = %d, dump = %d, dump_mmb = %d, dump_heap = %d)",
  66. val,
  67. !!(val & 1), !!(val & 2),
  68. !!(val & 4), !!(val & 8));
  69. }
  70. }