malloc_debug.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #define atoi __atoi
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include "malloc.h"
  19. #include "heap.h"
  20. int __malloc_debug = 0, __malloc_check = 0;
  21. #ifdef MALLOC_MMB_DEBUGGING
  22. int __malloc_mmb_debug = 0;
  23. #endif
  24. /* Debugging output is indented this may levels. */
  25. int __malloc_debug_cur_indent = 0;
  26. /* Print FMT and args indented at the current debug print level, followed
  27. by a newline, and change the level by INDENT. */
  28. void
  29. __malloc_debug_printf (int indent, const char *fmt, ...)
  30. {
  31. unsigned spaces = __malloc_debug_cur_indent * MALLOC_DEBUG_INDENT_SIZE;
  32. va_list val;
  33. while (spaces > 0)
  34. {
  35. putc (' ', stderr);
  36. spaces--;
  37. }
  38. va_start (val, fmt);
  39. vfprintf (stderr, fmt, val);
  40. va_end (val);
  41. putc ('\n', stderr);
  42. __malloc_debug_indent (indent);
  43. }
  44. void
  45. __malloc_debug_init (void)
  46. {
  47. char *ev = __getenv ("MALLOC_DEBUG");
  48. if (ev)
  49. {
  50. int val = atoi (ev);
  51. if (val & 1)
  52. __malloc_check = 1;
  53. if (val & 2)
  54. __malloc_debug = 1;
  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. }