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.

828 lines
22 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. tracedp.c
  5. Abstract:
  6. Sample trace provider program.
  7. // end_sdk
  8. Author:
  9. Jee Fung Pang (jeepang) 03-Dec-1997
  10. Revision History:
  11. Insung Park (insungp) 18-Jan-2001
  12. Modified tracedp so that when tracedp generates User Mof Events
  13. with some sample strings, integers, floats, and arrays.
  14. // begin_sdk
  15. --*/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <windows.h>
  19. #include <shellapi.h>
  20. #include <tchar.h>
  21. #include <wmistr.h>
  22. #include <guiddef.h>
  23. #include <evntrace.h>
  24. #define MAXEVENTS 5000
  25. #define MAXSTR 1024
  26. #define MAXTHREADS 128
  27. // sample string data
  28. #define WIDE_DATA_STRING L"Sample Wide String"
  29. #define COUNTED_DATA_STRING L"Sample Counted String"
  30. TRACEHANDLE LoggerHandle;
  31. #define ResourceName _T("MofResource")
  32. TCHAR ImagePath[MAXSTR];
  33. GUID TransactionGuid =
  34. {0xce5b1020, 0x8ea9, 0x11d0, 0xa4, 0xec, 0x00, 0xa0, 0xc9, 0x06, 0x29, 0x10};
  35. GUID ControlGuid[2] =
  36. {
  37. {0xd58c126f, 0xb309, 0x11d1, 0x96, 0x9e, 0x00, 0x00, 0xf8, 0x75, 0xa5, 0xbc},
  38. {0x7c6a708a, 0xba1e, 0x11d2, 0x8b, 0xbf, 0x00, 0x00, 0xf8, 0x06, 0xef, 0xe0}
  39. };
  40. TRACE_GUID_REGISTRATION TraceGuidReg[] =
  41. {
  42. { (LPGUID)&TransactionGuid,
  43. NULL
  44. }
  45. };
  46. typedef enum {
  47. TYPE_USER_EVENT,
  48. TYPE_INSTANCE_EVENT,
  49. TYPE_MOF_EVENT,
  50. TYPEPTR_GUID
  51. } TypeEventType;
  52. typedef struct _USER_EVENT {
  53. EVENT_TRACE_HEADER Header;
  54. ULONG EventInfo;
  55. } USER_EVENT, *PUSER_EVENT;
  56. typedef struct _USER_INSTANCE_EVENT {
  57. EVENT_INSTANCE_HEADER Header;
  58. ULONG mofData;
  59. } USER_INSTANCE_EVENT, *PUSER_INSTANCE_EVENT;
  60. // customized event to use sample data that follow
  61. typedef struct _USER_MOF_EVENT {
  62. EVENT_TRACE_HEADER Header;
  63. MOF_FIELD mofData;
  64. } USER_MOF_EVENT, *PUSER_MOF_EVENT;
  65. // sample data structure
  66. typedef struct _INTEGER_SAMPLE_EVENT {
  67. CHAR sc;
  68. UCHAR uc;
  69. SHORT sh;
  70. ULONG ul;
  71. } INTEGER_SAMPLE_EVENT, *PINTEGER_SAMPLE_EVENT;
  72. typedef struct _FLOAT_SAMPLE_EVENT {
  73. float fl;
  74. double db;
  75. } FLOAT_SAMPLE_EVENT, *PFLOAT_SAMPLE_EVENT;
  76. typedef struct _ARRAY_SAMPLE_EVENT {
  77. CHAR ca[9];
  78. } ARRAY_SAMPLE_EVENT, *PARRAY_SAMPLE_EVENT;
  79. TypeEventType EventType = TYPE_USER_EVENT;
  80. TRACEHANDLE RegistrationHandle[2];
  81. BOOLEAN TraceOnFlag;
  82. ULONG EnableLevel = 0;
  83. ULONG EnableFlags = 0;
  84. BOOLEAN bPersistData = FALSE;
  85. ULONG nSleepTime = 0;
  86. ULONG EventCount = 0;
  87. BOOLEAN bInstanceTrace=0, bUseGuidPtr=0, bUseMofPtr=0;
  88. BOOLEAN bIncorrect = FALSE;
  89. BOOLEAN bUseNullPtr = FALSE;
  90. BOOLEAN bFirstTime = TRUE;
  91. ULONG InitializeTrace(
  92. void
  93. );
  94. ULONG
  95. ControlCallback(
  96. IN WMIDPREQUESTCODE RequestCode,
  97. IN PVOID Context,
  98. IN OUT ULONG *InOutBufferSize,
  99. IN OUT PVOID Buffer
  100. );
  101. LPTSTR
  102. DecodeStatus(
  103. IN ULONG Status,
  104. TCHAR *ErrorMsg,
  105. IN ULONG StringSize
  106. );
  107. void
  108. LogProc();
  109. ULONG
  110. ahextoi(
  111. IN TCHAR *s
  112. );
  113. void
  114. StringToGuid(
  115. TCHAR *str,
  116. LPGUID guid
  117. );
  118. ULONG MaxEvents = MAXEVENTS;
  119. ULONG gnMultiReg = 1;
  120. ULONG
  121. ahextoi(
  122. IN TCHAR *s
  123. )
  124. /*++
  125. Routine Description:
  126. Converts a hex string into a number.
  127. Arguments:
  128. s - A hex string in TCHAR.
  129. Return Value:
  130. ULONG - The number in the string.
  131. --*/
  132. {
  133. int len;
  134. ULONG num, base, hex;
  135. len = _tcslen(s);
  136. hex = 0; base = 1; num = 0;
  137. while (--len >= 0) {
  138. if ( (s[len] == 'x' || s[len] == 'X') &&
  139. (s[len-1] == '0') )
  140. break;
  141. if (s[len] >= '0' && s[len] <= '9')
  142. num = s[len] - '0';
  143. else if (s[len] >= 'a' && s[len] <= 'f')
  144. num = (s[len] - 'a') + 10;
  145. else if (s[len] >= 'A' && s[len] <= 'F')
  146. num = (s[len] - 'A') + 10;
  147. else
  148. continue;
  149. hex += num * base;
  150. base = base * 16;
  151. }
  152. return hex;
  153. }
  154. void StringToGuid(
  155. TCHAR *str,
  156. LPGUID guid
  157. )
  158. /*++
  159. Routine Description:
  160. Converts a String into a GUID.
  161. Arguments:
  162. str - String representing a GUID.
  163. guid - Pointer to a GUID for ourput
  164. Return Value:
  165. None.
  166. --*/
  167. {
  168. TCHAR temp[10];
  169. int i, n;
  170. _tcsncpy(temp, str, 8);
  171. temp[8] = 0;
  172. guid->Data1 = ahextoi(temp);
  173. _tcsncpy(temp, &str[9], 4);
  174. temp[4] = 0;
  175. guid->Data2 = (USHORT) ahextoi(temp);
  176. _tcsncpy(temp, &str[14], 4);
  177. temp[4] = 0;
  178. guid->Data3 = (USHORT) ahextoi(temp);
  179. for (i=0; i<2; i++) {
  180. _tcsncpy(temp, &str[19 + (i*2)], 2);
  181. temp[2] = 0;
  182. guid->Data4[i] = (UCHAR) ahextoi(temp);
  183. }
  184. for (i=2; i<8; i++) {
  185. _tcsncpy(temp, &str[20 + (i*2)], 2);
  186. temp[2] = 0;
  187. guid->Data4[i] = (UCHAR) ahextoi(temp);
  188. }
  189. }
  190. __cdecl main(argc, argv)
  191. int argc;
  192. char **argv;
  193. /*++
  194. Routine Description:
  195. main() routine.
  196. Arguments:
  197. Usage: TraceDp [options] [number of events]
  198. -UseEventTraceHeader this is default.
  199. -UseEventInstanceHeader
  200. -UseMofPtrFlag
  201. -Thread [n] Sets the number of event-generating threads.
  202. -GuidPtr Use GUID pointer instead of GUID itself.
  203. -MofPtr Use MOF pointer for additional data.
  204. -GuidPtrMofPtr User GUID pointer and MOF pointer.
  205. -InCorrectMofPtr Use incorrect MOF pointer (Creates an error case).
  206. -NullMofPtr Use NULL MOF pointer (Creates an error case).
  207. -MultiReg Register multiple event GUIDS.
  208. -Sleep [n] Sets the sleep time before unregistering.
  209. [number of events] default is 5000
  210. Return Value:
  211. Error Code defined in winerror.h : If the function succeeds,
  212. it returns ERROR_SUCCESS (== 0).
  213. --*/
  214. {
  215. ULONG Status;
  216. LPGUID Guid = NULL;
  217. DWORD ThreadId;
  218. HANDLE hThreadVector[MAXTHREADS];
  219. ULONG i;
  220. ULONG nThreads = 1;
  221. LPTSTR *targv, *utargv = NULL;
  222. MaxEvents = MAXEVENTS;
  223. TraceOnFlag = FALSE;
  224. #ifdef UNICODE
  225. if ((targv = CommandLineToArgvW(
  226. GetCommandLineW(), // pointer to a command-line string
  227. &argc // receives the argument count
  228. )) == NULL)
  229. {
  230. return(GetLastError());
  231. };
  232. utargv = targv;
  233. #else
  234. targv = argv;
  235. #endif
  236. // process command line arguments to override defaults
  237. //
  238. while (--argc > 0)
  239. {
  240. targv ++;
  241. if (**targv == '-' || **targv == '/')
  242. {
  243. if(targv[0][0] == '/' ) targv[0][0] = '-';
  244. if (!_tcsicmp(targv[0],_T("-UseEventTraceHeader")))
  245. {
  246. EventType = TYPE_USER_EVENT;
  247. }
  248. else if (!_tcsicmp(targv[0],_T("-UseEventInstanceHeader")))
  249. {
  250. EventType = TYPE_INSTANCE_EVENT;
  251. }
  252. else if (!_tcsicmp(targv[0],_T("-UseMofPtrFlag")))
  253. {
  254. EventType = TYPE_MOF_EVENT;
  255. }
  256. // end_sdk
  257. else if (!_tcsicmp(targv[0],_T("-Persist")))
  258. {
  259. bPersistData = TRUE;
  260. }
  261. // begin_sdk
  262. else if (!_tcsicmp(targv[0],_T("-Thread")))
  263. {
  264. if (argc > 1) {
  265. targv++; --argc;
  266. nThreads = _ttoi(targv[0]);
  267. if (nThreads > MAXTHREADS)
  268. nThreads = MAXTHREADS;
  269. }
  270. }
  271. else if (!_tcsicmp(targv[0],_T("-GuidPtr")))
  272. {
  273. bUseGuidPtr = TRUE;
  274. }
  275. else if (!_tcsicmp(targv[0],_T("-MofPtr")))
  276. {
  277. bUseMofPtr = TRUE;
  278. }
  279. else if (!_tcsicmp(targv[0],_T("-GuidPtrMofPtr")))
  280. {
  281. bUseGuidPtr = TRUE;
  282. bUseMofPtr = TRUE;
  283. }
  284. else if (!_tcsicmp(targv[0],_T("-InCorrectMofPtr")))
  285. {
  286. bUseMofPtr = TRUE;
  287. bIncorrect = TRUE;
  288. }
  289. else if (!_tcsicmp(targv[0],_T("-NullMofPtr")))
  290. {
  291. bUseMofPtr = TRUE;
  292. bUseNullPtr = TRUE;
  293. bIncorrect = TRUE;;
  294. }
  295. else if (!_tcsicmp(targv[0],_T("-MultiReg")))
  296. {
  297. gnMultiReg = 2;
  298. }
  299. else if (!_tcsicmp(targv[0], _T("-Guid"))) {
  300. if (argc > 1) {
  301. if (targv[1][0] == _T('#')) {
  302. StringToGuid(&targv[1][1], &ControlGuid[0]);
  303. ++targv; --argc;
  304. }
  305. }
  306. }
  307. else if (!_tcsicmp(targv[0],_T("-Sleep")))
  308. {
  309. if (argc > 1) {
  310. targv++; --argc;
  311. nSleepTime = _ttoi(targv[0]);
  312. }
  313. }
  314. else
  315. {
  316. printf("Usage: TraceDp [options] [number of events]\n");
  317. printf("\t-UseEventTraceHeader this is default.\n");
  318. printf("\t-UseEventInstanceHeader\n");
  319. printf("\t-UseMofPtrFlag\n");
  320. printf("\t-Thread [n]\n");
  321. printf("\t-GuidPtr\n");
  322. printf("\t-MofPtr\n");
  323. printf("\t-GuidPtrMofPtr\n");
  324. printf("\t-InCorrectMofPtr\n");
  325. printf("\t-NullMofPtr\n");
  326. printf("\t-MultiReg\n");
  327. printf("\t-Guid #[guid] alternative control GUID\n");
  328. printf("\t-Sleep [n]\n");
  329. printf("\t[number of events] default is 5000\n");
  330. return 0;
  331. }
  332. }
  333. else if (** targv >= '0' && ** targv <= '9')
  334. {
  335. MaxEvents = _ttoi(targv[0]);
  336. }
  337. }
  338. if (utargv != NULL) {
  339. GlobalFree(utargv);
  340. }
  341. Status = InitializeTrace();
  342. if (Status != ERROR_SUCCESS) {
  343. return Status;
  344. }
  345. _tprintf(_T("Testing Logger with %d events (%d,%d)\n"),
  346. MaxEvents, EventType, bPersistData);
  347. while (! TraceOnFlag)
  348. _sleep(1000);
  349. for (i=0; i < nThreads; i++) {
  350. hThreadVector[i] = CreateThread(NULL,
  351. 0,
  352. (LPTHREAD_START_ROUTINE) LogProc,
  353. NULL,
  354. 0,
  355. (LPDWORD)&ThreadId);
  356. }
  357. WaitForMultipleObjects(nThreads, hThreadVector, TRUE, INFINITE);
  358. if (nSleepTime > 0) {
  359. _sleep(nSleepTime * 1000);
  360. }
  361. for (i=0; i<gnMultiReg; i++) {
  362. UnregisterTraceGuids(RegistrationHandle[i]);
  363. }
  364. return ERROR_SUCCESS;
  365. }
  366. LPTSTR
  367. DecodeStatus(
  368. IN ULONG Status,
  369. IN OUT TCHAR *ErrorMsg,
  370. IN ULONG StringSize
  371. )
  372. /*++
  373. Routine Description:
  374. Decodes error status.
  375. Arguments:
  376. Status - Return status of function calls to be decoded.
  377. Return Value:
  378. Pointer to a decoded error message.
  379. --*/
  380. {
  381. RtlZeroMemory(ErrorMsg, (StringSize * sizeof(TCHAR)));
  382. FormatMessage(
  383. FORMAT_MESSAGE_FROM_SYSTEM |
  384. FORMAT_MESSAGE_IGNORE_INSERTS,
  385. NULL,
  386. Status,
  387. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  388. (LPTSTR) ErrorMsg,
  389. StringSize,
  390. NULL );
  391. return ErrorMsg;
  392. }
  393. ULONG InitializeTrace(
  394. void
  395. )
  396. /*++
  397. Routine Description:
  398. Register traces.
  399. Arguments:
  400. Return Value:
  401. Error Status. ERROR_SUCCESS if successful.
  402. --*/
  403. {
  404. ULONG Status;
  405. ULONG i, j;
  406. if (!GetModuleFileName(NULL, ImagePath, MAXSTR)) {
  407. return (ERROR_FILE_NOT_FOUND);
  408. }
  409. for (i=0; i<gnMultiReg; i++) {
  410. Status = RegisterTraceGuids(
  411. (WMIDPREQUEST)ControlCallback, //use same callback function
  412. (PVOID)(INT_PTR)(0x12345678+i), // RequestContext
  413. (LPCGUID)&ControlGuid[i],
  414. 1,
  415. &TraceGuidReg[i],
  416. (LPCTSTR)&ImagePath[0],
  417. (LPCTSTR)ResourceName,
  418. &RegistrationHandle[i]
  419. );
  420. if (Status != ERROR_SUCCESS) {
  421. TCHAR ErrorMsg[MAXSTR];
  422. _tprintf(_T("Trace registration failed\n"));
  423. if( i > 0) {
  424. for (j=0; j<i; j++) {
  425. UnregisterTraceGuids(RegistrationHandle[j]);
  426. }
  427. }
  428. _tprintf(_T("InitializeTrace failed. i=%d, status=%d, %s\n"), i, Status, DecodeStatus(Status, ErrorMsg, MAXSTR));
  429. return(Status);
  430. }
  431. else {
  432. _tprintf(_T("Trace registered successfully\n"));
  433. }
  434. }
  435. return(Status);
  436. }
  437. ULONG
  438. ControlCallback(
  439. IN WMIDPREQUESTCODE RequestCode,
  440. IN PVOID Context,
  441. IN OUT ULONG *InOutBufferSize,
  442. IN OUT PVOID Buffer
  443. )
  444. /*++
  445. Routine Description:
  446. Callback function when enabled.
  447. Arguments:
  448. RequestCode - Flag for either enable or disable.
  449. Context - User-defined context.
  450. InOutBufferSize - not used.
  451. Buffer - WNODE_HEADER for the logger session.
  452. Return Value:
  453. Error Status. ERROR_SUCCESS if successful.
  454. --*/
  455. {
  456. ULONG Status;
  457. ULONG RetSize;
  458. Status = ERROR_SUCCESS;
  459. switch (RequestCode)
  460. {
  461. case WMI_ENABLE_EVENTS:
  462. {
  463. RetSize = 0;
  464. LoggerHandle = GetTraceLoggerHandle( Buffer );
  465. EnableLevel = GetTraceEnableLevel(LoggerHandle);
  466. EnableFlags = GetTraceEnableFlags(LoggerHandle);
  467. _tprintf(_T("Logging enabled to 0x%016I64x(%d,%d,%d)\n"),
  468. LoggerHandle, RequestCode, EnableLevel, EnableFlags);
  469. TraceOnFlag = TRUE;
  470. break;
  471. }
  472. case WMI_DISABLE_EVENTS:
  473. {
  474. TraceOnFlag = FALSE;
  475. RetSize = 0;
  476. LoggerHandle = 0;
  477. _tprintf(_T("\nLogging Disabled\n"));
  478. break;
  479. }
  480. default:
  481. {
  482. RetSize = 0;
  483. Status = ERROR_INVALID_PARAMETER;
  484. break;
  485. }
  486. }
  487. *InOutBufferSize = RetSize;
  488. return(Status);
  489. }
  490. void
  491. LogProc()
  492. /*++
  493. Routine Description:
  494. Generates events. It is spawned as separate threads.
  495. Based on the options given by users, it generates different events.
  496. Arguments:
  497. Return Value:
  498. None.
  499. --*/
  500. {
  501. USER_EVENT UserEvent;
  502. USER_INSTANCE_EVENT UserInstanceEvent;
  503. USER_MOF_EVENT UserMofEvent;
  504. EVENT_INSTANCE_INFO InstInfo;
  505. PMOF_FIELD mofField;
  506. ULONG i;
  507. PWNODE_HEADER Wnode;
  508. ULONG status;
  509. ULONG InstanceId;
  510. LPGUID Guid = NULL;
  511. ULONG nTemp;
  512. USHORT nSize, nStrEventSize;
  513. WCHAR wstrTemp[MAXSTR];
  514. INTEGER_SAMPLE_EVENT ise;
  515. FLOAT_SAMPLE_EVENT fse;
  516. ARRAY_SAMPLE_EVENT ase;
  517. CHAR *sse, *ptr;
  518. // some arbitrary data for MOF structs
  519. ise.sc = 'x';
  520. ise.uc = 'y';
  521. ise.sh = (SHORT)rand();
  522. ise.ul = (ULONG)rand();
  523. nTemp = 0;
  524. while (nTemp == 0) {
  525. nTemp = rand();
  526. }
  527. fse.fl = ((float)rand() / (float)nTemp);
  528. fse.db = ((double)rand() / (double)nTemp);
  529. ase.ca[0] = 'M';
  530. ase.ca[1] = 'i';
  531. ase.ca[2] = 'c';
  532. ase.ca[3] = 'r';
  533. ase.ca[4] = 'o';
  534. ase.ca[5] = 's';
  535. ase.ca[6] = 'o';
  536. ase.ca[7] = 'f';
  537. ase.ca[8] = 't';
  538. nStrEventSize = ((wcslen(WIDE_DATA_STRING) + 1) * sizeof(WCHAR)) + sizeof(SHORT) + (wcslen(COUNTED_DATA_STRING) * sizeof(WCHAR));
  539. sse = (PCHAR) malloc(nStrEventSize);
  540. if (NULL != sse) {
  541. ptr = sse;
  542. wcscpy(wstrTemp, WIDE_DATA_STRING);
  543. wstrTemp[wcslen(WIDE_DATA_STRING)] = L'\0';
  544. memcpy(ptr, wstrTemp, (wcslen(WIDE_DATA_STRING) + 1) * sizeof(WCHAR));
  545. ptr += (wcslen(WIDE_DATA_STRING) + 1) * sizeof(WCHAR);
  546. nSize = (USHORT)(wcslen(COUNTED_DATA_STRING) * sizeof(WCHAR));
  547. memcpy(ptr, &nSize, sizeof(USHORT));
  548. ptr += sizeof(USHORT);
  549. wcscpy(wstrTemp, COUNTED_DATA_STRING);
  550. memcpy(ptr, wstrTemp, wcslen(COUNTED_DATA_STRING) * sizeof(WCHAR));
  551. }
  552. RtlZeroMemory(&UserEvent, sizeof(UserEvent));
  553. Wnode = (PWNODE_HEADER) &UserEvent;
  554. UserEvent.Header.Size = sizeof(USER_EVENT);
  555. UserEvent.Header.Flags = WNODE_FLAG_TRACED_GUID;
  556. UserEvent.Header.Guid = TransactionGuid;
  557. if (bPersistData)
  558. UserEvent.Header.Flags |= WNODE_FLAG_PERSIST_EVENT;
  559. RtlZeroMemory(&UserInstanceEvent, sizeof(UserInstanceEvent));
  560. UserInstanceEvent.Header.Size = sizeof(UserInstanceEvent);
  561. UserInstanceEvent.Header.Flags = WNODE_FLAG_TRACED_GUID;
  562. if (bPersistData)
  563. UserInstanceEvent.Header.Flags |= WNODE_FLAG_PERSIST_EVENT;
  564. RtlZeroMemory(&UserMofEvent, sizeof(UserMofEvent));
  565. Wnode = (PWNODE_HEADER) &UserMofEvent;
  566. UserMofEvent.Header.Size = sizeof(UserMofEvent);
  567. UserMofEvent.Header.Flags = WNODE_FLAG_TRACED_GUID;
  568. UserMofEvent.Header.Guid = TransactionGuid;
  569. // end_sdk
  570. if (bPersistData)
  571. UserMofEvent.Header.Flags |= WNODE_FLAG_PERSIST_EVENT;
  572. // begin_sdk
  573. if (bUseGuidPtr) {
  574. UserEvent.Header.Flags |= WNODE_FLAG_USE_GUID_PTR;
  575. UserEvent.Header.GuidPtr = (ULONGLONG)&TransactionGuid;
  576. UserMofEvent.Header.Flags |= WNODE_FLAG_USE_GUID_PTR;
  577. UserMofEvent.Header.GuidPtr = (ULONGLONG)&TransactionGuid;
  578. }
  579. i = 0;
  580. while (TraceOnFlag) {
  581. if ((i % 4) == 0) {
  582. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  583. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  584. UserMofEvent.Header.Class.Type = 3;
  585. }
  586. else if ((i % 4) == 1) {
  587. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  588. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  589. UserMofEvent.Header.Class.Type = 4;
  590. }
  591. else if ((i % 4) == 2) {
  592. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  593. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_START;
  594. UserMofEvent.Header.Class.Type = 5;
  595. }
  596. else {
  597. UserEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  598. UserInstanceEvent.Header.Class.Type = EVENT_TRACE_TYPE_END;
  599. UserMofEvent.Header.Class.Type = 6;
  600. }
  601. switch (EventType)
  602. {
  603. case TYPE_INSTANCE_EVENT:
  604. if (UserInstanceEvent.Header.Class.Type == EVENT_TRACE_TYPE_START) {
  605. status = CreateTraceInstanceId(
  606. (PVOID) TraceGuidReg[0].RegHandle,
  607. & InstInfo);
  608. if (status != ERROR_SUCCESS) {
  609. TCHAR ErrorMsg[MAXSTR];
  610. fprintf(stderr,
  611. "CreatTraceInstanceId() status=%d, %ws\n",
  612. status, DecodeStatus(status, ErrorMsg, MAXSTR)
  613. );
  614. return;
  615. }
  616. }
  617. status = TraceEventInstance(
  618. LoggerHandle,
  619. (PEVENT_INSTANCE_HEADER) & UserInstanceEvent,
  620. & InstInfo,
  621. NULL);
  622. break;
  623. case TYPE_USER_EVENT:
  624. UserEvent.EventInfo = InterlockedIncrement(&EventCount);
  625. status = TraceEvent(
  626. LoggerHandle,
  627. (PEVENT_TRACE_HEADER) & UserEvent);
  628. break;
  629. case TYPE_MOF_EVENT:
  630. UserMofEvent.Header.Flags |= WNODE_FLAG_USE_MOF_PTR;
  631. mofField = (PMOF_FIELD) & UserMofEvent.mofData;
  632. if (UserMofEvent.Header.Class.Type == 4) {
  633. mofField->DataPtr = (ULONGLONG) (&ise);
  634. mofField->Length = sizeof(INTEGER_SAMPLE_EVENT);
  635. }
  636. else if (UserMofEvent.Header.Class.Type == 5) {
  637. mofField->DataPtr = (ULONGLONG) (&fse);
  638. mofField->Length = sizeof(FLOAT_SAMPLE_EVENT);
  639. }
  640. else if (UserMofEvent.Header.Class.Type == 6) {
  641. mofField->DataPtr = (ULONGLONG) (&ase);
  642. mofField->Length = sizeof(ARRAY_SAMPLE_EVENT);
  643. }
  644. else {
  645. mofField->DataPtr = (ULONGLONG) (sse);
  646. mofField->Length = nStrEventSize;
  647. }
  648. if (bUseNullPtr)
  649. mofField->DataPtr = (ULONGLONG) (NULL);
  650. if (bIncorrect)
  651. mofField->Length += 1000;
  652. status = TraceEvent(
  653. LoggerHandle,
  654. (PEVENT_TRACE_HEADER) & UserMofEvent);
  655. if (status != ERROR_SUCCESS) {
  656. fprintf(stderr, "Error(%d) while writing event.\n", status);
  657. }
  658. break;
  659. default:
  660. status = ERROR_SUCCESS;
  661. break;
  662. }
  663. // logger buffers out of memory should not prevent provider from
  664. // generating events. This will only cause events lost.
  665. //
  666. if (status == ERROR_NOT_ENOUGH_MEMORY) {
  667. status = ERROR_SUCCESS;
  668. }
  669. if (status != ERROR_SUCCESS) {
  670. TCHAR ErrorMsg[MAXSTR];
  671. _ftprintf(stderr, _T("\nError %s while writing event\n"),
  672. DecodeStatus(status, ErrorMsg, MAXSTR));
  673. _ftprintf( stderr, _T("Logging terminated due to error\n"));
  674. free(sse);
  675. return;
  676. }
  677. i++;
  678. if (i >= MaxEvents)
  679. break;
  680. if (!(i % 100))
  681. _tprintf(_T("."));
  682. _sleep(1);
  683. }
  684. free(sse);
  685. }