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.

127 lines
4.0 KiB

  1. #include "private.h"
  2. #include <ntsdexts.h>
  3. #ifdef __cplusplus
  4. #define CPPMOD extern "C"
  5. #else
  6. #define CPPMOD
  7. #endif
  8. #define DECLARE_API(s) \
  9. CPPMOD VOID \
  10. s( \
  11. HANDLE hCurrentProcess, \
  12. HANDLE hCurrentThread, \
  13. DWORD dwCurrentPc, \
  14. PNTSD_EXTENSION_APIS lpExtensionApis, \
  15. LPSTR lpArgumentString \
  16. )
  17. #define INIT_API() { \
  18. ExtensionApis = *lpExtensionApis; \
  19. ExtensionCurrentProcess = hCurrentProcess; \
  20. }
  21. #define dprintf (ExtensionApis.lpOutputRoutine)
  22. #define GetExpression (ExtensionApis.lpGetExpressionRoutine)
  23. #define GetSymbol (ExtensionApis.lpGetSymbolRoutine)
  24. #define Disassm (ExtensionApis.lpDisasmRoutine)
  25. #define CheckControlC (ExtensionApis.lpCheckControlCRoutine)
  26. #define ReadMemory(a,b,c,d) ReadProcessMemory( ExtensionCurrentProcess, (LPCVOID)(a), (b), (c), (d) )
  27. #define WriteMemory(a,b,c,d) WriteProcessMemory( ExtensionCurrentProcess, (LPVOID)(a), (LPVOID)(b), (c), (d) )
  28. NTSD_EXTENSION_APIS ExtensionApis;
  29. HANDLE ExtensionCurrentProcess;
  30. #ifdef IMAGEHLP_HEAP_DEBUG
  31. DECLARE_API( allocs )
  32. {
  33. PLIST_ENTRY Next;
  34. HEAP_BLOCK HeapBlock;
  35. ULONG Address;
  36. ULONG r;
  37. ULONG cb;
  38. LIST_ENTRY LocalHeapHeader;
  39. ULONG LocalTotalAllocs;
  40. ULONG LocalTotalMemory;
  41. HANDLE LocalhHeap;
  42. INIT_API();
  43. Address = GetExpression("imagehlp!TotalAllocs");
  44. r = ReadMemory(Address,
  45. &LocalTotalAllocs,
  46. sizeof(LocalTotalAllocs),
  47. &cb
  48. );
  49. if (!r || cb != sizeof(LocalTotalAllocs)) {
  50. dprintf("*** TotalAllocs unreadable\n");
  51. return;
  52. }
  53. Address = GetExpression("imagehlp!TotalMemory");
  54. r = ReadMemory(Address,
  55. &LocalTotalMemory,
  56. sizeof(LocalTotalMemory),
  57. &cb
  58. );
  59. if (!r || cb != sizeof(LocalTotalMemory)) {
  60. dprintf("*** TotalMemory unreadable\n");
  61. return;
  62. }
  63. Address = GetExpression("imagehlp!hHeap");
  64. r = ReadMemory(Address,
  65. &LocalhHeap,
  66. sizeof(LocalhHeap),
  67. &cb
  68. );
  69. if (!r || cb != sizeof(LocalhHeap)) {
  70. dprintf("*** hHeap unreadable\n");
  71. return;
  72. }
  73. Address = GetExpression("imagehlp!HeapHeader");
  74. r = ReadMemory(Address,
  75. &LocalHeapHeader,
  76. sizeof(LocalHeapHeader),
  77. &cb
  78. );
  79. if (!r || cb != sizeof(LocalHeapHeader)) {
  80. dprintf("*** HeapHeader unreadable\n");
  81. return;
  82. }
  83. Next = LocalHeapHeader.Flink;
  84. if (!Next) {
  85. return;
  86. }
  87. dprintf( "-----------------------------------------------------------------------------\n" );
  88. dprintf( "Memory Allocations for Heap 0x%08x, Allocs=%d, TotalMem=%d\n",
  89. LocalhHeap, LocalTotalAllocs, LocalTotalMemory );
  90. dprintf( "-----------------------------------------------------------------------------\n" );
  91. dprintf( "*\n" );
  92. while ((ULONG)Next != Address) {
  93. r = ReadMemory( CONTAINING_RECORD( Next, HEAP_BLOCK, ListEntry ),
  94. &HeapBlock,
  95. sizeof(HeapBlock),
  96. &cb
  97. );
  98. if (!r || cb != sizeof(HeapBlock)) {
  99. dprintf("*** list broken\n");
  100. return;
  101. }
  102. Next = HeapBlock.ListEntry.Flink;
  103. dprintf( "%8d %16s @ %5d\n", HeapBlock.Size, HeapBlock.File, HeapBlock.Line );
  104. }
  105. return;
  106. }
  107. #endif