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.

396 lines
9.8 KiB

  1. /*** kdutil.c - KD Extension Utility Functions
  2. *
  3. * This module contains KD Extension Utility Functions.
  4. *
  5. * Copyright (c) 1999 Microsoft Corporation
  6. * Author: Michael Tsang (MikeTs)
  7. * Created 06/22/99
  8. *
  9. * MODIFICATION HISTORY
  10. */
  11. #include "pch.h"
  12. /***EP MemZero - Fill target buffer with zeros
  13. *
  14. * ENTRY
  15. * uipAddr - target buffer address
  16. * dwSize - target buffer size
  17. *
  18. * EXIT
  19. * None
  20. */
  21. VOID MemZero(ULONG_PTR uipAddr, ULONG dwSize)
  22. {
  23. PUCHAR pbBuff;
  24. //
  25. // LPTR will zero init the buffer
  26. //
  27. if ((pbBuff = LocalAlloc(LPTR, dwSize)) != NULL)
  28. {
  29. if (!WriteMemory(uipAddr, pbBuff, dwSize, NULL))
  30. {
  31. DBG_ERROR(("MemZero: failed to write memory"));
  32. }
  33. LocalFree(pbBuff);
  34. }
  35. else
  36. {
  37. DBG_ERROR(("MemZero: failed to allocate buffer"));
  38. }
  39. } //MemZero
  40. /***EP ReadMemByte - Read a byte from target address
  41. *
  42. * ENTRY
  43. * uipAddr - target address
  44. *
  45. * EXIT
  46. * None
  47. */
  48. BYTE ReadMemByte(ULONG_PTR uipAddr)
  49. {
  50. BYTE bData = 0;
  51. if (!ReadMemory(uipAddr, &bData, sizeof(bData), NULL))
  52. {
  53. DBG_ERROR(("ReadMemByte: failed to read address %x", uipAddr));
  54. }
  55. return bData;
  56. } //ReadMemByte
  57. /***EP ReadMemWord - Read a word from target address
  58. *
  59. * ENTRY
  60. * uipAddr - target address
  61. *
  62. * EXIT
  63. * None
  64. */
  65. WORD ReadMemWord(ULONG_PTR uipAddr)
  66. {
  67. WORD wData = 0;
  68. if (!ReadMemory(uipAddr, &wData, sizeof(wData), NULL))
  69. {
  70. DBG_ERROR(("ReadMemWord: failed to read address %x", uipAddr));
  71. }
  72. return wData;
  73. } //ReadMemWord
  74. /***EP ReadMemDWord - Read a dword from target address
  75. *
  76. * ENTRY
  77. * uipAddr - target address
  78. *
  79. * EXIT
  80. * None
  81. */
  82. DWORD ReadMemDWord(ULONG_PTR uipAddr)
  83. {
  84. DWORD dwData = 0;
  85. if (!ReadMemory(uipAddr, &dwData, sizeof(dwData), NULL))
  86. {
  87. DBG_ERROR(("ReadMemDWord: failed to read address %x", uipAddr));
  88. }
  89. return dwData;
  90. } //ReadMemDWord
  91. /***EP ReadMemUlongPtr - Read a ulong ptr from target address
  92. *
  93. * ENTRY
  94. * uipAddr - target address
  95. *
  96. * EXIT
  97. * None
  98. */
  99. ULONG_PTR ReadMemUlongPtr(ULONG_PTR uipAddr)
  100. {
  101. ULONG_PTR uipData = 0;
  102. if (!ReadMemory(uipAddr, &uipData, sizeof(uipData), NULL))
  103. {
  104. DBG_ERROR(("ReadMemUlongPtr: failed to read address %x", uipAddr));
  105. }
  106. return uipData;
  107. } //ReadMemUlongPtr
  108. /***LP GetObjBuff - Allocate and read object buffer
  109. *
  110. * ENTRY
  111. * pdata -> object data
  112. *
  113. * EXIT
  114. * return the allocated object buffer pointer
  115. */
  116. PVOID LOCAL GetObjBuff(POBJDATA pdata)
  117. {
  118. PVOID pbuff;
  119. if ((pbuff = LocalAlloc(LPTR, pdata->dwDataLen)) == NULL)
  120. {
  121. DBG_ERROR(("failed to allocate object buffer (size=%d)",
  122. pdata->dwDataLen));
  123. }
  124. else if (!ReadMemory((ULONG_PTR)pdata->pbDataBuff,
  125. pbuff,
  126. pdata->dwDataLen,
  127. NULL))
  128. {
  129. DBG_ERROR(("failed to read object buffer at %x", pdata->pbDataBuff));
  130. LocalFree(pbuff);
  131. pbuff = NULL;
  132. }
  133. return pbuff;
  134. } //GetObjBuff
  135. /***LP GetNSObj - Find a name space object
  136. *
  137. * ENTRY
  138. * pszObjPath -> object path string
  139. * pnsScope - object scope to start the search (NULL means root)
  140. * puipns -> to hold the pnsobj address if found
  141. * pns -> buffer to hold the object found
  142. * dwfNS - flags
  143. *
  144. * EXIT-SUCCESS
  145. * returns DBGERR_NONE
  146. * EXIT-FAILURE
  147. * returns DBGERR_ code
  148. */
  149. LONG LOCAL GetNSObj(PSZ pszObjPath, PNSOBJ pnsScope, PULONG_PTR puipns,
  150. PNSOBJ pns, ULONG dwfNS)
  151. {
  152. LONG rc = DBGERR_NONE;
  153. BOOLEAN fSearchUp = (BOOLEAN)(!(dwfNS & NSF_LOCAL_SCOPE) &&
  154. (pszObjPath[0] != '\\') &&
  155. (pszObjPath[0] != '^') &&
  156. (STRLEN(pszObjPath) <= sizeof(NAMESEG)));
  157. BOOLEAN fMatch = TRUE;
  158. PSZ psz;
  159. NSOBJ NSObj, NSChildObj;
  160. if (*pszObjPath == '\\')
  161. {
  162. psz = &pszObjPath[1];
  163. pnsScope = NULL;
  164. }
  165. else
  166. {
  167. for (psz = pszObjPath;
  168. (*psz == '^') && (pnsScope != NULL) &&
  169. (pnsScope->pnsParent != NULL);
  170. psz++)
  171. {
  172. if (!ReadMemory((ULONG_PTR)pnsScope->pnsParent,
  173. &NSObj,
  174. sizeof(NSObj),
  175. NULL))
  176. {
  177. DBG_ERROR(("failed to read parent object at %x",
  178. pnsScope->pnsParent));
  179. rc = DBGERR_CMD_FAILED;
  180. break;
  181. }
  182. else
  183. {
  184. pnsScope = &NSObj;
  185. }
  186. }
  187. if ((rc == DBGERR_NONE) && (*psz == '^'))
  188. {
  189. if (dwfNS & NSF_WARN_NOTFOUND)
  190. {
  191. DBG_ERROR(("object %s not found", pszObjPath));
  192. }
  193. rc = DBGERR_CMD_FAILED;
  194. }
  195. }
  196. if ((rc == DBGERR_NONE) && (pnsScope == NULL))
  197. {
  198. if ((*puipns = READSYMULONGPTR("gpnsNameSpaceRoot")) == 0)
  199. {
  200. DBG_ERROR(("failed to get root object address"));
  201. rc = DBGERR_CMD_FAILED;
  202. }
  203. else if (!ReadMemory(*puipns, &NSObj, sizeof(NSObj), NULL))
  204. {
  205. DBG_ERROR(("failed to read NameSpace root object at %x", *puipns));
  206. rc = DBGERR_CMD_FAILED;
  207. }
  208. else
  209. {
  210. pnsScope = &NSObj;
  211. }
  212. }
  213. while ((rc == DBGERR_NONE) && (*psz != '\0'))
  214. {
  215. if (pnsScope->pnsFirstChild == NULL)
  216. {
  217. fMatch = FALSE;
  218. }
  219. else
  220. {
  221. PSZ pszEnd = STRCHR(psz, '.');
  222. ULONG dwLen = (ULONG)(pszEnd? (pszEnd - psz): STRLEN(psz));
  223. if (dwLen > sizeof(NAMESEG))
  224. {
  225. DBG_ERROR(("invalid name path %s", pszObjPath));
  226. rc = DBGERR_CMD_FAILED;
  227. }
  228. else
  229. {
  230. NAMESEG dwName = NAMESEG_BLANK;
  231. BOOLEAN fFound = FALSE;
  232. ULONG_PTR uip;
  233. ULONG_PTR uipFirstChild = (ULONG_PTR)pnsScope->pnsFirstChild;
  234. MEMCPY(&dwName, psz, dwLen);
  235. //
  236. // Search all siblings for a matching NameSeg.
  237. //
  238. for (uip = uipFirstChild;
  239. (uip != 0) &&
  240. ReadMemory(uip, &NSChildObj, sizeof(NSObj), NULL);
  241. uip = ((ULONG_PTR)NSChildObj.list.plistNext ==
  242. uipFirstChild)?
  243. 0: (ULONG_PTR)NSChildObj.list.plistNext)
  244. {
  245. if (NSChildObj.dwNameSeg == dwName)
  246. {
  247. *puipns = uip;
  248. fFound = TRUE;
  249. NSObj = NSChildObj;
  250. pnsScope = &NSObj;
  251. break;
  252. }
  253. }
  254. if (fFound)
  255. {
  256. psz += dwLen;
  257. if (*psz == '.')
  258. {
  259. psz++;
  260. }
  261. }
  262. else
  263. {
  264. fMatch = FALSE;
  265. }
  266. }
  267. }
  268. if ((rc == DBGERR_NONE) && !fMatch)
  269. {
  270. if (fSearchUp && (pnsScope->pnsParent != NULL))
  271. {
  272. if (!ReadMemory((ULONG_PTR)pnsScope->pnsParent,
  273. &NSObj,
  274. sizeof(NSObj),
  275. NULL))
  276. {
  277. DBG_ERROR(("failed to read parent object at %x",
  278. pnsScope->pnsParent));
  279. rc = DBGERR_CMD_FAILED;
  280. }
  281. else
  282. {
  283. fMatch = TRUE;
  284. pnsScope = &NSObj;
  285. }
  286. }
  287. else
  288. {
  289. if (dwfNS & NSF_WARN_NOTFOUND)
  290. {
  291. DBG_ERROR(("object %s not found", pszObjPath));
  292. }
  293. rc = DBGERR_CMD_FAILED;
  294. }
  295. }
  296. }
  297. if (rc != DBGERR_NONE)
  298. {
  299. *puipns = 0;
  300. }
  301. else if (pns != NULL)
  302. {
  303. MEMCPY(pns, pnsScope, sizeof(NSObj));
  304. }
  305. return rc;
  306. } //GetNSObj
  307. /***LP ParsePackageLen - parse package length
  308. *
  309. * ENTRY
  310. * ppbOp -> instruction pointer
  311. * ppbOpNext -> to hold pointer to next instruction (can be NULL)
  312. *
  313. * EXIT
  314. * returns package length
  315. */
  316. ULONG LOCAL ParsePackageLen(PUCHAR *ppbOp, PUCHAR *ppbOpNext)
  317. {
  318. ULONG dwLen;
  319. UCHAR bFollowCnt, i;
  320. if (ppbOpNext != NULL)
  321. *ppbOpNext = *ppbOp;
  322. dwLen = (ULONG)(**ppbOp);
  323. (*ppbOp)++;
  324. bFollowCnt = (UCHAR)((dwLen & 0xc0) >> 6);
  325. if (bFollowCnt != 0)
  326. {
  327. dwLen &= 0x0000000f;
  328. for (i = 0; i < bFollowCnt; ++i)
  329. {
  330. dwLen |= (ULONG)(**ppbOp) << (i*8 + 4);
  331. (*ppbOp)++;
  332. }
  333. }
  334. if (ppbOpNext != NULL)
  335. *ppbOpNext += dwLen;
  336. return dwLen;
  337. } //ParsePackageLen
  338. /***LP NameSegString - convert a NameSeg to an ASCIIZ stri
  339. *
  340. * ENTRY
  341. * dwNameSeg - NameSeg
  342. *
  343. * EXIT
  344. * returns string
  345. */
  346. PSZ LOCAL NameSegString(ULONG dwNameSeg)
  347. {
  348. static char szNameSeg[sizeof(NAMESEG) + 1] = {0};
  349. STRCPYN(szNameSeg, (PSZ)&dwNameSeg, sizeof(NAMESEG));
  350. return szNameSeg;
  351. } //NameSegString