Source code of Windows XP (NT5)
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.

323 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. regtrack.c
  5. Abstract:
  6. Routines to track calls to registry APIs. Used for debugging only.
  7. Author:
  8. Jim Schmidt (jimschm) 02-Sept-1997
  9. Revisions:
  10. --*/
  11. #include "pch.h"
  12. #include "migutilp.h"
  13. #include "regp.h"
  14. #ifdef DEBUG
  15. #undef RegOpenKeyExA
  16. #undef RegCreateKeyExA
  17. #undef RegOpenKeyExW
  18. #undef RegCreateKeyExW
  19. #define DBG_REGTRACK "RegTrack"
  20. #define NO_MATCH 0xffffffff
  21. DWORD g_DontCare;
  22. typedef struct {
  23. PCSTR File;
  24. DWORD Line;
  25. HKEY Key;
  26. CHAR SubKey[];
  27. } KEYTRACK, *PKEYTRACK;
  28. GROWLIST g_KeyTrackList = GROWLIST_INIT;
  29. DWORD
  30. pFindKeyReference (
  31. HKEY Key
  32. )
  33. {
  34. INT i;
  35. DWORD Items;
  36. PKEYTRACK KeyTrack;
  37. Items = GrowListGetSize (&g_KeyTrackList);
  38. for (i = (INT) (Items - 1) ; i >= 0 ; i--) {
  39. KeyTrack = (PKEYTRACK) GrowListGetItem (&g_KeyTrackList, (DWORD) i);
  40. if (KeyTrack && KeyTrack->Key == Key) {
  41. return (DWORD) i;
  42. }
  43. }
  44. return NO_MATCH;
  45. }
  46. VOID
  47. AddKeyReferenceA (
  48. HKEY Key,
  49. PCSTR SubKey,
  50. PCSTR File,
  51. DWORD Line
  52. )
  53. {
  54. PKEYTRACK KeyTrack;
  55. DWORD Size;
  56. Size = sizeof (KEYTRACK) + SizeOfString (SubKey);
  57. KeyTrack = (PKEYTRACK) MemAlloc (g_hHeap, 0, Size);
  58. KeyTrack->Key = Key;
  59. KeyTrack->File = File;
  60. KeyTrack->Line = Line;
  61. StringCopy (KeyTrack->SubKey, SubKey);
  62. GrowListAppend (&g_KeyTrackList, (PBYTE) KeyTrack, Size);
  63. MemFree (g_hHeap, 0, KeyTrack);
  64. }
  65. VOID
  66. AddKeyReferenceW (
  67. HKEY Key,
  68. PCWSTR SubKey,
  69. PCSTR File,
  70. DWORD Line
  71. )
  72. {
  73. PCSTR AnsiSubKey;
  74. AnsiSubKey = ConvertWtoA (SubKey);
  75. AddKeyReferenceA (Key, AnsiSubKey, File, Line);
  76. FreeConvertedStr (AnsiSubKey);
  77. }
  78. BOOL
  79. pDelKeyReference (
  80. HKEY Key
  81. )
  82. {
  83. DWORD Index;
  84. Index = pFindKeyReference (Key);
  85. if (Index != NO_MATCH) {
  86. GrowListDeleteItem (&g_KeyTrackList, Index);
  87. return TRUE;
  88. }
  89. return FALSE;
  90. }
  91. VOID
  92. DumpOpenKeys (
  93. VOID
  94. )
  95. {
  96. DWORD d;
  97. DWORD Items;
  98. PKEYTRACK KeyTrack;
  99. Items = GrowListGetSize (&g_KeyTrackList);
  100. if (Items) {
  101. DEBUGMSG ((DBG_ERROR, "Unclosed reg keys: %u", Items));
  102. }
  103. for (d = 0 ; d < Items ; d++) {
  104. KeyTrack = (PKEYTRACK) GrowListGetItem (&g_KeyTrackList, d);
  105. DEBUGMSG ((DBG_REGTRACK, "Open Key: %hs (%hs line %u)", KeyTrack->SubKey, KeyTrack->File, KeyTrack->Line));
  106. }
  107. }
  108. VOID
  109. RegTrackTerminate (
  110. VOID
  111. )
  112. {
  113. FreeGrowList (&g_KeyTrackList);
  114. }
  115. VOID
  116. OurRegOpenRootKeyA (
  117. HKEY Key,
  118. PCSTR SubKey,
  119. PCSTR File,
  120. DWORD Line
  121. )
  122. {
  123. AddKeyReferenceA (Key, SubKey, File, Line);
  124. }
  125. VOID
  126. OurRegOpenRootKeyW (
  127. HKEY Key,
  128. PCWSTR SubKey,
  129. PCSTR File,
  130. DWORD Line
  131. )
  132. {
  133. AddKeyReferenceW (Key, SubKey, File, Line);
  134. }
  135. LONG
  136. OurRegOpenKeyExA (
  137. HKEY Key,
  138. PCSTR SubKey,
  139. DWORD Unused,
  140. REGSAM SamMask,
  141. PHKEY ResultPtr,
  142. PCSTR File,
  143. DWORD Line
  144. )
  145. {
  146. LONG rc;
  147. rc = RegOpenKeyExA (Key, SubKey, Unused, SamMask, ResultPtr);
  148. if (rc == ERROR_SUCCESS) {
  149. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  150. }
  151. return rc;
  152. }
  153. LONG
  154. OurRegOpenKeyExW (
  155. HKEY Key,
  156. PCWSTR SubKey,
  157. DWORD Unused,
  158. REGSAM SamMask,
  159. PHKEY ResultPtr,
  160. PCSTR File,
  161. DWORD Line
  162. )
  163. {
  164. LONG rc;
  165. rc = RegOpenKeyExW (Key, SubKey, Unused, SamMask, ResultPtr);
  166. if (rc == ERROR_SUCCESS) {
  167. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  168. }
  169. return rc;
  170. }
  171. LONG
  172. OurCloseRegKey (
  173. HKEY Key,
  174. PCSTR File,
  175. DWORD Line
  176. )
  177. {
  178. LONG rc;
  179. rc = RealCloseRegKey (Key);
  180. if (rc == ERROR_SUCCESS) {
  181. if (!pDelKeyReference (Key)) {
  182. DEBUGMSG ((
  183. DBG_ERROR,
  184. "Reg key handle closed via CloseRegKey, but not opened "
  185. "with a tracked registry API. %s line %u",
  186. File,
  187. Line
  188. ));
  189. }
  190. }
  191. return rc;
  192. }
  193. LONG
  194. OurRegCreateKeyExA (
  195. HKEY Key,
  196. PCSTR SubKey,
  197. DWORD Reserved,
  198. PSTR Class,
  199. DWORD Options,
  200. REGSAM SamMask,
  201. LPSECURITY_ATTRIBUTES SecurityAttribs,
  202. PHKEY ResultPtr,
  203. PDWORD DispositionPtr,
  204. PCSTR File,
  205. DWORD Line
  206. )
  207. {
  208. LONG rc;
  209. rc = RegCreateKeyExA (
  210. Key,
  211. SubKey,
  212. Reserved,
  213. Class,
  214. Options,
  215. SamMask,
  216. SecurityAttribs,
  217. ResultPtr,
  218. DispositionPtr
  219. );
  220. if (rc == ERROR_SUCCESS) {
  221. AddKeyReferenceA (*ResultPtr, SubKey, File, Line);
  222. }
  223. return rc;
  224. }
  225. LONG
  226. OurRegCreateKeyExW (
  227. HKEY Key,
  228. PCWSTR SubKey,
  229. DWORD Reserved,
  230. PWSTR Class,
  231. DWORD Options,
  232. REGSAM SamMask,
  233. LPSECURITY_ATTRIBUTES SecurityAttribs,
  234. PHKEY ResultPtr,
  235. PDWORD DispositionPtr,
  236. PCSTR File,
  237. DWORD Line
  238. )
  239. {
  240. LONG rc;
  241. rc = RegCreateKeyExW (
  242. Key,
  243. SubKey,
  244. Reserved,
  245. Class,
  246. Options,
  247. SamMask,
  248. SecurityAttribs,
  249. ResultPtr,
  250. DispositionPtr
  251. );
  252. if (rc == ERROR_SUCCESS) {
  253. AddKeyReferenceW (*ResultPtr, SubKey, File, Line);
  254. }
  255. return rc;
  256. }
  257. #endif