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.

498 lines
11 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. GenTable.c
  5. Abstract:
  6. WinDbg Extension Api for walking RtlGenericTable structures
  7. Contains no direct ! entry points, but has makes it possible to
  8. enumerate through generic tables. The standard Rtl functions
  9. cannot be used by debugger extensions because they dereference
  10. pointers to data on the machine being debugged. The function
  11. KdEnumerateGenericTableWithoutSplaying implemented in this
  12. module can be used within kernel debugger extensions. The
  13. enumeration function RtlEnumerateGenericTable has no parallel
  14. in this module, since splaying the tree is an intrusive operation,
  15. and debuggers should try not to be intrusive.
  16. Author:
  17. Keith Kaplan [KeithKa] 09-May-96
  18. Environment:
  19. User Mode.
  20. Revision History:
  21. --*/
  22. #include "pch.h"
  23. #pragma hdrstop
  24. ULONG64
  25. KdParent (
  26. IN ULONG64 pLinks
  27. )
  28. /*++
  29. Routine Description:
  30. Analogous to RtlParent macro, but works in the kernel debugger.
  31. The description of RtlParent follows:
  32. The macro function Parent takes as input a pointer to a splay link in a
  33. tree and returns a pointer to the splay link of the parent of the input
  34. node. If the input node is the root of the tree the return value is
  35. equal to the input value.
  36. Arguments:
  37. Links - Pointer to a splay link in a tree.
  38. Return Value:
  39. PRTL_SPLAY_LINKS - Pointer to the splay link of the parent of the input
  40. node. If the input node is the root of the tree the
  41. return value is equal to the input value.
  42. --*/
  43. {
  44. ULONG64 Parent = 0;
  45. if ( GetFieldValue( pLinks,
  46. "RTL_SPLAY_LINKS",
  47. "Parent",
  48. Parent) ) {
  49. dprintf( "%08p: Unable to read pLinks\n", pLinks );
  50. return 0;
  51. }
  52. return Parent;
  53. }
  54. ULONG64
  55. KdLeftChild (
  56. IN ULONG64 pLinks
  57. )
  58. /*++
  59. Routine Description:
  60. Analogous to RtlLeftChild macro, but works in the kernel debugger.
  61. The description of RtlLeftChild follows:
  62. The macro function LeftChild takes as input a pointer to a splay link in
  63. a tree and returns a pointer to the splay link of the left child of the
  64. input node. If the left child does not exist, the return value is NULL.
  65. Arguments:
  66. Links - Pointer to a splay link in a tree.
  67. Return Value:
  68. ULONG64 - Pointer to the splay link of the left child of the input node.
  69. If the left child does not exist, the return value is NULL.
  70. --*/
  71. {
  72. ULONG64 LeftChild = 0;
  73. if ( GetFieldValue( pLinks,
  74. "RTL_SPLAY_LINKS",
  75. "LeftChild",
  76. LeftChild) ) {
  77. dprintf( "%08p: Unable to read pLinks\n", pLinks );
  78. return 0;
  79. }
  80. return LeftChild;
  81. }
  82. ULONG64
  83. KdRightChild (
  84. IN ULONG64 pLinks
  85. )
  86. /*++
  87. Routine Description:
  88. Analogous to RtlRightChild macro, but works in the kernel debugger.
  89. The description of RtlRightChild follows:
  90. The macro function RightChild takes as input a pointer to a splay link
  91. in a tree and returns a pointer to the splay link of the right child of
  92. the input node. If the right child does not exist, the return value is
  93. NULL.
  94. Arguments:
  95. Links - Pointer to a splay link in a tree.
  96. Return Value:
  97. PRTL_SPLAY_LINKS - Pointer to the splay link of the right child of the input node.
  98. If the right child does not exist, the return value is NULL.
  99. --*/
  100. {
  101. ULONG64 RightChild = 0;
  102. if ( GetFieldValue( pLinks,
  103. "RTL_SPLAY_LINKS",
  104. "RightChild",
  105. RightChild) ) {
  106. dprintf( "%08p: Unable to read pLinks\n", pLinks );
  107. return 0;
  108. }
  109. return RightChild;
  110. }
  111. BOOLEAN
  112. KdIsLeftChild (
  113. IN ULONG64 Links
  114. )
  115. /*++
  116. Routine Description:
  117. Analogous to RtlIsLeftChild macro, but works in the kernel debugger.
  118. The description of RtlIsLeftChild follows:
  119. The macro function IsLeftChild takes as input a pointer to a splay link
  120. in a tree and returns TRUE if the input node is the left child of its
  121. parent, otherwise it returns FALSE.
  122. Arguments:
  123. Links - Pointer to a splay link in a tree.
  124. Return Value:
  125. BOOLEAN - TRUE if the input node is the left child of its parent,
  126. otherwise it returns FALSE.
  127. --*/
  128. {
  129. return (KdLeftChild(KdParent(Links)) == (Links));
  130. }
  131. BOOLEAN
  132. KdIsRightChild (
  133. IN ULONG64 Links
  134. )
  135. /*++
  136. Routine Description:
  137. Analogous to RtlIsRightChild macro, but works in the kernel debugger.
  138. The description of RtlIsRightChild follows:
  139. The macro function IsRightChild takes as input a pointer to a splay link
  140. in a tree and returns TRUE if the input node is the right child of its
  141. parent, otherwise it returns FALSE.
  142. Arguments:
  143. Links - Pointer to a splay link in a tree.
  144. Return Value:
  145. BOOLEAN - TRUE if the input node is the right child of its parent,
  146. otherwise it returns FALSE.
  147. --*/
  148. {
  149. return (KdRightChild(KdParent(Links)) == (Links));
  150. }
  151. BOOLEAN
  152. KdIsGenericTableEmpty (
  153. IN ULONG64 Table
  154. )
  155. /*++
  156. Routine Description:
  157. Analogous to RtlIsGenericTableEmpty, but works in the kernel debugger.
  158. The description of RtlIsGenericTableEmpty follows:
  159. The function IsGenericTableEmpty will return to the caller TRUE if
  160. the input table is empty (i.e., does not contain any elements) and
  161. FALSE otherwise.
  162. Arguments:
  163. Table - Supplies a pointer to the Generic Table.
  164. Return Value:
  165. BOOLEAN - if enabled the tree is empty.
  166. --*/
  167. {
  168. ULONG64 TableRoot = 0;
  169. if (GetFieldValue(Table, "RTL_AVL_TABLE", "TableRoot", TableRoot)) {
  170. return TRUE;
  171. }
  172. //
  173. // Table is empty if the root pointer is null.
  174. //
  175. return ((TableRoot)?(FALSE):(TRUE));
  176. }
  177. ULONG64
  178. KdRealSuccessor (
  179. IN ULONG64 Links
  180. )
  181. /*++
  182. Routine Description:
  183. Analogous to RtlRealSuccessor, but works in the kernel debugger.
  184. The description of RtlRealSuccessor follows:
  185. The RealSuccessor function takes as input a pointer to a splay link
  186. in a tree and returns a pointer to the successor of the input node within
  187. the entire tree. If there is not a successor, the return value is NULL.
  188. Arguments:
  189. Links - Supplies a pointer to a splay link in a tree.
  190. Return Value:
  191. PRTL_SPLAY_LINKS - returns a pointer to the successor in the entire tree
  192. --*/
  193. {
  194. ULONG64 Ptr = 0;
  195. /*
  196. first check to see if there is a right subtree to the input link
  197. if there is then the real successor is the left most node in
  198. the right subtree. That is find and return P in the following diagram
  199. Links
  200. \
  201. .
  202. .
  203. .
  204. /
  205. P
  206. \
  207. */
  208. if ((Ptr = KdRightChild(Links)) != 0) {
  209. while (KdLeftChild(Ptr) != 0) {
  210. Ptr = KdLeftChild(Ptr);
  211. }
  212. return Ptr;
  213. }
  214. /*
  215. we do not have a right child so check to see if have a parent and if
  216. so find the first ancestor that we are a left decendent of. That
  217. is find and return P in the following diagram
  218. P
  219. /
  220. .
  221. .
  222. .
  223. Links
  224. */
  225. Ptr = Links;
  226. while (KdIsRightChild(Ptr)) {
  227. Ptr = KdParent(Ptr);
  228. }
  229. if (KdIsLeftChild(Ptr)) {
  230. return KdParent(Ptr);
  231. }
  232. //
  233. // otherwise we are do not have a real successor so we simply return
  234. // NULL
  235. //
  236. return 0;
  237. }
  238. ULONG64
  239. KdEnumerateGenericTableWithoutSplaying (
  240. IN ULONG64 pTable,
  241. IN PULONG64 RestartKey
  242. )
  243. /*++
  244. Routine Description:
  245. Analogous to RtlEnumerateGenericTableWithoutSplaying, but works in the
  246. kernel debugger. The description of RtlEnumerateGenericTableWithoutSplaying
  247. follows:
  248. The function EnumerateGenericTableWithoutSplaying will return to the
  249. caller one-by-one the elements of of a table. The return value is a
  250. pointer to the user defined structure associated with the element.
  251. The input parameter RestartKey indicates if the enumeration should
  252. start from the beginning or should return the next element. If the
  253. are no more new elements to return the return value is NULL. As an
  254. example of its use, to enumerate all of the elements in a table the
  255. user would write:
  256. *RestartKey = NULL;
  257. for (ptr = EnumerateGenericTableWithoutSplaying(Table, &RestartKey);
  258. ptr != NULL;
  259. ptr = EnumerateGenericTableWithoutSplaying(Table, &RestartKey)) {
  260. :
  261. }
  262. Arguments:
  263. Table - Pointer to the generic table to enumerate.
  264. RestartKey - Pointer that indicates if we should restart or return the next
  265. element. If the contents of RestartKey is NULL, the search
  266. will be started from the beginning.
  267. Return Value:
  268. PVOID - Pointer to the user data.
  269. --*/
  270. {
  271. ULONG NumElements;
  272. ULONG64 TableRoot;
  273. if ( GetFieldValue(pTable,
  274. "RTL_AVL_TABLE",
  275. "NumberGenericTableElements",
  276. NumElements) ) {
  277. dprintf( "%s: Unable to read pTable\n", FormatValue(pTable) );
  278. return 0;
  279. }
  280. if ( GetFieldValue(pTable,
  281. "RTL_AVL_TABLE",
  282. "BalancedRoot.RightChild",
  283. TableRoot) ) {
  284. dprintf( "%s: Unable to read pTable root\n", FormatValue(pTable) );
  285. return 0;
  286. }
  287. if (!NumElements) {
  288. //
  289. // Nothing to do if the table is empty.
  290. //
  291. return 0;
  292. } else {
  293. //
  294. // Will be used as the "iteration" through the tree.
  295. //
  296. ULONG64 NodeToReturn;
  297. //
  298. // If the restart flag is true then go to the least element
  299. // in the tree.
  300. //
  301. if (*RestartKey == 0) {
  302. //
  303. // We just loop until we find the leftmost child of the root.
  304. //
  305. for (
  306. NodeToReturn = TableRoot;
  307. KdLeftChild(NodeToReturn);
  308. NodeToReturn = KdLeftChild(NodeToReturn)
  309. ) {
  310. ;
  311. }
  312. *RestartKey = NodeToReturn;
  313. } else {
  314. //
  315. // The caller has passed in the previous entry found
  316. // in the table to enable us to continue the search. We call
  317. // KdRealSuccessor to step to the next element in the tree.
  318. //
  319. NodeToReturn = KdRealSuccessor(*RestartKey);
  320. if (NodeToReturn) {
  321. *RestartKey = NodeToReturn;
  322. }
  323. }
  324. //
  325. // If there actually is a next element in the enumeration
  326. // then the pointer to return is right after the list links.
  327. //
  328. return ((NodeToReturn)?
  329. (NodeToReturn+GetTypeSize("RTL_BALANCED_LINKS")/*+GetTypeSize("PVOID")*/)
  330. :0);
  331. }
  332. }