malloc_debug.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. int __malloc_debug = 0, __malloc_check = 0;
  20. #ifdef MALLOC_MMB_DEBUGGING
  21. int __malloc_mmb_debug = 0;
  22. #endif
  23. /* Debugging output is indented this may levels. */
  24. int __malloc_debug_cur_indent = 0;
  25. /* Print FMT and args indented at the current debug print level, followed
  26. by a newline, and change the level by INDENT. */
  27. void
  28. __malloc_debug_printf (int indent, const char *fmt, ...)
  29. {
  30. unsigned spaces = __malloc_debug_cur_indent * MALLOC_DEBUG_INDENT_SIZE;
  31. va_list val;
  32. while (spaces > 0)
  33. {
  34. putc (' ', stderr);
  35. spaces--;
  36. }
  37. va_start (val, fmt);
  38. vfprintf (stderr, fmt, val);
  39. va_end (val);
  40. putc ('\n', stderr);
  41. __malloc_debug_indent (indent);
  42. }
  43. void
  44. __malloc_debug_init (void)
  45. {
  46. char *ev = getenv ("MALLOC_DEBUG");
  47. if (ev)
  48. {
  49. int val = atoi (ev);
  50. if (val & 1)
  51. __malloc_check = 1;
  52. if (val & 2)
  53. __malloc_debug = 1;
  54. #ifdef MALLOC_MMB_DEBUGGING
  55. if (val & 4)
  56. __malloc_mmb_debug = 1;
  57. #endif
  58. #ifdef HEAP_DEBUGGING
  59. if (val & 8)
  60. __heap_debug = 1;
  61. #endif
  62. if (val)
  63. __malloc_debug_printf
  64. (0, "malloc_debug: initialized to %d (check = %d, dump = %d, dump_mmb = %d, dump_heap = %d)",
  65. val,
  66. !!(val & 1), !!(val & 2),
  67. !!(val & 4), !!(val & 8));
  68. }
  69. }