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.

247 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. debug.c
  5. Abstract:
  6. This file contains debugging macros for the Terminal Concentrator server.
  7. Author:
  8. Madan Appiah (madana) 10-Sep-1993
  9. Modifications - Sadagopan Rajaram 11th Nov 99
  10. Environment:
  11. User Mode - Win32
  12. Revision History:
  13. --*/
  14. //
  15. // Adds a MEMORYBLOCK to the memory tracking list.
  16. //
  17. #include <tcsrv.h>
  18. #if DBG==1
  19. LPMEMORYBLOCK g_TraceMemoryTable;
  20. CRITICAL_SECTION g_TraceMemoryCS;
  21. HGLOBAL
  22. TCReAlloc(
  23. HGLOBAL mem,
  24. DWORD size,
  25. LPCSTR comment
  26. )
  27. {
  28. HGLOBAL temp;
  29. LPMEMORYBLOCK pmbHead;
  30. DWORD nBytes=0;
  31. temp = DebugAlloc(GMEM_ZEROINIT, size, comment);
  32. if(temp != NULL){
  33. EnterCriticalSection( &g_TraceMemoryCS );
  34. pmbHead = g_TraceMemoryTable;
  35. while ( pmbHead && pmbHead->hglobal != mem )
  36. {
  37. pmbHead = pmbHead->pNext;
  38. }
  39. if(pmbHead){
  40. nBytes = pmbHead->dwBytes;
  41. }
  42. LeaveCriticalSection(&g_TraceMemoryCS);
  43. if(nBytes){
  44. memcpy(temp, mem, nBytes);
  45. DebugFree(mem);
  46. }
  47. }
  48. return temp;
  49. }
  50. HGLOBAL
  51. DebugMemoryAdd(
  52. HGLOBAL hglobal,
  53. DWORD dwBytes,
  54. LPCSTR pszComment
  55. )
  56. {
  57. if ( hglobal )
  58. {
  59. LPMEMORYBLOCK pmb = (LPMEMORYBLOCK) GlobalAlloc(
  60. GMEM_FIXED,
  61. sizeof(MEMORYBLOCK) );
  62. if ( !pmb )
  63. {
  64. GlobalFree( hglobal );
  65. return NULL;
  66. }
  67. pmb->hglobal = hglobal;
  68. pmb->dwBytes = dwBytes;
  69. pmb->pszComment = pszComment;
  70. EnterCriticalSection( &g_TraceMemoryCS );
  71. pmb->pNext = g_TraceMemoryTable;
  72. g_TraceMemoryTable = pmb;
  73. TCDebugPrint(("DebugAlloc: 0x%08x alloced %d (%s) with 0x%08x\n", hglobal, dwBytes,
  74. pmb->pszComment, pmb ));
  75. LeaveCriticalSection( &g_TraceMemoryCS );
  76. }
  77. return hglobal;
  78. }
  79. //
  80. // Removes a MEMORYBLOCK to the memory tracking list.
  81. //
  82. void
  83. DebugMemoryDelete(
  84. HGLOBAL hglobal )
  85. {
  86. if ( hglobal )
  87. {
  88. LPMEMORYBLOCK pmbHead;
  89. LPMEMORYBLOCK pmbLast = NULL;
  90. EnterCriticalSection( &g_TraceMemoryCS );
  91. pmbHead = g_TraceMemoryTable;
  92. while ( (pmbHead) && (pmbHead->hglobal != hglobal ))
  93. {
  94. pmbLast = pmbHead;
  95. pmbHead = pmbLast->pNext;
  96. }
  97. if ( pmbHead )
  98. {
  99. HGLOBAL *p;
  100. if ( pmbLast )
  101. {
  102. pmbLast->pNext = pmbHead->pNext;
  103. }
  104. else
  105. {
  106. g_TraceMemoryTable = pmbHead->pNext;
  107. }
  108. TCDebugPrint(("DebugFree: 0x%08x freed %d (%s)\n",pmbHead->hglobal,pmbHead->dwBytes,
  109. pmbHead->pszComment ));
  110. p = (HGLOBAL)((LPBYTE)hglobal + pmbHead->dwBytes);
  111. if ( *p != hglobal )
  112. {
  113. TCDebugPrint(("DebugFree: Heap check FAILED for 0x%08x %u bytes (%s).\n",
  114. hglobal, pmbHead->dwBytes, pmbHead->pszComment));
  115. }
  116. memset( hglobal, 0xFE, pmbHead->dwBytes + sizeof(HGLOBAL));
  117. memset( pmbHead, 0xFD, sizeof(MEMORYBLOCK) );
  118. pmbHead->pNext = NULL;
  119. GlobalFree( pmbHead );
  120. }
  121. else
  122. {
  123. HGLOBAL *p;
  124. TCDebugPrint(("DebugFree: 0x%08x not found in memory table\n", hglobal ));
  125. memset( hglobal, 0xFE, (int)GlobalSize( hglobal ));
  126. }
  127. LeaveCriticalSection( &g_TraceMemoryCS );
  128. }
  129. }
  130. //
  131. // Allocates memory and adds the MEMORYBLOCK to the memory tracking list.
  132. //
  133. HGLOBAL
  134. DebugAlloc(
  135. UINT uFlags,
  136. DWORD dwBytes,
  137. LPCSTR pszComment )
  138. {
  139. HGLOBAL hglobal;
  140. HGLOBAL *p;
  141. hglobal = GlobalAlloc( uFlags, dwBytes + sizeof(HGLOBAL));
  142. if (hglobal == NULL) {
  143. TCDebugPrint(("No memory %s %d\n", pszComment, GetLastError()));
  144. return NULL;
  145. }
  146. p = (HGLOBAL)((LPBYTE)hglobal + dwBytes);
  147. *p = hglobal;
  148. return DebugMemoryAdd( hglobal,dwBytes, pszComment );
  149. }
  150. //
  151. // Remove the MEMORYBLOCK to the memory tracking list, memsets the
  152. // memory to 0xFE and then frees the memory.
  153. //
  154. HGLOBAL
  155. DebugFree(
  156. HGLOBAL hglobal )
  157. {
  158. DebugMemoryDelete( hglobal );
  159. return GlobalFree( hglobal );
  160. }
  161. //
  162. // Checks the memory tracking list. If it is not empty, it will dump the
  163. // list and break.
  164. //
  165. void
  166. DebugMemoryCheck( )
  167. {
  168. BOOL fFoundLeak = FALSE;
  169. LPMEMORYBLOCK pmb;
  170. EnterCriticalSection( &g_TraceMemoryCS );
  171. pmb = g_TraceMemoryTable;
  172. while ( pmb )
  173. {
  174. LPMEMORYBLOCK pTemp;
  175. if ( fFoundLeak == FALSE )
  176. {
  177. TCDebugPrint(("\n***************************** Memory leak detected *****************************\n\n"));
  178. TCDebugPrint(("Memory leak at %x, %s", pmb->hglobal, pmb->pszComment));
  179. fFoundLeak = TRUE;
  180. }
  181. pTemp = pmb;
  182. pmb = pmb->pNext;
  183. memset( pTemp, 0xFD, sizeof(MEMORYBLOCK) );
  184. GlobalFree( pTemp );
  185. }
  186. if ( fFoundLeak == TRUE )
  187. {
  188. TCDebugPrint(("\n***************************** Memory leak detected *****************************\n\n"));
  189. }
  190. LeaveCriticalSection( &g_TraceMemoryCS );
  191. //BinlAssert( !fFoundLeak );
  192. }
  193. #endif // DBG==1