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.

249 lines
5.9 KiB

  1. /*++
  2. Copyright (c) 1990-1998 Microsoft Corporation, All Rights Reserved.
  3. Module Name:
  4. AtmSmDbg.c
  5. Abstract:
  6. This module contains all debug-related code.
  7. In debug mode we use our own memory management scheme to find out memory
  8. leaks etc.
  9. Author:
  10. Anil Francis Thomas (10/98)
  11. Environment:
  12. Kernel
  13. Revision History:
  14. --*/
  15. #include "precomp.h"
  16. #pragma hdrstop
  17. #define MODULE_ID MODULE_DEBUG
  18. #if DBG
  19. // global variable holding alloc Info
  20. ATMSMD_ALLOC_GLOBAL AtmSmDAllocGlobal;
  21. VOID
  22. AtmSmInitializeAuditMem(
  23. )
  24. {
  25. AtmSmDAllocGlobal.pAtmSmHead = (PATMSMD_ALLOCATION)NULL;
  26. AtmSmDAllocGlobal.pAtmSmTail = (PATMSMD_ALLOCATION)NULL;
  27. AtmSmDAllocGlobal.ulAtmSmAllocCount = 0;
  28. NdisAllocateSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  29. AtmSmDAllocGlobal.ulAtmSmInitDonePattern = INIT_DONE_PATTERN;
  30. }
  31. PVOID
  32. AtmSmAuditAllocMem(
  33. PVOID *ppPointer,
  34. ULONG ulSize,
  35. ULONG ulModuleNumber,
  36. ULONG ulLineNumber
  37. )
  38. {
  39. PUCHAR pBuffer;
  40. ULONG UNALIGNED * pulTrailer;
  41. PATMSMD_ALLOCATION pAllocInfo;
  42. NDIS_STATUS Status;
  43. if(INIT_DONE_PATTERN != AtmSmDAllocGlobal.ulAtmSmInitDonePattern){
  44. ASSERT(FALSE);
  45. AtmSmInitializeAuditMem();
  46. }
  47. Status = NdisAllocateMemoryWithTag(
  48. (PVOID *)&pAllocInfo,
  49. sizeof(ATMSMD_ALLOCATION) + ulSize + (2 * sizeof(ULONG)),
  50. (ULONG)MEMORY_TAG
  51. );
  52. if (Status != NDIS_STATUS_SUCCESS)
  53. {
  54. DbgErr(("AtmSmAuditAllocMem: Module 0x%X, line %d, Size %d failed!\n",
  55. ulModuleNumber, ulLineNumber, ulSize));
  56. pBuffer = NULL;
  57. }
  58. else
  59. {
  60. pBuffer = (PUCHAR)(pAllocInfo + 1);
  61. pulTrailer = (ULONG UNALIGNED *)(pBuffer + ulSize);
  62. *pulTrailer++ = TRAILER_PATTERN;
  63. *pulTrailer = TRAILER_PATTERN;
  64. pAllocInfo->ulSignature = (ULONG)MEMORY_TAG;
  65. pAllocInfo->ulModuleNumber = ulModuleNumber;
  66. pAllocInfo->ulLineNumber = ulLineNumber;
  67. pAllocInfo->ulSize = ulSize;
  68. pAllocInfo->Location = (UINT_PTR)ppPointer;
  69. pAllocInfo->Next = (PATMSMD_ALLOCATION)NULL;
  70. NdisAcquireSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  71. pAllocInfo->Prev = AtmSmDAllocGlobal.pAtmSmTail;
  72. if((PATMSMD_ALLOCATION)NULL == AtmSmDAllocGlobal.pAtmSmTail)
  73. {
  74. // empty list
  75. AtmSmDAllocGlobal.pAtmSmHead = AtmSmDAllocGlobal.pAtmSmTail = pAllocInfo;
  76. }
  77. else
  78. {
  79. AtmSmDAllocGlobal.pAtmSmTail->Next = pAllocInfo;
  80. }
  81. AtmSmDAllocGlobal.pAtmSmTail = pAllocInfo;
  82. AtmSmDAllocGlobal.ulAtmSmAllocCount++;
  83. NdisReleaseSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  84. }
  85. DbgLoud(("AtmSmAuditAllocMem: Module %p, line %d, %d bytes, [0x%x] <- 0x%x\n",
  86. ulModuleNumber, ulLineNumber, ulSize, ppPointer, pBuffer));
  87. return ((PVOID)pBuffer);
  88. }
  89. VOID
  90. AtmSmAuditFreeMem(
  91. PVOID Pointer
  92. )
  93. {
  94. PUCHAR pBuffer = (PUCHAR)Pointer;
  95. ULONG UNALIGNED * pulTrailer;
  96. PATMSMD_ALLOCATION pAllocInfo;
  97. pAllocInfo = (PATMSMD_ALLOCATION)(pBuffer - sizeof(ATMSMD_ALLOCATION));
  98. if(pAllocInfo->ulSignature != MEMORY_TAG){
  99. DbgErr(("AtmSmAuditFreeMem: unknown buffer %p!\n", Pointer));
  100. DbgBreakPoint();
  101. return;
  102. }
  103. DbgLoud(("AtmSmAuditFreeMem: Freeing Buffer %p pAudit %p!\n",
  104. Pointer, pAllocInfo));
  105. // check the trailer
  106. pulTrailer = (ULONG UNALIGNED *)(pBuffer + pAllocInfo->ulSize);
  107. if((*pulTrailer != TRAILER_PATTERN) ||
  108. (*(++pulTrailer) != TRAILER_PATTERN)){
  109. DbgErr(("AtmSmAuditFreeMem: Trailer over written! Alloc - %p "
  110. "Trailer - %p\n", pAllocInfo, pulTrailer));
  111. DbgBreakPoint();
  112. return;
  113. }
  114. NdisAcquireSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  115. pAllocInfo->ulSignature = (ULONG)'DEAD';
  116. if((PATMSMD_ALLOCATION)NULL != pAllocInfo->Prev)
  117. {
  118. pAllocInfo->Prev->Next = pAllocInfo->Next;
  119. }
  120. else
  121. {
  122. AtmSmDAllocGlobal.pAtmSmHead = pAllocInfo->Next;
  123. }
  124. if((PATMSMD_ALLOCATION)NULL != pAllocInfo->Next)
  125. {
  126. pAllocInfo->Next->Prev = pAllocInfo->Prev;
  127. }
  128. else
  129. {
  130. AtmSmDAllocGlobal.pAtmSmTail = pAllocInfo->Prev;
  131. }
  132. AtmSmDAllocGlobal.ulAtmSmAllocCount--;
  133. NdisReleaseSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  134. NdisFreeMemory(pAllocInfo, 0, 0);
  135. }
  136. VOID
  137. AtmSmShutdownAuditMem(
  138. )
  139. {
  140. if(AtmSmDAllocGlobal.ulAtmSmAllocCount)
  141. DbgErr(("Number of memory blocks still allocated - %u\n",
  142. AtmSmDAllocGlobal.ulAtmSmAllocCount));
  143. NdisFreeSpinLock(&AtmSmDAllocGlobal.AtmSmMemoryLock);
  144. AtmSmDAllocGlobal.ulAtmSmInitDonePattern = 'DAED';
  145. }
  146. VOID
  147. PrintATMAddr(
  148. IN char *pStr,
  149. IN PATM_ADDRESS pAtmAddr
  150. )
  151. /*++
  152. Routine Description:
  153. Print an ATM_ADDRESS address onto the debugger.
  154. Arguments:
  155. pStr - pointer to the string to be printed together with address
  156. pAtmAddr - pointer to an NSAP or E164 address
  157. Return Value:
  158. --*/
  159. {
  160. ULONG i,j;
  161. char HexChars[] = "0123456789ABCDEF";
  162. ULONG NumOfDigits;
  163. PUCHAR pucAtmAddr = pAtmAddr->Address;
  164. UCHAR AddrString[(ATM_ADDRESS_LENGTH*2) + 1];
  165. if ((NumOfDigits = pAtmAddr->NumberOfDigits) > ATM_ADDRESS_LENGTH){
  166. NumOfDigits = ATM_ADDRESS_LENGTH;
  167. }
  168. j = 0;
  169. for(i = 0; i < NumOfDigits; i++){
  170. AddrString[j++] = HexChars[(pucAtmAddr[i] >> 4)];
  171. AddrString[j++] = HexChars[(pucAtmAddr[i] &0xF)];
  172. }
  173. AddrString[j] = '\0';
  174. DbgPrint("%s(%s, %u): %s\n",
  175. pStr,
  176. (pAtmAddr->AddressType == ATM_E164) ? "E164" : "NSAP",
  177. NumOfDigits,
  178. AddrString);
  179. }
  180. #endif // DBG