Windows NT 4.0 source code leak
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.

206 lines
4.1 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. imagehlp.c
  5. Abstract:
  6. This function implements a generic simple symbol handler.
  7. Author:
  8. Wesley Witt (wesw) 1-Sep-1994
  9. Environment:
  10. User Mode
  11. --*/
  12. #include "private.h"
  13. HANDLE hHeap;
  14. #ifdef IMAGEHLP_HEAP_DEBUG
  15. LIST_ENTRY HeapHeader;
  16. ULONG TotalMemory;
  17. VOID PrintAllocations(VOID);
  18. ULONG TotalAllocs;
  19. #endif
  20. DWORD
  21. ImageHlpDllEntry(
  22. HINSTANCE hInstance,
  23. DWORD Reason,
  24. LPVOID Context
  25. )
  26. /*++
  27. Routine Description:
  28. DLL initialization function.
  29. Arguments:
  30. hInstance - Instance handle
  31. Reason - Reason for the entrypoint being called
  32. Context - Context record
  33. Return Value:
  34. TRUE - Initialization succeeded
  35. FALSE - Initialization failed
  36. --*/
  37. {
  38. if (Reason == DLL_PROCESS_ATTACH) {
  39. DisableThreadLibraryCalls( hInstance );
  40. #ifdef IMAGEHLP_HEAP_DEBUG
  41. InitializeListHead( &HeapHeader );
  42. #endif
  43. hHeap = HeapCreate( 0, 1024*1024, 0 );
  44. if (!hHeap) {
  45. return FALSE;
  46. } else {
  47. return TRUE;
  48. }
  49. }
  50. if (Reason == DLL_PROCESS_DETACH) {
  51. #ifdef IMAGEHLP_HEAP_DEBUG
  52. PrintAllocations();
  53. #endif
  54. if ( hHeap ) {
  55. HeapDestroy( hHeap );
  56. }
  57. }
  58. return TRUE;
  59. }
  60. #ifdef IMAGEHLP_HEAP_DEBUG
  61. VOID
  62. pCheckHeap(
  63. PVOID MemPtr,
  64. ULONG Line,
  65. LPSTR File
  66. )
  67. {
  68. CHAR buf[256];
  69. CHAR ext[4];
  70. if (!HeapValidate( hHeap, 0, MemPtr )) {
  71. wsprintf( buf, "IMAGEHLP: heap corruption 0x%08x " );
  72. _splitpath( File, NULL, NULL, &buf[strlen(buf)], ext );
  73. strcat( buf, ext );
  74. wsprintf( &buf[strlen(buf)], " @ %d\n", Line );
  75. OutputDebugString( buf );
  76. PrintAllocations();
  77. DebugBreak();
  78. }
  79. }
  80. #endif
  81. PVOID
  82. pMemAlloc(
  83. ULONG AllocSize
  84. #ifdef IMAGEHLP_HEAP_DEBUG
  85. , ULONG Line,
  86. LPSTR File
  87. #endif
  88. )
  89. {
  90. #ifdef IMAGEHLP_HEAP_DEBUG
  91. PHEAP_BLOCK hb;
  92. CHAR ext[4];
  93. hb = (PHEAP_BLOCK) HeapAlloc( hHeap, HEAP_ZERO_MEMORY, AllocSize + sizeof(HEAP_BLOCK) );
  94. if (hb) {
  95. TotalMemory += AllocSize;
  96. TotalAllocs += 1;
  97. InsertTailList( &HeapHeader, &hb->ListEntry );
  98. hb->Signature = HEAP_SIG;
  99. hb->Size = AllocSize;
  100. hb->Line = Line;
  101. _splitpath( File, NULL, NULL, hb->File, ext );
  102. strcat( hb->File, ext );
  103. return (PVOID) ((PUCHAR)hb + sizeof(HEAP_BLOCK));
  104. }
  105. return NULL;
  106. #else
  107. return HeapAlloc( hHeap, HEAP_ZERO_MEMORY, AllocSize );
  108. #endif
  109. }
  110. VOID
  111. pMemFree(
  112. PVOID MemPtr
  113. #ifdef IMAGEHLP_HEAP_DEBUG
  114. , ULONG Line,
  115. LPSTR File
  116. #endif
  117. )
  118. {
  119. #ifdef IMAGEHLP_HEAP_DEBUG
  120. PHEAP_BLOCK hb;
  121. if (!MemPtr) {
  122. return;
  123. }
  124. hb = (PHEAP_BLOCK) ((PUCHAR)MemPtr - sizeof(HEAP_BLOCK));
  125. if (hb->Signature != HEAP_SIG) {
  126. OutputDebugString( "IMAGEHLP: Corrupt heap block\n" );
  127. DebugBreak();
  128. }
  129. RemoveEntryList( &hb->ListEntry );
  130. TotalMemory -= hb->Size;
  131. TotalAllocs -= 1;
  132. HeapFree( hHeap, 0, (PVOID) hb );
  133. #else
  134. if (!MemPtr) {
  135. return;
  136. }
  137. HeapFree( hHeap, 0, MemPtr );
  138. #endif
  139. }
  140. #ifdef IMAGEHLP_HEAP_DEBUG
  141. VOID
  142. PrintAllocations(
  143. VOID
  144. )
  145. {
  146. PLIST_ENTRY Next;
  147. PHEAP_BLOCK hb;
  148. CHAR buf[256];
  149. LARGE_INTEGER PerfFreq;
  150. Next = HeapHeader.Flink;
  151. if (!Next) {
  152. return;
  153. }
  154. OutputDebugString( "-----------------------------------------------------------------------------\n" );
  155. wsprintf( buf, "Memory Allocations for Heap 0x%08x, Allocs=%d, TotalMem=%d\n", hHeap, TotalAllocs, TotalMemory );
  156. OutputDebugString( buf );
  157. OutputDebugString( "-----------------------------------------------------------------------------\n" );
  158. OutputDebugString( "*\n" );
  159. while ((ULONG)Next != (ULONG)&HeapHeader) {
  160. hb = CONTAINING_RECORD( Next, HEAP_BLOCK, ListEntry );
  161. Next = hb->ListEntry.Flink;
  162. wsprintf( buf, "%8d %16s @ %5d\n", hb->Size, hb->File, hb->Line );
  163. OutputDebugString( buf );
  164. }
  165. OutputDebugString( "*\n" );
  166. return;
  167. }
  168. #endif