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.

417 lines
9.4 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. routing\ip\wanarp\debug.c
  5. Abstract:
  6. Debug functions
  7. Revision History:
  8. AmritanR
  9. --*/
  10. #define __FILE_SIG__ DEBUG_SIG
  11. #include "inc.h"
  12. #define PRINT_BYTES(x) (UCHAR)x,(UCHAR)(x>>8),(UCHAR)(x>>16),(UCHAR)(x>>24)
  13. #if RT_LOCK_DEBUG
  14. KSPIN_LOCK g_kslLockLock;
  15. #endif
  16. #if RT_MEM_DEBUG
  17. RT_LOCK g_rlMemoryLock;
  18. LIST_ENTRY g_leAllocMemListHead;
  19. LIST_ENTRY g_leFreeMemListHead;
  20. #endif
  21. VOID
  22. RtInitializeDebug()
  23. {
  24. #if RT_TRACE_DEBUG
  25. g_byDebugLevel = RT_DBG_LEVEL_NONE;
  26. //g_byDebugLevel = RT_DBG_LEVEL_INFO;
  27. //g_byDebugLevel = 0x00;
  28. g_fDebugComp = 0xFFFFFFFF;
  29. #endif
  30. #if RT_LOCK_DEBUG
  31. KeInitializeSpinLock(&(g_kslLockLock));
  32. #endif
  33. #if RT_MEM_DEBUG
  34. RtInitializeSpinLock(&g_rlMemoryLock);
  35. InitializeListHead(&g_leAllocMemListHead);
  36. InitializeListHead(&g_leFreeMemListHead);
  37. #endif
  38. }
  39. #if RT_LOCK_DEBUG
  40. VOID
  41. RtpInitializeSpinLock(
  42. IN PRT_LOCK pLock,
  43. IN ULONG ulFileSig,
  44. IN ULONG ulLineNumber
  45. )
  46. {
  47. pLock->ulLockSig = RT_LOCK_SIG;
  48. pLock->ulFileSig = 0;
  49. pLock->ulLineNumber = 0;
  50. pLock->bAcquired = 0;
  51. pLock->pktLastThread = (PKTHREAD)NULL;
  52. KeInitializeSpinLock(&(pLock->kslLock));
  53. }
  54. VOID
  55. RtpAcquireSpinLock(
  56. IN PRT_LOCK pLock,
  57. OUT PKIRQL pkiIrql,
  58. IN ULONG ulFileSig,
  59. IN ULONG ulLineNumber,
  60. IN BOOLEAN bAtDpc
  61. )
  62. {
  63. PKTHREAD pThread;
  64. KIRQL kiInternalIrql;
  65. pThread = KeGetCurrentThread();
  66. if(bAtDpc)
  67. {
  68. kiInternalIrql = KeGetCurrentIrql();
  69. if(kiInternalIrql isnot DISPATCH_LEVEL)
  70. {
  71. DbgPrint("RTDBG: Called AcquireSpinLockAtDpc for lock at 0x%x when not at DISPATCH. File %c%c%c%c, Line %d\n",
  72. pLock,
  73. PRINT_BYTES(ulFileSig),
  74. ulLineNumber);
  75. DbgBreakPoint();
  76. }
  77. }
  78. KeAcquireSpinLock(&(g_kslLockLock),
  79. &kiInternalIrql);
  80. if(pLock->ulLockSig isnot RT_LOCK_SIG)
  81. {
  82. DbgPrint("RTDBG: Trying to acquire uninited lock 0x%x, File %c%c%c%c, Line %d\n",
  83. pLock,
  84. (CHAR)(ulFileSig & 0xff),
  85. (CHAR)((ulFileSig >> 8) & 0xff),
  86. (CHAR)((ulFileSig >> 16) & 0xff),
  87. (CHAR)((ulFileSig >> 24) & 0xff),
  88. ulLineNumber);
  89. DbgBreakPoint();
  90. }
  91. if(pLock->bAcquired isnot 0)
  92. {
  93. if(pLock->pktLastThread is pThread)
  94. {
  95. DbgPrint("RTDBG: Detected recursive locking!: pLock 0x%x, File %c%c%c%c, Line %d\n",
  96. pLock,
  97. PRINT_BYTES(ulFileSig),
  98. ulLineNumber);
  99. DbgPrint("RTDBG: pLock 0x%x already acquired in File %c%c%c%c, Line %d\n",
  100. pLock,
  101. PRINT_BYTES(pLock->ulFileSig),
  102. pLock->ulLineNumber);
  103. DbgBreakPoint();
  104. }
  105. }
  106. KeReleaseSpinLock(&(g_kslLockLock),
  107. kiInternalIrql);
  108. if(bAtDpc)
  109. {
  110. KeAcquireSpinLockAtDpcLevel(&(pLock->kslLock));
  111. }
  112. else
  113. {
  114. KeAcquireSpinLock(&(pLock->kslLock),
  115. pkiIrql);
  116. }
  117. //
  118. // Mark this lock.
  119. //
  120. pLock->pktLastThread = pThread;
  121. pLock->ulFileSig = ulFileSig;
  122. pLock->ulLineNumber = ulLineNumber;
  123. pLock->bAcquired = TRUE;
  124. }
  125. VOID
  126. RtpReleaseSpinLock(
  127. IN PRT_LOCK pLock,
  128. IN KIRQL kiIrql,
  129. IN ULONG ulFileSig,
  130. IN ULONG ulLineNumber,
  131. IN BOOLEAN bFromDpc
  132. )
  133. {
  134. if(pLock->ulLockSig isnot RT_LOCK_SIG)
  135. {
  136. DbgPrint("RTDBG: Trying to release uninited lock 0x%x, File %c%c%c%c, Line %d\n",
  137. pLock,
  138. PRINT_BYTES(ulFileSig),
  139. ulLineNumber);
  140. DbgBreakPoint();
  141. }
  142. if(pLock->bAcquired is 0)
  143. {
  144. DbgPrint("RTDBG: Detected release of unacquired lock 0x%x, File %c%c%c%c, Line %d\n",
  145. pLock,
  146. PRINT_BYTES(ulFileSig),
  147. ulLineNumber);
  148. DbgBreakPoint();
  149. }
  150. pLock->ulFileSig = ulFileSig;
  151. pLock->ulLineNumber = ulLineNumber;
  152. pLock->bAcquired = FALSE;
  153. pLock->pktLastThread = (PKTHREAD)NULL;
  154. if(bFromDpc)
  155. {
  156. KeReleaseSpinLockFromDpcLevel(&(pLock->kslLock));
  157. }
  158. else
  159. {
  160. KeReleaseSpinLock(&(pLock->kslLock),
  161. kiIrql);
  162. }
  163. }
  164. #endif // RT_LOCK_DEBUG
  165. #if RT_MEM_DEBUG
  166. PVOID
  167. RtpAllocate(
  168. IN POOL_TYPE ptPool,
  169. IN ULONG ulSize,
  170. IN ULONG ulTag,
  171. IN ULONG ulFileSig,
  172. IN ULONG ulLineNumber
  173. )
  174. {
  175. PVOID pBuffer;
  176. PRT_ALLOCATION pwaAlloc;
  177. KIRQL kiIrql;
  178. pwaAlloc = ExAllocatePoolWithTag(ptPool,
  179. ulSize+sizeof(RT_ALLOCATION),
  180. ulTag);
  181. if(pwaAlloc is NULL)
  182. {
  183. Trace(MEMORY, ERROR,
  184. ("Failed to allocate %d bytes in file %c%c%c%c, line %d\n",
  185. ulSize, PRINT_BYTES(ulFileSig), ulLineNumber));
  186. pBuffer = NULL;
  187. }
  188. else
  189. {
  190. pBuffer = (PVOID)&(pwaAlloc->pucData);
  191. pwaAlloc->ulMemSig = RT_MEMORY_SIG;
  192. pwaAlloc->ulFileSig = ulFileSig;
  193. pwaAlloc->ulLineNumber = ulLineNumber;
  194. pwaAlloc->ulSize = ulSize;
  195. RtAcquireSpinLock(&(g_rlMemoryLock), &kiIrql);
  196. InsertHeadList(&g_leAllocMemListHead,
  197. &(pwaAlloc->leLink));
  198. RtReleaseSpinLock(&g_rlMemoryLock, kiIrql);
  199. }
  200. return pBuffer;
  201. }
  202. VOID
  203. RtpFree(
  204. PVOID pvPointer,
  205. IN ULONG ulFileSig,
  206. IN ULONG ulLineNumber
  207. )
  208. {
  209. PRT_ALLOCATION pwaAlloc;
  210. KIRQL kiIrql;
  211. PRT_FREE pFree;
  212. pwaAlloc = CONTAINING_RECORD(pvPointer, RT_ALLOCATION, pucData);
  213. if(pwaAlloc->ulMemSig is RT_FREE_SIG)
  214. {
  215. DbgPrint("RTDBG: Trying to free memory that is already freed. Pointer0x%x, File %c%c%c%c, Line %d. Was freed at File %c%c%c%c, Line %d. \n",
  216. pvPointer,
  217. PRINT_BYTES(ulFileSig),
  218. ulLineNumber,
  219. PRINT_BYTES(pwaAlloc->ulFileSig),
  220. pwaAlloc->ulLineNumber);
  221. return;
  222. }
  223. if(pwaAlloc->ulMemSig isnot RT_MEMORY_SIG)
  224. {
  225. DbgPrint("RTDBG: Trying to free memory whose signature is wrong. Pointer 0x%x\n",
  226. pvPointer);
  227. DbgBreakPoint();
  228. return;
  229. }
  230. //
  231. // create a warp free block for it
  232. //
  233. pFree = ExAllocatePoolWithTag(NonPagedPool,
  234. sizeof(RT_FREE),
  235. FREE_TAG);
  236. //
  237. // Take the lock so that no one else touches the list
  238. //
  239. RtAcquireSpinLock(&(g_rlMemoryLock), &kiIrql);
  240. RemoveEntryList(&(pwaAlloc->leLink));
  241. if(pFree isnot NULL)
  242. {
  243. pFree->ulMemSig = RT_FREE_SIG;
  244. pFree->ulAllocFileSig = pwaAlloc->ulFileSig;
  245. pFree->ulAllocLineNumber = pwaAlloc->ulLineNumber;
  246. pFree->ulFreeFileSig = ulFileSig;
  247. pFree->ulFreeLineNumber = ulLineNumber;
  248. pFree->pStartAddr = (UINT_PTR)(pwaAlloc->pucData);
  249. pFree->ulSize = pwaAlloc->ulSize;
  250. pwaAlloc->ulMemSig = RT_FREE_SIG;
  251. pwaAlloc->ulFileSig = ulFileSig;
  252. pwaAlloc->ulLineNumber = ulLineNumber;
  253. InsertTailList(&g_leFreeMemListHead,
  254. &(pFree->leLink));
  255. }
  256. else
  257. {
  258. Trace(MEMORY, ERROR,
  259. ("Unable to create free block for allocation at %d %c%c%c%c, freed at %d %c%c%c%c. Size %d\n",
  260. pwaAlloc->ulLineNumber,
  261. PRINT_BYTES(pwaAlloc->ulFileSig),
  262. ulLineNumber,
  263. PRINT_BYTES(ulFileSig),
  264. pwaAlloc->ulSize));
  265. }
  266. RtReleaseSpinLock(&(g_rlMemoryLock), kiIrql);
  267. ExFreePool(pwaAlloc);
  268. }
  269. VOID
  270. RtAuditMemory()
  271. {
  272. PRT_ALLOCATION pwaAlloc;
  273. PLIST_ENTRY pleNode;
  274. PRT_FREE pFree;
  275. while(!IsListEmpty(&g_leAllocMemListHead))
  276. {
  277. pleNode = RemoveHeadList(&g_leAllocMemListHead);
  278. pwaAlloc = CONTAINING_RECORD(pleNode, RT_ALLOCATION, leLink);
  279. if(pwaAlloc->ulMemSig is RT_MEMORY_SIG)
  280. {
  281. DbgPrint("RTDBG: Unfreed memory. %d bytes. Pointer 0x%x, File %c%c%c%c, Line %d\n",
  282. pwaAlloc->ulSize,
  283. pwaAlloc->pucData,
  284. PRINT_BYTES(pwaAlloc->ulFileSig),
  285. pwaAlloc->ulLineNumber);
  286. DbgBreakPoint();
  287. ExFreePool(pwaAlloc);
  288. continue;
  289. }
  290. DbgPrint("RTDBG: Allocation with bad signature. Pointer 0x%x\n",
  291. pwaAlloc->pucData);
  292. DbgBreakPoint();
  293. continue;
  294. }
  295. while(!IsListEmpty(&g_leFreeMemListHead))
  296. {
  297. pleNode = RemoveHeadList(&g_leFreeMemListHead);
  298. pFree = CONTAINING_RECORD(pleNode, RT_FREE, leLink);
  299. if(pFree->ulMemSig is RT_FREE_SIG)
  300. {
  301. ExFreePool(pFree);
  302. continue;
  303. }
  304. DbgPrint("RTDBG: Freed memory with bad signature.\n");
  305. DbgBreakPoint();
  306. }
  307. }
  308. #endif