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.

428 lines
12 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wchar.h>
  4. #include <ole2.h>
  5. #include <wmistr.h>
  6. #include <evntrace.h>
  7. #include <time.h>
  8. #include <tchar.h>
  9. #define DEBUG
  10. #define MAXEVENTS 5000
  11. #define MAXSTR 1024
  12. TRACEHANDLE LoggerHandle;
  13. #define ResourceName _T("MofResource")
  14. TCHAR ImagePath[MAXSTR];
  15. GUID TransactionGuid[2] =
  16. {
  17. {0xce5b1020, 0x8ea9, 0x11d0, 0xa4, 0xec, 0x00, 0xa0, 0xc9, 0x06, 0x29, 0x10},
  18. {0xf684e86f, 0xba1d, 0x11d2, 0x8b, 0xbf, 0x00, 0x00, 0xf8, 0x06, 0xef, 0xe0}
  19. };
  20. GUID ControlGuid[2] =
  21. {
  22. {0xd58c126f, 0xb309, 0x11d1, 0x96, 0x9e, 0x00, 0x00, 0xf8, 0x75, 0xa5, 0xbc},
  23. {0x7c6a708a, 0xba1e, 0x11d2, 0x8b, 0xbf, 0x00, 0x00, 0xf8, 0x06, 0xef, 0xe0}
  24. };
  25. TRACE_GUID_REGISTRATION TraceGuidReg[2] =
  26. {
  27. { (LPGUID)&TransactionGuid[0],
  28. NULL
  29. },
  30. { (LPGUID)&TransactionGuid[1],
  31. NULL
  32. }
  33. };
  34. typedef struct _USER_EVENT {
  35. EVENT_TRACE_HEADER Header;
  36. MOF_FIELD mofData;
  37. } USER_EVENT, *PUSER_EVENT;
  38. typedef struct _USER_INSTANCE_EVENT {
  39. EVENT_INSTANCE_HEADER Header;
  40. MOF_FIELD mofData;
  41. } USER_INSTANCE_EVENT, *PUSER_INSTANCE_EVENT;
  42. TRACEHANDLE RegistrationHandle[2];
  43. BOOLEAN RegistrationSuccess;
  44. ULONG EnableLevel = 0;
  45. ULONG EnableFlags = 0;
  46. ULONG InitializeTrace(
  47. IN LPTSTR ExePath, FILE* fp
  48. );
  49. ULONG
  50. ControlCallback(
  51. IN WMIDPREQUESTCODE RequestCode,
  52. IN PVOID Context,
  53. IN OUT ULONG *InOutBufferSize,
  54. IN OUT PVOID Buffer
  55. );
  56. GUID StringToGuid(TCHAR *str);
  57. LPTSTR Decodestatus(IN ULONG Status);
  58. HANDLE ghTraceOnEvent;
  59. ULONG TraceOnFlag;
  60. UINT nSleepTime = 0;
  61. TCHAR ErrorMsg[MAXSTR];
  62. ULONG gnMultiReg=1;
  63. int __cdecl _tmain(int argc, _TCHAR **argv)
  64. {
  65. ULONG status;
  66. USER_EVENT UserEvent;
  67. USER_INSTANCE_EVENT UserInstanceEvent;
  68. EVENT_INSTANCE_INFO InstInfo;
  69. ULONG i;
  70. ULONG MaxEvents;
  71. //ULONG InstanceId;
  72. PWNODE_HEADER Wnode;
  73. TCHAR *str;
  74. int err;
  75. BOOL bInstanceTrace=0, bUseGuidPtr=0, bUseMofPtr=0;
  76. BOOL bIncorrect = FALSE;
  77. BOOL bUseNullPtr = FALSE;
  78. PMOF_FIELD mofField;
  79. TCHAR strMofData[MAXSTR];
  80. FILE *fp;
  81. fp = _tfopen(_T("provider.log"), _T("a+")); if(fp==NULL) {_tprintf(_T("pf=NULL\n"));};
  82. MaxEvents = MAXEVENTS;
  83. TraceOnFlag = 0;
  84. if (argc > 1)
  85. MaxEvents = _ttoi(argv[1]);
  86. if(argc > 2)
  87. ControlGuid[0] = StringToGuid(argv[2]);
  88. if(argc > 3)
  89. nSleepTime = _ttoi(argv[3]);
  90. err = UuidToString(&ControlGuid[0], &str);
  91. if(RPC_S_OK == err)
  92. _tprintf(_T("The ControlGuid is : %s\n"), str);
  93. else
  94. _tprintf(_T("Error(%d) converting uuid\n"), err);
  95. _ftprintf(fp, _T("The ControlGuid is : %s\n"), str);
  96. if(argc > 4) {
  97. if(!_tcscmp(_T("TraceInstance"), argv[4]))
  98. bInstanceTrace = TRUE;
  99. }
  100. if(argc > 5) {
  101. if(!_tcscmp(_T("GuidPtr"), argv[5]))
  102. bUseGuidPtr = TRUE;
  103. else if(!_tcscmp(_T("MofPtr"), argv[5]))
  104. bUseMofPtr = TRUE;
  105. else if(!_tcscmp(_T("GuidPtrMofPtr"), argv[5])) {
  106. bUseGuidPtr = TRUE;
  107. bUseMofPtr = TRUE;
  108. }
  109. else if (!_tcscmp(_T("InCorrectMofPtr"), argv[5])) {
  110. bUseMofPtr = TRUE;
  111. bIncorrect = TRUE;
  112. }
  113. else if (!_tcscmp(_T("NullMofPtr"), argv[5])) {
  114. bUseMofPtr = TRUE;
  115. bUseNullPtr = TRUE;
  116. bIncorrect = TRUE;
  117. }
  118. }
  119. if(argc > 6) {
  120. if(!_tcscmp(_T("MultiReg"), argv[6]))
  121. gnMultiReg=2; //use 2 registrations for now
  122. }
  123. status = InitializeTrace(_T("tracedp.exe"), fp);
  124. if (status != ERROR_SUCCESS) {
  125. _ftprintf(fp, _T("InitializeTrace failed, status=%d, %s\n"), status, Decodestatus(status));
  126. return 0;
  127. }
  128. _tprintf(_T("Testing Logger with %d events\n"), MaxEvents);
  129. RtlZeroMemory(&UserEvent, sizeof(UserEvent));
  130. Wnode = (PWNODE_HEADER) &UserEvent;
  131. UserEvent.Header.Size = sizeof(USER_EVENT);
  132. UserEvent.Header.Flags = WNODE_FLAG_TRACED_GUID;
  133. if(bUseGuidPtr) {
  134. _tprintf(_T("\n********Use Guid Pointer**********\n"));
  135. UserEvent.Header.Flags |= WNODE_FLAG_USE_GUID_PTR;
  136. UserEvent.Header.GuidPtr = (ULONGLONG)&TransactionGuid[0];
  137. }
  138. else
  139. UserEvent.Header.Guid = TransactionGuid[0];
  140. RtlZeroMemory(&UserInstanceEvent, sizeof(UserInstanceEvent));
  141. UserInstanceEvent.Header.Size = sizeof(USER_INSTANCE_EVENT);
  142. UserInstanceEvent.Header.Flags = WNODE_FLAG_TRACED_GUID;
  143. if(bUseMofPtr) {
  144. _tprintf(_T("\n=======Use Mof Pointer========\n"));
  145. _tcscpy(strMofData, str);
  146. UserEvent.Header.Flags |= WNODE_FLAG_USE_MOF_PTR;
  147. mofField = (PMOF_FIELD) & UserEvent.mofData;
  148. if (bUseNullPtr)
  149. mofField->DataPtr = (ULONGLONG) (NULL);
  150. else
  151. mofField->DataPtr = (ULONGLONG) (strMofData);
  152. if (bIncorrect)
  153. mofField->Length = sizeof(TCHAR) * (_tcslen(strMofData) + 1000);
  154. else
  155. mofField->Length = sizeof(TCHAR) * (_tcslen(strMofData) + 1);
  156. UserInstanceEvent.Header.Flags |= WNODE_FLAG_USE_MOF_PTR;
  157. mofField = (PMOF_FIELD) & UserInstanceEvent.mofData;
  158. if (bUseNullPtr)
  159. mofField->DataPtr = (ULONGLONG) (NULL);
  160. else
  161. mofField->DataPtr = (ULONGLONG) (strMofData);
  162. if (bIncorrect)
  163. mofField->Length = sizeof(TCHAR) * (_tcslen(strMofData) + 10000);
  164. else
  165. mofField->Length = sizeof(TCHAR) * (_tcslen(strMofData) + 1);
  166. }
  167. if(bInstanceTrace) {
  168. status = CreateTraceInstanceId((PVOID)TraceGuidReg[0].RegHandle, &InstInfo);
  169. _tprintf(_T("\n-------TraceEventInstance-----\n"));
  170. if (status != ERROR_SUCCESS) {
  171. _ftprintf(fp, _T("CreatTraceInstanceId() failed. status=%d, %s\n"), status, Decodestatus(status));
  172. }
  173. }
  174. _ftprintf(fp, _T("%d Events, %s, %s, %s, %s, sleep time=%d\n"), MaxEvents,
  175. bInstanceTrace? _T("TraceEventInstance"): _T("TraceEvent"),
  176. bUseGuidPtr? _T("Use GuidPtr"): _T("Use Guid"),
  177. bUseMofPtr? _T("Use MofPtr"): _T("Not use MofPtr"),
  178. gnMultiReg==1 ? _T("Single Registration"): _T("Multiple Registrations"),
  179. nSleepTime);
  180. i = 0;
  181. while (1) {
  182. if(WAIT_FAILED == WaitForSingleObject(ghTraceOnEvent, INFINITE))
  183. {
  184. _tprintf(_T("Error(%d) waiting for ghTraceOnEvent object\n"), GetLastError());
  185. }
  186. if (TraceOnFlag == 1 && i < MaxEvents) {
  187. i++;
  188. if (i == ((i/2) * 2) ) {
  189. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  190. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  191. }
  192. else {
  193. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  194. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  195. }
  196. if(bInstanceTrace) {
  197. status = TraceEventInstance(LoggerHandle, (PEVENT_INSTANCE_HEADER)&UserInstanceEvent, &InstInfo, NULL);
  198. if (status != ERROR_SUCCESS) {
  199. _tprintf(_T("%d TraceEventInstance() failed, status=%d %s\n"), i, status, Decodestatus(status));
  200. _ftprintf(fp, _T("\ni=%d TraceEventInstance() failed, break! status=%d %s\n"), i, status, Decodestatus(status));
  201. return 0;
  202. }
  203. }
  204. else {
  205. status = TraceEvent(LoggerHandle, (PEVENT_TRACE_HEADER) &UserEvent);
  206. if (status != ERROR_SUCCESS) {
  207. fprintf(stderr, "Error(%d) while writing event.\n", status);
  208. _ftprintf(fp, _T("\ni=%d TraceEvent() failed, break!\nstatus=%d %s\n"), i, status, Decodestatus(status));
  209. return 0;
  210. }
  211. }
  212. if (i >= MaxEvents) {
  213. _ftprintf(fp, _T("\ni=%d MaxEvents=%d break!\n\n"), i, MaxEvents);
  214. }
  215. else if (!(i % 100)) {
  216. _ftprintf(fp, _T("."));
  217. _tprintf(_T("."));
  218. if(nSleepTime)
  219. _sleep(nSleepTime);
  220. }
  221. }
  222. if (TraceOnFlag == 2) {
  223. _ftprintf(fp, _T("\ni=%d TraceOnFlag == 2 break!\n\n"), i);
  224. break;
  225. }
  226. }
  227. fclose(fp);
  228. CloseHandle(ghTraceOnEvent);
  229. for(i=0; i< gnMultiReg; i++)
  230. UnregisterTraceGuids(RegistrationHandle[i]);
  231. return (0);
  232. }
  233. ULONG InitializeTrace(
  234. IN LPTSTR ExePath, FILE* fp
  235. )
  236. {
  237. ULONG Status;
  238. ULONG i, j;
  239. Status = GetModuleFileName(NULL, &ImagePath[0], MAXSTR*sizeof(TCHAR));
  240. if (Status == 0) {
  241. return (ERROR_FILE_NOT_FOUND);
  242. }
  243. ghTraceOnEvent = CreateEvent(
  244. NULL, // security attributes
  245. TRUE, // manual reset
  246. FALSE, // initial state
  247. NULL // pointer to event-object
  248. );
  249. if(NULL == ghTraceOnEvent)
  250. {
  251. Status=GetLastError();
  252. _tprintf(_T("Error(%d) creating TraceOnEvent\n"), Status);
  253. return Status;
  254. }
  255. for (i=0; i<gnMultiReg; i++) {
  256. Status = RegisterTraceGuids(
  257. (WMIDPREQUEST)ControlCallback, //use same callback function
  258. (PVOID)(INT_PTR)(0x12345678+i), // RequestContext
  259. (LPCGUID)&ControlGuid[i],
  260. 1,
  261. &TraceGuidReg[i],
  262. (LPCTSTR)&ImagePath[0],
  263. (LPCTSTR)ResourceName,
  264. &RegistrationHandle[i]);
  265. if (Status != ERROR_SUCCESS) {
  266. _tprintf(_T("Trace registration failed\n"));
  267. RegistrationSuccess = FALSE;
  268. if(i>0)
  269. for (j=0; j<i; j++)
  270. UnregisterTraceGuids(RegistrationHandle[i]);
  271. _ftprintf(fp, _T("InitializeTrace failed. i=%d, status=%d, %s\n"), i, Status, Decodestatus(Status));
  272. return(Status);
  273. }
  274. else {
  275. _tprintf(_T("Trace registered successfully\n"));
  276. RegistrationSuccess = TRUE;
  277. }
  278. }
  279. return(Status);
  280. }
  281. ULONG
  282. ControlCallback(
  283. IN WMIDPREQUESTCODE RequestCode,
  284. IN PVOID Context,
  285. IN OUT ULONG *InOutBufferSize,
  286. IN OUT PVOID Buffer
  287. )
  288. {
  289. ULONG Status;
  290. ULONG RetSize;
  291. Status = ERROR_SUCCESS;
  292. switch (RequestCode)
  293. {
  294. case WMI_ENABLE_EVENTS:
  295. {
  296. RetSize = 0;
  297. LoggerHandle = GetTraceLoggerHandle( Buffer );
  298. EnableLevel = GetTraceEnableLevel(LoggerHandle);
  299. EnableFlags = GetTraceEnableFlags(LoggerHandle);
  300. _tprintf(_T("Logging enabled to %I64u\n"), LoggerHandle);
  301. TraceOnFlag = 1;
  302. SetEvent(ghTraceOnEvent);
  303. break;
  304. }
  305. case WMI_DISABLE_EVENTS:
  306. {
  307. TraceOnFlag = 2;
  308. RetSize = 0;
  309. LoggerHandle = 0;
  310. _tprintf(_T("\nLogging Disabled\n"));
  311. SetEvent(ghTraceOnEvent);
  312. break;
  313. }
  314. default:
  315. {
  316. RetSize = 0;
  317. Status = ERROR_INVALID_PARAMETER;
  318. break;
  319. }
  320. }
  321. *InOutBufferSize = RetSize;
  322. return(Status);
  323. }
  324. GUID StringToGuid(TCHAR *str)
  325. {
  326. GUID guid;
  327. TCHAR temp[10];
  328. int i, n;
  329. _tprintf(_T("sizeof GUID = %d GUID=%s\n"), sizeof(guid), str);
  330. temp[8]=_T('\0');
  331. _tcsncpy(temp, str, 8);
  332. _stscanf(temp, _T("%x"), &(guid.Data1));
  333. temp[4]=_T('\0');
  334. _tcsncpy(temp, &str[9], 4);
  335. _stscanf(temp, _T("%x"), &(guid.Data2));
  336. _tcsncpy(temp, &str[14], 4);
  337. _stscanf(temp, _T("%x"), &(guid.Data3));
  338. temp[2]=_T('\0');
  339. for(i=0;i<8;i++)
  340. {
  341. temp[0]=str[19+((i<2)?2*i:2*i+1)]; // to accomodate the minus sign after
  342. temp[1]=str[20+((i<2)?2*i:2*i+1)]; // the first two chars
  343. _stscanf(temp, _T("%x"), &n); // if directly used more than byte alloc
  344. guid.Data4[i]=(unsigned char)n; // causes overrun of memory
  345. }
  346. return guid;
  347. }
  348. LPTSTR
  349. Decodestatus(
  350. IN ULONG Status
  351. )
  352. {
  353. memset( ErrorMsg, 0, MAXSTR );
  354. FormatMessage(
  355. FORMAT_MESSAGE_FROM_SYSTEM |
  356. FORMAT_MESSAGE_IGNORE_INSERTS,
  357. NULL,
  358. Status,
  359. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  360. (LPTSTR) ErrorMsg,
  361. MAXSTR,
  362. NULL );
  363. return ErrorMsg;
  364. }