Leaked source code of windows server 2003
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.

231 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. heap.h
  5. Abstract:
  6. WinDbg Extension Api
  7. Author:
  8. Kshitiz K Sharma
  9. Environment:
  10. User/Kernel Mode.
  11. Revision History:
  12. --*/
  13. #define HEAP_GRANULARITY_SHIFT 3 // Log2( HEAP_GRANULARITY )
  14. #define HEAP_MAXIMUM_BLOCK_SIZE (USHORT)(((0x10000 << HEAP_GRANULARITY_SHIFT) - PageSize) >> HEAP_GRANULARITY_SHIFT)
  15. #define HEAP_MAXIMUM_FREELISTS 128
  16. #define HEAP_MAXIMUM_SEGMENTS 64
  17. #define HEAP_ENTRY_BUSY 0x01
  18. #define HEAP_ENTRY_EXTRA_PRESENT 0x02
  19. #define HEAP_ENTRY_FILL_PATTERN 0x04
  20. #define HEAP_ENTRY_VIRTUAL_ALLOC 0x08
  21. #define HEAP_ENTRY_LAST_ENTRY 0x10
  22. #define HEAP_ENTRY_SETTABLE_FLAG1 0x20
  23. #define HEAP_ENTRY_SETTABLE_FLAG2 0x40
  24. #define HEAP_ENTRY_SETTABLE_FLAG3 0x80
  25. #define HEAP_ENTRY_SETTABLE_FLAGS 0xE0
  26. #define HEAP_SEGMENT_SIGNATURE 0xFFEEFFEE
  27. #define HEAP_SEGMENT_USER_ALLOCATED (ULONG)0x00000001
  28. #define HEAP_SIGNATURE (ULONG)0xEEFFEEFF
  29. #define HEAP_LOCK_USER_ALLOCATED (ULONG)0x80000000
  30. #define HEAP_VALIDATE_PARAMETERS_ENABLED (ULONG)0x40000000
  31. #define HEAP_VALIDATE_ALL_ENABLED (ULONG)0x20000000
  32. #define HEAP_SKIP_VALIDATION_CHECKS (ULONG)0x10000000
  33. #define HEAP_CAPTURE_STACK_BACKTRACES (ULONG)0x08000000
  34. #define CHECK_HEAP_TAIL_SIZE GetTypeSize("nt!_HEAP_ENTRY")
  35. #define CHECK_HEAP_TAIL_FILL 0xAB
  36. #define FREE_HEAP_FILL 0xFEEEFEEE
  37. #define ALLOC_HEAP_FILL 0xBAADF00D
  38. #define HEAP_MAXIMUM_SMALL_TAG 0xFF
  39. #define HEAP_SMALL_TAG_MASK (HEAP_MAXIMUM_SMALL_TAG << HEAP_TAG_SHIFT)
  40. #define HEAP_NEED_EXTRA_FLAGS ((HEAP_TAG_MASK ^ HEAP_SMALL_TAG_MASK) | \
  41. HEAP_CAPTURE_STACK_BACKTRACES | \
  42. HEAP_SETTABLE_USER_VALUE \
  43. )
  44. #define HEAP_NUMBER_OF_PSEUDO_TAG (HEAP_MAXIMUM_FREELISTS+1)
  45. #define HEAP_NO_SERIALIZE 0x00000001 // winnt
  46. #define HEAP_GROWABLE 0x00000002 // winnt
  47. #define HEAP_GENERATE_EXCEPTIONS 0x00000004 // winnt
  48. #define HEAP_ZERO_MEMORY 0x00000008 // winnt
  49. #define HEAP_REALLOC_IN_PLACE_ONLY 0x00000010 // winnt
  50. #define HEAP_TAIL_CHECKING_ENABLED 0x00000020 // winnt
  51. #define HEAP_FREE_CHECKING_ENABLED 0x00000040 // winnt
  52. #define HEAP_DISABLE_COALESCE_ON_FREE 0x00000080 // winnt
  53. #define HEAP_CREATE_ALIGN_16 0x00010000 // winnt Create heap with 16 byte alignment (obsolete)
  54. #define HEAP_CREATE_ENABLE_TRACING 0x00020000 // winnt Create heap call tracing enabled (obsolete)
  55. #define HEAP_SETTABLE_USER_VALUE 0x00000100
  56. #define HEAP_SETTABLE_USER_FLAG1 0x00000200
  57. #define HEAP_SETTABLE_USER_FLAG2 0x00000400
  58. #define HEAP_SETTABLE_USER_FLAG3 0x00000800
  59. #define HEAP_SETTABLE_USER_FLAGS 0x00000E00
  60. #define HEAP_CLASS_0 0x00000000 // process heap
  61. #define HEAP_CLASS_1 0x00001000 // private heap
  62. #define HEAP_CLASS_2 0x00002000 // Kernel Heap
  63. #define HEAP_CLASS_3 0x00003000 // GDI heap
  64. #define HEAP_CLASS_4 0x00004000 // User heap
  65. #define HEAP_CLASS_5 0x00005000 // Console heap
  66. #define HEAP_CLASS_6 0x00006000 // User Desktop heap
  67. #define HEAP_CLASS_7 0x00007000 // Csrss Shared heap
  68. #define HEAP_CLASS_8 0x00008000 // Csr Port heap
  69. #define HEAP_CLASS_MASK 0x0000F000
  70. #define HEAP_MAXIMUM_TAG 0x0FFF // winnt
  71. #define HEAP_GLOBAL_TAG 0x0800
  72. #define HEAP_PSEUDO_TAG_FLAG 0x8000 // winnt
  73. #define HEAP_TAG_SHIFT 18 // winnt
  74. #define HEAP_MAKE_TAG_FLAGS( b, o ) ((ULONG)((b) + ((o) << 18))) // winnt
  75. #define HEAP_TAG_MASK (HEAP_MAXIMUM_TAG << HEAP_TAG_SHIFT)
  76. #define HEAP_CREATE_VALID_MASK (HEAP_NO_SERIALIZE | \
  77. HEAP_GROWABLE | \
  78. HEAP_GENERATE_EXCEPTIONS | \
  79. HEAP_ZERO_MEMORY | \
  80. HEAP_REALLOC_IN_PLACE_ONLY | \
  81. HEAP_TAIL_CHECKING_ENABLED | \
  82. HEAP_FREE_CHECKING_ENABLED | \
  83. HEAP_DISABLE_COALESCE_ON_FREE | \
  84. HEAP_CLASS_MASK | \
  85. HEAP_CREATE_ALIGN_16 | \
  86. HEAP_CREATE_ENABLE_TRACING)
  87. //
  88. // Definitions from \nt\base\ntos\inc\heappage.h
  89. //
  90. #define PAGE_HEAP_ENABLE_PAGE_HEAP 0x0001
  91. #define PAGE_HEAP_COLLECT_STACK_TRACES 0x0002
  92. #define PAGE_HEAP_NO_UMDH_SUPPORT 0x0004
  93. #define PAGE_HEAP_RESERVED_08 0x0008
  94. #define PAGE_HEAP_CATCH_BACKWARD_OVERRUNS 0x0010
  95. #define PAGE_HEAP_UNALIGNED_ALLOCATIONS 0x0020
  96. #define PAGE_HEAP_SMART_MEMORY_USAGE 0x0040 // obsolete
  97. #define PAGE_HEAP_USE_SIZE_RANGE 0x0080
  98. #define PAGE_HEAP_USE_DLL_RANGE 0x0100
  99. #define PAGE_HEAP_USE_RANDOM_DECISION 0x0200
  100. #define PAGE_HEAP_USE_DLL_NAMES 0x0400
  101. #define PAGE_HEAP_USE_FAULT_INJECTION 0x0800
  102. #define PAGE_HEAP_PROTECT_META_DATA 0x1000
  103. #define PAGE_HEAP_CHECK_NO_SERIALIZE_ACCESS 0x2000
  104. #define PAGE_HEAP_NO_LOCK_CHECKS 0x4000
  105. #define PAGE_HEAP_USE_READONLY 0x8000
  106. //
  107. // heap walking contexts.
  108. //
  109. #define CONTEXT_START_GLOBALS 11
  110. #define CONTEXT_START_HEAP 1
  111. #define CONTEXT_END_HEAP 2
  112. #define CONTEXT_START_SEGMENT 3
  113. #define CONTEXT_END_SEGMENT 4
  114. #define CONTEXT_FREE_BLOCK 5
  115. #define CONTEXT_BUSY_BLOCK 6
  116. #define CONTEXT_LOOKASIDE_BLOCK 7
  117. #define CONTEXT_VIRTUAL_BLOCK 8
  118. #define CONTEXT_END_BLOCKS 9
  119. #define CONTEXT_ERROR 10
  120. #define CONTEXT_LFH_HEAP 11
  121. #define CONTEXT_START_SUBSEGMENT 12
  122. #define CONTEXT_END_SUBSEGMENT 13
  123. #define SCANPROCESS 1
  124. #define SCANHEAP 2
  125. #define SCANSEGMENT 3
  126. extern ULONG ScanLevel;
  127. typedef BOOLEAN (*HEAP_ITERATOR_CALLBACK)(
  128. IN ULONG Context,
  129. IN ULONG64 HeapAddress,
  130. IN ULONG64 SegmentAddress,
  131. IN ULONG64 EntryAddress,
  132. IN ULONG64 Data
  133. );
  134. void
  135. ScanProcessHeaps (
  136. IN ULONG64 AddressToDump,
  137. IN ULONG64 ProcessPeb,
  138. HEAP_ITERATOR_CALLBACK HeapCallback
  139. );
  140. void InspectLeaks (
  141. IN ULONG64 AddressToDump,
  142. IN ULONG64 ProcessPeb
  143. );
  144. void
  145. DumpEntryHeader();
  146. void
  147. DumpEntryInfo(
  148. IN ULONG64 HeapAddress,
  149. IN ULONG64 SegmentAddress,
  150. ULONG64 EntryAddress
  151. );
  152. void
  153. DumpEntryFlagDescription (
  154. ULONG Flags
  155. );
  156. BOOLEAN
  157. SearchVMReference (
  158. HANDLE hProcess,
  159. ULONG64 Base,
  160. ULONG64 EndAddress
  161. );
  162. extern ULONG PageSize;
  163. extern ULONG HeapEntrySize;
  164. extern ULONG PointerSize;
  165. BOOLEAN
  166. InitializeHeapExtension();
  167. VOID
  168. HeapDetectLeaks();
  169. VOID
  170. HeapFindBlock (
  171. LPCTSTR szArguments
  172. );
  173. VOID
  174. HeapStat(
  175. LPCTSTR szArguments
  176. );