heap_debug.c 3.7 KB

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