Source code of Windows XP (NT5)
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.

450 lines
11 KiB

  1. #define MAX_STR 256
  2. #define MAX_GUIDS 10
  3. #define _UNICODE
  4. #define UNICODE
  5. #include "stdafx.h"
  6. #pragma warning (disable : 4786)
  7. #pragma warning (disable : 4275)
  8. #include <iostream>
  9. #include <strstream>
  10. #include <fstream>
  11. #include <string>
  12. #include <sstream>
  13. #include <map>
  14. #include <list>
  15. using namespace std;
  16. #include <tchar.h>
  17. #include <process.h>
  18. #include <windows.h>
  19. #ifdef NONNT5
  20. typedef unsigned long ULONG_PTR;
  21. #endif
  22. #include <wmistr.h>
  23. #include <guiddef.h>
  24. #include <initguid.h>
  25. #include <evntrace.h>
  26. #include <WTYPES.H>
  27. /*
  28. #include "stdafx.h"
  29. #include <string>
  30. #include <iosfwd>
  31. #include <iostream>
  32. #include <fstream>
  33. #include <ctime>
  34. #include <list>
  35. using namespace std;
  36. #include <malloc.h>
  37. #include <windows.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <windows.h>
  41. #include <tchar.h>
  42. #include <wmistr.h>
  43. #include <objbase.h>
  44. #include <initguid.h>
  45. #include <evntrace.h>
  46. */
  47. #include "struct.h"
  48. #include "utils.h"
  49. #include "Readfile.h"
  50. #include "umprovider.h"
  51. TRACEHANDLE LoggerHandle = (TRACEHANDLE) 2;
  52. ULONG TraceEventFlag = 0, EnableLevel = 0, EnableFlags = 0;
  53. ULONG LoggerEnableLevel, LoggerEnableFlag;
  54. ULONG TraceMode;
  55. ULONG DataBlockSize = 0;
  56. ULONG TraceEventInstanceFlag = 0;
  57. PREGISTER_TRACE_GUID RegisterTraceGuid;
  58. GUID TransactionGuid[12] = { { 0xa7301ec8, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  59. { 0xa8558716, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  60. { 0xa9226a42, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  61. { 0xaa395d64, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  62. { 0xaa8ccfb2, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  63. { 0xaad4563e, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  64. { 0xab171816, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  65. { 0xab55153a, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  66. { 0xab84c442, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  67. { 0xabb210f0, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  68. { 0xabe1bff8, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 },
  69. { 0xb01e8dee, 0x1200, 0x11d3, 0x99, 0x06, 0x00, 0xc0, 0x4f, 0x79, 0xb3, 0xf8 } };
  70. typedef struct _BLOCK1 {
  71. UCHAR EventChar;
  72. ULONG EventUlong;
  73. } BLOCK1, *PBLOCK1;
  74. typedef struct _EVENT_BLOCK1 {
  75. EVENT_TRACE_HEADER Header;
  76. BLOCK1 Block1;
  77. } EVENT_BLOCK1, *P_EVENT_BLOCK1;
  78. typedef struct _EVENT_INSTANCE_BLOCK1 {
  79. EVENT_INSTANCE_HEADER Header;
  80. UCHAR EventChar;
  81. ULONG EventUlong;
  82. } EVENT_INSTANCE_BLOCK1, *P_EVENT_INSTANCE_BLOCK1;
  83. typedef struct _EVENT_BLOCK2 {
  84. EVENT_TRACE_HEADER Header;
  85. MOF_FIELD MofField;
  86. } EVENT_BLOCK2, *P_EVENT_BLOCK2;
  87. BLOCK1 Block1;
  88. EVENT_BLOCK1 EventBlock1;
  89. EVENT_BLOCK2 EventBlock2;
  90. EVENT_INSTANCE_BLOCK1 EventInstanceBlock1;
  91. TRACE_GUID_REGISTRATION TraceGuidReg[MAX_GUIDS];
  92. main(int argc, char* argv[])
  93. {
  94. LPTSTR CommandFile;
  95. TRACEHANDLE RegistrationHandle;
  96. ULONG Status;
  97. LPTSTR *commandLine;
  98. LPGUID pGuid;
  99. ULONG Count;
  100. ULONG GuidCount = 0;
  101. //CLogger *pLoggingInfo;
  102. LPCTSTR LogFileName;
  103. LPTSTR *targv;
  104. int i;
  105. pGuid = (LPGUID ) malloc(MAX_GUIDS * sizeof(GUID));
  106. CommandFile = (LPTSTR) malloc(sizeof(TCHAR)*MAX_STR);
  107. LogFileName = (LPCTSTR) malloc(sizeof(TCHAR)*MAX_STR);
  108. targv = (LPTSTR *) malloc(argc*sizeof(LPTSTR));
  109. for( i = 0; i < argc; i++ )
  110. targv[i] = (LPTSTR) malloc(sizeof(TCHAR)*MAX_STR);
  111. SplitCommandLine( GetCommandLine(), targv);
  112. for( i = 0; i < MAX_GUIDS; i++)
  113. TraceGuidReg[i].Guid = &TransactionGuid[i];
  114. Count = 1;
  115. while ( Count <= (argc-1) )
  116. {
  117. if ( !_tcsicmp(targv[Count], _T("-guids") ))
  118. {
  119. Count++;
  120. if (Count <= argc)
  121. {
  122. ConvertAsciiToGuid( targv[Count], &pGuid[GuidCount]);
  123. GuidCount++;
  124. Count++;
  125. }
  126. continue;
  127. }
  128. if( !_tcsicmp(targv[Count], _T("-file") ))
  129. {
  130. Count++;
  131. if ( Count <= argc)
  132. {
  133. CommandFile = targv[Count];
  134. Count++;
  135. }
  136. continue;
  137. }
  138. if( !_tcsicmp(targv[Count], _T("-flag") ))
  139. {
  140. Count++;
  141. if( Count <= argc)
  142. {
  143. LoggerEnableFlag = atoi(argv[Count]);
  144. Count++;
  145. }
  146. continue;
  147. }
  148. if( !_tcsicmp(targv[Count], _T("-level") ))
  149. {
  150. Count++;
  151. if( Count <= argc)
  152. {
  153. LoggerEnableLevel = atoi(argv[Count]);
  154. Count++;
  155. }
  156. continue;
  157. }
  158. if( !_tcsicmp(targv[Count], _T("-mode") ))
  159. {
  160. Count++;
  161. if( Count <= argc)
  162. {
  163. TraceMode = atoi(argv[Count]);
  164. Count++;
  165. continue;
  166. }
  167. }
  168. if( !_tcsicmp(targv[Count], _T("-TraceEventInstance") ))
  169. {
  170. Count++;
  171. TraceEventInstanceFlag = 1;
  172. continue;
  173. }
  174. if( !_tcsicmp(targv[Count], _T("-log") ))
  175. {
  176. Count++;
  177. if( Count <= argc )
  178. {
  179. //LogFileName = _T(argv[Count]);
  180. LogFileName = _T("C:\\");
  181. Count++;
  182. continue;
  183. }
  184. }
  185. Count++;
  186. continue;
  187. }
  188. Count = sizeof(REGISTER_TRACE_GUID);
  189. RegisterTraceGuid = (PREGISTER_TRACE_GUID ) malloc(sizeof(REGISTER_TRACE_GUID));
  190. RegisterTraceGuid->ControlGuid = (LPGUID) malloc(sizeof(GUID));
  191. RegisterTraceGuid->MofImagePath = (TCHAR *) malloc (sizeof(TCHAR)*MAX_STR);
  192. RegisterTraceGuid->MofResourceName = (TCHAR *) malloc (sizeof(TCHAR)*MAX_STR);
  193. if( RegisterTraceGuid == NULL)
  194. {
  195. printf("\nGufooo");
  196. exit(0);
  197. }
  198. InitializeRegisterTraceGuid( RegisterTraceGuid );
  199. RegisterTraceGuid->UseMofPtrFlag = 0;
  200. RegisterTraceGuid->UseGuidPtrFlag = 0;
  201. Status = ReadInputFile( CommandFile, RegisterTraceGuid );
  202. if( RegisterTraceGuid->GuidCount != 0)
  203. RegisterTraceGuid->GuidCount = GuidCount;
  204. if( RegisterTraceGuid->CallBackFunction != 0)
  205. RegisterTraceGuid->CallBackFunction = ProviderCallBack;
  206. if( RegisterTraceGuid->TraceGuidReg != 0 )
  207. RegisterTraceGuid->TraceGuidReg = ( PTRACE_GUID_REGISTRATION) &TraceGuidReg[0];
  208. if( RegisterTraceGuid->RegistrationHandle != 0 )
  209. RegisterTraceGuid->RegistrationHandle = &RegistrationHandle;
  210. if( RegisterTraceGuid->GuidCount != 0 )
  211. RegisterTraceGuid->GuidCount = MAX_GUIDS;
  212. //InitializeLogging( LogFileName, pLoggingInfo);
  213. //if (pLoggingInfo)
  214. //{
  215. //pLoggingInfo->LogTCHAR(_T("\n*******************************************************\n"));
  216. //pLoggingInfo->LogTCHAR(_T("StartTraceAPI TCO test "));
  217. //pLoggingInfo->Flush();
  218. //}
  219. Status = RegisterTraceGuids( ProviderCallBack,
  220. NULL, //Context
  221. pGuid,
  222. RegisterTraceGuid->GuidCount,
  223. RegisterTraceGuid->TraceGuidReg,
  224. RegisterTraceGuid->MofImagePath,
  225. RegisterTraceGuid->MofResourceName,
  226. RegisterTraceGuid->RegistrationHandle );
  227. if( RegisterTraceGuid->UnRegistrationHandle != 0 )
  228. {
  229. Status = UnregisterTraceGuids( (TRACEHANDLE) 0 );
  230. return;
  231. }
  232. if( Status == ERROR_SUCCESS)
  233. {
  234. Count = 0;
  235. do
  236. {
  237. if( (TraceEventFlag ) && ( TraceEventInstanceFlag) )
  238. {
  239. Status = TraceEventInstances( TraceMode, (Count%10), Count);
  240. Count++;
  241. }
  242. else if (TraceEventFlag)
  243. {
  244. Status = TraceEvents( TraceMode, (Count%10), (Count%100));
  245. Count++;
  246. }
  247. }while(1);
  248. Status = UnregisterTraceGuids( *(RegisterTraceGuid->RegistrationHandle));
  249. }
  250. }
  251. ULONG ProviderCallBack(WMIDPREQUESTCODE RequestCode,
  252. PVOID Context,
  253. ULONG *InOutBufferSize,
  254. PVOID Buffer)
  255. {
  256. switch (RequestCode )
  257. {
  258. case WMI_ENABLE_EVENTS:
  259. {
  260. TraceEventFlag = 1;
  261. LoggerHandle = GetTraceLoggerHandle( Buffer );
  262. EnableLevel = GetTraceEnableLevel(LoggerHandle);
  263. EnableFlags = GetTraceEnableFlags(LoggerHandle);
  264. break;
  265. }
  266. case WMI_DISABLE_EVENTS:
  267. {
  268. TraceEventFlag = 0;
  269. break;
  270. }
  271. }
  272. return 0;
  273. }
  274. void
  275. InitializeRegisterTraceGuid( PREGISTER_TRACE_GUID RegisterTraceGuid )
  276. {
  277. //Initialize with some random values and then read from the input
  278. //file. If any of these parameters needs to be tested with 0,
  279. //input files will set this as zero.
  280. if( RegisterTraceGuid )
  281. {
  282. RegisterTraceGuid->GuidCount = 0xa5;
  283. RegisterTraceGuid->CallBackFunction = (PVOID) 0xa5a5a5;
  284. RegisterTraceGuid->TraceGuidReg = (PTRACE_GUID_REGISTRATION) (0xa5a5a5);
  285. RegisterTraceGuid->RegistrationHandle = (PTRACEHANDLE) (0xa5a5a5);
  286. RegisterTraceGuid->UnRegistrationHandle = (PTRACEHANDLE) (0xa5a5a5);
  287. }
  288. }
  289. ULONG
  290. TraceEvents ( ULONG TraceMode, ULONG TraceGuid, ULONG Count)
  291. {
  292. ULONG Status;
  293. switch ( TraceMode )
  294. {
  295. case 0 :
  296. {
  297. if( RegisterTraceGuid->UseMofPtrFlag != 1)
  298. {
  299. EventBlock1.Header.Size = sizeof(EventBlock1);
  300. ULONG Temp = sizeof(Block1);
  301. EventBlock1.Header.Guid = TransactionGuid[TraceGuid];
  302. EventBlock1.Header.Flags = WNODE_FLAG_TRACED_GUID;
  303. if( RegisterTraceGuid->UseGuidPtrFlag == 1)
  304. {
  305. EventBlock1.Header.Flags |= WNODE_FLAG_USE_GUID_PTR;
  306. EventBlock1.Header.GuidPtr = (ULONGLONG) &TransactionGuid[TraceGuid];
  307. }
  308. EventBlock1.Block1.EventChar = (UCHAR) Count;
  309. EventBlock1.Block1.EventUlong = Count;
  310. Status = TraceEvent( LoggerHandle, (PEVENT_TRACE_HEADER) &EventBlock1);
  311. }
  312. else
  313. {
  314. EventBlock2.Header.Size = sizeof(EventBlock2);
  315. EventBlock2.Header.Flags = WNODE_FLAG_USE_MOF_PTR;
  316. EventBlock2.Header.Guid = TransactionGuid[TraceGuid];
  317. EventBlock2.Header.Flags |= WNODE_FLAG_TRACED_GUID;
  318. EventBlock2.MofField.DataPtr = (ULONGLONG) &Block1;
  319. EventBlock2.MofField.Length = sizeof(BLOCK1);
  320. if( RegisterTraceGuid->UseGuidPtrFlag == 1)
  321. {
  322. EventBlock2.Header.Flags |= WNODE_FLAG_USE_GUID_PTR;
  323. EventBlock2.Header.GuidPtr = (ULONGLONG) &TransactionGuid[TraceGuid];
  324. }
  325. Block1.EventChar = (UCHAR) Count;
  326. Block1.EventUlong = Count;
  327. Status = TraceEvent( LoggerHandle, (PEVENT_TRACE_HEADER) &EventBlock2);
  328. }
  329. break;
  330. }
  331. }
  332. return Status;
  333. }
  334. ULONG
  335. TraceEventInstances ( ULONG TraceMode, ULONG TraceGuid, ULONG Count)
  336. {
  337. ULONG Status;
  338. EVENT_INSTANCE_INFO InstanceInfo;
  339. switch ( TraceMode )
  340. {
  341. case 0 :
  342. {
  343. EventInstanceBlock1.Header.Size = sizeof(EventInstanceBlock1);
  344. EventInstanceBlock1.Header.Flags = WNODE_FLAG_TRACED_GUID;
  345. if( RegisterTraceGuid->UseMofPtrFlag == 1)
  346. EventInstanceBlock1.Header.Flags |= WNODE_FLAG_USE_MOF_PTR;
  347. EventInstanceBlock1.EventChar = (UCHAR) Count;
  348. EventInstanceBlock1.EventUlong = Count;
  349. Status = CreateTraceInstanceId( TraceGuidReg[TraceGuid].RegHandle,
  350. &InstanceInfo);
  351. if( Status == ERROR_SUCCESS )
  352. Status = TraceEventInstance( LoggerHandle, (PEVENT_INSTANCE_HEADER) &EventBlock1,
  353. &InstanceInfo,
  354. NULL);
  355. break;
  356. }
  357. }
  358. return Status;
  359. }
  360. /*
  361. int InitializeLogging( LPCTSTR lpctstrTCOFile, CLogger *&pLoggingInfo)
  362. {
  363. HRESULT hr = S_OK;
  364. // t_string tsError;
  365. if (lpctstrTCOFile)
  366. {
  367. // Open *our* logger file.
  368. pLoggingInfo = new CLogger(lpctstrTCOFile, false);
  369. hr = pLoggingInfo->GetOpenStatus();
  370. if (FAILED (hr))
  371. {
  372. // tsError = _T("Could not open detail log file \"");
  373. // tsError += lpctstrTCOFile;
  374. // tsError += _T("\".");
  375. //*plpstrReturnedError = NewTCHAR(tsError.c_str());
  376. return hr;
  377. }
  378. }
  379. return hr;
  380. }
  381. */