malloc_debug.c 1.8 KB

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