heap_debug.c 3.6 KB

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