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.

3581 lines
139 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. traceprt.c
  5. Abstract:
  6. Trace formatting library. Converts binary trace file to CSV format,
  7. and other formattted string formats.
  8. Author:
  9. Jee Fung Pang (jeepang) 03-Dec-1997
  10. Revision History:
  11. GorN: 10/09/2000: ItemHRESULT added
  12. --*/
  13. #ifdef __cplusplus
  14. extern "C"{
  15. #endif
  16. #define UNICODE
  17. #define _UNICODE
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <nt.h>
  21. #include <ntrtl.h>
  22. #include <nturtl.h>
  23. #include <windows.h>
  24. #include <shellapi.h>
  25. #include <tchar.h>
  26. #include <wmistr.h>
  27. #include <initguid.h>
  28. #include <evntrace.h>
  29. #include <ntwmi.h>
  30. #include <netevent.h>
  31. #include <netevent.dbg>
  32. #include <winerror.dbg>
  33. #pragma warning( disable : 4005) // Disable warning message 4005
  34. #include <ntstatus.h>
  35. #include <ntstatus.dbg>
  36. #pragma warning( default : 4005) // Enable warning message 4005
  37. #define TRACE_EXPORTS 1
  38. #include "traceint.h"
  39. #undef TRACE_API
  40. #define TRACE_API
  41. #include "traceprt.h"
  42. #include <sddl.h> // for ConvertStringSidToSid //
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. #ifdef MANAGED_TRACING
  47. //For importing COM types from C# decoder
  48. #pragma warning (disable: 4278)
  49. //#import "mscorlib.tlb" raw_interfaces_only
  50. #import "traceevent.tlb" no_namespace named_guids //this file is obtained by executing "tlbexp traceevent.dll".
  51. #endif
  52. #include <set>
  53. // %1 GUID Friendly Name string
  54. // %2 GUID SubType Name string
  55. // %3 Thread ID ULONG_PTR
  56. // %4 System Time String
  57. // %5 Kernel Time or User Time String
  58. // %6 User Time or NULL String
  59. // %7 Sequence Number LONG
  60. // %8 Unused String
  61. // %9 CPU Number LONG
  62. // %128 Indent String
  63. ULONG TimerResolution = 10;
  64. __int64 ElapseTime;
  65. #define MAXBUFS 16384
  66. #define MAXCHARS MAXBUFS/sizeof(TCHAR)
  67. #define MAXBUFS2 2 * MAXBUFS
  68. #define MAXCHARS2 MAXBUFS2/sizeof(TCHAR)
  69. #define MAXITEMS 256
  70. #define MAXINDENT 256
  71. #define MAXNAMEARG 256
  72. #define MAXHEXDUMP 512
  73. TCHAR ItemBuf[MAXBUFS]; // Temporary String Item buffer
  74. TCHAR ItemBBuf[MAXBUFS2]; // Item String Buffer
  75. TCHAR * ItemBBufEnd ;
  76. TCHAR * pItemBuf[MAXITEMS]; // Pointer to items in the String Item Buffer.
  77. BYTE ItemRBuf[MAXBUFS]; // Item Raw Byte Buffer
  78. ULONG_PTR * pItemRBuf[MAXITEMS]; // Pointer to Raw Byte Items
  79. SIZE_T ItemRSize; // Size of Item in Raw Buffer.
  80. BOOL bItemIsString = FALSE ; // And type of item
  81. LONG ItemsInRBuf = 0;
  82. ULONG PointerSize = sizeof(PVOID) ;
  83. BYTE Event[4096];
  84. HANDLE hLibrary ;
  85. TCHAR StdPrefix[MAXSTR];
  86. TCHAR IndentBuf[MAXINDENT + 1];
  87. CHAR StrA[MAXBUFS]; //tempory Ansi String Buffer
  88. WCHAR StrW[MAXBUFS]; //tempory Unicode String Buffer
  89. TCHAR IList[MAXBUFS]; //tempory List for List Long
  90. LPTSTR gTraceFormatSearchPath = NULL;
  91. #define TRACE_FORMAT_SEARCH_PATH L"TRACE_FORMAT_SEARCH_PATH"
  92. #define TRACE_FORMAT_PREFIX L"TRACE_FORMAT_PREFIX"
  93. //#define STD_PREFIX L"[%!CPU!]%!PID!.%!TID!::%!NOW! [%!FILE!] "
  94. #define STD_PREFIX_NOSEQ L"[%9!d!]%8!04X!.%3!04X!::%4!s! [%1!s!]"
  95. #define STD_PREFIX_SEQ L"[%9!d!]%8!04X!.%3!04X!::%4!s! %7!08x! [%1!s!]"
  96. static TCHAR *STD_PREFIX = STD_PREFIX_NOSEQ;
  97. static BOOL bGMT = FALSE;
  98. static BOOL bSequence = FALSE;
  99. static BOOL bIndent = FALSE;
  100. static BOOL bUsePrefix = TRUE ;
  101. static BOOL bStructuredFormat = FALSE ;
  102. DWORD fIPV6 = 0 ; //Determins if RtlIpv6AddressToString supported (1 if yes, 2 if no, 0 not checked yet)
  103. typedef LPWSTR (*RTLIPV6ADDRESSTOSTRING)(const in6_addr * IP6Addr, LPWSTR S);
  104. RTLIPV6ADDRESSTOSTRING pRtlIpv6AddressToString = NULL ;
  105. const TCHAR *pNoValueString = _T("<NoValue>");
  106. #ifdef MANAGED_TRACING
  107. ITraceMessageDecoder *pCSharpDecoder = NULL; //pointer to CSharp decoder
  108. TCHAR* ptCSFormattedMessage = NULL; //Will point to the formatted message returned by CSharp message decoder
  109. #endif
  110. DWORD
  111. LoadGuidFile(OUT PLIST_ENTRY *HeadEventList,
  112. IN LPGUID pGuid
  113. );
  114. struct mofinfo_less {
  115. bool operator() (const PMOF_INFO& a, const PMOF_INFO &b) const
  116. {
  117. //
  118. // Two MOF_INFO instances are considered equivalent if they
  119. // have the same Guid and TypeIndex.
  120. // If Guids match, strDescription is copied.
  121. //
  122. int result = memcmp(&a->Guid, &b->Guid, sizeof(b->Guid));
  123. if (result == 0) {
  124. if ((b->strDescription == NULL ) &&
  125. (a->strDescription != NULL )) {
  126. b->strDescription = a->strDescription;
  127. } else {
  128. if ((a->strDescription == NULL ) &&
  129. (b->strDescription != NULL )) {
  130. a->strDescription = b->strDescription;
  131. }
  132. }
  133. return a->TypeIndex < b->TypeIndex;
  134. }
  135. return result < 0;
  136. }
  137. };
  138. typedef std::set<PMOF_INFO, mofinfo_less> MOF_SET;
  139. MOF_SET *pFmtInfoSet = 0;
  140. DWORD InsertFmtInfoSet(PMOF_INFO pMofInfo)
  141. /*++
  142. Routine Description:
  143. Insert pMofInfo into pFmtInfoSet.
  144. Arguments:
  145. Return Value:
  146. ERROR_SUCCESS if succeeded.
  147. ERROR_OUTOFMEMORY if out of memory.
  148. Notes:
  149. --*/
  150. {
  151. if (pFmtInfoSet == 0) {
  152. pFmtInfoSet = new MOF_SET();
  153. if (pFmtInfoSet == NULL) {
  154. return(ERROR_OUTOFMEMORY);
  155. }
  156. }
  157. pFmtInfoSet->insert(pMofInfo);
  158. return ERROR_SUCCESS;
  159. } // InsertFmtInfoSet()
  160. void ReplaceStringUnsafe(TCHAR* buf, TCHAR* find, TCHAR* replace)
  161. {
  162. TCHAR source[MAXSTR], *src, *dst;
  163. int nf = _tcslen(find);
  164. int nr = _tcslen(replace);
  165. src = source;
  166. dst = buf;
  167. _tcsncpy(source, buf, MAXSTR );
  168. for (;;) {
  169. TCHAR* p = src;
  170. for (;;) {
  171. p = _tcsstr(p, find);
  172. if (!p) goto exit_outer_loop;
  173. // special kludge not to replace
  174. // %!Someting! when it is actually %%!Something!
  175. if (p == source || p[0] != '%' || p[-1] != '%') {
  176. break;
  177. }
  178. p += nf;
  179. }
  180. memcpy(dst, src, (p-src) * sizeof(TCHAR) );
  181. dst += p-src;
  182. src = p + nf;
  183. _tcsncpy(dst, replace,(MAXSTR - (p-source)));
  184. dst += nr;
  185. }
  186. exit_outer_loop:
  187. _tcscpy(dst, src);
  188. }
  189. TCHAR* FindValue(TCHAR* buf, TCHAR* ValueName)
  190. {
  191. static TCHAR valueBuf[256]; // largest identifier in PDB
  192. TCHAR *p = _tcsstr(buf, ValueName);
  193. TCHAR *q = p;
  194. if (p) {
  195. p += _tcslen(ValueName);
  196. q = p;
  197. while ( *p && !_istspace(*p) ) ++p;
  198. memcpy(valueBuf, q, (p-q) * sizeof(TCHAR) );
  199. }
  200. valueBuf[p-q] = 0;
  201. return valueBuf;
  202. }
  203. int FindIntValue(TCHAR* buf, TCHAR* ValueName)
  204. {
  205. TCHAR* v = FindValue(buf, ValueName), *end;
  206. int sgn = 1;
  207. if (v[0] == '+') {
  208. ++v;
  209. } else if (v[0] == '-') {
  210. sgn = -1;
  211. ++v;
  212. }
  213. return sgn * _tcstol(v, &end, 10);
  214. }
  215. PMOF_INFO
  216. GetMofInfoHead(
  217. OUT PLIST_ENTRY * EventListhead,
  218. IN LPGUID pGuid,
  219. IN LPTSTR strType,
  220. IN LONG TypeIndex,
  221. IN ULONG TypeOfType,
  222. IN LPTSTR TypeFormat,
  223. IN BOOL bBestMatch
  224. );
  225. void RemoveMofInfo(PLIST_ENTRY pMofInfo);
  226. ULONG ahextoi(TCHAR *s);
  227. PTCHAR GuidToString(PTCHAR s, LPGUID piid);
  228. ULONG
  229. WINAPI
  230. GetTraceGuidsW(
  231. TCHAR * GuidFile,
  232. PLIST_ENTRY * HeadEventList
  233. );
  234. VOID
  235. EtwpConvertHex4(OUT WCHAR *buffer,
  236. IN DWORD n,
  237. IN DWORD count
  238. )
  239. /*++
  240. Routine Description:
  241. Implementing _snwprintf(buffer,
  242. count,
  243. L"0x%04X",
  244. n);
  245. Arguments:
  246. Return Value:
  247. None.
  248. Notes:
  249. --*/
  250. {
  251. const WCHAR hexdigit[] = L"0123456789ABCDEF";
  252. if (count <= 4) {
  253. return;
  254. }
  255. buffer[0] = hexdigit[(n / 16 / 16 / 16) % 16];
  256. buffer[1] = hexdigit[(n / 16 / 16) % 16];
  257. buffer[2] = hexdigit[(n / 16) % 16];
  258. buffer[3] = hexdigit[n % 16];
  259. buffer[4] = 0;
  260. return;
  261. } // EtwpConvertHex4()
  262. VOID
  263. EtwpConvertUnsignedInt(OUT WCHAR *buffer,
  264. IN DWORD n,
  265. IN DWORD count
  266. )
  267. /*++
  268. Routine Description:
  269. Implementing _snwprintf(buffer,
  270. count,
  271. L"%u",
  272. n);
  273. Arguments:
  274. Return Value:
  275. None.
  276. Notes:
  277. --*/
  278. {
  279. WCHAR buffer2[64];
  280. WCHAR* p = buffer2 + 64;
  281. DWORD NumDigits = 1;
  282. *--p = 0;
  283. do {
  284. *--p = (WCHAR)(n % 10 + '0');
  285. n /= 10;
  286. NumDigits ++;
  287. }
  288. while ((n > 0) && (NumDigits < 32));
  289. if (count < NumDigits) {
  290. return;
  291. }
  292. memcpy(buffer, p, NumDigits*sizeof(WCHAR));
  293. return;
  294. } // EtwpConvertUnsignedInt()
  295. VOID
  296. EtwpConvertUnsignedLong(OUT WCHAR *buffer,
  297. IN DWORD n,
  298. IN DWORD count
  299. )
  300. /*++
  301. Routine Description:
  302. Implementing _snwprintf(buffer,
  303. count,
  304. L"%8lu",
  305. n);
  306. Arguments:
  307. Return Value:
  308. None.
  309. Notes:
  310. --*/
  311. {
  312. if (count <= 8) {
  313. return;
  314. }
  315. buffer[8] = 0;
  316. buffer[7] = (n % 10) + '0';
  317. buffer[6] = (n / 10) % 10 + '0';
  318. buffer[5] = (n / 10 / 10) % 10 + '0';
  319. buffer[4] = (n / 10 / 10 / 10) % 10 + '0';
  320. buffer[3] = (n / 10 / 10 / 10 / 10) % 10 + '0';
  321. buffer[2] = (n / 10 / 10 / 10 / 10 / 10) % 10 + '0';
  322. buffer[1] = (n / 10 / 10 / 10 / 10 / 10 / 10) % 10 + '0';
  323. buffer[0] = (n / 10 / 10 / 10 / 10 / 10 / 10 / 10) % 10 + '0';
  324. return;
  325. } // EtwpConvertUnsignedLong
  326. VOID
  327. EtwpConvertTimeStamp(OUT WCHAR * buffer,
  328. IN SYSTEMTIME sysTime,
  329. IN DWORD count
  330. )
  331. /*++
  332. Routine Description:
  333. Implementing _snwprintf(buffer,
  334. count,
  335. L"%02d/%02d/%04d-%02d:%02d:%02d.%03d",
  336. sysTime.wMonth,
  337. sysTime.wDay,
  338. sysTime.wYear,
  339. sysTime.wHour,
  340. sysTime.wMinute,
  341. sysTime.wSecond,
  342. sysTime.wMilliseconds
  343. );
  344. Arguments:
  345. Return Value:
  346. None.
  347. Notes:
  348. --*/
  349. {
  350. if (count <= 23) { // 23 = wcslen(L"%02d/%02d/%04d-%02d:%02d:%02d.%03d")
  351. return;
  352. }
  353. memcpy(buffer,
  354. L"00/00/2000-00:00:00.000",
  355. 48 // 48 = (wcslen(L"00/00/2000-00:00:00.000")+1)*sizeof(WCHAR)
  356. );
  357. buffer[0] = (WCHAR)(sysTime.wMonth / 10 + '0');
  358. buffer[1] = (WCHAR)(sysTime.wMonth % 10 + '0');
  359. buffer[3] = (WCHAR)(sysTime.wDay / 10 + '0');
  360. buffer[4] = (WCHAR)(sysTime.wDay % 10 + '0');
  361. buffer[8] = (WCHAR)(sysTime.wYear % 100 / 10 + '0');
  362. buffer[9] = (WCHAR)(sysTime.wYear % 10 + '0');
  363. buffer[11] = (WCHAR)(sysTime.wHour / 10 + '0');
  364. buffer[12] = (WCHAR)(sysTime.wHour % 10 + '0');
  365. buffer[14] = (WCHAR)(sysTime.wMinute / 10 + '0');
  366. buffer[15] = (WCHAR)(sysTime.wMinute % 10 + '0');
  367. buffer[17] = (WCHAR)(sysTime.wSecond / 10 + '0');
  368. buffer[18] = (WCHAR)(sysTime.wSecond % 10 + '0');
  369. buffer[20] = (WCHAR)(sysTime.wMilliseconds / 100 + '0');
  370. buffer[21] = (WCHAR)((sysTime.wMilliseconds / 10) % 10 + '0');
  371. buffer[22] = (WCHAR)(sysTime.wMilliseconds % 10 + '0');
  372. buffer[23] = 0;
  373. } // EtwpConvertTimeStamp()
  374. static
  375. void
  376. reduce(
  377. PCHAR Src
  378. )
  379. {
  380. char *Start = Src;
  381. if (!Src) {
  382. return;
  383. }
  384. while (*Src) {
  385. if ('\t' == *Src)
  386. *Src = ' ';
  387. else if (',' == *Src)
  388. *Src = ' ';
  389. else if ('\n' == *Src)
  390. *Src = ',';
  391. else if ('\r' == *Src)
  392. *Src = ' ';
  393. ++Src;
  394. }
  395. --Src;
  396. while ((Start < Src) && ((' ' == *Src) || (',' == *Src))) {
  397. *Src = 0x00;
  398. --Src;
  399. }
  400. }
  401. static void reduceW(WCHAR *Src)
  402. {
  403. WCHAR *Start = Src;
  404. if (!Src) {
  405. return;
  406. }
  407. while (*Src) {
  408. if (L'\t' == *Src) {
  409. *Src = L' ';
  410. } else if (L',' == *Src) {
  411. *Src = L' ';
  412. } else if (L'\n' == *Src) {
  413. *Src = L',';
  414. } else if (L'\r' == *Src) {
  415. *Src = L' ';
  416. }
  417. ++Src;
  418. }
  419. --Src;
  420. while ((Start < Src) && ((L' ' == *Src) || (L',' == *Src))) {
  421. *Src = 0x00;
  422. --Src;
  423. }
  424. }
  425. #ifdef UNICODE
  426. #define _stnprintf _snwprintf
  427. #else
  428. #define _stnprintf _snprintf
  429. #endif
  430. int FormatTimeDelta(TCHAR *buffer, size_t count, LONGLONG time)
  431. {
  432. SYSTEMTIME st;
  433. int s = 0, result;
  434. ZeroMemory(&st, sizeof(st) );
  435. if (count == 0) {
  436. return -1;
  437. }
  438. if (time < 0) {
  439. *buffer++ = '-';
  440. --count;
  441. time = -time;
  442. s = 1;
  443. }
  444. // Get rid of the nano and micro seconds
  445. time /= 10000;
  446. st.wMilliseconds = (USHORT)(time % 1000);
  447. time /= 1000;
  448. if (time == 0) {
  449. result = _stnprintf(buffer,count,L"%dms",st.wMilliseconds);
  450. goto end;
  451. }
  452. st.wSecond = (USHORT)(time % 60);
  453. time /= 60;
  454. st.wMinute = (USHORT)(time % 60);
  455. time /= 60;
  456. if (time == 0) {
  457. if (st.wMinute <= 10) {
  458. result = _stnprintf(buffer,count,L"%d.%03ds",st.wMinute * 60 + st.wSecond, st.wMilliseconds);
  459. } else {
  460. result = _stnprintf(buffer,count,L"%d:%d.%03ds",st.wMinute, st.wSecond, st.wMilliseconds);
  461. }
  462. goto end;
  463. }
  464. st.wHour = (USHORT)(time % 24);
  465. time /= 24;
  466. if (time == 0) {
  467. result = _stnprintf(buffer,count,L"%d:%d:%d.%03ds",st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  468. goto end;
  469. }
  470. st.wDay = (USHORT)time;
  471. result = _stnprintf(buffer,count,L"%d~%d:%d:%d.%03ds",st.wDay,st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  472. end:
  473. if (result >= 0) {
  474. result += s;
  475. }
  476. return result;
  477. }
  478. typedef struct _ERROR_MAP {
  479. NTSTATUS MessageId;
  480. char *SymbolicName;
  481. } ERROR_MAP;
  482. PMOF_INFO
  483. EtwpFindMsgFmt(const GUID& guid,
  484. int MessageNo,
  485. LPTSTR wstr
  486. )
  487. /*++
  488. Routine Description:
  489. Find message format given guid and MessageNo. If only
  490. guid is matched, strDescription is copied to wstr.
  491. Arguments:
  492. Return Value:
  493. Matched format.
  494. Notes:
  495. --*/
  496. {
  497. MOF_INFO Key;
  498. DWORD Status;
  499. if (pFmtInfoSet == NULL) {
  500. return NULL;
  501. }
  502. memcpy(&Key.Guid, &guid, sizeof(guid));
  503. Key.TypeIndex = MessageNo;
  504. Key.strDescription = NULL;
  505. MOF_SET::const_iterator iter;
  506. iter = pFmtInfoSet->find(&Key);
  507. if (iter == pFmtInfoSet->end()) {
  508. if (Key.strDescription != NULL) {
  509. // Unknown MessageNo
  510. _tcscpy(wstr, Key.strDescription);
  511. } else {
  512. // Unknown Guid.
  513. }
  514. return NULL;
  515. }
  516. if (Key.strDescription != NULL) {
  517. _tcscpy(wstr, (*iter)->strDescription);
  518. }
  519. return *iter;
  520. } // EtwpFindMsgFmt()
  521. #ifdef MANAGED_TRACING
  522. HRESULT InitializeCSharpDecoder(){
  523. // Initialize COM and create an instance of the InterfaceImplementation class:
  524. CoInitialize(NULL);
  525. HRESULT hr = CoCreateInstance(CLSID_TraceProvider,
  526. NULL, CLSCTX_ALL,
  527. IID_ITraceMessageDecoder, reinterpret_cast<void**>(&pCSharpDecoder));
  528. if (FAILED(hr)) {
  529. pCSharpDecoder = NULL;
  530. CoUninitialize();
  531. }
  532. return hr;
  533. }
  534. #endif
  535. #ifdef MANAGED_TRACING
  536. void UninitializeCSharpDecoder(){
  537. if (NULL != pCSharpDecoder) {
  538. pCSharpDecoder->Release();
  539. CoUninitialize();
  540. }
  541. }
  542. #endif
  543. SIZE_T
  544. WINAPI
  545. FormatTraceEventW(
  546. PLIST_ENTRY HeadEventList,
  547. PEVENT_TRACE pInEvent,
  548. TCHAR *EventBuf,
  549. ULONG SizeEventBuf,
  550. TCHAR * pszMask
  551. )
  552. {
  553. PEVENT_TRACE_HEADER pHeader;
  554. PEVENT_TRACE pEvent = NULL;
  555. ULONG TraceMarker, TraceType;
  556. TCHAR tstrName[MAXSTR];
  557. TCHAR tstrType[MAXSTR];
  558. ULONG tstrTypeOfType = 0;
  559. TCHAR * tstrFormat;
  560. int iItemCount;
  561. ULONG_PTR MessageSequence = -1 ;
  562. USHORT MessageNumber = 0 ;
  563. USHORT MessageFlags = 0 ;
  564. char * pMessageData;
  565. ULONG MessageLength ;
  566. PMOF_INFO pMofInfo = NULL;
  567. DWORD TempLen;
  568. char *pCSMessageData ;
  569. BOOL bCSharpEvent = 0;
  570. const TCHAR tstrCSharpMsgGuid[]=_T("b4955bf0-3af1-4740-b475-99055d3fe9aa");
  571. TCHAR tstrFailureInfo[128];
  572. BOOL bManagedTracingEnabled = FALSE;
  573. #ifdef MANAGED_TRACING
  574. static BOOL bFirstTime=TRUE;
  575. HRESULT hr;
  576. #endif
  577. PSTRUCTUREDMESSAGE pStructuredMessage = (PSTRUCTUREDMESSAGE)EventBuf;
  578. ULONG_PTR pStructuredOffset = (ULONG_PTR)pStructuredMessage + sizeof(STRUCTUREDMESSAGE);
  579. RtlZeroMemory(EventBuf, SizeEventBuf); // just in case the caller was untidy
  580. #ifdef MANAGED_TRACING
  581. bManagedTracingEnabled = TRUE;
  582. #endif
  583. ItemBBufEnd = ItemBBuf + MAXBUFS2 -1; //initialise pointer to end of buffer
  584. if (pInEvent == NULL) {
  585. return(0);
  586. }
  587. pEvent = pInEvent ;
  588. // Make a copy of the PTR and length as we may adjust these depending
  589. // on the header
  590. pMessageData = (char *) pEvent->MofData ;
  591. MessageLength = pEvent->MofLength ;
  592. pHeader = (PEVENT_TRACE_HEADER) &pEvent->Header;
  593. TCHAR mguid[100];
  594. //#ifdef MANAGED_TRACING
  595. if (_tcscmp(GuidToString(mguid,&pEvent->Header.Guid),tstrCSharpMsgGuid) == 0) {
  596. bCSharpEvent = 1;
  597. pCSMessageData = pMessageData;
  598. }
  599. //#endif
  600. TraceMarker = ((PSYSTEM_TRACE_HEADER)pInEvent)->Marker;
  601. if ((TraceMarker & TRACE_MESSAGE)== TRACE_MESSAGE ) {
  602. // This handles the TRACE_MESSAGE type.
  603. TraceType = TRACE_HEADER_TYPE_MESSAGE ; // This one has special processing
  604. //
  605. // Now Process the header options
  606. //
  607. MessageNumber = ((PMESSAGE_TRACE_HEADER)pEvent)->Packet.MessageNumber ; // Message Number
  608. MessageFlags = ((PMESSAGE_TRACE_HEADER)pEvent)->Packet.OptionFlags ;
  609. // Note that the order in which these are added is critical New entries must
  610. // be added at the end!
  611. //
  612. // [First Entry] Sequence Number
  613. if (MessageFlags&TRACE_MESSAGE_SEQUENCE) {
  614. RtlCopyMemory(&MessageSequence, pMessageData, sizeof(ULONG)) ;
  615. pMessageData += sizeof(ULONG) ;
  616. MessageLength -= sizeof(ULONG);
  617. }
  618. // [Second Entry] GUID ? or CompnentID ?
  619. if (MessageFlags&TRACE_MESSAGE_COMPONENTID) {
  620. RtlCopyMemory(&pEvent->Header.Guid,pMessageData,sizeof(ULONG)) ;
  621. pMessageData += sizeof(ULONG) ;
  622. MessageLength -= sizeof(ULONG) ;
  623. } else if (MessageFlags&TRACE_MESSAGE_GUID) { // Can't have both
  624. RtlCopyMemory(&pEvent->Header.Guid,pMessageData, sizeof(GUID));
  625. pMessageData += sizeof(GUID) ;
  626. MessageLength -= sizeof(GUID);
  627. }
  628. // [Third Entry] Timestamp?
  629. // After a certain point the OS moved this timestamp into place for us
  630. // And canonicalised it, fortunately prior to that it was always zero
  631. if (MessageFlags&TRACE_MESSAGE_TIMESTAMP) {
  632. if ((pEvent->Header.TimeStamp.HighPart == 0) && (pEvent->Header.TimeStamp.LowPart == 0))
  633. {
  634. RtlCopyMemory(&pEvent->Header.TimeStamp.LowPart, pMessageData, sizeof(ULONG));
  635. pMessageData += sizeof(ULONG);
  636. RtlCopyMemory(&pEvent->Header.TimeStamp.HighPart, pMessageData, sizeof(ULONG));
  637. pMessageData += sizeof(ULONG);
  638. } else {
  639. pMessageData += sizeof(LARGE_INTEGER);
  640. }
  641. MessageLength -= sizeof(LARGE_INTEGER);
  642. }
  643. // [Fourth Entry] System Information?
  644. if (MessageFlags&TRACE_MESSAGE_SYSTEMINFO) {
  645. pHeader = (PEVENT_TRACE_HEADER) &pEvent->Header;
  646. RtlCopyMemory(&pHeader->ThreadId, pMessageData, sizeof(ULONG)) ;
  647. pMessageData += sizeof(ULONG);
  648. MessageLength -=sizeof(ULONG);
  649. RtlCopyMemory(&pHeader->ProcessId,pMessageData, sizeof(ULONG)) ;
  650. pMessageData += sizeof(ULONG);
  651. MessageLength -=sizeof(ULONG);
  652. }
  653. //
  654. // Add New Header Entries immediately before this comment!
  655. //
  656. } else {
  657. // Must be WNODE_HEADER
  658. //
  659. TraceType = 0;
  660. pEvent = pInEvent ;
  661. MessageNumber = pEvent->Header.Class.Type ;
  662. if (MessageNumber == 0xFF) { // W2K Compatability escape code
  663. if (pEvent->MofLength >= sizeof(USHORT)) {
  664. // The real Message Number is in the first USHORT
  665. memcpy(&MessageNumber,pEvent->MofData,sizeof(USHORT)) ;
  666. pMessageData += sizeof(USHORT);
  667. MessageLength -= sizeof(USHORT);
  668. }
  669. }
  670. }
  671. // Reset the Pointer and length if they have been adjusted
  672. pEvent->MofData = pMessageData ;
  673. pEvent->MofLength = MessageLength ;
  674. pHeader = (PEVENT_TRACE_HEADER) &pEvent->Header;
  675. if ( IsEqualGUID(&pEvent->Header.Guid, &EventTraceGuid)
  676. && pEvent->Header.Class.Type == EVENT_TRACE_TYPE_INFO) {
  677. PTRACE_LOGFILE_HEADER head = (PTRACE_LOGFILE_HEADER)pEvent->MofData;
  678. if (head->TimerResolution > 0) {
  679. TimerResolution = head->TimerResolution / 10000;
  680. }
  681. ElapseTime = head->EndTime.QuadPart -
  682. pEvent->Header.TimeStamp.QuadPart;
  683. PointerSize = head->PointerSize;
  684. if (PointerSize < 2 ) // minimum is 16 bits
  685. PointerSize = 4 ; // defaults = 32 bits
  686. }
  687. for (int attempt = 1; attempt <= 2; attempt++) {
  688. DWORD Status;
  689. tstrName[0] = L'\0';
  690. pMofInfo = EtwpFindMsgFmt((pEvent->Header.Guid),
  691. MessageNumber,
  692. tstrName
  693. );
  694. if ((pMofInfo != NULL) || (tstrName[0] != L'\0')) {
  695. break;
  696. }
  697. if (attempt == 2) {
  698. _tcscpy(tstrName, GUID_TYPE_UNKNOWN);
  699. break;
  700. }
  701. Status = LoadGuidFile(&HeadEventList,
  702. &(pEvent->Header.Guid)
  703. );
  704. switch (Status) {
  705. case ERROR_BAD_FORMAT:
  706. _tcscpy(tstrName, GUID_TYPE_OBSOLETE);
  707. break;
  708. case ERROR_PATH_NOT_FOUND:
  709. _tcscpy(tstrName, GUID_TYPE_UNKNOWN);
  710. break;
  711. default:
  712. break;
  713. }
  714. if ((Status == ERROR_BAD_FORMAT) ||
  715. (Status == ERROR_PATH_NOT_FOUND)
  716. ) {
  717. break;
  718. }
  719. } // for
  720. if (IsEqualGUID(&(pEvent->Header.Guid), &EventTraceGuid)) {
  721. _tcscpy(tstrName, GUID_TYPE_HEADER);
  722. }
  723. if (pEvent != NULL) {
  724. PITEM_DESC pItem;
  725. PCHAR ptr = NULL;
  726. PCHAR iMofPtr = NULL;
  727. ULONG ulongword;
  728. PLIST_ENTRY Head, Next;
  729. int i;
  730. if ((pMofInfo != NULL) && (pMofInfo->strType != NULL)) {
  731. _sntprintf(tstrType,MAXSTR,_T("%s"),pMofInfo->strType);
  732. tstrFormat = pMofInfo->TypeFormat; // Pointer to the format string
  733. tstrTypeOfType = pMofInfo->TypeOfType; // And the type of Format
  734. } else {
  735. _sntprintf(tstrType,MAXSTR,_T("%3d"),MessageNumber);
  736. tstrFormat = NULL ;
  737. tstrTypeOfType = 0 ;
  738. }
  739. // From here on we start processing the parameters, we actually do
  740. // two versions. One is built for original #type format statements,
  741. // and everything is converted to being an ASCII string.
  742. // The other is built for #type2 format statements and everything is
  743. // converted into a string of raw bytes aliggned on a 64-bit boundary.
  744. iItemCount = 0 ; // How many Items we process
  745. pItemBuf[iItemCount] = ItemBBuf; // Where they go (Strings)
  746. // Make Parameter %1 Type Name
  747. TempLen = _tcslen(tstrName);
  748. memcpy(pItemBuf[iItemCount],
  749. tstrName,
  750. min(TempLen + 1, ItemBBufEnd - pItemBuf[iItemCount]) * sizeof(WCHAR)
  751. );
  752. pItemBuf[iItemCount + 1] = pItemBuf[iItemCount] + TempLen + 1;
  753. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same for Raw bytes
  754. iItemCount ++;
  755. if (bStructuredFormat) {
  756. pStructuredMessage->TraceGuid = EventTraceGuid ;
  757. RtlCopyMemory((PVOID)pStructuredOffset,tstrName, min(_tcslen(tstrName) + 1, NAMESIZE) * sizeof(WCHAR));
  758. pStructuredMessage->GuidName = pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  759. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  760. }
  761. // Make Parameter %2 Type sub Type
  762. TempLen = _tcslen(tstrType);
  763. memcpy(pItemBuf[iItemCount],
  764. tstrType,
  765. min(TempLen + 1, ItemBBufEnd - pItemBuf[iItemCount]) * sizeof(WCHAR)
  766. );
  767. pItemBuf[iItemCount + 1] = pItemBuf[iItemCount] + TempLen + 1;
  768. pItemRBuf[iItemCount] = (ULONG_PTR *)pItemBuf[iItemCount]; // just use the same for raw bytes
  769. iItemCount ++;
  770. if (bStructuredFormat) {
  771. RtlCopyMemory((PVOID)pStructuredOffset,tstrType,min(_tcslen(tstrType) + 1, NAMESIZE) * sizeof(WCHAR));
  772. pStructuredMessage->GuidTypeName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  773. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  774. }
  775. // Make Parameter %3 ThreadId
  776. RtlCopyMemory(&pItemRBuf[iItemCount] , &pHeader->ThreadId, sizeof(ULONG)) ;
  777. EtwpConvertHex4(pItemBuf[iItemCount],
  778. pHeader->ThreadId,
  779. ItemBBufEnd - pItemBuf[iItemCount]
  780. );
  781. TempLen = _tcslen(pItemBuf[iItemCount]);
  782. pItemBuf[iItemCount + 1] = pItemBuf[iItemCount] + TempLen + 1;
  783. iItemCount++;
  784. if (bStructuredFormat) {
  785. RtlCopyMemory(&pStructuredMessage->ThreadId,&pHeader->ThreadId,sizeof(ULONG));
  786. }
  787. // Make Parameter %4 System Time
  788. if (tstrFormat != NULL) {
  789. FILETIME stdTime, localTime;
  790. SYSTEMTIME sysTime;
  791. stdTime.dwHighDateTime = pEvent->Header.TimeStamp.HighPart;
  792. stdTime.dwLowDateTime = pEvent->Header.TimeStamp.LowPart;
  793. if (bGMT) {
  794. FileTimeToSystemTime(&stdTime, &sysTime);
  795. } else {
  796. FileTimeToLocalFileTime(&stdTime, &localTime);
  797. FileTimeToSystemTime(&localTime, &sysTime);
  798. }
  799. if (bStructuredFormat) {
  800. RtlCopyMemory(&pStructuredMessage->SystemTime,&sysTime,sizeof(SYSTEMTIME));
  801. }
  802. EtwpConvertTimeStamp(pItemBuf[iItemCount],
  803. sysTime,
  804. ItemBBufEnd - pItemBuf[iItemCount]
  805. );
  806. } else {
  807. _sntprintf(pItemBuf[iItemCount], ItemBBufEnd - pItemBuf[iItemCount], _T("%20I64u"), pHeader->TimeStamp.QuadPart);
  808. }
  809. TempLen = _tcslen(pItemBuf[iItemCount]);
  810. pItemBuf[iItemCount + 1] =
  811. pItemBuf[iItemCount] + TempLen + 1;
  812. pItemRBuf[iItemCount] = (ULONG_PTR *)pItemBuf[iItemCount]; // just use the same
  813. iItemCount ++;
  814. // Make Parameter %5 Kernel Time
  815. EtwpConvertUnsignedLong(pItemBuf[iItemCount],
  816. pHeader->KernelTime * TimerResolution,
  817. ItemBBufEnd - pItemBuf[iItemCount]
  818. );
  819. TempLen = _tcslen(pItemBuf[iItemCount]);
  820. pItemBuf[iItemCount + 1] =
  821. pItemBuf[iItemCount] + TempLen + 1;
  822. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  823. iItemCount ++;
  824. if (bStructuredFormat) {
  825. ULONG kTime = pHeader->KernelTime * TimerResolution ;
  826. RtlCopyMemory(&pStructuredMessage->KernelTime,&kTime,sizeof(ULONG));
  827. }
  828. // Make Parameter %6 User Time
  829. EtwpConvertUnsignedLong(pItemBuf[iItemCount],
  830. pHeader->UserTime * TimerResolution,
  831. ItemBBufEnd - pItemBuf[iItemCount]
  832. );
  833. TempLen = _tcslen(pItemBuf[iItemCount]);
  834. pItemBuf[iItemCount + 1] =
  835. pItemBuf[iItemCount] + TempLen + 1;
  836. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  837. iItemCount ++;
  838. if (bStructuredFormat) {
  839. ULONG uTime = pHeader->UserTime * TimerResolution ;
  840. RtlCopyMemory(&pStructuredMessage->UserTime,&uTime,sizeof(ULONG));
  841. }
  842. // Make Parameter %7 Sequence Number
  843. EtwpConvertUnsignedInt(pItemBuf[iItemCount],
  844. MessageSequence,
  845. ItemBBufEnd - pItemBuf[iItemCount]
  846. );
  847. TempLen = _tcslen(pItemBuf[iItemCount]);
  848. pItemBuf[iItemCount + 1] =
  849. pItemBuf[iItemCount] + TempLen + 1;
  850. pItemRBuf[iItemCount] = (ULONG_PTR *) MessageSequence ; // Raw just point at the value
  851. iItemCount ++;
  852. if (bStructuredFormat) {
  853. RtlCopyMemory(&pStructuredMessage->SequenceNum,&MessageSequence,sizeof(ULONG));
  854. }
  855. // Make Parameter %8 ProcessId
  856. RtlCopyMemory(&pItemRBuf[iItemCount],&pHeader->ProcessId,sizeof(ULONG));
  857. EtwpConvertHex4(pItemBuf[iItemCount],
  858. pHeader->ProcessId,
  859. ItemBBufEnd - pItemBuf[iItemCount]
  860. );
  861. TempLen = _tcslen(pItemBuf[iItemCount]);
  862. pItemBuf[iItemCount + 1] =
  863. pItemBuf[iItemCount] + TempLen + 1;
  864. iItemCount ++;
  865. if (bStructuredFormat) {
  866. RtlCopyMemory(&pStructuredMessage->ProcessId,&pHeader->ProcessId,sizeof(ULONG));
  867. }
  868. // Make Parameter %9 CPU Number
  869. EtwpConvertUnsignedInt(pItemBuf[iItemCount],
  870. ((PWMI_CLIENT_CONTEXT)&(pEvent->ClientContext))->ProcessorNumber,
  871. ItemBBufEnd - pItemBuf[iItemCount]
  872. );
  873. TempLen = _tcslen(pItemBuf[iItemCount]);
  874. pItemBuf[iItemCount + 1] = pItemBuf[iItemCount] + TempLen + 1;
  875. pItemRBuf[iItemCount] = (ULONG_PTR *) (((PWMI_CLIENT_CONTEXT)&(pEvent->ClientContext))->ProcessorNumber) ;
  876. iItemCount ++;
  877. if (bStructuredFormat) {
  878. RtlCopyMemory(&pStructuredMessage->CpuNumber,
  879. &(((PWMI_CLIENT_CONTEXT)&(pEvent->ClientContext))->ProcessorNumber),
  880. sizeof(UCHAR));
  881. }
  882. if ((pMofInfo != NULL) && bStructuredFormat) {
  883. pStructuredMessage->Indent = pMofInfo->Indent ;
  884. if (pMofInfo->FunctionName != NULL) {
  885. RtlCopyMemory((PVOID)pStructuredOffset,pMofInfo->FunctionName,min(_tcslen(pMofInfo->FunctionName) + 1, NAMESIZE) * sizeof(WCHAR));
  886. pStructuredMessage->FunctionName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  887. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  888. }
  889. if (pMofInfo->TraceFlagsName != NULL) {
  890. RtlCopyMemory((PVOID)pStructuredOffset,pMofInfo->TraceFlagsName,min(_tcslen(pMofInfo->TraceFlagsName) + 1, NAMESIZE) * sizeof(WCHAR));
  891. pStructuredMessage->FlagsName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  892. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  893. }
  894. if (pMofInfo->TraceLevelsName != NULL) {
  895. RtlCopyMemory((PVOID)pStructuredOffset,pMofInfo->TraceLevelsName,min(_tcslen(pMofInfo->TraceLevelsName) + 1, NAMESIZE) * sizeof(WCHAR));
  896. pStructuredMessage->LevelName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  897. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  898. }
  899. if (pMofInfo->ComponentName != NULL) {
  900. RtlCopyMemory((PVOID)pStructuredOffset,pMofInfo->ComponentName,min(_tcslen(pMofInfo->ComponentName) + 1, NAMESIZE) * sizeof(WCHAR));
  901. pStructuredMessage->ComponentName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  902. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  903. }
  904. if (pMofInfo->SubComponentName != NULL) {
  905. RtlCopyMemory((PVOID)pStructuredOffset,pMofInfo->SubComponentName,min(_tcslen(pMofInfo->SubComponentName) + 1, NAMESIZE) * sizeof(WCHAR));
  906. pStructuredMessage->SubComponentName= pStructuredOffset - (ULONG_PTR)pStructuredMessage;
  907. pStructuredOffset += (_tcslen((TCHAR *)pStructuredOffset) +1) * sizeof(WCHAR) ;
  908. }
  909. }
  910. // Done processing Parameters
  911. if (pMofInfo != NULL) {
  912. Head = pMofInfo->ItemHeader;
  913. pMofInfo->EventCount ++;
  914. Next = Head->Flink;
  915. } else {
  916. Head = Next = NULL ;
  917. }
  918. #ifdef MANAGED_TRACING
  919. if (bCSharpEvent && NULL == pCSharpDecoder && bFirstTime) {
  920. hr = InitializeCSharpDecoder();
  921. }
  922. #endif
  923. #ifdef MANAGED_TRACING
  924. //Now we branch out depending on whether the current
  925. //event was generated by C# or not.
  926. if (bCSharpEvent) {
  927. if (NULL != pCSharpDecoder) {
  928. //TODO[1]: Is there a potential for pMessageData to be updated ?
  929. long dataSize;
  930. int result;
  931. ptCSFormattedMessage = ItemBuf;
  932. result = pCSharpDecoder->DecodeTraceMessage((BYTE*)pCSMessageData, ptCSFormattedMessage, MAXBUFS*sizeof(TCHAR)/2, &dataSize);
  933. if (0 == result) {
  934. ptCSFormattedMessage = (TCHAR *)malloc(2*dataSize);
  935. if (NULL != ptCSFormattedMessage) {
  936. result = pCSharpDecoder->DecodeTraceMessage((BYTE*)pCSMessageData, ptCSFormattedMessage, MAXBUFS*sizeof(TCHAR)/2, &dataSize);
  937. } else {
  938. _stprintf(tstrFailureInfo,_T("Out of memory"));
  939. pItemRBuf[iItemCount++] = (ULONG_PTR*)tstrFailureInfo;
  940. tstrTypeOfType = 2;
  941. }
  942. }
  943. if (-1 == result) {
  944. //An exception would have been thrown in the C# decoder.
  945. _stprintf(tstrFailureInfo,_T("Badly formed arguments to C# decoder"));
  946. pItemRBuf[iItemCount++] = (ULONG_PTR*)tstrFailureInfo;
  947. tstrTypeOfType = 2;
  948. } else if (NULL != ptCSFormattedMessage) {
  949. pItemRBuf[iItemCount++] = (ULONG_PTR*)ptCSFormattedMessage;
  950. tstrTypeOfType = 2;
  951. }
  952. } else {
  953. if (bFirstTime)
  954. _stprintf(tstrFailureInfo,_T("%s [0x%x]"),_T("Failed to initalize C# decoder"),hr);
  955. else _stprintf(tstrFailureInfo,_T("Failed to initalize C# decoder"));
  956. pItemRBuf[iItemCount++] = (ULONG_PTR*)tstrFailureInfo;
  957. tstrTypeOfType = 2;
  958. }
  959. bFirstTime = FALSE;
  960. }
  961. #endif
  962. #ifdef MANAGED_TRACING
  963. else {
  964. #endif
  965. if (!bManagedTracingEnabled && bCSharpEvent) {
  966. _sntprintf(tstrFailureInfo,sizeof(tstrFailureInfo)/sizeof(TCHAR) - sizeof(TCHAR),_T("C# decoding is not enabled"));
  967. pItemRBuf[iItemCount++] = (ULONG_PTR*)tstrFailureInfo;
  968. tstrTypeOfType = 2;
  969. } else {
  970. __try {
  971. if ( Head != Next ) {
  972. iMofPtr = (char *) malloc(pEvent->MofLength + sizeof(UNICODE_NULL));
  973. if (iMofPtr == NULL) {
  974. return -1;
  975. }
  976. RtlCopyMemory(iMofPtr, pEvent->MofData, pEvent->MofLength);
  977. ptr = iMofPtr;
  978. while (Head != Next) {
  979. ULONG * ULongPtr = (ULONG *) & ItemRBuf[0];
  980. USHORT * UShortPtr = (USHORT *) & ItemRBuf[0];
  981. LONGLONG * LongLongPtr = (LONGLONG *) & ItemRBuf[0];
  982. ULONGLONG * ULongLongPtr = (ULONGLONG *) & ItemRBuf[0];
  983. double * DoublePtr = (double *) & ItemRBuf[0];
  984. TCHAR * PtrFmt1, * PtrFmt2 ;
  985. pItem = CONTAINING_RECORD(Next, ITEM_DESC, Entry);
  986. if ((ULONG) (ptr - iMofPtr) >= pEvent->MofLength) {
  987. break;
  988. }
  989. bItemIsString = FALSE ; // Assume its a RAW value
  990. ItemRSize = 0 ; // Raw length of zero
  991. switch (pItem->ItemType) {
  992. case ItemChar:
  993. case ItemUChar:
  994. ItemRSize = sizeof(CHAR);
  995. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  996. ptr += ItemRSize;
  997. break;
  998. case ItemCharSign:
  999. ItemRSize = sizeof(CHAR) * 2;
  1000. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1001. ItemRBuf[2] = '\0';
  1002. ptr += ItemRSize;
  1003. break;
  1004. case ItemCharShort:
  1005. ItemRSize = sizeof(CHAR);
  1006. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1007. ptr += ItemRSize;
  1008. break;
  1009. case ItemShort:
  1010. ItemRSize = sizeof(USHORT);
  1011. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1012. ptr += ItemRSize;
  1013. break;
  1014. case ItemDouble:
  1015. ItemRSize = sizeof(double);
  1016. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1017. _sntprintf(ItemBuf,MAXBUFS, _T("%g"), * DoublePtr);
  1018. ptr += ItemRSize;
  1019. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  1020. break;
  1021. case ItemUShort:
  1022. ItemRSize = sizeof(USHORT);
  1023. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1024. ptr += ItemRSize;
  1025. break;
  1026. case ItemLong:
  1027. ItemRSize = sizeof(LONG);
  1028. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1029. ptr += ItemRSize;
  1030. break;
  1031. case ItemULong:
  1032. ItemRSize = sizeof(ULONG);
  1033. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1034. ptr += ItemRSize;
  1035. break;
  1036. case ItemULongX:
  1037. ItemRSize = sizeof(ULONG);
  1038. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1039. ptr += ItemRSize;
  1040. break;
  1041. case ItemPtr :
  1042. PtrFmt2 = _T("%08X%08X") ;
  1043. PtrFmt1 = _T("%08X") ;
  1044. // goto ItemPtrCommon ;
  1045. //ItemPtrCommon:
  1046. {
  1047. ULONG ulongword2;
  1048. if (PointerSize == 8) { // 64 bits
  1049. RtlCopyMemory(&ulongword,ptr,4);
  1050. RtlCopyMemory(&ulongword2,ptr+4,4);
  1051. _sntprintf(ItemBuf,MAXBUFS, PtrFmt2 , ulongword2,ulongword);
  1052. } else { // assumes 32 bit otherwise
  1053. RtlCopyMemory(&ulongword,ptr,PointerSize);
  1054. _sntprintf(ItemBuf,MAXBUFS, PtrFmt1 , ulongword);
  1055. }
  1056. ItemRSize = 0 ; // Pointers are always co-erced to be strings
  1057. ptr += PointerSize;
  1058. }
  1059. break;
  1060. case ItemIPAddr:
  1061. ItemRSize = 0; // Only String form exists
  1062. memcpy(&ulongword, ptr, sizeof(ULONG));
  1063. // Convert it to readable form
  1064. //
  1065. _sntprintf(
  1066. ItemBuf, MAXBUFS,
  1067. _T("%03d.%03d.%03d.%03d"),
  1068. (ulongword >> 0) & 0xff,
  1069. (ulongword >> 8) & 0xff,
  1070. (ulongword >> 16) & 0xff,
  1071. (ulongword >> 24) & 0xff);
  1072. ptr += sizeof (ULONG);
  1073. break;
  1074. case ItemIPV6Addr:
  1075. #define SIZEOFIPV6 16
  1076. ItemRSize = 0; // Only String form exists
  1077. {
  1078. TCHAR Addr_Str[40] ;
  1079. BYTE Bin6_Addr[SIZEOFIPV6] ;
  1080. if (fIPV6 == 0) { // Don't know if IPV is supported
  1081. HMODULE hNTDLL = NULL ;
  1082. fIPV6 = 2 ; //Assume failure
  1083. if ((hNTDLL=LoadLibraryEx(L"ntdll.dll",NULL,0)) != NULL) {
  1084. if ((pRtlIpv6AddressToString=(RTLIPV6ADDRESSTOSTRING)GetProcAddress(hNTDLL,"RtlIpv6AddressToStringW")) != NULL) {
  1085. fIPV6 = 1 ; //got it
  1086. }
  1087. }
  1088. }
  1089. // By now we know whether we have IPV6 or not
  1090. if (fIPV6 == 1) { // IPV6 is Supported
  1091. memcpy(Bin6_Addr, ptr, SIZEOFIPV6);
  1092. // Convert it to readable form
  1093. pRtlIpv6AddressToString ((const in6_addr *)Bin6_Addr,Addr_Str);
  1094. _sntprintf(ItemBuf, MAXBUFS,_T("%s"),Addr_Str);
  1095. } else if (fIPV6 == 2) { // IPV6 is not supported
  1096. _sntprintf(ItemBuf, MAXBUFS,_T("Cannot decode IPV6 on this System"));
  1097. }
  1098. ptr += SIZEOFIPV6;
  1099. }
  1100. break;
  1101. case ItemMACAddr:
  1102. ItemRSize = 0; // Only String form exists
  1103. #define SIZEOFMAC 6 // MAC Addresses are 6 Bytes
  1104. {
  1105. BYTE MACBuff[SIZEOFMAC + 1] = {0} ;
  1106. memcpy(MACBuff, ptr, SIZEOFMAC);
  1107. // Convert it to readable form
  1108. //
  1109. _sntprintf(
  1110. ItemBuf, MAXBUFS,
  1111. _T("%02X-%02X-%02X-%02X-%02X-%02X"),
  1112. MACBuff[0],MACBuff[1],MACBuff[2],MACBuff[3],MACBuff[4],MACBuff[5]);
  1113. ptr += SIZEOFMAC ;
  1114. }
  1115. break;
  1116. case ItemPort:
  1117. ItemRSize = 0; // Only String form exists
  1118. _sntprintf(ItemBuf,MAXBUFS, _T("%u"), (UCHAR)ptr[0] * 256 + (UCHAR)ptr[1] * 1);
  1119. ptr += sizeof (USHORT);
  1120. break;
  1121. case ItemLongLong:
  1122. ItemRSize = sizeof(LONGLONG);
  1123. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1124. _sntprintf(ItemBuf,MAXBUFS, _T("%16I64x"), *LongLongPtr);
  1125. ptr += sizeof(LONGLONG);
  1126. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  1127. break;
  1128. case ItemULongLong:
  1129. ItemRSize = sizeof(ULONGLONG);
  1130. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  1131. _sntprintf(ItemBuf,MAXBUFS, _T("%16I64x"), *ULongLongPtr);
  1132. ptr += sizeof(ULONGLONG);
  1133. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  1134. break;
  1135. case ItemString:
  1136. case ItemRString:
  1137. {
  1138. SIZE_T pLen = strlen((CHAR *) ptr);
  1139. if (pLen > 0) {
  1140. if (pLen >= MAXBUFS) {
  1141. strncpy(StrA, ptr,MAXBUFS);
  1142. StrA[MAXBUFS-1] = '\0';
  1143. } else {
  1144. strcpy(StrA,ptr);
  1145. }
  1146. if (pItem->ItemType == ItemRString) {
  1147. reduce(StrA);
  1148. }
  1149. #ifdef UNICODE
  1150. MultiByteToWideChar(CP_ACP, 0, StrA, -1, StrW, MAXBUFS);
  1151. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), StrW);
  1152. ((TCHAR*)ItemRBuf)[MAXCHARS-1] =_T('\0');
  1153. #else
  1154. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s"), StrA);
  1155. ItemRBuf[MAXCHARS-1] = '\0';
  1156. #endif /* #ifdef UNICODE */
  1157. } else {
  1158. _sntprintf((TCHAR *)ItemRBuf,MAXCHARS,_T("<NULL>"));
  1159. }
  1160. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1161. bItemIsString = TRUE;
  1162. ptr += (pLen + 1);
  1163. break;
  1164. }
  1165. case ItemRWString:
  1166. case ItemWString:
  1167. {
  1168. size_t pLen = 0;
  1169. size_t iTemp;
  1170. if (*(WCHAR *) ptr) {
  1171. if (pItem->ItemType == ItemRWString) {
  1172. reduceW((WCHAR *) ptr);
  1173. }
  1174. pLen = ((wcslen((WCHAR*)ptr) + 1) * sizeof(WCHAR));
  1175. if (pLen > MAXBUFS* sizeof(WCHAR)) {
  1176. pLen = MAXBUFS * sizeof(WCHAR);
  1177. }
  1178. memcpy(StrW, ptr, pLen);
  1179. for (iTemp = 0; iTemp < pLen / 2; iTemp++) {
  1180. if (((USHORT) StrW[iTemp] == (USHORT) 0xFFFF)) {
  1181. StrW[iTemp] = (USHORT) 0;
  1182. }
  1183. }
  1184. StrW[pLen / 2] = StrW[(pLen / 2) + 1]= '\0';
  1185. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), StrW);
  1186. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1187. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1188. bItemIsString = TRUE;
  1189. }
  1190. ptr += pLen; // + sizeof(ULONG);
  1191. break;
  1192. }
  1193. case ItemDSString: // Counted String
  1194. {
  1195. USHORT pLen = 256 * ((USHORT) * ptr) + ((USHORT) * (ptr + 1));
  1196. ptr += sizeof(USHORT);
  1197. if (pLen > MAXBUFS) {
  1198. pLen = MAXBUFS;
  1199. }
  1200. if (pLen > 0) {
  1201. memcpy(StrA, ptr, pLen);
  1202. StrA[pLen] = 0 ;
  1203. #ifdef UNICODE
  1204. MultiByteToWideChar(CP_ACP, 0, StrA, -1, StrW, MAXBUFS);
  1205. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), StrW);
  1206. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1207. #else
  1208. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), StrA);
  1209. ItemRBuf[MAXCHARS-1] = '\0';
  1210. #endif
  1211. }
  1212. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1213. bItemIsString = TRUE;
  1214. ptr += (pLen);
  1215. break;
  1216. }
  1217. case ItemPString: // Counted String
  1218. {
  1219. SHORT pLen = ((BYTE)((char) * ptr)) + (256 * (BYTE)((char) * (ptr + 1)));
  1220. ptr += sizeof(USHORT);
  1221. if ((!strncmp((const char*)ptr,"NULL",4))) {
  1222. ptr += 5;
  1223. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  1224. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1225. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1226. bItemIsString = TRUE ;
  1227. break;
  1228. }
  1229. if (pLen > MAXBUFS) {
  1230. pLen = MAXBUFS;
  1231. }
  1232. if ((ULONG) ((ptr+pLen) - iMofPtr) >= pEvent->MofLength) {
  1233. pLen = pEvent->MofLength - (ptr -iMofPtr) ;
  1234. }
  1235. if (pLen > 0) {
  1236. memcpy(StrA, ptr, pLen);
  1237. StrA[pLen] = 0 ;
  1238. #ifdef UNICODE
  1239. MultiByteToWideChar(CP_ACP, 0, StrA, -1, StrW, MAXBUFS);
  1240. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), StrW);
  1241. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1242. #else
  1243. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), StrA);
  1244. ItemRBuf[MAXCHARS-1] = '\0';
  1245. #endif /* #ifdef UNICODE */
  1246. } else {
  1247. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1248. ItemRBuf[MAXCHARS-1] = '\0';
  1249. }
  1250. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1251. bItemIsString = TRUE;
  1252. ptr += (pLen);
  1253. break;
  1254. }
  1255. case ItemDSWString: // DS Counted Wide Strings
  1256. case ItemPWString: // Counted Wide Strings
  1257. {
  1258. USHORT pLen;
  1259. PCHAR oriptr = ptr;
  1260. ptr += sizeof(USHORT);
  1261. if ( (!wcsncmp((const wchar_t*)ptr,L"NULL",4)) && (pItem->ItemType == ItemPWString)) {
  1262. ptr += ((wcslen((WCHAR*)ptr) + 1) * sizeof(WCHAR));
  1263. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), L"<NULL>");
  1264. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1265. bItemIsString = TRUE ;
  1266. break;
  1267. }
  1268. pLen = ( pItem->ItemType == ItemDSWString)
  1269. ? (256 * ((USHORT) * oriptr) + ((USHORT) * (oriptr + 1)))
  1270. : (* ((USHORT *) oriptr));
  1271. if (pLen > MAXBUFS* sizeof(WCHAR)) {
  1272. pLen = MAXBUFS * sizeof(WCHAR);
  1273. }
  1274. if (pLen > 0) {
  1275. memcpy(StrW, ptr, pLen);
  1276. StrW[pLen / sizeof(WCHAR)] = L'\0';
  1277. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), StrW);
  1278. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1279. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1280. bItemIsString = TRUE;
  1281. }
  1282. ptr += pLen;
  1283. break;
  1284. }
  1285. case ItemNWString: // Non Null Terminated String
  1286. {
  1287. USHORT Size;
  1288. Size = (USHORT) (pEvent->MofLength -
  1289. (ULONG) (ptr - iMofPtr));
  1290. if (Size > MAXBUFS) {
  1291. Size = MAXBUFS;
  1292. }
  1293. if (Size > 0) {
  1294. memcpy(StrW, ptr, Size);
  1295. StrW[Size / 2] = '\0';
  1296. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), StrW);
  1297. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1298. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1299. bItemIsString = TRUE;
  1300. }
  1301. ptr += Size;
  1302. break;
  1303. }
  1304. case ItemMLString: // Multi Line String
  1305. {
  1306. SIZE_T pLen;
  1307. char * src, * dest;
  1308. BOOL inQ = FALSE;
  1309. BOOL skip = FALSE;
  1310. UINT lineCount = 0;
  1311. ptr += sizeof(UCHAR) * 2;
  1312. pLen = strlen(ptr);
  1313. if (pLen > 0) {
  1314. src = ptr;
  1315. dest = StrA;
  1316. while (*src != '\0') {
  1317. if (*src == '\n') {
  1318. if (!lineCount) {
  1319. * dest ++ = ' ';
  1320. }
  1321. lineCount ++;
  1322. } else if (*src == '\"') {
  1323. if (inQ) {
  1324. char strCount[32];
  1325. char * cpy;
  1326. sprintf(strCount, "{%dx}", lineCount);
  1327. cpy = &strCount[0];
  1328. while (*cpy != '\0') {
  1329. * dest ++ = * cpy ++;
  1330. }
  1331. }
  1332. inQ = !inQ;
  1333. } else if (!skip) {
  1334. *dest++ = *src;
  1335. }
  1336. skip = (lineCount > 1 && inQ);
  1337. src++;
  1338. }
  1339. *dest = '\0';
  1340. #ifdef UNICODE
  1341. MultiByteToWideChar(CP_ACP, 0, StrA, -1, StrW, MAXBUFS);
  1342. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), StrW);
  1343. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1344. #else
  1345. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), StrA);
  1346. ItemRBuf[MAXCHARS-1] = '\0';
  1347. #endif /* #ifdef UNICODE */
  1348. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1349. bItemIsString = TRUE;
  1350. }
  1351. ptr += (pLen);
  1352. break;
  1353. }
  1354. case ItemSid:
  1355. {
  1356. TCHAR UserName[64];
  1357. TCHAR Domain[64];
  1358. TCHAR FullName[256];
  1359. ULONG asize = 0;
  1360. ULONG bsize = 0;
  1361. ULONG Sid[64];
  1362. PULONG pSid = &Sid[0];
  1363. SID_NAME_USE Se;
  1364. ULONG nSidLength;
  1365. pSid = (PULONG) ptr;
  1366. if (* pSid == 0) {
  1367. ptr += 4;
  1368. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  1369. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1370. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1371. bItemIsString = TRUE ;
  1372. } else if ((!strncmp((const char*)pSid,"NULL",4))) {
  1373. ptr += 5;
  1374. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  1375. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1376. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1377. bItemIsString = TRUE ;
  1378. } else {
  1379. ptr += 8; // skip the TOKEN_USER structure
  1380. nSidLength = 8 + (4 * ptr[1]);
  1381. asize = 64;
  1382. bsize = 64;
  1383. // LookupAccountSid cannot accept asize, bsize as size_t
  1384. if (LookupAccountSid(
  1385. NULL,
  1386. (PSID) ptr,
  1387. (LPTSTR) & UserName[0],
  1388. &asize,
  1389. (LPTSTR) & Domain[0],
  1390. & bsize,
  1391. & Se)) {
  1392. LPTSTR pFullName = & FullName[0];
  1393. _sntprintf(pFullName, 256, _T("\\\\%s\\%s"), Domain, UserName);
  1394. FullName[255] = _T('\0');
  1395. asize = (ULONG) _tcslen(pFullName); // Truncate here
  1396. if (asize > 0) {
  1397. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), pFullName);
  1398. }
  1399. } else {
  1400. LPTSTR sidStr;
  1401. if ( ConvertSidToStringSid(pSid, &sidStr) ) {
  1402. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s"), sidStr);
  1403. } else {
  1404. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s(%d)"), _T("System"), GetLastError() );
  1405. }
  1406. }
  1407. SetLastError(ERROR_SUCCESS);
  1408. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1409. bItemIsString = TRUE;
  1410. ptr += nSidLength;
  1411. }
  1412. break;
  1413. }
  1414. case ItemChar4:
  1415. ItemRSize = 4 * sizeof(TCHAR);
  1416. _sntprintf(ItemBuf,MAXBUFS,
  1417. _T("%c%c%c%c"),
  1418. ptr[0], ptr[1], ptr[2], ptr[3]);
  1419. ptr += ItemRSize ;
  1420. _tcsncpy((LPTSTR)ItemRBuf, ItemBuf,MAXCHARS);
  1421. bItemIsString = TRUE;
  1422. break;
  1423. case ItemCharHidden:
  1424. ItemRSize = 0 ;
  1425. if ( EOF != _stscanf(pItem->ItemList,_T("%d"),&ItemRSize) ) {
  1426. if (ItemRSize > MAXBUFS) {
  1427. ItemRSize = MAXBUFS ;
  1428. }
  1429. RtlCopyMemory(ItemBuf,ptr,ItemRSize);
  1430. ptr += ItemRSize ;
  1431. _tcsncpy((LPTSTR)ItemRBuf, ItemBuf,MAXCHARS);
  1432. bItemIsString = TRUE ;
  1433. }
  1434. break;
  1435. case ItemWChar:
  1436. ItemRSize = 0 ;
  1437. if ( EOF != _stscanf(pItem->ItemList,_T("%d"),&ItemRSize) ) {
  1438. if (ItemRSize > MAXBUFS/sizeof(WCHAR)) {
  1439. ItemRSize = MAXBUFS/sizeof(WCHAR) ;
  1440. }
  1441. RtlCopyMemory(ItemBuf,ptr,ItemRSize*sizeof(WCHAR));
  1442. ptr += ItemRSize*sizeof(WCHAR) ;
  1443. _tcsncpy((LPTSTR)ItemRBuf, ItemBuf,MAXCHARS);
  1444. bItemIsString = TRUE ;
  1445. }
  1446. break;
  1447. case ItemSetByte:
  1448. ItemRSize = sizeof(BYTE);
  1449. goto ItemSetCommon;
  1450. case ItemSetShort:
  1451. ItemRSize = sizeof(USHORT);
  1452. goto ItemSetCommon;
  1453. case ItemSetLong:
  1454. ItemRSize = sizeof(ULONG);
  1455. // goto ItemSetCommon;
  1456. ItemSetCommon:
  1457. {
  1458. TCHAR * name;
  1459. ULONG Countr = 0;
  1460. ULONG ItemMask = 0;
  1461. BOOL first = TRUE;
  1462. RtlCopyMemory(&ItemMask, ptr, ItemRSize);
  1463. RtlZeroMemory(IList, MAXBUFS * sizeof(TCHAR));
  1464. ptr += ItemRSize;
  1465. _tcscpy(ItemBuf, _T("["));
  1466. _sntprintf(IList, MAXBUFS,_T("%s"), pItem->ItemList);
  1467. name = _tcstok(IList, _T(","));
  1468. while ( name != NULL ) {
  1469. // While there are tokens in "string"
  1470. //
  1471. if (ItemMask & (1 << Countr) ) {
  1472. if (!first) _tcscat(ItemBuf, _T(","));
  1473. _tcscat(ItemBuf, name); first = FALSE;
  1474. }
  1475. // Get next token:
  1476. //
  1477. name = _tcstok( NULL, _T(","));
  1478. Countr++;
  1479. }
  1480. while (Countr < ItemRSize * 8) {
  1481. if (ItemMask & (1 << Countr) ) {
  1482. TCHAR smallBuf[20];
  1483. EtwpConvertUnsignedInt(smallBuf,
  1484. Countr,
  1485. 20
  1486. );
  1487. if (!first) _tcscat(ItemBuf, _T(","));
  1488. _tcscat(ItemBuf, smallBuf); first = FALSE;
  1489. }
  1490. Countr++;
  1491. }
  1492. _tcscat(ItemBuf, _T("]") );
  1493. ItemRSize = 0; // Strings will be the same Raw and otherwise
  1494. }
  1495. break;
  1496. case ItemListByte:
  1497. ItemRSize = sizeof(BYTE);
  1498. goto ItemListCommon;
  1499. case ItemListShort:
  1500. ItemRSize = sizeof(USHORT);
  1501. goto ItemListCommon;
  1502. case ItemListLong:
  1503. ItemRSize = sizeof(ULONG);
  1504. // goto ItemListCommon;
  1505. ItemListCommon:
  1506. {
  1507. TCHAR * name;
  1508. ULONG Countr = 0;
  1509. ULONG ItemIndex = 0;
  1510. RtlCopyMemory(&ItemIndex, ptr, ItemRSize);
  1511. RtlZeroMemory(IList, MAXBUFS * sizeof(TCHAR));
  1512. ptr += ItemRSize;
  1513. ItemRSize = 0; // Strings will be the same Raw and otherwise
  1514. _sntprintf(ItemBuf,MAXBUFS, _T("!%X!"),ItemIndex);
  1515. _sntprintf(IList, MAXBUFS, _T("%s"), pItem->ItemList);
  1516. name = _tcstok(IList, _T(","));
  1517. while ( name != NULL ) {
  1518. // While there are tokens in "string"
  1519. //
  1520. if (ItemIndex == Countr ++) {
  1521. _sntprintf(ItemBuf,MAXBUFS, _T("%s"), name);
  1522. break;
  1523. }
  1524. // Get next token:
  1525. //
  1526. name = _tcstok( NULL, _T(","));
  1527. }
  1528. }
  1529. break;
  1530. case ItemNTerror:
  1531. ItemRSize = 0; // Only string form exists
  1532. RtlCopyMemory(ItemRBuf, ptr, sizeof(ULONG));
  1533. ptr += sizeof(ULONG);
  1534. // Translate the NT Error Message
  1535. if ((FormatMessage(
  1536. FORMAT_MESSAGE_FROM_SYSTEM |
  1537. FORMAT_MESSAGE_IGNORE_INSERTS,
  1538. NULL,
  1539. *ULongPtr,
  1540. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1541. ItemBuf,
  1542. MAXBUFS,
  1543. NULL )) == 0) {
  1544. _sntprintf(ItemBuf,MAXBUFS,_T("!NT Error %u unrecognised!"),*ULongPtr);
  1545. }
  1546. break;
  1547. case ItemMerror:
  1548. ItemRSize = 0; // Only string form exists
  1549. RtlCopyMemory(ItemRBuf, ptr, sizeof(ULONG));
  1550. ptr += sizeof(ULONG);
  1551. // Translate the Module Message
  1552. if (pItem->ItemList == NULL) {
  1553. _sntprintf(ItemBuf,MAXBUFS,_T("! Error %u No Module Name!"),*ULongPtr);
  1554. } else {
  1555. if ((hLibrary = LoadLibraryEx(
  1556. pItem->ItemList, // file name of module
  1557. NULL, // reserved, must be NULL
  1558. LOAD_LIBRARY_AS_DATAFILE // entry-point execution flag
  1559. )) == NULL) {
  1560. _sntprintf(ItemBuf,MAXBUFS,_T("!ItemMerror %u : LoadLibrary of %s failed %d!"),
  1561. *ULongPtr,
  1562. pItem->ItemList,
  1563. GetLastError());
  1564. } else {
  1565. if ((FormatMessage(
  1566. FORMAT_MESSAGE_FROM_HMODULE |
  1567. FORMAT_MESSAGE_FROM_SYSTEM |
  1568. FORMAT_MESSAGE_IGNORE_INSERTS,
  1569. hLibrary,
  1570. *ULongPtr,
  1571. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1572. ItemBuf,
  1573. MAXBUFS,
  1574. NULL )) == 0) {
  1575. _sntprintf(ItemBuf,MAXBUFS,_T("!Module Error %u unrecognised!"),*ULongPtr);
  1576. }
  1577. if (!FreeLibrary((HMODULE) hLibrary)) {
  1578. _sntprintf(ItemBuf,MAXBUFS,_T("Failed to free library (%s) handle, err = %d"),
  1579. pItem->ItemList, GetLastError());
  1580. }
  1581. }
  1582. }
  1583. break;
  1584. case ItemHRESULT:
  1585. {
  1586. NTSTATUS TempNTSTATUS, Error ;
  1587. ItemRSize = 0 ;
  1588. RtlCopyMemory(&TempNTSTATUS, ptr, sizeof(NTSTATUS));
  1589. ptr += sizeof(ULONG);
  1590. Error = TempNTSTATUS;
  1591. if (TempNTSTATUS == 0) { // Special case STATUS_SUCCESS just like everyone else!
  1592. _sntprintf(ItemBuf,MAXBUFS,_T("S_OK"));
  1593. } else {
  1594. const ERROR_MAP* map = (ERROR_MAP*)winerrorSymbolicNames;
  1595. _sntprintf(ItemBuf,MAXBUFS,_T("HRESULT=%8X"),TempNTSTATUS);
  1596. if ( FACILITY_NT_BIT & TempNTSTATUS ) {
  1597. map = (ERROR_MAP*)ntstatusSymbolicNames;
  1598. Error &= ~FACILITY_NT_BIT;
  1599. } else if (HRESULT_FACILITY(Error) == FACILITY_WIN32) {
  1600. Error &= 0xFFFF;
  1601. }
  1602. while (map->MessageId != 0xFFFFFFFF) {
  1603. if (map->MessageId == Error) {
  1604. _sntprintf(ItemBuf,MAXBUFS,_T("0x%08x(%S)"), TempNTSTATUS, map->SymbolicName);
  1605. break;
  1606. }
  1607. ++map;
  1608. }
  1609. }
  1610. }
  1611. break;
  1612. case ItemNTSTATUS:
  1613. {
  1614. int iTemp = 0 ;
  1615. NTSTATUS TempNTSTATUS ;
  1616. ItemRSize = 0 ;
  1617. RtlCopyMemory(&TempNTSTATUS, ptr, sizeof(NTSTATUS));
  1618. ptr += sizeof(ULONG);
  1619. if (TempNTSTATUS == 0) { // Special case STATUS_SUCCESS just like everyone else!
  1620. _sntprintf(ItemBuf,MAXBUFS,_T("STATUS_SUCCESS"));
  1621. } else {
  1622. _sntprintf(ItemBuf,MAXBUFS,_T("NTSTATUS=%8X"),TempNTSTATUS);
  1623. while (ntstatusSymbolicNames[iTemp].MessageId != 0xFFFFFFFF) {
  1624. if (ntstatusSymbolicNames[iTemp].MessageId == TempNTSTATUS) {
  1625. _sntprintf(ItemBuf,MAXBUFS,_T("0x%08x(%S)"), TempNTSTATUS, ntstatusSymbolicNames[iTemp].SymbolicName);
  1626. break;
  1627. }
  1628. iTemp++ ;
  1629. }
  1630. }
  1631. }
  1632. break;
  1633. case ItemWINERROR:
  1634. {
  1635. int iTemp = 0 ;
  1636. DWORD TempWINERROR ;
  1637. ItemRSize = 0 ;
  1638. RtlCopyMemory(&TempWINERROR, ptr, sizeof(DWORD));
  1639. ptr += sizeof(ULONG);
  1640. _sntprintf(ItemBuf,MAXBUFS,_T("WINERROR=%8X"),TempWINERROR);
  1641. while (winerrorSymbolicNames[iTemp].MessageId != 0xFFFFFFFF) {
  1642. if (winerrorSymbolicNames[iTemp].MessageId == TempWINERROR) {
  1643. _sntprintf(ItemBuf,MAXBUFS,_T("%d(%S)"), TempWINERROR, winerrorSymbolicNames[iTemp].SymbolicName);
  1644. break;
  1645. }
  1646. iTemp++ ;
  1647. }
  1648. }
  1649. break;
  1650. case ItemNETEVENT:
  1651. {
  1652. int iTemp = 0 ;
  1653. DWORD TempNETEVENT ;
  1654. ItemRSize = 0 ;
  1655. RtlCopyMemory(&TempNETEVENT, ptr, sizeof(DWORD));
  1656. ptr += sizeof(ULONG);
  1657. _sntprintf(ItemBuf,MAXBUFS,_T("NETEVENT=%8X"),TempNETEVENT);
  1658. while (neteventSymbolicNames[iTemp].MessageId != 0xFFFFFFFF) {
  1659. if (neteventSymbolicNames[iTemp].MessageId == TempNETEVENT) {
  1660. _sntprintf(ItemBuf,MAXBUFS,_T("%S"), neteventSymbolicNames[iTemp].SymbolicName);
  1661. break;
  1662. }
  1663. iTemp++ ;
  1664. }
  1665. }
  1666. break;
  1667. case ItemGuid:
  1668. GuidToString(ItemBuf, (LPGUID) ptr);
  1669. ItemRSize = 0; // Only string form exists
  1670. ptr += sizeof(GUID);
  1671. break;
  1672. case ItemTimeDelta:
  1673. {
  1674. LONGLONG time;
  1675. RtlCopyMemory(&time, ptr, sizeof(time));
  1676. FormatTimeDelta(ItemBuf, MAXBUFS, time);
  1677. ItemRSize = 0; // Only string form exists
  1678. ptr += sizeof(LONGLONG);
  1679. }
  1680. break;
  1681. case ItemWaitTime:
  1682. {
  1683. LONGLONG time;
  1684. RtlCopyMemory(&time, ptr, sizeof(time));
  1685. if (time <= 0) {
  1686. time = -time;
  1687. ItemBuf[0]='+';
  1688. FormatTimeDelta(ItemBuf+1, MAXBUFS-1, time);
  1689. ItemRSize = 0; // Only string form exists
  1690. ptr += sizeof(LONGLONG);
  1691. break;
  1692. }
  1693. // Fall thru
  1694. }
  1695. case ItemTimestamp:
  1696. {
  1697. LARGE_INTEGER LargeTmp;
  1698. FILETIME stdTime, localTime;
  1699. SYSTEMTIME sysTime;
  1700. RtlCopyMemory(&LargeTmp, ptr, sizeof(ULONGLONG));
  1701. stdTime.dwHighDateTime = LargeTmp.HighPart;
  1702. stdTime.dwLowDateTime = LargeTmp.LowPart;
  1703. if (!FileTimeToLocalFileTime(&stdTime, &localTime)) {
  1704. _sntprintf(ItemBuf,MAXBUFS,_T("FileTimeToLocalFileTime error 0x%8X\n"),GetLastError());
  1705. break;
  1706. }
  1707. if (!FileTimeToSystemTime(&localTime, &sysTime)) {
  1708. _sntprintf(ItemBuf,MAXBUFS,_T("FileTimeToSystemTime error 0x%8X\n"),GetLastError());
  1709. break;
  1710. }
  1711. EtwpConvertTimeStamp(ItemBuf,
  1712. sysTime,
  1713. MAXBUFS
  1714. );
  1715. }
  1716. ItemRSize = 0; // Only string form exists
  1717. ptr += sizeof(ULONGLONG);
  1718. break;
  1719. // Dump an arbitrary block of data in Hex
  1720. case ItemHexDump:
  1721. { USHORT hLen = ((char) * ptr) + (256 * ((char) * (ptr + 1)));
  1722. ptr += sizeof(USHORT);
  1723. if ((!strncmp((const char*)ptr,"NULL",4))) {
  1724. ptr += 5;
  1725. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  1726. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1727. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  1728. bItemIsString = TRUE ;
  1729. break;
  1730. }
  1731. if (hLen > MAXHEXDUMP) {
  1732. hLen = MAXHEXDUMP;
  1733. }
  1734. if (hLen > 0) {
  1735. #define MAXHEXBUFF 8
  1736. TCHAR HexBuff[MAXHEXBUFF+1] ; // "XX "
  1737. BYTE StrX[MAXHEXDUMP +1] ;
  1738. memcpy(StrX, ptr, hLen);
  1739. StrX[hLen] = 0 ;
  1740. *ItemRBuf = _T('\0');
  1741. for (i=0 ; i < hLen; i++) {
  1742. if ((i & 0xF)== 0) {
  1743. _sntprintf((TCHAR *)HexBuff, MAXHEXBUFF,_T("\n\t"));
  1744. _tcsncat((TCHAR *)ItemRBuf,HexBuff, MAXCHARS);
  1745. }
  1746. _sntprintf((TCHAR *)HexBuff,MAXHEXBUFF,_T("%02X "),StrX[i]);
  1747. _tcsncat((TCHAR *)ItemRBuf,HexBuff, MAXCHARS);
  1748. }
  1749. _sntprintf((TCHAR *)HexBuff, MAXHEXBUFF,_T("\n\t"));
  1750. _tcsncat((TCHAR *)ItemRBuf,HexBuff, MAXCHARS);
  1751. ((TCHAR*)ItemRBuf)[MAXCHARS-1] = _T('\0');
  1752. } else {
  1753. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  1754. ItemRBuf[MAXCHARS-1] = '\0';
  1755. }
  1756. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1757. bItemIsString = TRUE;
  1758. ptr += (hLen);
  1759. }
  1760. break;
  1761. default:
  1762. ptr += sizeof (int);
  1763. } // switch (pItem->ItemType)
  1764. if (ItemRSize == 0) {
  1765. // Raw and String are the same
  1766. memcpy(pItemBuf[iItemCount],
  1767. ItemBuf,
  1768. min(wcslen(ItemBuf)+1, ItemBBufEnd - pItemBuf[iItemCount])*sizeof(WCHAR)
  1769. );
  1770. pItemBuf[iItemCount + 1] = pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  1771. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount];
  1772. } else {
  1773. if (ItemRSize > MAXBUFS) {
  1774. ItemRSize = MAXBUFS ;
  1775. }
  1776. pItemRBuf[iItemCount] = pItemRBuf[iItemCount+1] = 0 ;
  1777. pItemBuf[iItemCount+1] = pItemBuf[iItemCount];
  1778. if (!bItemIsString) {
  1779. RtlCopyMemory(&pItemRBuf[iItemCount],ItemRBuf,ItemRSize);
  1780. } else {
  1781. // Share scratch buffer
  1782. if (ItemRSize < sizeof(WCHAR) * (ItemBBufEnd - pItemBuf[iItemCount])) {
  1783. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount] ;
  1784. RtlCopyMemory(pItemRBuf[iItemCount], ItemRBuf, ItemRSize + sizeof(WCHAR));
  1785. pItemBuf[iItemCount+1] =(TCHAR *)pItemRBuf[iItemCount] +
  1786. ItemRSize/sizeof(WCHAR) +
  1787. 1;
  1788. } else {
  1789. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount];
  1790. }
  1791. }
  1792. }
  1793. iItemCount ++;
  1794. Next = Next->Flink;
  1795. } // while (Head != Next)
  1796. // Ok we are finished with the MofData
  1797. //
  1798. free(iMofPtr);
  1799. } // if (Head!= Next)
  1800. } // try
  1801. __except(EXCEPTION_EXECUTE_HANDLER)
  1802. {
  1803. TCHAR * Buffer = EventBuf ;
  1804. DWORD BufferSize = SizeEventBuf ;
  1805. if (bStructuredFormat) {
  1806. Buffer = (TCHAR *)pStructuredOffset;
  1807. BufferSize -= DWORD((pStructuredOffset - (ULONG_PTR)pStructuredMessage) & 0xFFFFFFFF);
  1808. pStructuredMessage->FormattedString = pStructuredOffset - (ULONG_PTR)pStructuredMessage ;
  1809. }
  1810. _sntprintf(Buffer,
  1811. BufferSize,
  1812. _T("\n*****FormatMessage %s(%s) of %s, parameter %d raised an exception*****\n"),
  1813. tstrName,
  1814. tstrType,
  1815. tstrFormat,
  1816. iItemCount);
  1817. if (iMofPtr) free(iMofPtr);
  1818. if (!bStructuredFormat) {
  1819. return( (SIZE_T)_tcslen(EventBuf));
  1820. } else {
  1821. return(pStructuredOffset - (ULONG_PTR)pStructuredMessage + _tcslen((TCHAR *) &pStructuredMessage + pStructuredMessage->FormattedString));
  1822. }
  1823. }
  1824. } // else condition for if(!bManagedTracingEnabled && bCSharpEvent)
  1825. #ifdef MANAGED_TRACING
  1826. }//else condition for if (bCSharpEvent)
  1827. #endif
  1828. }
  1829. // All argument processing is complete
  1830. // No prepare the final formatting.
  1831. if ((tstrFormat == NULL) || (tstrFormat[0] == 0)) {
  1832. TCHAR GuidString[32] ;
  1833. // Build a helpful format
  1834. if (!IsEqualGUID(&pEvent->Header.Guid, &EventTraceGuid) ) {
  1835. GuidToString(GuidString, (LPGUID) &pEvent->Header.Guid);
  1836. TCHAR * Buffer = EventBuf ;
  1837. DWORD BufferSize = SizeEventBuf ;
  1838. if (bStructuredFormat) {
  1839. Buffer = (TCHAR *)pStructuredOffset;
  1840. BufferSize -= DWORD((pStructuredOffset - (ULONG_PTR)pStructuredMessage) & 0xFFFFFFFF);
  1841. pStructuredMessage->FormattedString = pStructuredOffset - (ULONG_PTR)pStructuredMessage ;
  1842. }
  1843. _sntprintf(Buffer,
  1844. BufferSize,
  1845. _T("%s(%s): GUID=%s (No Format Information found)."),
  1846. pItemBuf[0], // name if any
  1847. pItemBuf[1], // sub name or number
  1848. GuidString // GUID
  1849. );
  1850. } else {
  1851. // Display nothing for a header for now
  1852. // Might be a good place to display some general info ?
  1853. }
  1854. } else {
  1855. DWORD dwResult;
  1856. TCHAR * TBuffer = EventBuf ;
  1857. DWORD TBufferSize = SizeEventBuf ;
  1858. if (tstrTypeOfType == 2) {
  1859. if (pItemBuf[iItemCount] < ItemBBufEnd) {
  1860. *( pItemBuf[iItemCount] ) = 0;
  1861. } else {
  1862. ItemBBuf[MAXBUFS2 - 1] = 0;
  1863. }
  1864. if (bStructuredFormat) {
  1865. TBuffer = (TCHAR *)pStructuredOffset;
  1866. TBufferSize -= DWORD((pStructuredOffset - (ULONG_PTR)pStructuredMessage) & 0xFFFFFFFF);
  1867. pStructuredMessage->FormattedString = pStructuredOffset - (ULONG_PTR)pStructuredMessage ;
  1868. }
  1869. __try
  1870. {
  1871. ULONG ReturnLength ;
  1872. dwResult = (DWORD)TraceFormatMessage(
  1873. tstrFormat, // message format
  1874. 0,
  1875. FALSE, // Don't ignore inserts,
  1876. #if defined(UNICODE)
  1877. FALSE, // Arguments Are not Ansi,
  1878. #else // #if defined(UNICODE)
  1879. TRUE, // Arguments are Ansi
  1880. #endif // #if defined(UNICODE)
  1881. TRUE, // Arguments Are An Array,
  1882. (va_list *) pItemRBuf, // Arguments,
  1883. TBuffer, // Buffer,
  1884. TBufferSize, // maximum size of message buffer
  1885. &ReturnLength // Coutnof Data Returned
  1886. );
  1887. if (ReturnLength == 0) {
  1888. TCHAR * LBuffer = EventBuf ;
  1889. DWORD LBufferSize = SizeEventBuf ;
  1890. if (bStructuredFormat) {
  1891. LBuffer = (TCHAR *)pStructuredOffset;
  1892. LBufferSize -= DWORD((pStructuredOffset - (ULONG_PTR)pStructuredMessage) & 0xFFFFFFFF);
  1893. pStructuredMessage->FormattedString = pStructuredOffset - (ULONG_PTR)pStructuredMessage ;
  1894. }
  1895. _sntprintf(LBuffer,
  1896. LBufferSize,
  1897. _T("FormatMessage (#Typev) Failed 0x%X (%s/%s) (\n"),
  1898. dwResult,
  1899. pItemBuf[0],
  1900. pItemBuf[1]);
  1901. if (!bStructuredFormat) {
  1902. return( (SIZE_T)_tcslen(EventBuf));
  1903. } else {
  1904. return(pStructuredOffset - (ULONG_PTR)pStructuredMessage + _tcslen((TCHAR *) &pStructuredMessage + pStructuredMessage->FormattedString));
  1905. }
  1906. } else {
  1907. }
  1908. #ifdef MANAGED_TRACING
  1909. //Need to free memory, if allocated before the call to DecodeTraceMessage
  1910. if (ptCSFormattedMessage != NULL && ptCSFormattedMessage != ItemBuf) {
  1911. free(ptCSFormattedMessage);
  1912. ptCSFormattedMessage = NULL;
  1913. }
  1914. #endif
  1915. }
  1916. __except(EXCEPTION_EXECUTE_HANDLER)
  1917. {
  1918. TCHAR * EBuffer = EventBuf ;
  1919. DWORD EBufferSize = SizeEventBuf ;
  1920. if (bStructuredFormat) {
  1921. EBuffer = (TCHAR *)pStructuredOffset;
  1922. EBufferSize -= DWORD((pStructuredOffset - (ULONG_PTR)pStructuredMessage) & 0xFFFFFFFF);
  1923. pStructuredMessage->FormattedString = pStructuredOffset - (ULONG_PTR)pStructuredMessage ;
  1924. }
  1925. _sntprintf(EBuffer,
  1926. EBufferSize,
  1927. _T("\n*****FormatMessage (#Typev) raised an exception (Format = %s) ****\n**** [Check for missing \"!\" Formats]*****\n"), tstrFormat);
  1928. if (!bStructuredFormat) {
  1929. return( (SIZE_T)_tcslen(EventBuf));
  1930. } else {
  1931. return(pStructuredOffset - (ULONG_PTR)pStructuredMessage + _tcslen((TCHAR *) &pStructuredMessage + pStructuredMessage->FormattedString));
  1932. }
  1933. }
  1934. } else {
  1935. return(-12);
  1936. }
  1937. }
  1938. if (pszMask != NULL) {
  1939. // Has he imposed a Filter?
  1940. //
  1941. if (_tcsstr(_tcslwr(pszMask), _tcslwr(tstrName)) !=0) {
  1942. if (!bStructuredFormat) {
  1943. return( (SIZE_T)_tcslen(EventBuf));
  1944. } else {
  1945. return(pStructuredOffset - (ULONG_PTR)pStructuredMessage + _tcslen((TCHAR *) &pStructuredMessage + pStructuredMessage->FormattedString));
  1946. }
  1947. } else {
  1948. return(0);
  1949. }
  1950. }
  1951. if (!bStructuredFormat) {
  1952. return( (SIZE_T)_tcslen(EventBuf));
  1953. } else {
  1954. return(pStructuredOffset - (ULONG_PTR)pStructuredMessage + _tcslen((TCHAR *) &pStructuredMessage + pStructuredMessage->FormattedString));
  1955. }
  1956. }
  1957. PMOF_INFO
  1958. GetMofInfoHead(
  1959. PLIST_ENTRY * HeadEventList,
  1960. LPGUID pGuid,
  1961. LPTSTR strType,
  1962. LONG TypeIndex,
  1963. ULONG TypeOfType,
  1964. LPTSTR TypeFormat,
  1965. BOOL bBestMatch
  1966. )
  1967. {
  1968. PLIST_ENTRY Head, Next;
  1969. PMOF_INFO pMofInfo;
  1970. TCHAR *p;
  1971. DWORD Status;
  1972. // Search the eventList for this Guid and find the head
  1973. //
  1974. if (HeadEventList == NULL) {
  1975. return NULL ;
  1976. }
  1977. if (*HeadEventList == NULL) {
  1978. if ( (*HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY))) == NULL)
  1979. return NULL;
  1980. RtlZeroMemory(*HeadEventList, sizeof(LIST_ENTRY));
  1981. InitializeListHead(*HeadEventList);
  1982. }
  1983. // Traverse the list and look for the Mof info head for this Guid.
  1984. //
  1985. Head = *HeadEventList;
  1986. Next = Head->Flink;
  1987. if (bBestMatch) {
  1988. PMOF_INFO pBestMatch = NULL;
  1989. while (Head != Next) {
  1990. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  1991. if (IsEqualGUID(&pMofInfo->Guid, pGuid)) {
  1992. if (pMofInfo->TypeIndex == TypeIndex) {
  1993. return pMofInfo;
  1994. } else if (pMofInfo->strType == NULL) {
  1995. pBestMatch = pMofInfo;
  1996. }
  1997. }
  1998. Next = Next->Flink;
  1999. }
  2000. if (pBestMatch != NULL) {
  2001. return pBestMatch;
  2002. }
  2003. } else {
  2004. while (Head != Next) {
  2005. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2006. if ( (strType != NULL)
  2007. && (pMofInfo->strType != NULL)
  2008. && (IsEqualGUID(&pMofInfo->Guid, pGuid))
  2009. && (!(_tcscmp(strType, pMofInfo->strType)))) {
  2010. return pMofInfo;
  2011. } else if ( (strType == NULL)
  2012. && (pMofInfo->strType == NULL)
  2013. && (IsEqualGUID(&pMofInfo->Guid, pGuid))) {
  2014. return pMofInfo;
  2015. }
  2016. Next = Next->Flink;
  2017. }
  2018. }
  2019. // If One does not exist, create one.
  2020. //
  2021. if ( (pMofInfo = (PMOF_INFO) malloc(sizeof(MOF_INFO))) == NULL) {
  2022. return NULL;
  2023. }
  2024. RtlZeroMemory(pMofInfo, sizeof(MOF_INFO));
  2025. memcpy(&pMofInfo->Guid, pGuid, sizeof(GUID));
  2026. pMofInfo->ItemHeader = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY));
  2027. if ( pMofInfo->ItemHeader == NULL) {
  2028. free(pMofInfo);
  2029. return NULL;
  2030. }
  2031. RtlZeroMemory(pMofInfo->ItemHeader, sizeof(LIST_ENTRY));
  2032. if (strType != NULL) {
  2033. if ((pMofInfo->strType = (LPTSTR) malloc((_tcslen(strType) + 1) * sizeof(TCHAR))) == NULL ) {
  2034. free(pMofInfo->ItemHeader);
  2035. free(pMofInfo);
  2036. return NULL ;
  2037. }
  2038. _tcscpy(pMofInfo->strType,strType);
  2039. }
  2040. if (TypeOfType != 0) {
  2041. pMofInfo->TypeOfType = TypeOfType;
  2042. }
  2043. if (TypeFormat != NULL) {
  2044. if ((pMofInfo->TypeFormat =
  2045. (LPTSTR) malloc((_tcslen(TypeFormat) + 1) * sizeof(TCHAR)))== NULL) {
  2046. free(pMofInfo->strType);
  2047. free(pMofInfo->ItemHeader);
  2048. free(pMofInfo);
  2049. return NULL ;
  2050. }
  2051. _tcscpy(pMofInfo->TypeFormat,TypeFormat);
  2052. }
  2053. p = pMofInfo->TypeFormat;
  2054. if (p) {
  2055. while (*p != 0) {
  2056. if (*p == 'Z' && p > (pMofInfo->TypeFormat) && p[-1] == '!' && p[1] == '!') {
  2057. *p = 's';
  2058. }
  2059. ++p;
  2060. }
  2061. }
  2062. pMofInfo->TypeIndex = bBestMatch ? -1 : TypeIndex;
  2063. InitializeListHead(pMofInfo->ItemHeader);
  2064. InsertTailList(*HeadEventList, &pMofInfo->Entry);
  2065. Status = InsertFmtInfoSet(pMofInfo);
  2066. if (Status == ERROR_OUTOFMEMORY) {
  2067. return NULL;
  2068. }
  2069. return pMofInfo;
  2070. } // GetMofInfoHead()
  2071. DWORD
  2072. LoadGuidFile(OUT PLIST_ENTRY *HeadEventList,
  2073. IN LPGUID pGuid
  2074. )
  2075. /*++
  2076. Routine Description:
  2077. Load GUID file specified by pGuid, and insert event description into
  2078. HeadEventList.
  2079. Arguments:
  2080. Return Value:
  2081. ERROR_SUCCESS if succeeded,
  2082. ERROR_BAD_FORMAT obselete format used,
  2083. ERROR_PATH_NOT_FOUND other failures.
  2084. Notes:
  2085. --*/
  2086. {
  2087. TCHAR filename[MAX_PATH], filepath[MAX_PATH];
  2088. LPTSTR lpFilePart ;
  2089. LPTSTR lppath = NULL;
  2090. INT len, waslen = 0 ;
  2091. DWORD Status = ERROR_PATH_NOT_FOUND;
  2092. if (gTraceFormatSearchPath != NULL) {
  2093. //a trace path has been assigned, so use it instead of the environment variable
  2094. lppath = (LPTSTR)malloc((_tcslen(gTraceFormatSearchPath)+1) * sizeof(TCHAR));
  2095. if (lppath != NULL) {
  2096. _tcscpy(lppath, gTraceFormatSearchPath);
  2097. }
  2098. }
  2099. if (lppath == NULL) {
  2100. //no path passed in, so get it off the environment variable
  2101. while (((len = GetEnvironmentVariable(TRACE_FORMAT_SEARCH_PATH, lppath, waslen )) - waslen) > 0) {
  2102. if (len - waslen > 0 ) {
  2103. if (lppath != NULL) {
  2104. free(lppath);
  2105. }
  2106. if ( !(lppath = (LPTSTR) malloc((len+1) * sizeof(TCHAR)) ) ) {
  2107. break;
  2108. }
  2109. waslen = len ;
  2110. }
  2111. }
  2112. }
  2113. if (lppath != NULL) {
  2114. // Try to find it on the path //
  2115. swprintf(filename,L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.tmf",
  2116. pGuid->Data1,pGuid->Data2,pGuid->Data3,
  2117. pGuid->Data4[0],pGuid->Data4[1],pGuid->Data4[2],pGuid->Data4[3],
  2118. pGuid->Data4[4],pGuid->Data4[5],pGuid->Data4[6],pGuid->Data4[7] );
  2119. if ((len = SearchPath(
  2120. lppath, // search path semi-colon seperated
  2121. filename, // file name with extension
  2122. NULL, // file extension (not reqd.)
  2123. MAX_PATH, // size of buffer
  2124. filepath, // found file name buffer
  2125. &lpFilePart // file component
  2126. ) !=0) && (len <= MAX_PATH)) {
  2127. //_tprintf(_T("Opening file %s\n"),filepath);
  2128. Status = GetTraceGuidsW(filepath, HeadEventList);
  2129. }
  2130. free(lppath);
  2131. }
  2132. return Status;
  2133. } // LoadGuidFile()
  2134. void
  2135. MapGuidToName(
  2136. OUT PLIST_ENTRY *HeadEventList,
  2137. IN LPGUID pGuid,
  2138. IN ULONG nType,
  2139. OUT LPTSTR wstr
  2140. )
  2141. {
  2142. if (IsEqualGUID(pGuid, &EventTraceGuid)) {
  2143. _tcscpy(wstr, GUID_TYPE_HEADER);
  2144. } else if (!UserDefinedGuid(*HeadEventList,pGuid, wstr)) {
  2145. BOOL Success;
  2146. Success = LoadGuidFile(HeadEventList, pGuid);
  2147. if (Success) {
  2148. if (!UserDefinedGuid(*HeadEventList,pGuid, wstr)) {
  2149. _tcscpy(wstr, GUID_TYPE_UNKNOWN);
  2150. }
  2151. return;
  2152. }
  2153. }
  2154. }
  2155. ULONG
  2156. UserDefinedGuid(
  2157. OUT PLIST_ENTRY HeadEventList,
  2158. IN LPGUID pGuid,
  2159. OUT LPTSTR wstr
  2160. )
  2161. {
  2162. PLIST_ENTRY Head, Next;
  2163. PMOF_INFO pMofInfo;
  2164. // Search the eventList for this Guid and find the head
  2165. //
  2166. if (HeadEventList == NULL) {
  2167. /*
  2168. HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY));
  2169. if(HeadEventList == NULL)
  2170. return FALSE;
  2171. InitializeListHead(HeadEventList); */
  2172. return FALSE;
  2173. }
  2174. // Traverse the list and look for the Mof info head for this Guid.
  2175. //
  2176. Head = HeadEventList;
  2177. Next = Head->Flink;
  2178. while (Head != Next && Next != NULL) {
  2179. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2180. if (pMofInfo != NULL && IsEqualGUID(&pMofInfo->Guid, pGuid)) {
  2181. if ( pMofInfo->strDescription == NULL) {
  2182. return FALSE;
  2183. } else {
  2184. _tcscpy(wstr, pMofInfo->strDescription);
  2185. return TRUE;
  2186. }
  2187. }
  2188. Next = Next->Flink;
  2189. }
  2190. return FALSE;
  2191. }
  2192. ULONG
  2193. WINAPI
  2194. GetTraceGuidsW(
  2195. TCHAR * GuidFile,
  2196. PLIST_ENTRY * HeadEventList )
  2197. {
  2198. FILE * f=NULL;
  2199. TCHAR line[MAXSTR],
  2200. nextline[MAXSTR],
  2201. arg[MAXSTR],
  2202. strGuid[MAXSTR];
  2203. PMOF_TYPE types;
  2204. LPGUID Guid;
  2205. UINT i,
  2206. n;
  2207. TCHAR * name,
  2208. * s,
  2209. * guidName;
  2210. PMOF_INFO pMofInfo;
  2211. SIZE_T len = 0;
  2212. UINT typeCount = 0;
  2213. BOOL inInfo = FALSE;
  2214. BOOL eof = FALSE ;
  2215. BOOL nextlineF = FALSE ;
  2216. if (HeadEventList == NULL) {
  2217. return 0 ;
  2218. }
  2219. if (*HeadEventList == NULL) {
  2220. if ( (*HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY))) == NULL)
  2221. return 0 ;
  2222. RtlZeroMemory(*HeadEventList, sizeof(LIST_ENTRY));
  2223. InitializeListHead(*HeadEventList);
  2224. }
  2225. Guid = (LPGUID) malloc(sizeof(GUID));
  2226. if (Guid == NULL) {
  2227. return 0;
  2228. }
  2229. types = (PMOF_TYPE) malloc(MAXTYPE * sizeof(MOF_TYPE));
  2230. if (types == NULL) {
  2231. free(Guid);
  2232. return 0;
  2233. }
  2234. RtlZeroMemory(types, MAXTYPE * sizeof(MOF_TYPE));
  2235. RtlZeroMemory(line, MAXSTR * sizeof(TCHAR));
  2236. RtlZeroMemory(strGuid, MAXSTR * sizeof(TCHAR));
  2237. f = _tfopen( GuidFile, _T("r"));
  2238. if (f == NULL) {
  2239. free(Guid);
  2240. free(types);
  2241. return 0;
  2242. }
  2243. n = 0;
  2244. while (!eof ) {
  2245. if (nextlineF) { // Sometimes we read ahead a bit
  2246. _tcscpy(line, nextline);
  2247. nextlineF = FALSE ;
  2248. } else {
  2249. if (_fgetts(line, MAXSTR, f) == NULL) {
  2250. eof = TRUE ;
  2251. break;
  2252. }
  2253. }
  2254. line[MAXSTR-1] = _T('\0');
  2255. // jump_inside:;
  2256. if (line[0] == '/') {
  2257. continue;
  2258. } else if (line[0] == '{') {
  2259. inInfo = TRUE;
  2260. } else if ( line[0] == '}') {
  2261. typeCount = 0;
  2262. inInfo = FALSE;
  2263. } else if (inInfo) {
  2264. ITEM_TYPE type;
  2265. PTCHAR ItemListValue = NULL;
  2266. name = _tcstok(line, _T("\n\t,"));
  2267. s = _tcstok(NULL, _T(" \n\t,(["));
  2268. if (s != NULL && name != NULL ) {
  2269. if (!_tcsicmp(s,STR_ItemChar)) type = ItemChar;
  2270. else if (!_tcsicmp(s,STR_ItemUChar)) type = ItemUChar;
  2271. else if (!_tcsicmp(s,STR_ItemCharShort)) type = ItemCharShort;
  2272. else if (!_tcsicmp(s,STR_ItemCharSign)) type = ItemCharSign;
  2273. else if (!_tcsicmp(s,STR_ItemShort)) type = ItemShort;
  2274. else if (!_tcsicmp(s,STR_ItemHRESULT)) type = ItemHRESULT;
  2275. else if (!_tcsicmp(s,STR_ItemDouble)) type = ItemDouble;
  2276. else if (!_tcsicmp(s,STR_ItemUShort)) type = ItemUShort;
  2277. else if (!_tcsicmp(s,STR_ItemLong)) type = ItemLong;
  2278. else if (!_tcsicmp(s,STR_ItemULong)) type = ItemULong;
  2279. else if (!_tcsicmp(s,STR_ItemULongX)) type = ItemULongX;
  2280. else if (!_tcsicmp(s,STR_ItemLongLong)) type = ItemLongLong;
  2281. else if (!_tcsicmp(s,STR_ItemULongLong)) type = ItemULongLong;
  2282. else if (!_tcsicmp(s,STR_ItemString)) type = ItemString;
  2283. else if (!_tcsicmp(s,STR_ItemWString)) type = ItemWString;
  2284. else if (!_tcsicmp(s,STR_ItemRString)) type = ItemRString;
  2285. else if (!_tcsicmp(s,STR_ItemRWString)) type = ItemRWString;
  2286. else if (!_tcsicmp(s,STR_ItemPString)) type = ItemPString;
  2287. else if (!_tcsicmp(s,STR_ItemMLString)) type = ItemMLString;
  2288. else if (!_tcsicmp(s,STR_ItemNWString)) type = ItemNWString;
  2289. else if (!_tcsicmp(s,STR_ItemPWString)) type = ItemPWString;
  2290. else if (!_tcsicmp(s,STR_ItemDSString)) type = ItemDSString;
  2291. else if (!_tcsicmp(s,STR_ItemDSWString)) type = ItemDSWString;
  2292. else if (!_tcsicmp(s,STR_ItemPtr)) type = ItemPtr;
  2293. else if (!_tcsicmp(s,STR_ItemSid)) type = ItemSid;
  2294. else if (!_tcsicmp(s,STR_ItemChar4)) type = ItemChar4;
  2295. else if (!_tcsicmp(s,STR_ItemIPAddr)) type = ItemIPAddr;
  2296. else if (!_tcsicmp(s,STR_ItemIPV6Addr)) type = ItemIPV6Addr;
  2297. else if (!_tcsicmp(s,STR_ItemMACAddr)) type = ItemMACAddr;
  2298. else if (!_tcsicmp(s,STR_ItemPort)) type = ItemPort;
  2299. else if (!_tcsicmp(s,STR_ItemListLong)) type = ItemListLong;
  2300. else if (!_tcsicmp(s,STR_ItemListShort)) type = ItemListShort;
  2301. else if (!_tcsicmp(s,STR_ItemListByte)) type = ItemListByte;
  2302. else if (!_tcsicmp(s,STR_ItemSetLong)) type = ItemSetLong;
  2303. else if (!_tcsicmp(s,STR_ItemSetShort)) type = ItemSetShort;
  2304. else if (!_tcsicmp(s,STR_ItemSetByte)) type = ItemSetByte;
  2305. else if (!_tcsicmp(s,STR_ItemNTerror)) type = ItemNTerror;
  2306. else if (!_tcsicmp(s,STR_ItemMerror)) type = ItemMerror;
  2307. else if (!_tcsicmp(s,STR_ItemTimestamp)) type = ItemTimestamp;
  2308. else if (!_tcsicmp(s,STR_ItemGuid)) type = ItemGuid;
  2309. else if (!_tcsicmp(s,STR_ItemWaitTime)) type = ItemWaitTime;
  2310. else if (!_tcsicmp(s,STR_ItemTimeDelta)) type = ItemTimeDelta;
  2311. else if (!_tcsicmp(s,STR_ItemNTSTATUS)) type = ItemNTSTATUS;
  2312. else if (!_tcsicmp(s,STR_ItemWINERROR)) type = ItemWINERROR;
  2313. else if (!_tcsicmp(s,STR_ItemNETEVENT)) type = ItemNETEVENT;
  2314. else if (!_tcsicmp(s,STR_ItemCharHidden)) type = ItemCharHidden;
  2315. else if (!_tcsicmp(s,STR_ItemWChar)) type = ItemWChar;
  2316. else if (!_tcsicmp(s,STR_ItemHexDump)) type = ItemHexDump;
  2317. else type = ItemUnknown;
  2318. // Get List elements
  2319. if ((type == ItemListLong) || (type == ItemListShort) || (type == ItemListByte)
  2320. ||(type == ItemSetLong) || (type == ItemSetShort) || (type == ItemSetByte) ) {
  2321. s = _tcstok(NULL, _T("()"));
  2322. ItemListValue =
  2323. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  2324. if (ItemListValue == NULL) {
  2325. fclose(f);
  2326. free(Guid);
  2327. free(types);
  2328. return 1 ;
  2329. }
  2330. RtlCopyMemory(
  2331. ItemListValue,
  2332. s,
  2333. (_tcslen(s) + 1) * sizeof(TCHAR));
  2334. }
  2335. // Get Module specification for ItemMerror
  2336. if ((type == ItemMerror)) {
  2337. TCHAR * ppos ;
  2338. s = _tcstok(NULL, _T(" \t"));
  2339. ppos = _tcsrchr(s,'\n');
  2340. if (ppos != NULL) {
  2341. *ppos = UNICODE_NULL ;
  2342. ItemListValue =
  2343. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  2344. if (ItemListValue == NULL) {
  2345. fclose(f);
  2346. free(Guid);
  2347. free(types);
  2348. return 1 ;
  2349. }
  2350. RtlCopyMemory(
  2351. ItemListValue,
  2352. s,
  2353. (_tcslen(s) + 1) * sizeof(TCHAR));
  2354. }
  2355. }
  2356. // Get size for ItemCharHidden or other counted value
  2357. if ((type == ItemCharHidden)||(type == ItemWChar)) {
  2358. TCHAR * ppos ;
  2359. s = _tcstok(NULL, _T("["));
  2360. ppos = _tcsrchr(s,']');
  2361. if (ppos != NULL) {
  2362. *ppos = UNICODE_NULL ;
  2363. ItemListValue =
  2364. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  2365. if (ItemListValue == NULL) {
  2366. fclose(f);
  2367. free(Guid);
  2368. free(types);
  2369. return 1 ;
  2370. }
  2371. RtlCopyMemory(
  2372. ItemListValue,
  2373. s,
  2374. (_tcslen(s) + 1) * sizeof(TCHAR));
  2375. }
  2376. }
  2377. if (typeCount == 0) {
  2378. AddMofInfo(
  2379. * HeadEventList,
  2380. Guid,
  2381. NULL,
  2382. -1,
  2383. name,
  2384. type,
  2385. NULL,
  2386. 0,
  2387. NULL);
  2388. } else {
  2389. for (i = 0; i < typeCount; i ++) {
  2390. AddMofInfo(
  2391. * HeadEventList,
  2392. Guid,
  2393. types[i].strType,
  2394. types[i].TypeIndex,
  2395. name,
  2396. type,
  2397. ItemListValue,
  2398. types[i].TypeType,
  2399. types[i].TypeFormat);
  2400. }
  2401. }
  2402. }
  2403. } else if (line[0] == '#') {
  2404. TCHAR * token, * etoken;
  2405. int Indent ; // Special parameter values(numeric) from comments
  2406. TCHAR FuncName[MAXNAMEARG], // Special parameter values from comment
  2407. LevelName[MAXNAMEARG],
  2408. CompIDName[MAXNAMEARG],
  2409. SubCompName[MAXNAMEARG],
  2410. *v ;
  2411. FuncName[0] = LevelName[0] = CompIDName[0] = SubCompName[0] = '\0' ;
  2412. //This is a workaround to defend against newlines in TMF files.
  2413. while (!nextlineF && !eof) {
  2414. if (_fgetts(nextline,MAXSTR,f) != NULL) {
  2415. if ((nextline[0] != '{') && nextline[0] != '#') {
  2416. TCHAR * eol ;
  2417. if ((eol = _tcsrchr(line,'\n')) != NULL) {
  2418. *eol = 0 ;
  2419. }
  2420. _tcsncat(line,nextline,MAXSTR-_tcslen(line)) ;
  2421. line[MAXSTR-1] = _T('\0');
  2422. } else {
  2423. nextlineF = TRUE ;
  2424. }
  2425. } else {
  2426. eof = TRUE ;
  2427. }
  2428. }
  2429. // Find any special names in the comments
  2430. // As this gets longer we should make it generic
  2431. Indent = FindIntValue(line,_T("INDENT=")); // Indentation Level
  2432. v = FindValue(line,_T("FUNC=")); // Function Name
  2433. _tcsncpy(FuncName, v, MAXNAMEARG);
  2434. v = FindValue(line,_T("LEVEL=")); // Tracing level or Flags
  2435. _tcsncpy(LevelName, v, MAXNAMEARG);
  2436. v = FindValue(line,_T("COMPNAME=")); // Component ID
  2437. _tcsncpy(CompIDName, v, MAXNAMEARG);
  2438. v = FindValue(line,_T("SUBCOMP=")); // Sub Component ID
  2439. _tcsncpy(SubCompName, v, MAXNAMEARG);
  2440. token = _tcstok(line,_T(" \t"));
  2441. if (_tcsicmp(token,_T("#typev")) == 0) {
  2442. types[typeCount].TypeType = 2 ;
  2443. } else {
  2444. DWORD Status = -10;
  2445. if (_tcsicmp(token,_T("#type")) == 0) {
  2446. Status = ERROR_BAD_FORMAT;
  2447. }
  2448. fclose(f);
  2449. free(Guid);
  2450. free(types);
  2451. return(Status);
  2452. }
  2453. token = _tcstok( NULL, _T(" \t\n")); // Get Type Name
  2454. _tcscpy(types[typeCount].strType,token);
  2455. token =_tcstok( NULL, _T("\"\n,")); // Look for a Format String
  2456. if (token != NULL) {
  2457. types[typeCount].TypeIndex = _ttoi(token); // Get the type Index
  2458. token =_tcstok( NULL, _T("\n"));
  2459. }
  2460. etoken = NULL;
  2461. if (token != NULL) {
  2462. etoken = _tcsrchr(token,_T('\"')); // Find the closing quote
  2463. }
  2464. if (etoken !=NULL) {
  2465. etoken[0] = 0;
  2466. } else {
  2467. token = NULL;
  2468. }
  2469. if (token != NULL) {
  2470. if (token[0] == '%' && token[1] == '0') {
  2471. // add standard prefix
  2472. if (bUsePrefix && StdPrefix[0] == 0) {
  2473. // need to initialize it.
  2474. LPTSTR Prefix = NULL ; int newlen, waslen = 0 ;
  2475. while (((newlen = GetEnvironmentVariable(TRACE_FORMAT_PREFIX, Prefix, waslen )) - waslen) > 0) {
  2476. if (newlen - waslen > 0 ) {
  2477. if (Prefix != NULL) {
  2478. free(Prefix);
  2479. }
  2480. Prefix = (LPTSTR) malloc((newlen+1) * sizeof(TCHAR)) ;
  2481. if (Prefix == NULL) {
  2482. fclose(f);
  2483. free(Guid);
  2484. free(types);
  2485. return -11 ;
  2486. }
  2487. waslen = newlen ;
  2488. }
  2489. }
  2490. if (Prefix) {
  2491. _tcsncpy(StdPrefix, Prefix, MAXSTR);
  2492. } else {
  2493. _tcscpy(StdPrefix, STD_PREFIX);
  2494. }
  2495. free(Prefix) ;
  2496. }
  2497. _tcscpy(types[typeCount].TypeFormat,StdPrefix);
  2498. _tcscat(types[typeCount].TypeFormat,token + 2);
  2499. } else {
  2500. _tcscpy(types[typeCount].TypeFormat,token);
  2501. }
  2502. // process the special variable names
  2503. // Make generic in future
  2504. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!FUNC!"), FuncName);
  2505. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!LEVEL!"), LevelName);
  2506. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!COMPNAME!"), CompIDName);
  2507. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!SUBCOMP!"), SubCompName);
  2508. } else {
  2509. types[typeCount].TypeFormat[0] = types[typeCount].TypeFormat[1]
  2510. = 0;
  2511. }
  2512. if ( types[typeCount].TypeFormat[0] == 0
  2513. && types[typeCount].TypeFormat[1] == 0) {
  2514. pMofInfo = GetMofInfoHead(
  2515. HeadEventList,
  2516. Guid,
  2517. types[typeCount].strType,
  2518. types[typeCount].TypeIndex,
  2519. types[typeCount].TypeType,
  2520. NULL,
  2521. FALSE);
  2522. } else {
  2523. pMofInfo = GetMofInfoHead(
  2524. HeadEventList,
  2525. Guid,
  2526. types[typeCount].strType,
  2527. types[typeCount].TypeIndex,
  2528. types[typeCount].TypeType,
  2529. types[typeCount].TypeFormat,
  2530. FALSE);
  2531. }
  2532. if (pMofInfo == NULL) {
  2533. fclose(f);
  2534. free(Guid);
  2535. free(types);
  2536. return 0;
  2537. }
  2538. if (_tcslen(strGuid) > 0) {
  2539. pMofInfo->strDescription = (PTCHAR) malloc((_tcslen(strGuid) + 1) * sizeof(TCHAR));
  2540. if (pMofInfo->strDescription == NULL) {
  2541. fclose(f);
  2542. free(Guid);
  2543. free(types);
  2544. return 0;
  2545. }
  2546. _tcscpy(pMofInfo->strDescription, strGuid);
  2547. }
  2548. if (bStructuredFormat) {
  2549. pMofInfo->Indent = Indent ;
  2550. pMofInfo->FunctionName = pMofInfo->TraceFlagsName = pMofInfo->TraceLevelsName = pMofInfo->ComponentName = pMofInfo->SubComponentName = NULL ;
  2551. if ((FuncName[0] != '\0') && (pMofInfo->FunctionName = (PTCHAR) malloc((_tcslen(FuncName) + 1) * sizeof(TCHAR))) != NULL) {
  2552. RtlCopyMemory(pMofInfo->FunctionName,FuncName,min(_tcslen(FuncName) + 1, MAXNAMEARG) * sizeof(WCHAR));
  2553. }
  2554. if ((LevelName[0] != '\0') && (pMofInfo->TraceLevelsName = (PTCHAR) malloc((_tcslen(LevelName) + 1) * sizeof(TCHAR))) != NULL) {
  2555. RtlCopyMemory(pMofInfo->TraceLevelsName,LevelName,min(_tcslen(LevelName) + 1, MAXNAMEARG) * sizeof(WCHAR));
  2556. }
  2557. if ((CompIDName[0] != '\0') && (pMofInfo->ComponentName = (PTCHAR) malloc((_tcslen(CompIDName) + 1) * sizeof(TCHAR))) != NULL) {
  2558. RtlCopyMemory(pMofInfo->ComponentName,CompIDName,min(_tcslen(CompIDName) + 1, MAXNAMEARG) * sizeof(WCHAR));
  2559. }
  2560. if ((SubCompName[0] != '\0') && (pMofInfo->SubComponentName = (PTCHAR) malloc((_tcslen(SubCompName) + 1) * sizeof(TCHAR))) != NULL) {
  2561. RtlCopyMemory(pMofInfo->SubComponentName,SubCompName,min(_tcslen(SubCompName) + 1, MAXNAMEARG) * sizeof(WCHAR));
  2562. }
  2563. }
  2564. typeCount++;
  2565. if (typeCount >= MAXTYPE) {
  2566. fclose(f);
  2567. free(Guid);
  2568. free(types);
  2569. return(-11);
  2570. }
  2571. } else if ( (line[0] >= '0' && line[0] <= '9')
  2572. || (line[0] >= 'a' && line[0] <= 'f')
  2573. || (line[0] >= 'A' && line[0] <= 'F')) {
  2574. typeCount = 0;
  2575. _tcsncpy(arg, line, 8);
  2576. arg[8] = 0;
  2577. Guid->Data1 = ahextoi(arg);
  2578. _tcsncpy(arg, &line[9], 4);
  2579. arg[4] = 0;
  2580. Guid->Data2 = (USHORT) ahextoi(arg);
  2581. _tcsncpy(arg, &line[14], 4);
  2582. arg[4] = 0;
  2583. Guid->Data3 = (USHORT) ahextoi(arg);
  2584. for (i = 0; i < 2; i ++) {
  2585. _tcsncpy(arg, &line[19 + (i * 2)], 2);
  2586. arg[2] = 0;
  2587. Guid->Data4[i] = (UCHAR) ahextoi(arg);
  2588. }
  2589. for (i = 2; i < 8; i ++) {
  2590. _tcsncpy(arg, &line[20 + (i * 2)], 2);
  2591. arg[2] = 0;
  2592. Guid->Data4[i] = (UCHAR) ahextoi(arg);
  2593. }
  2594. // Find the next non whitespace character
  2595. //
  2596. guidName = &line[36];
  2597. while (*guidName == ' '|| *guidName == '\t') {
  2598. guidName++;
  2599. }
  2600. // cut comment out (if present)
  2601. {
  2602. TCHAR* comment = _tcsstr(guidName, TEXT("//"));
  2603. if (comment) {
  2604. // remove whitespace
  2605. --comment;
  2606. while (comment >= guidName && _istspace(*comment)) --comment;
  2607. *++comment = 0;
  2608. }
  2609. }
  2610. len = _tcslen(guidName);
  2611. s = guidName;
  2612. while (len > 0) {
  2613. len -= 1;
  2614. if (*s == '\n' || *s == '\0' || *s == '\t') {
  2615. *s = '\0';
  2616. break;
  2617. }
  2618. s++;
  2619. }
  2620. pMofInfo = GetMofInfoHead(
  2621. HeadEventList, Guid, NULL, -1, 0, NULL, FALSE);
  2622. if (pMofInfo == NULL) {
  2623. fclose(f);
  2624. free(Guid);
  2625. free(types);
  2626. return(ULONG) 0;
  2627. }
  2628. if (pMofInfo->strDescription != NULL) {
  2629. free(pMofInfo->strDescription);
  2630. pMofInfo->strDescription = NULL;
  2631. }
  2632. _tcsncpy(strGuid, guidName,MAXSTR);
  2633. strGuid[MAXSTR-1] = _T('\0');
  2634. pMofInfo->strDescription = (PTCHAR) malloc((_tcslen(guidName) + 1) * sizeof(TCHAR));
  2635. if (pMofInfo->strDescription == NULL ) {
  2636. // We really have problems
  2637. fclose(f);
  2638. free(Guid);
  2639. free(types);
  2640. return(ULONG) 0 ;
  2641. }
  2642. _tcscpy(pMofInfo->strDescription, strGuid);
  2643. n++ ; // Thats one more GUID
  2644. }
  2645. RtlZeroMemory(line, MAXSTR * sizeof(TCHAR));
  2646. }
  2647. fclose(f);
  2648. free(Guid);
  2649. free(types);
  2650. return(ULONG) n;
  2651. }
  2652. ULONG
  2653. ahextoi(
  2654. TCHAR *s
  2655. )
  2656. {
  2657. SSIZE_T len;
  2658. ULONG num, base, hex;
  2659. len = _tcslen(s);
  2660. hex = 0;
  2661. base = 1;
  2662. num = 0;
  2663. while (--len >= 0) {
  2664. if ((s[len] == 'x' || s[len] == 'X') && (s[len-1] == '0')) {
  2665. break;
  2666. }
  2667. if (s[len] >= '0' && s[len] <= '9') {
  2668. num = s[len] - '0';
  2669. } else if (s[len] >= 'a' && s[len] <= 'f') {
  2670. num = (s[len] - 'a') + 10;
  2671. } else if (s[len] >= 'A' && s[len] <= 'F') {
  2672. num = (s[len] - 'A') + 10;
  2673. } else {
  2674. continue;
  2675. }
  2676. hex += num * base;
  2677. base = base * 16;
  2678. }
  2679. return hex;
  2680. }
  2681. void
  2682. WINAPI
  2683. SummaryTraceEventListW(
  2684. TCHAR * SummaryBlock,
  2685. ULONG SizeSummaryBlock,
  2686. PLIST_ENTRY EventListHead
  2687. )
  2688. {
  2689. PLIST_ENTRY Head, Next;
  2690. PMOF_INFO pMofInfo;
  2691. TCHAR strGuid[MAXSTR];
  2692. TCHAR strName[MAXSTR];
  2693. TCHAR strBuffer[MAXSTR];
  2694. if ((EventListHead == NULL) || (SummaryBlock == NULL)) {
  2695. return;
  2696. }
  2697. RtlZeroMemory(SummaryBlock, sizeof(TCHAR) * SizeSummaryBlock);
  2698. Head = EventListHead;
  2699. Next = Head->Flink;
  2700. while (Head != Next) {
  2701. RtlZeroMemory(strName, 256);
  2702. RtlZeroMemory(strBuffer, 256);
  2703. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2704. if (pMofInfo->EventCount > 0) {
  2705. GuidToString((PTCHAR) strGuid, &pMofInfo->Guid);
  2706. MapGuidToName(&EventListHead, &pMofInfo->Guid, 0, strName);
  2707. _sntprintf(strBuffer,
  2708. MAXSTR,
  2709. _T("|%10d %-20s %-10s %36s|\n"),
  2710. pMofInfo->EventCount,
  2711. strName,
  2712. pMofInfo->strType ? pMofInfo->strType : _T("General"),
  2713. strGuid);
  2714. _tcscat(SummaryBlock, strBuffer);
  2715. if (_tcslen(SummaryBlock) >= SizeSummaryBlock) {
  2716. return;
  2717. }
  2718. }
  2719. Next = Next->Flink;
  2720. }
  2721. }
  2722. PTCHAR
  2723. GuidToString(
  2724. PTCHAR s,
  2725. LPGUID piid
  2726. )
  2727. {
  2728. _stprintf(s, _T("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"),
  2729. piid->Data1,
  2730. piid->Data2,
  2731. piid->Data3,
  2732. piid->Data4[0],
  2733. piid->Data4[1],
  2734. piid->Data4[2],
  2735. piid->Data4[3],
  2736. piid->Data4[4],
  2737. piid->Data4[5],
  2738. piid->Data4[6],
  2739. piid->Data4[7]);
  2740. return(s);
  2741. }
  2742. // This routine is called by the WBEM interface routine for each property
  2743. // found for this Guid. The ITEM_DESC structure is allocted for each Property.
  2744. //
  2745. VOID
  2746. AddMofInfo(
  2747. PLIST_ENTRY HeadEventList,
  2748. const GUID * Guid,
  2749. LPTSTR strType,
  2750. ULONG typeIndex,
  2751. LPTSTR strDesc,
  2752. ITEM_TYPE ItemType,
  2753. TCHAR * ItemList,
  2754. ULONG typeOfType,
  2755. LPTSTR typeFormat
  2756. )
  2757. {
  2758. PITEM_DESC pItem;
  2759. PLIST_ENTRY pListHead;
  2760. PMOF_INFO pMofInfo;
  2761. if (strDesc == NULL) {
  2762. return;
  2763. }
  2764. pItem = (PITEM_DESC) malloc(sizeof(ITEM_DESC));
  2765. if (pItem == NULL)return; //silent error
  2766. RtlZeroMemory(pItem, sizeof(ITEM_DESC));
  2767. pItem->strDescription =
  2768. (LPTSTR) malloc((_tcslen(strDesc) + 1) * sizeof(TCHAR));
  2769. if ( pItem->strDescription == NULL ) {
  2770. free(pItem);
  2771. return;
  2772. }
  2773. _tcscpy(pItem->strDescription, strDesc);
  2774. pItem->ItemType = ItemType;
  2775. pItem->ItemList = ItemList ;
  2776. pMofInfo = GetMofInfoHead(
  2777. (PLIST_ENTRY *) HeadEventList,
  2778. (LPGUID) Guid,
  2779. strType,
  2780. typeIndex,
  2781. typeOfType,
  2782. typeFormat,
  2783. FALSE);
  2784. if (pMofInfo != NULL) {
  2785. pListHead = pMofInfo->ItemHeader;
  2786. InsertTailList(pListHead, &pItem->Entry);
  2787. } else {
  2788. free(pItem->strDescription);
  2789. free(pItem);
  2790. }
  2791. }
  2792. void
  2793. WINAPI
  2794. CleanupTraceEventList(
  2795. PLIST_ENTRY HeadEventList
  2796. )
  2797. {
  2798. PLIST_ENTRY Head, Next;
  2799. PMOF_INFO pMofInfo;
  2800. if (HeadEventList == NULL) {
  2801. return;
  2802. }
  2803. Head = HeadEventList;
  2804. Next = Head->Flink;
  2805. while (Head != Next) {
  2806. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2807. RemoveEntryList(&pMofInfo->Entry);
  2808. RemoveMofInfo(pMofInfo->ItemHeader);
  2809. free(pMofInfo->ItemHeader);
  2810. free(pMofInfo->strDescription);
  2811. free(pMofInfo->TypeFormat);
  2812. free(pMofInfo->strType);
  2813. free(pMofInfo->FunctionName);
  2814. free(pMofInfo->TraceFlagsName);
  2815. free(pMofInfo->TraceLevelsName);
  2816. free(pMofInfo->ComponentName);
  2817. free(pMofInfo->SubComponentName);
  2818. Next = Next->Flink;
  2819. pFmtInfoSet->erase(pMofInfo); //remove from the cache
  2820. free(pMofInfo);
  2821. }
  2822. free(HeadEventList);
  2823. #ifdef MANAGED_TRACING
  2824. UninitializeCSharpDecoder();
  2825. #endif
  2826. }
  2827. void
  2828. RemoveMofInfo(
  2829. PLIST_ENTRY pMofInfo
  2830. )
  2831. {
  2832. PLIST_ENTRY Head, Next;
  2833. PITEM_DESC pItem;
  2834. Head = pMofInfo;
  2835. Next = Head->Flink;
  2836. while (Head != Next) {
  2837. pItem = CONTAINING_RECORD(Next, ITEM_DESC, Entry);
  2838. Next = Next->Flink;
  2839. RemoveEntryList(&pItem->Entry);
  2840. if (pItem->strDescription != NULL) {
  2841. free(pItem->strDescription);
  2842. }
  2843. if (pItem->ItemList != NULL) {
  2844. free(pItem->ItemList);
  2845. }
  2846. free(pItem);
  2847. }
  2848. }
  2849. // Now for some of the ASCII Stuff
  2850. //
  2851. SIZE_T
  2852. WINAPI
  2853. FormatTraceEventA(
  2854. PLIST_ENTRY HeadEventList,
  2855. PEVENT_TRACE pInEvent,
  2856. CHAR * EventBuf,
  2857. ULONG SizeEventBuf,
  2858. CHAR * pszMask
  2859. )
  2860. {
  2861. SIZE_T Status;
  2862. ULONG uSizeEventBuf;
  2863. TCHAR * EventBufW;
  2864. TCHAR * pszMaskW;
  2865. EventBufW = (TCHAR *) malloc(SizeEventBuf * sizeof(TCHAR) + 2);
  2866. if (EventBufW == (TCHAR *) NULL) {
  2867. return -1;
  2868. }
  2869. pszMaskW = (TCHAR *) NULL;
  2870. if (pszMask != NULL && strlen(pszMask) != 0) {
  2871. pszMaskW = (TCHAR *) malloc(strlen(pszMask) * sizeof(TCHAR));
  2872. if (pszMaskW == (TCHAR *) NULL) {
  2873. free(EventBufW);
  2874. return -1;
  2875. }
  2876. RtlZeroMemory(pszMaskW, strlen(pszMask) * sizeof(TCHAR));
  2877. }
  2878. uSizeEventBuf = SizeEventBuf;
  2879. Status = FormatTraceEventW(
  2880. HeadEventList,
  2881. pInEvent,
  2882. EventBufW,
  2883. SizeEventBuf,
  2884. pszMaskW);
  2885. if (Status == 0) {
  2886. free(EventBufW);
  2887. free(pszMaskW);
  2888. return -1;
  2889. }
  2890. RtlZeroMemory(EventBuf,uSizeEventBuf);
  2891. WideCharToMultiByte(
  2892. CP_ACP,
  2893. 0,
  2894. EventBufW,
  2895. (int) _tcslen(EventBufW), // Truncate in Sundown!!
  2896. EventBuf,
  2897. uSizeEventBuf,
  2898. NULL,
  2899. NULL);
  2900. free(EventBufW);
  2901. free(pszMaskW);
  2902. return Status;
  2903. }
  2904. ULONG
  2905. WINAPI
  2906. GetTraceGuidsA(
  2907. CHAR * GuidFile,
  2908. PLIST_ENTRY * HeadEventList
  2909. )
  2910. {
  2911. INT Status;
  2912. WCHAR * GuidFileW;
  2913. SIZE_T len;
  2914. if ((len = strlen(GuidFile)) == 0) {
  2915. return 0;
  2916. }
  2917. GuidFileW = (TCHAR *) malloc(sizeof(WCHAR) * strlen(GuidFile) + 2);
  2918. if (GuidFileW == NULL) {
  2919. return 0;
  2920. }
  2921. if ((MultiByteToWideChar(
  2922. CP_ACP,
  2923. 0,
  2924. GuidFile,
  2925. -1,
  2926. GuidFileW,
  2927. (int) strlen(GuidFile) + 1)) // Truncate in Sundown!!
  2928. == 0) {
  2929. free(GuidFileW);
  2930. return 0;
  2931. }
  2932. Status = GetTraceGuidsW(GuidFileW, HeadEventList);
  2933. free (GuidFileW);
  2934. return Status;
  2935. }
  2936. void
  2937. WINAPI
  2938. SummaryTraceEventListA(
  2939. CHAR * SummaryBlock,
  2940. ULONG SizeSummaryBlock,
  2941. PLIST_ENTRY EventListhead
  2942. )
  2943. {
  2944. TCHAR * SummaryBlockW;
  2945. if (SizeSummaryBlock <= 0 || SizeSummaryBlock * sizeof(TCHAR) <= 0 ) {
  2946. return;
  2947. }
  2948. SummaryBlockW = (TCHAR *) malloc(SizeSummaryBlock * sizeof(TCHAR));
  2949. if (SummaryBlockW == (TCHAR *)NULL) {
  2950. return;
  2951. }
  2952. //RtlZeroMemory(SummaryBlock, SizeSummaryBlock);
  2953. RtlZeroMemory(SummaryBlockW, SizeSummaryBlock*sizeof(TCHAR));
  2954. SummaryTraceEventListW(
  2955. SummaryBlockW,
  2956. SizeSummaryBlock * ((ULONG)sizeof(TCHAR)),
  2957. EventListhead);
  2958. WideCharToMultiByte(
  2959. CP_ACP,
  2960. 0,
  2961. SummaryBlockW,
  2962. (int)_tcslen(SummaryBlockW), // Truncate in Sundown!!
  2963. SummaryBlock,
  2964. SizeSummaryBlock,
  2965. NULL,
  2966. NULL);
  2967. free(SummaryBlockW);
  2968. }
  2969. void
  2970. WINAPI
  2971. GetTraceElapseTime(
  2972. __int64 * pElapseTime
  2973. )
  2974. {
  2975. * pElapseTime = ElapseTime;
  2976. }
  2977. ULONG
  2978. WINAPI
  2979. SetTraceFormatParameter(
  2980. PARAMETER_TYPE Parameter ,
  2981. PVOID ParameterValue
  2982. )
  2983. {
  2984. ULONG ulLen;
  2985. switch (Parameter) {
  2986. case ParameterINDENT:
  2987. bIndent = PtrToUlong(ParameterValue);
  2988. break;
  2989. case ParameterSEQUENCE:
  2990. bSequence = PtrToUlong(ParameterValue);
  2991. if (bSequence) {
  2992. STD_PREFIX = STD_PREFIX_SEQ;
  2993. } else {
  2994. STD_PREFIX = STD_PREFIX_NOSEQ;
  2995. }
  2996. break;
  2997. case ParameterGMT:
  2998. bGMT = PtrToUlong(ParameterValue);
  2999. break;
  3000. case ParameterTraceFormatSearchPath:
  3001. ulLen = _tcslen((LPTSTR)ParameterValue);
  3002. if (gTraceFormatSearchPath != NULL) {
  3003. free(gTraceFormatSearchPath);
  3004. }
  3005. gTraceFormatSearchPath = (LPTSTR)malloc((ulLen+1) * sizeof(TCHAR));
  3006. if (gTraceFormatSearchPath != NULL) {
  3007. _tcscpy(gTraceFormatSearchPath, (const wchar_t *) ParameterValue);
  3008. gTraceFormatSearchPath[ulLen] = L'\000';
  3009. }
  3010. break;
  3011. case ParameterUsePrefix:
  3012. bUsePrefix = PtrToUlong(ParameterValue);
  3013. break;
  3014. case ParameterSetPrefix:
  3015. ulLen = _tcslen((LPTSTR)ParameterValue);
  3016. gTraceFormatSearchPath = (LPTSTR)malloc((ulLen+1) * sizeof(TCHAR));
  3017. if (ulLen < MAXSTR) {
  3018. _tcscpy(StdPrefix, (const wchar_t *) ParameterValue);
  3019. StdPrefix[ulLen] = L'\000';
  3020. }
  3021. break;
  3022. case ParameterStructuredFormat:
  3023. bStructuredFormat = PtrToUlong(ParameterValue);
  3024. break;
  3025. }
  3026. return 0 ;
  3027. }
  3028. ULONG
  3029. WINAPI
  3030. GetTraceFormatParameter(
  3031. PARAMETER_TYPE Parameter ,
  3032. PVOID ParameterValue
  3033. )
  3034. {
  3035. switch (Parameter) {
  3036. case ParameterINDENT: return bIndent;
  3037. case ParameterSEQUENCE: return bSequence;
  3038. case ParameterGMT: return bGMT;
  3039. case ParameterUsePrefix: return bUsePrefix;
  3040. case ParameterStructuredFormat: return bStructuredFormat;
  3041. case ParameterInterfaceVersion: return TRACEPRT_INTERFACE_VERSION ;
  3042. }
  3043. return 0 ;
  3044. }
  3045. LPTSTR
  3046. WINAPI
  3047. GetTraceFormatSearchPath(void) {
  3048. return gTraceFormatSearchPath;
  3049. }