malloc_debug.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #define vfprintf __vfprintf
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <stdarg.h>
  19. #include "malloc.h"
  20. #include "heap.h"
  21. int __malloc_debug = 0, __malloc_check = 0;
  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. unsigned spaces = __malloc_debug_cur_indent * MALLOC_DEBUG_INDENT_SIZE;
  33. va_list val;
  34. while (spaces > 0)
  35. {
  36. putc (' ', stderr);
  37. spaces--;
  38. }
  39. va_start (val, fmt);
  40. vfprintf (stderr, fmt, val);
  41. va_end (val);
  42. putc ('\n', stderr);
  43. __malloc_debug_indent (indent);
  44. }
  45. void
  46. __malloc_debug_init (void)
  47. {
  48. char *ev = __getenv ("MALLOC_DEBUG");
  49. if (ev)
  50. {
  51. int val = atoi (ev);
  52. if (val & 1)
  53. __malloc_check = 1;
  54. if (val & 2)
  55. __malloc_debug = 1;
  56. #ifdef MALLOC_MMB_DEBUGGING
  57. if (val & 4)
  58. __malloc_mmb_debug = 1;
  59. #endif
  60. #ifdef HEAP_DEBUGGING
  61. if (val & 8)
  62. __heap_debug = 1;
  63. #endif
  64. if (val)
  65. __malloc_debug_printf
  66. (0, "malloc_debug: initialized to %d (check = %d, dump = %d, dump_mmb = %d, dump_heap = %d)",
  67. val,
  68. !!(val & 1), !!(val & 2),
  69. !!(val & 4), !!(val & 8));
  70. }
  71. }