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.

312 lines
6.6 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. tcutils.c
  5. Abstract:
  6. This module contains support routines for the traffic DLL.
  7. Author:
  8. Jim Stewart (jstew) August 14, 1996
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #include <wincon.h>
  14. #include <winuser.h>
  15. #if DBG
  16. BOOLEAN ConsoleInitialized = FALSE;
  17. HANDLE DebugFileHandle = INVALID_HANDLE_VALUE;
  18. PTCHAR DebugFileName = L"/temp/traffic.log";
  19. PTCHAR TRAFFIC_DBG = L"Traffic.dbg";
  20. VOID
  21. WsAssert(
  22. IN PVOID FailedAssertion,
  23. IN PVOID FileName,
  24. IN ULONG LineNumber
  25. )
  26. {
  27. BOOL ok;
  28. CHAR choice[16];
  29. DWORD bytes;
  30. DWORD error;
  31. IF_DEBUG(CONSOLE) {
  32. WSPRINT(( " failed: %s\n at line %ld of %s\n",
  33. FailedAssertion, LineNumber, FileName ));
  34. do {
  35. WSPRINT(( "[B]reak/[I]gnore? " ));
  36. bytes = sizeof(choice);
  37. ok = ReadFile(
  38. GetStdHandle(STD_INPUT_HANDLE),
  39. &choice,
  40. bytes,
  41. &bytes,
  42. NULL
  43. );
  44. if ( ok ) {
  45. if ( toupper(choice[0]) == 'I' ) {
  46. break;
  47. }
  48. if ( toupper(choice[0]) == 'B' ) {
  49. DEBUGBREAK();
  50. }
  51. } else {
  52. error = GetLastError( );
  53. }
  54. } while ( TRUE );
  55. return;
  56. }
  57. RtlAssert( FailedAssertion, FileName, LineNumber, NULL );
  58. } // WsAssert
  59. VOID
  60. WsPrintf (
  61. char *Format,
  62. ...
  63. )
  64. {
  65. va_list arglist;
  66. char OutputBuffer[1024];
  67. ULONG length;
  68. BOOL ret;
  69. length = (ULONG)wsprintfA( OutputBuffer, "TRAFFIC [%05d]: ",
  70. GetCurrentThreadId() );
  71. va_start( arglist, Format );
  72. wvsprintfA( OutputBuffer + length, Format, arglist );
  73. va_end( arglist );
  74. IF_DEBUG(DEBUGGER) {
  75. DbgPrint( "%s", OutputBuffer );
  76. }
  77. IF_DEBUG(CONSOLE) {
  78. if ( !ConsoleInitialized ) {
  79. CONSOLE_SCREEN_BUFFER_INFO csbi;
  80. COORD coord;
  81. ConsoleInitialized = TRUE;
  82. (VOID)AllocConsole( );
  83. (VOID)GetConsoleScreenBufferInfo(
  84. GetStdHandle(STD_OUTPUT_HANDLE),
  85. &csbi
  86. );
  87. coord.X = (SHORT)(csbi.srWindow.Right - csbi.srWindow.Left + 1);
  88. coord.Y = (SHORT)((csbi.srWindow.Bottom - csbi.srWindow.Top + 1) * 20);
  89. (VOID)SetConsoleScreenBufferSize(
  90. GetStdHandle(STD_OUTPUT_HANDLE),
  91. coord
  92. );
  93. }
  94. length = strlen( OutputBuffer );
  95. ret = WriteFile(
  96. GetStdHandle(STD_OUTPUT_HANDLE),
  97. (LPVOID )OutputBuffer,
  98. length,
  99. &length,
  100. NULL
  101. );
  102. if ( !ret ) {
  103. DbgPrint( "WsPrintf: console WriteFile failed: %ld\n",
  104. GetLastError( ) );
  105. }
  106. }
  107. IF_DEBUG(FILE) {
  108. if ( DebugFileHandle == INVALID_HANDLE_VALUE ) {
  109. DebugFileHandle = CreateFile(
  110. DebugFileName,
  111. GENERIC_READ | GENERIC_WRITE,
  112. FILE_SHARE_READ,
  113. NULL,
  114. CREATE_ALWAYS,
  115. 0,
  116. NULL
  117. );
  118. }
  119. if ( DebugFileHandle == INVALID_HANDLE_VALUE ) {
  120. //DbgPrint( "WsPrintf: Failed to open traffic debug log file %s: %ld\n",
  121. // DebugFileName, GetLastError( ) );
  122. } else {
  123. length = strlen( OutputBuffer );
  124. ret = WriteFile(
  125. DebugFileHandle,
  126. (LPVOID )OutputBuffer,
  127. length,
  128. &length,
  129. NULL
  130. );
  131. if ( !ret ) {
  132. DbgPrint( "WsPrintf: file WriteFile failed: %ld\n",
  133. GetLastError( ) );
  134. }
  135. }
  136. }
  137. } // WsPrintf
  138. #endif
  139. ULONG
  140. LockedDec(
  141. IN PULONG Count
  142. )
  143. /*++
  144. Routine Description:
  145. This routine is a debug routine used for checking decrements on counts.
  146. It asserts if the count goes negative. The Macro LockedDecrement calls it.
  147. Arguments:
  148. pointer to the count.
  149. Return Value:
  150. none
  151. --*/
  152. {
  153. ULONG Result;
  154. Result = InterlockedDecrement( (PLONG)Count );
  155. ASSERT( Result < 0x80000000 );
  156. return( Result );
  157. }
  158. #if DBG
  159. VOID
  160. SetupDebugInfo()
  161. /*++
  162. Description:
  163. This routine reads in a debug file that may contain debug instructions.
  164. Arguments:
  165. none
  166. Return Value:
  167. none
  168. --*/
  169. {
  170. HANDLE handle;
  171. //
  172. // If there is a file in the current directory called "tcdebug"
  173. // open it and read the first line to set the debugging flags.
  174. //
  175. handle = CreateFile(
  176. TRAFFIC_DBG,
  177. GENERIC_READ,
  178. FILE_SHARE_READ | FILE_SHARE_WRITE,
  179. NULL,
  180. OPEN_EXISTING,
  181. 0,
  182. NULL
  183. );
  184. if( handle == INVALID_HANDLE_VALUE ) {
  185. //
  186. // Set default value. changed - Oferbar
  187. //
  188. //DebugMask = DEBUG_DEBUGGER | DEBUG_CONSOLE;
  189. DebugMask |= DEBUG_ERRORS; // always dump errors
  190. DebugMask |= DEBUG_FILE; // always print a log.
  191. DebugMask |= DEBUG_WARNINGS; // until Beta3, we want the warnings too
  192. } else {
  193. CHAR buffer[11];
  194. DWORD bytesRead;
  195. RtlZeroMemory( buffer, sizeof(buffer) );
  196. if ( ReadFile( handle, buffer, 10, &bytesRead, NULL ) ) {
  197. buffer[bytesRead] = '\0';
  198. DebugMask = strtoul( buffer, NULL, 16 );
  199. } else {
  200. WSPRINT(( "read file failed: %ld\n", GetLastError( ) ));
  201. }
  202. CloseHandle( handle );
  203. }
  204. }
  205. VOID
  206. CloseDbgFile(
  207. )
  208. /*++
  209. Routine Description:
  210. This closes the debug output file if its open.
  211. Arguments:
  212. none
  213. Return Value:
  214. none
  215. --*/
  216. {
  217. if (DebugFileHandle != INVALID_HANDLE_VALUE) {
  218. CloseHandle( DebugFileHandle );
  219. }
  220. }
  221. #endif
  222.