Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
7.3 KiB

  1. /***
  2. *heapdump.c - Output the heap data bases
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Display the heap data bases.
  8. *
  9. * NOTE: This module is NOT released with the C libs. It is for
  10. * debugging purposes only.
  11. *
  12. *Revision History:
  13. * 06-28-89 JCR Module created.
  14. * 07-18-89 JCR Added _heap_print_regions() routine
  15. * 11-13-89 GJF Added MTHREAD support, also fixed copyright
  16. * 12-13-89 GJF Changed name of include file to heap.h
  17. * 12-19-89 GJF Removed references to plastdesc
  18. * 03-11-90 GJF Made the calling type _CALLTYPE1 and added #include
  19. * <cruntime.h>.
  20. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  21. * 09-06-94 CFW Replace MTHREAD with _MT.
  22. * 02-14-95 GJF Appended Mac version of source file (somewhat cleaned
  23. * up), with appropriate #ifdef-s.
  24. * 04-30-95 GJF Made conditional on WINHEAP.
  25. * 05-17-99 PML Remove all Macintosh support.
  26. *
  27. *******************************************************************************/
  28. #ifndef WINHEAP
  29. #include <cruntime.h>
  30. #include <heap.h>
  31. #include <malloc.h>
  32. #include <mtdll.h>
  33. #include <stdio.h>
  34. #ifdef DEBUG
  35. /***
  36. *_heap_print_all - Print the whole heap
  37. *
  38. *Purpose:
  39. *
  40. *Entry:
  41. * <void>
  42. *
  43. *Exit:
  44. * <void>
  45. *
  46. *Exceptions:
  47. *
  48. *******************************************************************************/
  49. void __cdecl _heap_print_all(void)
  50. {
  51. /* lock the heap
  52. */
  53. _mlock(_HEAP_LOCK);
  54. _heap_print_regions_lk();
  55. _heap_print_desc_lk();
  56. _heap_print_emptylist_lk();
  57. _heap_print_heaplist_lk();
  58. /* release the heap lock
  59. */
  60. _munlock(_HEAP_LOCK);
  61. }
  62. /***
  63. *_heap_print_regions - Print the heap region table
  64. *
  65. *Purpose:
  66. *
  67. *Entry:
  68. * <void>
  69. *
  70. *Exit:
  71. * <void>
  72. *
  73. *Exceptions:
  74. *
  75. *******************************************************************************/
  76. #ifdef _MT
  77. void __cdecl _heap_print_regions(void)
  78. {
  79. /* lock the heap
  80. */
  81. _mlock(_HEAP_LOCK);
  82. _heap_print_regions_lk();
  83. /* release the heap lock
  84. */
  85. _munlock(_HEAP_LOCK);
  86. }
  87. void __cdecl _heap_print_regions_lk(void)
  88. #else /* ndef _MT */
  89. void __cdecl _heap_print_regions(void)
  90. #endif /* _MT */
  91. {
  92. int i;
  93. printf("\n--- Heap Regions ---\n\n");
  94. printf("\t_heap_growsize (_amblksiz) = \t%x\n", _heap_growsize);
  95. printf("\t_heap_regionsize = \t%x\n\n", _heap_regionsize);
  96. printf("\t_regbase\t_currsize\t_totalsize\n");
  97. printf("\t--------\t---------\t----------\n");
  98. for (i=0; i < _HEAP_REGIONMAX; i++) {
  99. printf("\t%x\t\t%x\t\t%x\n",
  100. _heap_regions[i]._regbase,
  101. _heap_regions[i]._currsize,
  102. _heap_regions[i]._totalsize);
  103. }
  104. }
  105. /***
  106. *_heap_print_desc - Print the heap descriptor
  107. *
  108. *Purpose:
  109. *
  110. *Entry:
  111. * <void>
  112. *
  113. *Exit:
  114. * <void>
  115. *
  116. *Exceptions:
  117. *
  118. *******************************************************************************/
  119. #ifdef _MT
  120. void __cdecl _heap_print_desc(void)
  121. {
  122. _mlock(_HEAP_LOCK);
  123. _heap_print_desc_lk();
  124. _munlock(_HEAP_LOCK);
  125. }
  126. void __cdecl _heap_print_desc_lk(void)
  127. #else /* ndef _MT */
  128. void __cdecl _heap_print_desc(void)
  129. #endif /* _MT */
  130. {
  131. printf("\n--- Heap Descriptor ---\n\n");
  132. printf("\tpfirstdesc = %p\n", _heap_desc.pfirstdesc);
  133. printf("\tproverdesc = %p\n", _heap_desc.proverdesc);
  134. printf("\temptylist = %p\n", _heap_desc.emptylist);
  135. printf("\t&sentinel = %p\n", &(_heap_desc.sentinel));
  136. }
  137. /***
  138. *_heap_print_emptylist - Print out the empty heap desc list
  139. *
  140. *Purpose:
  141. *
  142. *Entry:
  143. * <void>
  144. *
  145. *Exit:
  146. * <void>
  147. *
  148. *Exceptions:
  149. *
  150. *******************************************************************************/
  151. #ifdef _MT
  152. void __cdecl _heap_print_emptylist(void)
  153. {
  154. /* lock the heap
  155. */
  156. _mlock(_HEAP_LOCK);
  157. _heap_print_emptylist_lk();
  158. /* release the heap lock
  159. */
  160. _munlock(_HEAP_LOCK);
  161. }
  162. void __cdecl _heap_print_emptylist_lk(void)
  163. #else /* ndef _MT */
  164. void __cdecl _heap_print_emptylist(void)
  165. #endif /* _MT */
  166. {
  167. _PBLKDESC p;
  168. int i;
  169. printf("\n--- Heap Empty Descriptor List ---\n\n");
  170. if ((p = _heap_desc.emptylist) == NULL) {
  171. printf("\t *** List is empty ***\n");
  172. return;
  173. }
  174. for (i=1; p != NULL; p=p->pnextdesc, i++) {
  175. printf("\t(%i) Address = %p\n", i, p);
  176. printf("\t\tpnextdesc = %p, pblock = %p\n\n",
  177. p->pnextdesc, p->pblock);
  178. }
  179. printf("\t--- End of table ---\n");
  180. }
  181. /***
  182. *_heap_print_heaplist - Print out the heap desc list
  183. *
  184. *Purpose:
  185. *
  186. *Entry:
  187. * <void>
  188. *
  189. *Exit:
  190. * <void>
  191. *
  192. *Exceptions:
  193. *
  194. *******************************************************************************/
  195. #ifdef _MT
  196. void __cdecl _heap_print_heaplist(void)
  197. {
  198. /* lock the heap
  199. */
  200. _mlock(_HEAP_LOCK);
  201. _heap_print_heaplist_lk();
  202. /* release the heap lock
  203. */
  204. _munlock(_HEAP_LOCK);
  205. }
  206. void __cdecl _heap_print_heaplist_lk(void)
  207. #else /* ndef _MT */
  208. void __cdecl _heap_print_heaplist(void)
  209. #endif /* _MT */
  210. {
  211. _PBLKDESC p;
  212. _PBLKDESC next;
  213. int i;
  214. int error = 0;
  215. printf("\n--- Heap Descriptor List ---\n\n");
  216. if ((p = _heap_desc.pfirstdesc) == NULL) {
  217. printf("\t *** List is empty ***\n");
  218. return;
  219. }
  220. for (i=1; p != NULL; i++) {
  221. next = p->pnextdesc;
  222. /* Print descriptor address */
  223. printf("\t(%i) Address = %p ", i, p);
  224. if (p == &_heap_desc.sentinel)
  225. printf("<SENTINEL>\n");
  226. else if (p == _heap_desc.proverdesc)
  227. printf("<ROVER>\n");
  228. else
  229. printf("\n");
  230. /* Print descriptor contents */
  231. printf("\t\tpnextdesc = %p, pblock = %p",
  232. p->pnextdesc, p->pblock);
  233. if (p == &_heap_desc.sentinel) {
  234. if (next != NULL) {
  235. printf("\n\t*** ERROR: sentinel.pnextdesc != NULL ***\n");
  236. error++;
  237. }
  238. }
  239. else if (_IS_INUSE(p))
  240. printf(", usersize = %u <INUSE>", _BLKSIZE(p));
  241. else if (_IS_FREE(p))
  242. printf(", usersize = %u <FREE>", _BLKSIZE(p));
  243. else if (_IS_DUMMY(p))
  244. printf(", size = %u <DUMMY>", _MEMSIZE(p));
  245. else {
  246. printf(",\n\t*** ERROR: unknown status ***\n");
  247. error++;
  248. }
  249. printf("\n\n");
  250. if (_heap_desc.pfirstdesc == &_heap_desc.sentinel) {
  251. printf("[No memory in heap]\n");
  252. }
  253. p = next;
  254. }
  255. if (error)
  256. printf("\n\t *** ERRORS IN HEAP TABLE ***\n");
  257. printf("\t--- End of table ---\n");
  258. }
  259. #endif /* DEBUG */
  260. #endif /* WINHEAP */