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.

294 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. mp_dbg.c
  5. Abstract:
  6. This module contains all debug-related code.
  7. Revision History:
  8. Who When What
  9. -------- -------- ----------------------------------------------
  10. DChen 11-01-99 created
  11. Notes:
  12. --*/
  13. #include "precomp.h"
  14. #if DBG
  15. /**
  16. Constants
  17. **/
  18. #define _FILENUMBER 'GBED'
  19. // Bytes to appear on each line of dump output.
  20. //
  21. #define DUMP_BytesPerLine 16
  22. ULONG MPDebugLevel = MP_WARN;
  23. ULONG MPAllocCount = 0; // the number of outstanding allocs
  24. NDIS_SPIN_LOCK MPMemoryLock; // spinlock for the debug mem list
  25. LIST_ENTRY MPMemoryList;
  26. BOOLEAN MPInitDone = FALSE; // debug mem list init flag
  27. NDIS_STATUS MPAuditAllocMem(
  28. PVOID *pPointer,
  29. UINT Size,
  30. UINT Flags,
  31. NDIS_PHYSICAL_ADDRESS HighestAddr,
  32. ULONG FileNumber,
  33. ULONG LineNumber
  34. )
  35. {
  36. NDIS_STATUS Status;
  37. PMP_ALLOCATION pAllocInfo;
  38. if (!MPInitDone)
  39. {
  40. NdisAllocateSpinLock(&MPMemoryLock);
  41. InitializeListHead(&MPMemoryList);
  42. MPInitDone = TRUE;
  43. }
  44. //
  45. // Ensure Flags is non-zero,
  46. // otherwise NdisAllocateMemoryWithTag should be used
  47. //
  48. ASSERT(Flags);
  49. Status = NdisAllocateMemory(
  50. (PVOID *)(&pAllocInfo),
  51. (UINT)(Size + sizeof(MP_ALLOCATION)),
  52. Flags,
  53. HighestAddr);
  54. if (pAllocInfo == (PMP_ALLOCATION)NULL)
  55. {
  56. DBGPRINT(MP_LOUD,
  57. ("MPAuditAllocMemCore: file %d, line %d, Size %d failed!\n",
  58. FileNumber, LineNumber, Size));
  59. *pPointer = NULL;
  60. }
  61. else
  62. {
  63. *pPointer = (PVOID)&(pAllocInfo->UserData);
  64. MP_MEMSET(*pPointer, Size, 0xc);
  65. pAllocInfo->Signature = 'DOOG';
  66. pAllocInfo->FileNumber = FileNumber;
  67. pAllocInfo->LineNumber = LineNumber;
  68. pAllocInfo->Size = Size;
  69. pAllocInfo->Location = pPointer;
  70. pAllocInfo->Flags = Flags;
  71. NdisAcquireSpinLock(&MPMemoryLock);
  72. InsertTailList(&MPMemoryList, &pAllocInfo->List);
  73. MPAllocCount++;
  74. NdisReleaseSpinLock(&MPMemoryLock);
  75. }
  76. DBGPRINT(MP_LOUD,
  77. ("MPAuditAllocMem: file %c%c%c%c, line %d, %d bytes, [0x"PTR_FORMAT"] <- 0x"PTR_FORMAT"\n",
  78. (CHAR)(FileNumber & 0xff),
  79. (CHAR)((FileNumber >> 8) & 0xff),
  80. (CHAR)((FileNumber >> 16) & 0xff),
  81. (CHAR)((FileNumber >> 24) & 0xff),
  82. LineNumber, Size, pPointer, *pPointer));
  83. return(Status);
  84. }
  85. NDIS_STATUS MPAuditAllocMemTag(
  86. PVOID * pPointer,
  87. UINT Size,
  88. ULONG FileNumber,
  89. ULONG LineNumber
  90. )
  91. {
  92. NDIS_STATUS Status;
  93. PMP_ALLOCATION pAllocInfo;
  94. if (!MPInitDone)
  95. {
  96. NdisAllocateSpinLock(&MPMemoryLock);
  97. InitializeListHead(&MPMemoryList);
  98. MPInitDone = TRUE;
  99. }
  100. Status = NdisAllocateMemoryWithTag(
  101. (PVOID *)(&pAllocInfo),
  102. (UINT)(Size + sizeof(MP_ALLOCATION)),
  103. NIC_TAG);
  104. if (pAllocInfo == (PMP_ALLOCATION)NULL)
  105. {
  106. *pPointer = NULL;
  107. DBGPRINT(MP_LOUD,
  108. ("MPAuditAllocMemCore: file %d, line %d, Size %d failed!\n",
  109. FileNumber, LineNumber, Size));
  110. }
  111. else
  112. {
  113. *pPointer = (PVOID)&(pAllocInfo->UserData);
  114. MP_MEMSET(*pPointer, Size, 0xc);
  115. pAllocInfo->Signature = 'DOOG';
  116. pAllocInfo->FileNumber = FileNumber;
  117. pAllocInfo->LineNumber = LineNumber;
  118. pAllocInfo->Size = Size;
  119. pAllocInfo->Location = pPointer;
  120. pAllocInfo->Flags = 0;
  121. NdisAcquireSpinLock(&MPMemoryLock);
  122. InsertTailList(&MPMemoryList, &pAllocInfo->List);
  123. MPAllocCount++;
  124. NdisReleaseSpinLock(&MPMemoryLock);
  125. }
  126. DBGPRINT(MP_LOUD,
  127. ("MPAuditAllocMemTag: file %c%c%c%c, line %d, %d bytes, [0x"PTR_FORMAT"] <- 0x"PTR_FORMAT"\n",
  128. (CHAR)(FileNumber & 0xff),
  129. (CHAR)((FileNumber >> 8) & 0xff),
  130. (CHAR)((FileNumber >> 16) & 0xff),
  131. (CHAR)((FileNumber >> 24) & 0xff),
  132. LineNumber, Size, pPointer, *pPointer));
  133. return(Status);
  134. }
  135. VOID MPAuditFreeMem(
  136. IN PVOID Pointer,
  137. IN UINT Size,
  138. IN UINT Flags
  139. )
  140. {
  141. PMP_ALLOCATION pAllocInfo;
  142. pAllocInfo = CONTAINING_RECORD(Pointer, MP_ALLOCATION, UserData);
  143. ASSERT(pAllocInfo->Signature == (ULONG)'DOOG');
  144. ASSERT(pAllocInfo->Size == Size);
  145. ASSERT(pAllocInfo->Flags == Flags);
  146. NdisAcquireSpinLock(&MPMemoryLock);
  147. pAllocInfo->Signature = (ULONG)'DEAD';
  148. RemoveEntryList(&pAllocInfo->List);
  149. MPAllocCount--;
  150. NdisReleaseSpinLock(&MPMemoryLock);
  151. NdisFreeMemory(pAllocInfo, Size + sizeof(MP_ALLOCATION), Flags);
  152. }
  153. VOID mpDbgPrintUnicodeString(
  154. IN PUNICODE_STRING UnicodeString
  155. )
  156. {
  157. UCHAR Buffer[256];
  158. USHORT i;
  159. for (i = 0; (i < UnicodeString->Length / 2) && (i < 255); i++)
  160. {
  161. Buffer[i] = (UCHAR)UnicodeString->Buffer[i];
  162. }
  163. Buffer[i] = '\0';
  164. DbgPrint("%s", Buffer);
  165. }
  166. // Hex dump 'cb' bytes starting at 'p' grouping 'ulGroup' bytes together.
  167. // For example, with 'ulGroup' of 1, 2, and 4:
  168. //
  169. // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  170. // 0000 0000 0000 0000 0000 0000 0000 0000 |................|
  171. // 00000000 00000000 00000000 00000000 |................|
  172. //
  173. // If 'fAddress' is true, the memory address dumped is prepended to each
  174. // line.
  175. //
  176. VOID
  177. Dump(
  178. IN CHAR* p,
  179. IN ULONG cb,
  180. IN BOOLEAN fAddress,
  181. IN ULONG ulGroup
  182. )
  183. {
  184. INT cbLine;
  185. while (cb)
  186. {
  187. cbLine = (cb < DUMP_BytesPerLine) ? cb : DUMP_BytesPerLine;
  188. DumpLine( p, cbLine, fAddress, ulGroup );
  189. cb -= cbLine;
  190. p += cbLine;
  191. }
  192. }
  193. VOID
  194. DumpLine(
  195. IN CHAR* p,
  196. IN ULONG cb,
  197. IN BOOLEAN fAddress,
  198. IN ULONG ulGroup
  199. )
  200. {
  201. CHAR* pszDigits = "0123456789ABCDEF";
  202. CHAR szHex[ ((2 + 1) * DUMP_BytesPerLine) + 1 ];
  203. CHAR* pszHex = szHex;
  204. CHAR szAscii[ DUMP_BytesPerLine + 1 ];
  205. CHAR* pszAscii = szAscii;
  206. ULONG ulGrouped = 0;
  207. if (fAddress)
  208. {
  209. DbgPrint( "E100: %p: ", p );
  210. }
  211. else
  212. {
  213. DbgPrint( "E100: " );
  214. }
  215. while (cb)
  216. {
  217. *pszHex++ = pszDigits[ ((UCHAR )*p) / 16 ];
  218. *pszHex++ = pszDigits[ ((UCHAR )*p) % 16 ];
  219. if (++ulGrouped >= ulGroup)
  220. {
  221. *pszHex++ = ' ';
  222. ulGrouped = 0;
  223. }
  224. *pszAscii++ = (*p >= 32 && *p < 128) ? *p : '.';
  225. ++p;
  226. --cb;
  227. }
  228. *pszHex = '\0';
  229. *pszAscii = '\0';
  230. DbgPrint(
  231. "%-*s|%-*s|\n",
  232. (2 * DUMP_BytesPerLine) + (DUMP_BytesPerLine / ulGroup), szHex,
  233. DUMP_BytesPerLine, szAscii );
  234. }
  235. #endif // DBG