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.

257 lines
6.6 KiB

  1. // Copyright (c) 1998-1999 Microsoft Corporation
  2. /******************************************************************************
  3. *
  4. * DBGTRACE.C
  5. *
  6. * enable or disable tracing
  7. *
  8. *
  9. *
  10. *******************************************************************************/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <ntddkbd.h>
  15. #include <ntddmou.h>
  16. #include <windows.h>
  17. #include <winbase.h>
  18. #include <winerror.h>
  19. #include <winstaw.h>
  20. #include <icadd.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <locale.h>
  25. #include <utilsub.h>
  26. #include "dbgtrace.h"
  27. #include "printfoa.h"
  28. WCHAR CurDir[ 256 ];
  29. WCHAR WinStation[MAX_IDS_LEN+1];
  30. WCHAR TraceOption[MAX_OPTION];
  31. int bTraceOption = FALSE;
  32. int fDebugger = FALSE;
  33. int fTimestamp = FALSE;
  34. int fHelp = FALSE;
  35. int fSystem = FALSE;
  36. int fAll = FALSE;
  37. ULONG TraceClass = 0;
  38. ULONG TraceEnable = 0;
  39. ULONG LogonId;
  40. TOKMAP ptm[] = {
  41. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, WinStation},
  42. {L"/c", TMFLAG_OPTIONAL, TMFORM_LONGHEX, sizeof(ULONG), &TraceClass},
  43. {L"/e", TMFLAG_OPTIONAL, TMFORM_LONGHEX, sizeof(ULONG), &TraceEnable},
  44. {L"/d", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &fDebugger},
  45. {L"/t", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &fTimestamp},
  46. {L"/o", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_OPTION, TraceOption},
  47. {L"/system", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &fSystem},
  48. {L"/all", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(int), &fAll},
  49. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN, sizeof(USHORT), &fHelp},
  50. {0, 0, 0, 0, 0}
  51. };
  52. void SetSystemTrace( PICA_TRACE );
  53. void SetStackTrace( PICA_TRACE );
  54. /*******************************************************************************
  55. *
  56. * main
  57. *
  58. ******************************************************************************/
  59. int __cdecl
  60. main(INT argc, CHAR **argv)
  61. {
  62. WCHAR *CmdLine;
  63. WCHAR **argvW;
  64. ULONG rc;
  65. int i;
  66. ICA_TRACE Trace;
  67. setlocale(LC_ALL, ".OCP");
  68. /*
  69. * Massage the command line.
  70. */
  71. argvW = MassageCommandLine((DWORD)argc);
  72. if (argvW == NULL) {
  73. ErrorPrintf(IDS_ERROR_MALLOC);
  74. return(FAILURE);
  75. }
  76. /*
  77. * parse the cmd line without parsing the program name (argc-1, argv+1)
  78. */
  79. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  80. /*
  81. * Check for error from ParseCommandLine
  82. */
  83. if ( fHelp || (rc && !(rc & PARSE_FLAG_NO_PARMS)) ) {
  84. if ( !fHelp ) {
  85. ErrorPrintf(IDS_ERROR_PARAMS);
  86. ErrorPrintf(IDS_USAGE_1);
  87. ErrorPrintf(IDS_USAGE_2);
  88. return(FAILURE);
  89. } else {
  90. Message(IDS_USAGE_1);
  91. Message(IDS_USAGE_2);
  92. return(SUCCESS);
  93. }
  94. }
  95. //Check if we are running under Terminal Server
  96. if(!AreWeRunningTerminalServices())
  97. {
  98. ErrorPrintf(IDS_ERROR_NOT_TS);
  99. return(FAILURE);
  100. }
  101. if ( fAll ) {
  102. TraceClass = 0xffffffff;
  103. TraceEnable = 0xffffffff;
  104. }
  105. /*
  106. * Get current directory
  107. */
  108. (VOID) GetCurrentDirectory( 256, CurDir );
  109. /*
  110. * Get the LogonId
  111. */
  112. if ( ptm[0].tmFlag & TMFLAG_PRESENT ) {
  113. if ( iswdigit( WinStation[0] ) ) {
  114. LogonId = (ULONG) wcstol( WinStation, NULL, 10 );
  115. } else {
  116. if ( !LogonIdFromWinStationName( SERVERNAME_CURRENT, WinStation, &LogonId ) ) {
  117. StringErrorPrintf( IDS_ERROR_SESSION, WinStation );
  118. return(-1);
  119. }
  120. }
  121. if ( fSystem )
  122. wsprintf( Trace.TraceFile, L"%s\\winframe.log", CurDir );
  123. else
  124. wsprintf( Trace.TraceFile, L"%s\\%s.log", CurDir, WinStation );
  125. } else {
  126. LogonId = GetCurrentLogonId();
  127. if ( fSystem )
  128. wsprintf( Trace.TraceFile, L"%s\\winframe.log", CurDir );
  129. else
  130. wsprintf( Trace.TraceFile, L"%s\\%u.log", CurDir, LogonId );
  131. }
  132. /*
  133. * Build trace structure
  134. */
  135. Trace.fDebugger = fDebugger ? TRUE : FALSE;
  136. Trace.fTimestamp = fTimestamp ? FALSE : TRUE;
  137. Trace.TraceClass = TraceClass;
  138. Trace.TraceEnable = TraceEnable;
  139. if ( TraceClass == 0 || TraceEnable == 0 )
  140. Trace.TraceFile[0] = '\0';
  141. /*
  142. * Fill in the trace option if any
  143. */
  144. bTraceOption = ptm[5].tmFlag & TMFLAG_PRESENT;
  145. if ( bTraceOption )
  146. memcpy(Trace.TraceOption, TraceOption, sizeof(TraceOption));
  147. else
  148. memset(Trace.TraceOption, 0, sizeof(TraceOption));
  149. /*
  150. * Set trace information
  151. */
  152. if ( fSystem )
  153. SetSystemTrace( &Trace );
  154. else
  155. SetStackTrace( &Trace );
  156. return(0);
  157. }
  158. void
  159. SetSystemTrace( PICA_TRACE pTrace )
  160. {
  161. /*
  162. * Set trace information
  163. */
  164. if ( !WinStationSetInformation( SERVERNAME_CURRENT,
  165. LogonId,
  166. WinStationSystemTrace,
  167. pTrace,
  168. sizeof(ICA_TRACE) ) ) {
  169. Message(IDS_ERROR_SET_TRACE, GetLastError());
  170. return;
  171. }
  172. if ( pTrace->TraceClass == 0 || pTrace->TraceEnable == 0 ) {
  173. Message( IDS_TRACE_DIS_LOG );
  174. } else {
  175. Message( IDS_TRACE_EN_LOG );
  176. wprintf( L"- %08x %08x [%s] %s\n", pTrace->TraceClass, pTrace->TraceEnable,
  177. pTrace->TraceFile, fDebugger ? TEXT("Debugger") : TEXT("") );
  178. }
  179. }
  180. void
  181. SetStackTrace( PICA_TRACE pTrace )
  182. {
  183. WINSTATIONINFOCLASS InfoClass;
  184. ULONG InfoSize;
  185. /*
  186. * Check for console
  187. */
  188. if ( LogonId == 0 ) {
  189. Message( IDS_TRACE_UNSUPP );
  190. return;
  191. }
  192. /*
  193. * Set trace information
  194. */
  195. if ( !WinStationSetInformation( SERVERNAME_CURRENT,
  196. LogonId,
  197. WinStationTrace,
  198. pTrace,
  199. sizeof(ICA_TRACE))) {
  200. Message(IDS_ERROR_SET_TRACE, GetLastError());
  201. return;
  202. }
  203. if ( pTrace->TraceClass == 0 || pTrace->TraceEnable == 0 ) {
  204. Message( IDS_TRACE_DISABLED, LogonId );
  205. } else {
  206. Message( IDS_TRACE_ENABLED, LogonId );
  207. wprintf( L"- %08x %08x [%s] %s\n", pTrace->TraceClass, pTrace->TraceEnable,
  208. pTrace->TraceFile, fDebugger ? "Debugger" : "" );
  209. }
  210. }