heap_debug.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * libc/stdlib/malloc/heap_debug.c -- optional heap debugging routines
  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 <stdarg.h>
  16. #include <string.h>
  17. libc_hidden_proto(vfprintf)
  18. libc_hidden_proto(fprintf)
  19. #include "malloc.h"
  20. #include "heap.h"
  21. #ifdef HEAP_DEBUGGING
  22. int __heap_debug = 0;
  23. #endif
  24. static void
  25. __heap_dump_freelist (struct heap *heap)
  26. {
  27. struct heap_free_area *fa;
  28. for (fa = heap->free_areas; fa; fa = fa->next)
  29. __malloc_debug_printf (0,
  30. "0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx",
  31. (long)fa,
  32. (long)HEAP_FREE_AREA_START (fa),
  33. (long)HEAP_FREE_AREA_END (fa),
  34. fa->size,
  35. (long)fa->prev,
  36. (long)fa->next);
  37. }
  38. /* Output a text representation of HEAP to stderr, labelling it with STR. */
  39. void
  40. __heap_dump (struct heap *heap, const char *str)
  41. {
  42. static int recursed = 0;
  43. if (! recursed)
  44. {
  45. __heap_check (heap, str);
  46. recursed = 1;
  47. __malloc_debug_printf (1, "%s: heap @0x%lx:", str, (long)heap);
  48. __heap_dump_freelist (heap);
  49. __malloc_debug_indent (-1);
  50. recursed = 0;
  51. }
  52. }
  53. /* Output an error message to stderr, and exit. STR is printed with the
  54. failure message. */
  55. static void
  56. __heap_check_failure (struct heap *heap, struct heap_free_area *fa,
  57. const char *str, char *fmt, ...)
  58. {
  59. va_list val;
  60. if (str)
  61. fprintf (stderr, "\nHEAP CHECK FAILURE %s: ", str);
  62. else
  63. fprintf (stderr, "\nHEAP CHECK FAILURE: ");
  64. va_start (val, fmt);
  65. vfprintf (stderr, fmt, val);
  66. va_end (val);
  67. __putc ('\n', stderr);
  68. __malloc_debug_set_indent (0);
  69. __malloc_debug_printf (1, "heap dump:");
  70. __heap_dump_freelist (heap);
  71. __exit (22);
  72. }
  73. /* Do some consistency checks on HEAP. If they fail, output an error
  74. message to stderr, and exit. STR is printed with the failure message. */
  75. void
  76. __heap_check (struct heap *heap, const char *str)
  77. {
  78. typedef unsigned long ul_t;
  79. struct heap_free_area *fa, *prev;
  80. struct heap_free_area *first_fa = heap->free_areas;
  81. if (first_fa && first_fa->prev)
  82. __heap_check_failure (heap, first_fa, str,
  83. "first free-area has non-zero prev pointer:\n\
  84. first free-area = 0x%lx\n\
  85. (0x%lx)->prev = 0x%lx\n",
  86. (ul_t)first_fa,
  87. (ul_t)first_fa, (ul_t)first_fa->prev);
  88. for (prev = 0, fa = first_fa; fa; prev = fa, fa = fa->next)
  89. {
  90. if (((ul_t)HEAP_FREE_AREA_END (fa) & (HEAP_GRANULARITY - 1))
  91. || (fa->size & (HEAP_GRANULARITY - 1)))
  92. __heap_check_failure (heap, fa, str, "alignment error:\n\
  93. (0x%lx)->start = 0x%lx\n\
  94. (0x%lx)->size = 0x%lx\n",
  95. (ul_t)fa,
  96. (ul_t)HEAP_FREE_AREA_START (fa),
  97. (ul_t)fa, fa->size);
  98. if (fa->prev != prev)
  99. __heap_check_failure (heap, fa, str, "prev pointer corrupted:\n\
  100. (0x%lx)->next = 0x%lx\n\
  101. (0x%lx)->prev = 0x%lx\n",
  102. (ul_t)prev, (ul_t)prev->next,
  103. (ul_t)fa, (ul_t)fa->prev);
  104. if (prev)
  105. {
  106. ul_t start = (ul_t)HEAP_FREE_AREA_START (fa);
  107. ul_t prev_end = (ul_t)HEAP_FREE_AREA_END (prev);
  108. if (prev_end >= start)
  109. __heap_check_failure (heap, fa, str,
  110. "start %s with prev free-area end:\n\
  111. (0x%lx)->prev = 0x%lx\n\
  112. (0x%lx)->start = 0x%lx\n\
  113. (0x%lx)->end = 0x%lx\n",
  114. (prev_end == start ? "unmerged" : "overlaps"),
  115. (ul_t)fa, (ul_t)prev,
  116. (ul_t)fa, start,
  117. (ul_t)prev, prev_end);
  118. }
  119. }
  120. }