heap_debug.c 3.4 KB

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