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.

216 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 2002-2002 Microsoft Corporation
  3. Module Name:
  4. CmnDbgU.c
  5. Abstract:
  6. Debug code for CmnUser.lib
  7. Author:
  8. George V. Reilly (GeorgeRe) 16-Jan-2002
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. BOOLEAN g_UlVerboseErrors = TRUE;
  13. BOOLEAN g_UlBreakOnError = TRUE;
  14. ULONGLONG g_UlDebug = 0;
  15. ULONG g_BytesAllocated, g_BytesFreed;
  16. ULONG g_NumAllocs, g_NumFrees;
  17. PCSTR
  18. HttpCmnpDbgFindFilePart(
  19. IN PCSTR pPath
  20. )
  21. {
  22. // Strip off the path from the path.
  23. PCSTR pFilePart = strrchr( pPath, '\\' );
  24. return (pFilePart == NULL) ? pPath : pFilePart + 1;
  25. } // HttpCmnpDbgFindFilePart
  26. VOID
  27. __cdecl
  28. HttpCmnDbgPrint(
  29. IN PCH Format,
  30. ...
  31. )
  32. {
  33. #define PRINTF_BUFFER_LEN 512
  34. va_list arglist;
  35. CHAR Buffer[PRINTF_BUFFER_LEN];
  36. int cb;
  37. va_start(arglist, Format);
  38. cb = _vsnprintf((char*) Buffer, sizeof(Buffer), Format, arglist);
  39. va_end(arglist);
  40. if (cb < 0)
  41. {
  42. cb = sizeof(Buffer);
  43. }
  44. // _vsnprintf doesn't always NUL-terminate the buffer
  45. Buffer[DIMENSION(Buffer)-1] = '\0';
  46. OutputDebugStringA(Buffer);
  47. } // HttpCmnDbgPrint
  48. VOID
  49. HttpCmnDbgAssert(
  50. PCSTR pszAssert,
  51. PCSTR pszFilename,
  52. ULONG LineNumber
  53. )
  54. {
  55. HttpCmnDbgPrint(
  56. "Assertion failed: %s:%lu: %s\n",
  57. HttpCmnpDbgFindFilePart( pszFilename ), LineNumber, pszAssert
  58. );
  59. DebugBreak();
  60. } // HttpCmnDbgAssert
  61. NTSTATUS
  62. HttpCmnDbgStatus(
  63. NTSTATUS Status,
  64. PCSTR pszFilename,
  65. ULONG LineNumber
  66. )
  67. {
  68. if (!NT_SUCCESS(Status))
  69. {
  70. if (g_UlVerboseErrors)
  71. {
  72. HttpCmnDbgPrint(
  73. "HttpCmnDbgStatus: %s:%lu returning 0x%08lx\n",
  74. HttpCmnpDbgFindFilePart( pszFilename ),
  75. LineNumber,
  76. Status
  77. );
  78. }
  79. if (g_UlBreakOnError)
  80. {
  81. DebugBreak();
  82. }
  83. }
  84. return Status;
  85. } // HttpCmnDbgStatus
  86. VOID
  87. HttpCmnDbgBreakOnError(
  88. PCSTR pszFilename,
  89. ULONG LineNumber
  90. )
  91. {
  92. if (g_UlBreakOnError)
  93. {
  94. HttpCmnDbgPrint("HttpCmnDebugBreakOnError @ %s:%lu\n",
  95. HttpCmnpDbgFindFilePart( pszFilename ),
  96. LineNumber
  97. );
  98. DebugBreak();
  99. }
  100. }
  101. VOID
  102. HttpCmnInitAllocator(
  103. VOID
  104. )
  105. {
  106. g_BytesAllocated = g_BytesFreed = 0;
  107. g_NumAllocs = g_NumFrees = 0;
  108. }
  109. VOID
  110. HttpCmnTermAllocator(
  111. VOID
  112. )
  113. {
  114. // ASSERT(g_BytesAllocated == g_BytesFreed);
  115. ASSERT(g_NumAllocs == g_NumFrees);
  116. }
  117. PVOID
  118. HttpCmnAllocate(
  119. IN POOL_TYPE PoolType,
  120. IN SIZE_T NumBytes,
  121. IN ULONG PoolTag,
  122. IN PCSTR pFileName,
  123. IN USHORT LineNumber)
  124. {
  125. PVOID pMem = HeapAlloc(GetProcessHeap(), 0, NumBytes);
  126. // CODEWORK: steal the debug header/trailer stuff from ..\sys\debug.c
  127. // or migrate it into ..\common
  128. UNREFERENCED_PARAMETER(PoolType);
  129. UNREFERENCED_PARAMETER(PoolTag);
  130. UNREFERENCED_PARAMETER(pFileName);
  131. UNREFERENCED_PARAMETER(LineNumber);
  132. if (NULL != pMem)
  133. {
  134. InterlockedExchangeAdd((PLONG) &g_BytesAllocated, (LONG) NumBytes);
  135. InterlockedIncrement((PLONG) &g_NumAllocs);
  136. }
  137. // UrlAclTrace("Allocated: %p\n", pMem);
  138. return pMem;
  139. } // HttpCmnAllocate
  140. VOID
  141. HttpCmnFree(
  142. IN PVOID pMem,
  143. IN ULONG PoolTag,
  144. IN PCSTR pFileName,
  145. IN USHORT LineNumber)
  146. {
  147. UNREFERENCED_PARAMETER(PoolTag);
  148. UNREFERENCED_PARAMETER(pFileName);
  149. UNREFERENCED_PARAMETER(LineNumber);
  150. if (NULL != pMem)
  151. {
  152. SIZE_T NumBytes = 0; // BUGBUG
  153. InterlockedExchangeAdd((PLONG) &g_BytesFreed, (LONG) NumBytes);
  154. InterlockedIncrement((PLONG) &g_NumFrees);
  155. }
  156. // UrlAclTrace("Freed: %p\n", pMem);
  157. HeapFree(GetProcessHeap(), 0, pMem);
  158. } // HttpCmnFree