heap_debug.c 3.5 KB

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