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.

402 lines
13 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. vad.c
  5. Abstract:
  6. WinDbg Extension Api
  7. Author:
  8. Lou Perazzoli (loup) 12-Jun-1992
  9. Environment:
  10. User Mode.
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. #pragma hdrstop
  15. CHAR *ProtectString[] = {
  16. "NO_ACCESS",
  17. "READONLY",
  18. "EXECUTE",
  19. "EXECUTE_READ",
  20. "READWRITE",
  21. "WRITECOPY",
  22. "EXECUTE_READWRITE",
  23. "EXECUTE_WRITECOPY"
  24. };
  25. DECLARE_API( vad )
  26. /*++
  27. Routine Description:
  28. Dumps all vads for process.
  29. Arguments:
  30. args - Address Flags
  31. Return Value:
  32. None
  33. --*/
  34. {
  35. ULONG64 Next;
  36. ULONG64 VadToDump;
  37. ULONG64 ParentStored;
  38. ULONG64 First;
  39. ULONG64 Left;
  40. ULONG64 Prev;
  41. ULONG Flags;
  42. ULONG Done;
  43. ULONG Level = 0;
  44. ULONG Count = 0;
  45. ULONG AverageLevel = 0;
  46. ULONG MaxLevel = 0;
  47. ULONG VadFlagsPrivateMemory=0, VadFlagsNoChange=0;
  48. ULONG PhysicalMapping=0,ImageMap=0,NoChange=0,LargePages=0,MemCommit=0,PrivateMemory=0,Protection=0;
  49. ULONG64 StartingVpn=0, EndingVpn=0, Parent=0, LeftChild=0, RightChild=0;
  50. ULONG64 ControlArea=0, FirstPrototypePte=0, LastContiguousPte=0, CommitCharge=0;
  51. VadToDump = 0;
  52. Flags = 0;
  53. if (GetExpressionEx(args, &VadToDump, &args)) {
  54. Flags = (ULONG) GetExpression(args);
  55. }
  56. if (VadToDump == 0) {
  57. dprintf("Specify the address of a VAD within the VAD tree\n");
  58. return E_INVALIDARG;
  59. }
  60. First = VadToDump;
  61. if (First == 0) {
  62. return E_INVALIDARG;
  63. }
  64. #define ReadFirstVad(fld, var) GetFieldValue(First, (PUCHAR) "nt!_MMVAD", #fld, var)
  65. #define ReadFirstVadShort(fld, var) GetFieldValue(First, (PUCHAR) "nt!_MMVAD_SHORT", #fld, var)
  66. if ( ReadFirstVadShort(u.VadFlags.PrivateMemory, VadFlagsPrivateMemory) ) {
  67. dprintf("%08p: Unable to get contents of VAD1\n",First );
  68. return E_INVALIDARG;
  69. }
  70. ReadFirstVadShort(u.VadFlags.NoChange, VadFlagsNoChange);
  71. ReadFirstVadShort(StartingVpn, StartingVpn);
  72. ReadFirstVadShort(EndingVpn, EndingVpn);
  73. if (ReadFirstVadShort(Parent, Parent)) {
  74. ReadFirstVadShort(u1.Parent, Parent);
  75. }
  76. ReadFirstVadShort(LeftChild, LeftChild);
  77. ReadFirstVadShort(RightChild, RightChild);
  78. ReadFirstVadShort(u.VadFlags.CommitCharge, CommitCharge);
  79. if (Flags == 1) {
  80. ULONG FileOffset=0;
  81. ULONG64 ListFlink=0, ListBlink=0, Banked=0;
  82. ULONG CopyOnWrite=0,Inherit=0,ExtendableFile=0,SecNoChange=0;
  83. ULONG OneSecured=0,MultipleSecured=0,ReadOnly=0,StoredInVad=0;
  84. //
  85. // Dump only this vad.
  86. //
  87. if ((VadFlagsPrivateMemory == 0) ||
  88. (VadFlagsNoChange == 1)) {
  89. if ( ReadFirstVad(ControlArea, ControlArea) ) {
  90. dprintf("%08p: Unable to get contents of VAD2\n",First );
  91. return E_INVALIDARG;
  92. }
  93. ReadFirstVad(FirstPrototypePte, FirstPrototypePte);
  94. ReadFirstVad(LastContiguousPte, LastContiguousPte);
  95. ReadFirstVad(u2.VadFlags2.CopyOnWrite, CopyOnWrite);
  96. ReadFirstVad(u2.VadFlags2.Inherit, Inherit);
  97. ReadFirstVad(u2.VadFlags2.ExtendableFile, ExtendableFile);
  98. ReadFirstVad(u2.VadFlags2.SecNoChange, SecNoChange);
  99. ReadFirstVad(u2.VadFlags2.OneSecured, OneSecured);
  100. ReadFirstVad(u2.VadFlags2.MultipleSecured, MultipleSecured);
  101. ReadFirstVad(u2.VadFlags2.ReadOnly, ReadOnly);
  102. ReadFirstVad(u2.VadFlags2.StoredInVad, StoredInVad);
  103. ReadFirstVad(u2.VadFlags2.FileOffset, FileOffset);
  104. ReadFirstVad(u3.List.Flink, ListFlink);
  105. ReadFirstVad(u3.List.Blink, ListBlink);
  106. ReadFirstVad(u4.Banked, Banked);
  107. }
  108. ReadFirstVad(u.VadFlags.CommitCharge, CommitCharge);
  109. ReadFirstVad(u.VadFlags.PhysicalMapping, PhysicalMapping);
  110. ReadFirstVad(u.VadFlags.ImageMap, ImageMap);
  111. ReadFirstVad(u.VadFlags.Protection, Protection);
  112. ReadFirstVad(u.VadFlags.NoChange, NoChange);
  113. ReadFirstVad(u.VadFlags.LargePages, LargePages);
  114. ReadFirstVad(u.VadFlags.MemCommit, MemCommit);
  115. ReadFirstVad(u.VadFlags.PrivateMemory, PrivateMemory);
  116. dprintf("\nVAD @ %8p\n",VadToDump);
  117. dprintf(" Start VPN: %8p End VPN: %8p Control Area: %8p\n",
  118. StartingVpn,
  119. EndingVpn,
  120. ControlArea);
  121. dprintf(" First ProtoPte: %8p Last PTE %8p Commit Charge %8lx (%ld.)\n",
  122. FirstPrototypePte,
  123. LastContiguousPte,
  124. (ULONG)CommitCharge,
  125. (ULONG)CommitCharge
  126. );
  127. dprintf(" Secured.Flink %8p Blink %8p Banked/Extend: %8p Offset %lx\n",
  128. ListFlink,
  129. ListBlink,
  130. Banked,
  131. FileOffset);
  132. dprintf(" ");
  133. if (PhysicalMapping) { dprintf("PhysicalMapping "); }
  134. if (ImageMap) { dprintf("ImageMap "); }
  135. Inherit ? dprintf("ViewShare ") : dprintf("ViewUnmap ");
  136. if (NoChange) { dprintf("NoChange "); }
  137. if (CopyOnWrite) { dprintf("CopyOnWrite "); }
  138. if (LargePages) { dprintf("LargePages "); }
  139. if (MemCommit) { dprintf("MemCommit "); }
  140. if (PrivateMemory) { dprintf("PrivateMemory "); }
  141. dprintf ("%s\n\n",ProtectString[Protection & 7]);
  142. if (ExtendableFile) { dprintf("ExtendableFile "); }
  143. if (SecNoChange) { dprintf("SecNoChange "); }
  144. if (OneSecured) { dprintf("OneSecured "); }
  145. if (MultipleSecured) { dprintf("MultipleSecured "); }
  146. if (ReadOnly) { dprintf("ReadOnly "); }
  147. if (StoredInVad) { dprintf("StoredInVad "); }
  148. dprintf ("\n\n");
  149. return E_INVALIDARG;
  150. }
  151. Prev = First;
  152. while (LeftChild != 0) {
  153. if ( CheckControlC() ) {
  154. return E_INVALIDARG;
  155. }
  156. Prev = First;
  157. First = LeftChild;
  158. Level += 1;
  159. if (Level > MaxLevel) {
  160. MaxLevel = Level;
  161. }
  162. if (Flags & 2) {
  163. dprintf("Reading LeftChild VAD %08p\n",First );
  164. }
  165. if ( ReadFirstVadShort(LeftChild, LeftChild) ) {
  166. dprintf("%08p %08p: Unable to get contents of VAD3\n", First, Prev);
  167. return E_INVALIDARG;
  168. }
  169. }
  170. ReadFirstVadShort(StartingVpn, StartingVpn);
  171. ReadFirstVadShort(EndingVpn, EndingVpn);
  172. if (ReadFirstVadShort(Parent, Parent)) {
  173. ReadFirstVadShort(u1.Parent, Parent);
  174. Parent &= ~0x3;
  175. }
  176. ReadFirstVadShort(RightChild, RightChild);
  177. ReadFirstVadShort(u.VadFlags.CommitCharge, CommitCharge);
  178. ReadFirstVadShort(u.VadFlags.PrivateMemory, PrivateMemory);
  179. ReadFirstVadShort(u.VadFlags.PhysicalMapping, PhysicalMapping);
  180. ReadFirstVadShort(u.VadFlags.ImageMap, ImageMap);
  181. ReadFirstVadShort(u.VadFlags.Protection, Protection);
  182. dprintf("VAD level start end commit\n");
  183. dprintf("%p (%2ld) %8p %8p %4ld %s %s %s\n",
  184. First,
  185. Level,
  186. StartingVpn,
  187. EndingVpn,
  188. (ULONG)CommitCharge,
  189. PrivateMemory ? "Private" : "Mapped ",
  190. ImageMap ? "Exe " :
  191. PhysicalMapping ? "Phys" : " ",
  192. ProtectString[Protection & 7]
  193. );
  194. Count += 1;
  195. AverageLevel += Level;
  196. Next = First;
  197. while (Next != 0) {
  198. if ( CheckControlC() ) {
  199. return E_INVALIDARG;
  200. }
  201. if (RightChild == 0) {
  202. Done = TRUE;
  203. //
  204. // Pre-AVL vad trees were delimited by a parent pointer of NULL.
  205. //
  206. while ((ParentStored = Parent) != 0) {
  207. if ( CheckControlC() ) {
  208. return E_INVALIDARG;
  209. }
  210. Level -= 1;
  211. //
  212. // Locate the first ancestor of this node of which this
  213. // node is the left child of and return that node as the
  214. // next element.
  215. //
  216. First = ParentStored & ~0x3;
  217. if ( ReadFirstVadShort( LeftChild, LeftChild) ||
  218. (ReadFirstVadShort(Parent, Parent) &&
  219. ReadFirstVadShort(u1.Parent, Parent)) ) {
  220. dprintf("%08p %08p: Unable to get contents of VAD4\n",Parent, Prev);
  221. return E_INVALIDARG;
  222. }
  223. Parent &= ~0x3;
  224. //
  225. // AVL vad trees are delimited by a parent pointer that equals
  226. // the current vad so check for that explicitly here so that
  227. // this same extension can work on both types of kernels.
  228. //
  229. if (Parent == First) {
  230. break;
  231. }
  232. Prev = First;
  233. ReadFirstVadShort(StartingVpn, StartingVpn);
  234. ReadFirstVadShort(EndingVpn, EndingVpn);
  235. ReadFirstVadShort(RightChild, RightChild);
  236. ReadFirstVadShort(u.VadFlags.CommitCharge, CommitCharge);
  237. ReadFirstVadShort(u.VadFlags.PrivateMemory, PrivateMemory);
  238. ReadFirstVadShort(u.VadFlags.PhysicalMapping, PhysicalMapping);
  239. ReadFirstVadShort(u.VadFlags.ImageMap, ImageMap);
  240. ReadFirstVadShort(u.VadFlags.Protection, Protection);
  241. if (LeftChild == Next) {
  242. Next = ParentStored;
  243. dprintf("%p (%2ld) %8p %8p %4ld %s %s %s\n",
  244. Next,
  245. Level,
  246. StartingVpn,
  247. EndingVpn,
  248. (ULONG)CommitCharge,
  249. PrivateMemory ? "Private" : "Mapped ",
  250. ImageMap ? "Exe " :
  251. PhysicalMapping ? "Phys" : " ",
  252. ProtectString[Protection & 7]
  253. );
  254. Done = FALSE;
  255. Count += 1;
  256. AverageLevel += Level;
  257. break;
  258. }
  259. Next = ParentStored;
  260. }
  261. if (Done) {
  262. Next = 0;
  263. break;
  264. }
  265. } else {
  266. //
  267. // A right child exists, locate the left most child of that right child.
  268. //
  269. Next = RightChild;
  270. Level += 1;
  271. if (Level > MaxLevel) {
  272. MaxLevel = Level;
  273. }
  274. First = Next;
  275. if ( ReadFirstVadShort(LeftChild, LeftChild) ) {
  276. dprintf("%08p %08p: Unable to get contents of VAD5\n",Next, Prev);
  277. return E_INVALIDARG;
  278. }
  279. while ((Left = LeftChild) != 0) {
  280. if ( CheckControlC() ) {
  281. return E_INVALIDARG;
  282. }
  283. Level += 1;
  284. if (Level > MaxLevel) {
  285. MaxLevel = Level;
  286. }
  287. Next = Left;
  288. First = Next;
  289. if ( ReadFirstVadShort(LeftChild, LeftChild) ) {
  290. dprintf("%08p %08p: Unable to get contents of VAD6\n",Next, Prev);
  291. return E_INVALIDARG;
  292. }
  293. Prev = First;
  294. }
  295. ReadFirstVadShort(StartingVpn, StartingVpn);
  296. ReadFirstVadShort(EndingVpn, EndingVpn);
  297. if (ReadFirstVadShort(Parent, Parent)) {
  298. ReadFirstVadShort(u1.Parent, Parent);
  299. Parent &= ~0x3;
  300. }
  301. ReadFirstVadShort(RightChild, RightChild);
  302. ReadFirstVadShort(u.VadFlags.CommitCharge, CommitCharge);
  303. ReadFirstVadShort(u.VadFlags.PrivateMemory, PrivateMemory);
  304. ReadFirstVadShort(u.VadFlags.PhysicalMapping, PhysicalMapping);
  305. ReadFirstVadShort(u.VadFlags.ImageMap, ImageMap);
  306. ReadFirstVadShort(u.VadFlags.Protection, Protection);
  307. dprintf("%p (%2ld) %8p %8p %4ld %s %s %s\n",
  308. Next,
  309. Level,
  310. StartingVpn,
  311. EndingVpn,
  312. (ULONG)CommitCharge,
  313. PrivateMemory ? "Private" : "Mapped ",
  314. ImageMap ? "Exe " :
  315. PhysicalMapping ? "Phys" : " ",
  316. ProtectString[Protection & 7]
  317. );
  318. Count += 1;
  319. AverageLevel += Level;
  320. }
  321. }
  322. dprintf("\nTotal VADs: %5ld average level: %4ld maximum depth: %ld\n",
  323. Count, 1+(AverageLevel/Count),MaxLevel);
  324. #undef ReadFirstVad
  325. return S_OK;
  326. }