Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2860 lines
84 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. // %1 GUID Friendly Name string
  44. // %2 GUID SubType Name string
  45. // %3 Thread ID ULONG_PTR
  46. // %4 System Time String
  47. // %5 Kernel Time or User Time String
  48. // %6 User Time or NULL String
  49. // %7 Sequence Number LONG
  50. // %8 Unused String
  51. // %9 CPU Number LONG
  52. // %128 Indent String
  53. ULONG TimerResolution = 10;
  54. __int64 ElapseTime;
  55. ULONG UserMode = FALSE ; // TODO: Pick this up from the stream itself.
  56. #define MAXBUFS 4096
  57. #define MAXCHARS MAXBUFS/sizeof(TCHAR)
  58. #define MAXBUFS2 2 * MAXBUFS
  59. #define MAXCHARS2 MAXBUFS2/sizeof(TCHAR)
  60. #define MAXITEMS 256
  61. #define MAXINDENT 256
  62. #define MAXNAMEARG 256
  63. TCHAR ItemBuf[MAXBUFS]; // Temporary String Item buffer
  64. TCHAR ItemBBuf[MAXBUFS2]; // Item String Buffer
  65. TCHAR * pItemBuf[MAXITEMS]; // Pointer to items in the String Item Buffer.
  66. BYTE ItemRBuf[MAXBUFS]; // Item Raw Byte Buffer
  67. ULONG_PTR * pItemRBuf[MAXITEMS]; // Pointer to Raw Byte Items
  68. SIZE_T ItemRSize; // Size of Item in Raw Buffer.
  69. BOOL bItemIsString = FALSE ; // And type of item
  70. int iItemCount, i;
  71. LONG ItemsInBuf = 0;
  72. LONG ItemsInRBuf = 0;
  73. ULONG PointerSize = sizeof(PVOID) ;
  74. BYTE Event[4096];
  75. HANDLE hLibrary ;
  76. TCHAR StdPrefix[MAXSTR];
  77. TCHAR IndentBuf[MAXINDENT + 1];
  78. INT Indent;
  79. #define TRACE_FORMAT_SEARCH_PATH L"TRACE_FORMAT_SEARCH_PATH"
  80. #define TRACE_FORMAT_PREFIX L"TRACE_FORMAT_PREFIX"
  81. //#define STD_PREFIX L"[%!CPU!]%!PID!.%!TID!::%!NOW! [%!FILE!] "
  82. #define STD_PREFIX_NOSEQ L"[%9!d!]%8!04X!.%3!04X!::%4!s! [%1!s!]"
  83. #define STD_PREFIX_SEQ L"[%9!d!]%8!04X!.%3!04X!::%4!s! %7!08x! [%1!s!]"
  84. TCHAR *STD_PREFIX = STD_PREFIX_NOSEQ;
  85. BOOL bSequence = FALSE;
  86. BOOL bGmt = FALSE;
  87. BOOL bIndent = FALSE;
  88. TCHAR *pNoValueString = _T("<NoValue>");
  89. void ReplaceStringUnsafe(TCHAR* buf, TCHAR* find, TCHAR* replace)
  90. {
  91. TCHAR source[MAXCHARS], *src, *dst;
  92. int nf = _tcslen(find);
  93. int nr = _tcslen(replace);
  94. src = source;
  95. dst = buf;
  96. _tcsncpy(source, buf, MAXCHARS );
  97. for(;;) {
  98. TCHAR* p = src;
  99. for(;;) {
  100. p = _tcsstr(p, find);
  101. if (!p) goto exit_outer_loop;
  102. // special kludge not to replace
  103. // %!Someting! when it is actually %%!Something!
  104. if (p == source || p[0] != '%' || p[-1] != '%') {
  105. break;
  106. }
  107. p += nf;
  108. }
  109. memcpy(dst, src, (p-src) * sizeof(TCHAR) );
  110. dst += p-src;
  111. src = p + nf;
  112. _tcsncpy(dst, replace,(MAXCHARS - (p-source)));
  113. dst += nr;
  114. }
  115. exit_outer_loop:
  116. _tcscpy(dst, src);
  117. }
  118. TCHAR* FindValue(TCHAR* buf, TCHAR* ValueName)
  119. {
  120. static TCHAR valueBuf[256]; // largest identifier in PDB
  121. TCHAR *p = _tcsstr(buf, ValueName);
  122. TCHAR *q = p;
  123. if (p) {
  124. p += _tcslen(ValueName);
  125. q = p;
  126. while ( *p && !isspace(*p) ) ++p;
  127. memcpy(valueBuf, q, (p-q) * sizeof(TCHAR) );
  128. }
  129. valueBuf[p-q] = 0;
  130. return valueBuf;
  131. }
  132. int FindIntValue(TCHAR* buf, TCHAR* ValueName)
  133. {
  134. TCHAR* v = FindValue(buf, ValueName), *end;
  135. int sgn = 1;
  136. if (v[0] == '+') {
  137. ++v;
  138. } else if (v[0] == '-') {
  139. sgn = -1;
  140. ++v;
  141. }
  142. return sgn * _tcstol(v, &end, 10);
  143. }
  144. PMOF_INFO
  145. GetMofInfoHead(
  146. OUT PLIST_ENTRY * EventListhead,
  147. IN LPGUID pGuid,
  148. IN LPTSTR strType,
  149. IN LONG TypeIndex,
  150. IN ULONG TypeOfType,
  151. IN LPTSTR TypeFormat,
  152. IN BOOL bBestMatch
  153. );
  154. void RemoveMofInfo(PLIST_ENTRY pMofInfo);
  155. ULONG ahextoi(TCHAR *s);
  156. PTCHAR GuidToString(PTCHAR s, LPGUID piid);
  157. ULONG
  158. WINAPI
  159. GetTraceGuidsW(
  160. TCHAR * GuidFile,
  161. PLIST_ENTRY * HeadEventList
  162. );
  163. static
  164. void
  165. reduce(
  166. PCHAR Src
  167. )
  168. {
  169. char *Start = Src;
  170. if (!Src)
  171. return;
  172. while (*Src)
  173. {
  174. if ('\t' == *Src)
  175. *Src = ' ';
  176. else if (',' == *Src)
  177. *Src = ' ';
  178. else if ('\n' == *Src)
  179. *Src = ',';
  180. else if ('\r' == *Src)
  181. *Src = ' ';
  182. ++Src;
  183. }
  184. --Src;
  185. while ((Start < Src) && ((' ' == *Src) || (',' == *Src)))
  186. {
  187. *Src = 0x00;
  188. --Src;
  189. }
  190. }
  191. static void reduceW(WCHAR *Src)
  192. {
  193. WCHAR *Start = Src;
  194. if (!Src)
  195. return;
  196. while (*Src)
  197. {
  198. if (L'\t' == *Src)
  199. *Src = L' ';
  200. else if (L',' == *Src)
  201. *Src = L' ';
  202. else if (L'\n' == *Src)
  203. *Src = L',';
  204. else if (L'\r' == *Src)
  205. *Src = L' ';
  206. ++Src;
  207. }
  208. --Src;
  209. while ((Start < Src) && ((L' ' == *Src) || (L',' == *Src)))
  210. {
  211. *Src = 0x00;
  212. --Src;
  213. }
  214. }
  215. #ifdef UNICODE
  216. # define _stnprintf _snwprintf
  217. #else
  218. # define _stnprintf _snprintf
  219. #endif
  220. int FormatTimeDelta(TCHAR *buffer, size_t count, LONGLONG time)
  221. {
  222. SYSTEMTIME st;
  223. int s = 0, result;
  224. ZeroMemory(&st, sizeof(st) );
  225. if (count == 0)
  226. return -1;
  227. if (time < 0) {
  228. *buffer++ = '-';
  229. --count;
  230. time = -time;
  231. s = 1;
  232. }
  233. // Get rid of the nano and micro seconds
  234. time /= 10000;
  235. st.wMilliseconds = (USHORT)(time % 1000);
  236. time /= 1000;
  237. if (time == 0) {
  238. result = _stnprintf(buffer,count,L"%dms",st.wMilliseconds);
  239. goto end;
  240. }
  241. st.wSecond = (USHORT)(time % 60);
  242. time /= 60;
  243. st.wMinute = (USHORT)(time % 60);
  244. time /= 60;
  245. if (time == 0) {
  246. if (st.wMinute <= 10) {
  247. result = _stnprintf(buffer,count,L"%d.%03ds",st.wMinute * 60 + st.wSecond, st.wMilliseconds);
  248. } else {
  249. result = _stnprintf(buffer,count,L"%d:%d.%03ds",st.wMinute, st.wSecond, st.wMilliseconds);
  250. }
  251. goto end;
  252. }
  253. st.wHour = (USHORT)(time % 24);
  254. time /= 24;
  255. if (time == 0) {
  256. result = _stnprintf(buffer,count,L"%d:%d:%d.%03ds",st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  257. goto end;
  258. }
  259. st.wDay = (USHORT)time;
  260. result = _stnprintf(buffer,count,L"%d~%d:%d:%d.%03ds",st.wDay,st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  261. end:
  262. if (result >= 0)
  263. result += s;
  264. return result;
  265. }
  266. typedef struct _ERROR_MAP{
  267. NTSTATUS MessageId;
  268. char *SymbolicName;
  269. } ERROR_MAP;
  270. SIZE_T
  271. WINAPI
  272. FormatTraceEventW(
  273. PLIST_ENTRY HeadEventList,
  274. PEVENT_TRACE pInEvent,
  275. TCHAR *EventBuf,
  276. ULONG SizeEventBuf,
  277. TCHAR * pszMask
  278. )
  279. {
  280. PEVENT_TRACE_HEADER pHeader;
  281. PEVENT_TRACE pEvent = NULL;
  282. ULONG TraceMarker, TraceType;
  283. TCHAR tstrName[MAXSTR];
  284. TCHAR tstrType[MAXSTR];
  285. ULONG tstrTypeOfType = 0;
  286. TCHAR * tstrFormat;
  287. int iItemCount, i;
  288. SIZE_T ItemsInBuf = 0;
  289. ULONG_PTR MessageSequence = -1 ;
  290. USHORT MessageNumber = 0 ;
  291. USHORT MessageFlags = 0 ;
  292. char * pMessageData ;
  293. ULONG MessageLength ;
  294. if (pInEvent == NULL)
  295. {
  296. return (0);
  297. }
  298. pEvent = pInEvent ;
  299. // Make a copy of the PTR and length as we may adjust these depending
  300. // on the header
  301. pMessageData = pEvent->MofData ;
  302. MessageLength = pEvent->MofLength ;
  303. TraceMarker = ((PSYSTEM_TRACE_HEADER)pInEvent)->Marker;
  304. if ((TraceMarker & TRACE_MESSAGE)== TRACE_MESSAGE ) {
  305. // This handles the TRACE_MESSAGE type.
  306. TraceType = TRACE_HEADER_TYPE_MESSAGE ; // This one has special processing
  307. //
  308. // Now Process the header options
  309. //
  310. MessageNumber = ((PMESSAGE_TRACE_HEADER)pEvent)->Packet.MessageNumber ; // Message Number
  311. MessageFlags = ((PMESSAGE_TRACE_HEADER)pEvent)->Packet.OptionFlags ;
  312. // Note that the order in which these are added is critical New entries must
  313. // be added at the end!
  314. //
  315. // [First Entry] Sequence Number
  316. if (MessageFlags&TRACE_MESSAGE_SEQUENCE) {
  317. RtlCopyMemory(&MessageSequence, pMessageData, sizeof(ULONG)) ;
  318. pMessageData += sizeof(ULONG) ;
  319. MessageLength -= sizeof(ULONG);
  320. }
  321. // [Second Entry] GUID ? or CompnentID ?
  322. if (MessageFlags&TRACE_MESSAGE_COMPONENTID) {
  323. RtlCopyMemory(&pEvent->Header.Guid,pMessageData,sizeof(ULONG)) ;
  324. pMessageData += sizeof(ULONG) ;
  325. MessageLength -= sizeof(ULONG) ;
  326. } else if (MessageFlags&TRACE_MESSAGE_GUID) { // Can't have both
  327. RtlCopyMemory(&pEvent->Header.Guid,pMessageData, sizeof(GUID));
  328. pMessageData += sizeof(GUID) ;
  329. MessageLength -= sizeof(GUID);
  330. }
  331. // [Third Entry] Timestamp?
  332. if (MessageFlags&TRACE_MESSAGE_TIMESTAMP) {
  333. RtlCopyMemory(&pEvent->Header.TimeStamp,pMessageData,sizeof(LARGE_INTEGER));
  334. pMessageData += sizeof(LARGE_INTEGER);
  335. MessageLength -= sizeof(LARGE_INTEGER);
  336. }
  337. // [Fourth Entry] System Information?
  338. if (MessageFlags&TRACE_MESSAGE_SYSTEMINFO) {
  339. pHeader = (PEVENT_TRACE_HEADER) &pEvent->Header;
  340. RtlCopyMemory(&pHeader->ThreadId, pMessageData, sizeof(ULONG)) ;
  341. pMessageData += sizeof(ULONG);
  342. MessageLength -=sizeof(ULONG);
  343. RtlCopyMemory(&pHeader->ProcessId,pMessageData, sizeof(ULONG)) ;
  344. pMessageData += sizeof(ULONG);
  345. MessageLength -=sizeof(ULONG);
  346. }
  347. //
  348. // Add New Header Entries immediately before this comment!
  349. //
  350. }
  351. else
  352. {
  353. // Must be WNODE_HEADER
  354. //
  355. TraceType = 0;
  356. pEvent = pInEvent ;
  357. MessageNumber = pEvent->Header.Class.Type ;
  358. if (MessageNumber == 0xFF) { // W2K Compatability escape code
  359. if (pEvent->MofLength >= sizeof(USHORT)) {
  360. // The real Message Number is in the first USHORT
  361. memcpy(&MessageNumber,pEvent->MofData,sizeof(USHORT)) ;
  362. pMessageData += sizeof(USHORT);
  363. MessageLength -= sizeof(USHORT);
  364. }
  365. }
  366. }
  367. // Reset the Pointer and length if they have been adjusted
  368. pEvent->MofData = pMessageData ;
  369. pEvent->MofLength = MessageLength ;
  370. pHeader = (PEVENT_TRACE_HEADER) &pEvent->Header;
  371. MapGuidToName(
  372. &HeadEventList,
  373. & pEvent->Header.Guid,
  374. MessageNumber,
  375. tstrName);
  376. if ( IsEqualGUID(&pEvent->Header.Guid, &EventTraceGuid)
  377. && pEvent->Header.Class.Type == EVENT_TRACE_TYPE_INFO)
  378. {
  379. PTRACE_LOGFILE_HEADER head = (PTRACE_LOGFILE_HEADER)pEvent->MofData;
  380. if (head->TimerResolution > 0)
  381. {
  382. TimerResolution = head->TimerResolution / 10000;
  383. }
  384. ElapseTime = head->EndTime.QuadPart -
  385. pEvent->Header.TimeStamp.QuadPart;
  386. PointerSize = head->PointerSize;
  387. if (PointerSize < 2 ) // minimum is 16 bits
  388. PointerSize = 4 ; // defaults = 32 bits
  389. }
  390. if (pEvent != NULL)
  391. {
  392. PITEM_DESC pItem;
  393. char str[MAXSTR];
  394. #ifdef UNICODE
  395. TCHAR wstr[MAXSTR];
  396. #endif /* #ifdef UNICODE */
  397. PCHAR ptr = NULL;
  398. PCHAR iMofPtr = NULL;
  399. ULONG ulongword;
  400. PMOF_INFO pMofInfo = NULL;
  401. PLIST_ENTRY Head, Next;
  402. int i;
  403. pMofInfo = GetMofInfoHead(
  404. (PLIST_ENTRY *) HeadEventList,
  405. &pEvent->Header.Guid,
  406. NULL,
  407. MessageNumber,
  408. 0,
  409. NULL,
  410. TRUE);
  411. if((pMofInfo != NULL) && (pMofInfo->strType != NULL))
  412. {
  413. TCHAR* p ;
  414. _sntprintf(tstrType,MAXSTR,_T("%s"),pMofInfo->strType);
  415. tstrFormat = pMofInfo->TypeFormat; // Pointer to the format string
  416. tstrTypeOfType = pMofInfo->TypeOfType; // And the type of Format
  417. p = tstrFormat;
  418. if(p) {
  419. while (*p != 0) {
  420. if(*p == 'Z' && p > tstrFormat && p[-1] == '!' && p[1] == '!') {
  421. *p = 's';
  422. }
  423. ++p;
  424. }
  425. }
  426. }
  427. else
  428. {
  429. _sntprintf(tstrType,MAXSTR,_T("%3d"),MessageNumber);
  430. tstrFormat = NULL ;
  431. tstrTypeOfType = 0 ;
  432. }
  433. // From here on we start processing the parameters, we actually do
  434. // two versions. One is built for original #type format statements,
  435. // and everything is converted to being an ASCII string.
  436. // The other is built for #type2 format statements and everything is
  437. // converted into a string of raw bytes aliggned on a 64-bit boundary.
  438. iItemCount = 0 ; // How many Items we process
  439. for (i = 0; i < MAXITEMS; i++) // Clean up the pointers
  440. {
  441. pItemBuf[i] = pNoValueString;
  442. pItemRBuf[i] = 0 ;
  443. }
  444. RtlZeroMemory(ItemBuf, MAXBUFS * sizeof(TCHAR));
  445. RtlZeroMemory(ItemBBuf, (2*MAXBUFS) * sizeof(TCHAR));
  446. RtlZeroMemory(ItemRBuf, MAXBUFS * sizeof(BYTE));
  447. RtlZeroMemory(EventBuf, SizeEventBuf * sizeof(TCHAR));
  448. pItemBuf[iItemCount] = ItemBBuf; // Where they go (Strings)
  449. // Make Parameter %1 Type Name
  450. _sntprintf(pItemBuf[iItemCount],
  451. MAXBUFS2-ItemsInBuf,
  452. _T("%s"),
  453. tstrName);
  454. pItemBuf[iItemCount + 1] =
  455. pItemBuf[iItemCount] + _tcslen(tstrName) + 1;
  456. ItemsInBuf = ItemsInBuf + _tcslen(tstrName) + 1;
  457. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same for Raw bytes
  458. iItemCount ++;
  459. // Make Parameter %2 Type sub Type
  460. _sntprintf(pItemBuf[iItemCount],
  461. MAXBUFS2-ItemsInBuf,
  462. _T("%s"),
  463. tstrType);
  464. pItemBuf[iItemCount + 1] =
  465. pItemBuf[iItemCount] + _tcslen(tstrType) + 1;
  466. ItemsInBuf = ItemsInBuf + _tcslen(tstrType) + 1;
  467. pItemRBuf[iItemCount] = (ULONG_PTR *)pItemBuf[iItemCount]; // just use the same for raw bytes
  468. iItemCount ++;
  469. // Make Parameter %3 ThreadId
  470. RtlCopyMemory(&pItemRBuf[iItemCount] , &pHeader->ThreadId, sizeof(ULONG)) ;
  471. _sntprintf(ItemBuf,
  472. MAXBUFS,
  473. _T("0x%04X"),
  474. pItemRBuf[iItemCount]);
  475. _sntprintf(pItemBuf[iItemCount],
  476. MAXBUFS2-ItemsInBuf,
  477. _T("0x%04X"),
  478. pHeader->ThreadId);
  479. pItemBuf[iItemCount + 1] =
  480. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  481. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  482. iItemCount++;
  483. // Make Parameter %4 System Time
  484. if (tstrFormat != NULL)
  485. {
  486. FILETIME stdTime, localTime;
  487. SYSTEMTIME sysTime;
  488. stdTime.dwHighDateTime = pEvent->Header.TimeStamp.HighPart;
  489. stdTime.dwLowDateTime = pEvent->Header.TimeStamp.LowPart;
  490. if (bGmt) {
  491. FileTimeToSystemTime(&stdTime, &sysTime);
  492. } else {
  493. FileTimeToLocalFileTime(&stdTime, &localTime);
  494. FileTimeToSystemTime(&localTime, &sysTime);
  495. }
  496. _sntprintf(ItemBuf,
  497. MAXBUFS,
  498. _T("%02d/%02d/%04d-%02d:%02d:%02d.%03d"),
  499. sysTime.wMonth,
  500. sysTime.wDay,
  501. sysTime.wYear,
  502. sysTime.wHour,
  503. sysTime.wMinute,
  504. sysTime.wSecond,
  505. sysTime.wMilliseconds);
  506. }
  507. else
  508. {
  509. _sntprintf(ItemBuf, MAXBUFS, _T("%20I64u"), pHeader->TimeStamp.QuadPart);
  510. }
  511. _sntprintf(pItemBuf[iItemCount],
  512. MAXBUFS2-ItemsInBuf,
  513. _T("%s"),
  514. ItemBuf);
  515. pItemBuf[iItemCount + 1] =
  516. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  517. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  518. pItemRBuf[iItemCount] = (ULONG_PTR *)pItemBuf[iItemCount]; // just use the same
  519. iItemCount ++;
  520. if (!UserMode)
  521. {
  522. // Make Parameter %5 Kernel Time
  523. _sntprintf(ItemBuf,
  524. MAXBUFS,
  525. _T("%8lu"),
  526. pHeader->KernelTime * TimerResolution);
  527. _sntprintf(pItemBuf[iItemCount],
  528. MAXBUFS2-ItemsInBuf,
  529. _T("%s"),
  530. ItemBuf);
  531. pItemBuf[iItemCount + 1] =
  532. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  533. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  534. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  535. iItemCount ++;
  536. // Make Parameter %6 User Time
  537. _sntprintf(ItemBuf,
  538. MAXBUFS,
  539. _T("%8lu"),
  540. pHeader->UserTime * TimerResolution);
  541. _sntprintf(pItemBuf[iItemCount],
  542. MAXBUFS2-ItemsInBuf,
  543. _T("%s"),
  544. ItemBuf);
  545. pItemBuf[iItemCount + 1] =
  546. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  547. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  548. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  549. iItemCount ++;
  550. }
  551. else
  552. {
  553. // Make Parameter %5 processor Time
  554. _sntprintf(ItemBuf, MAXBUFS,_T("%I64u"), pHeader->ProcessorTime);
  555. _sntprintf(pItemBuf[iItemCount], MAXBUFS2-ItemsInBuf,_T("%s"), ItemBuf);
  556. pItemBuf[iItemCount + 1] =
  557. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  558. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  559. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  560. iItemCount ++;
  561. // Make Parameter %6 NULL
  562. _sntprintf(ItemBuf, MAXBUFS, _T("%s"), NULL);
  563. _sntprintf(pItemBuf[iItemCount], MAXBUFS2-ItemsInBuf,_T("%s"), ItemBuf);
  564. pItemBuf[iItemCount + 1] =
  565. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  566. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  567. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount]; // just use the same
  568. iItemCount ++;
  569. }
  570. // Make Parameter %7 Sequence Number
  571. _sntprintf(ItemBuf, MAXBUFS, _T("%d"), MessageSequence);
  572. _sntprintf(pItemBuf[iItemCount], MAXBUFS2-ItemsInBuf, _T("%s"), ItemBuf);
  573. pItemBuf[iItemCount + 1] =
  574. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  575. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  576. pItemRBuf[iItemCount] = (ULONG_PTR *) MessageSequence ; // Raw just point at the value
  577. iItemCount ++;
  578. // Make Parameter %8 ProcessId
  579. RtlCopyMemory(&pItemRBuf[iItemCount],&pHeader->ProcessId,sizeof(ULONG));
  580. _sntprintf(ItemBuf,MAXBUFS,_T("0x%04X"),pItemRBuf[iItemCount]);
  581. _sntprintf(pItemBuf[iItemCount], MAXBUFS2-ItemsInBuf, _T("%s"), ItemBuf);
  582. pItemBuf[iItemCount + 1] =
  583. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  584. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  585. iItemCount ++;
  586. // Make Parameter %9 CPU Number
  587. _sntprintf(ItemBuf,
  588. MAXBUFS,
  589. _T("%d"),
  590. ((PWMI_CLIENT_CONTEXT)&(pEvent->ClientContext))->ProcessorNumber);
  591. _sntprintf(pItemBuf[iItemCount], MAXBUFS2-ItemsInBuf, _T("%s"), ItemBuf);
  592. pItemBuf[iItemCount + 1] =
  593. pItemBuf[iItemCount] + _tcslen(ItemBuf) + 1;
  594. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + 1;
  595. pItemRBuf[iItemCount] = (ULONG_PTR *) (((PWMI_CLIENT_CONTEXT)&(pEvent->ClientContext))->ProcessorNumber) ;
  596. iItemCount ++;
  597. // Done processing Parameters
  598. if (pMofInfo != NULL) {
  599. Head = pMofInfo->ItemHeader;
  600. pMofInfo->EventCount ++;
  601. Next = Head->Flink;
  602. } else {
  603. Head = Next = NULL ;
  604. }
  605. __try {
  606. iMofPtr = (char *) malloc(pEvent->MofLength + sizeof(UNICODE_NULL));
  607. if(iMofPtr == NULL)
  608. return -1;
  609. RtlZeroMemory(iMofPtr, pEvent->MofLength + sizeof(UNICODE_NULL));
  610. RtlCopyMemory(iMofPtr, pEvent->MofData, pEvent->MofLength);
  611. ptr = iMofPtr;
  612. while (Head != Next)
  613. {
  614. ULONG * ULongPtr = (ULONG *) & ItemRBuf[0];
  615. USHORT * UShortPtr = (USHORT *) & ItemRBuf[0];
  616. LONGLONG * LongLongPtr = (LONGLONG *) & ItemRBuf[0];
  617. ULONGLONG * ULongLongPtr = (ULONGLONG *) & ItemRBuf[0];
  618. double * DoublePtr = (double *) & ItemRBuf[0];
  619. TCHAR * PtrFmt1, * PtrFmt2 ;
  620. pItem = CONTAINING_RECORD(Next, ITEM_DESC, Entry);
  621. if ((ULONG) (ptr - iMofPtr) >= pEvent->MofLength)
  622. {
  623. break;
  624. }
  625. RtlZeroMemory(ItemBuf, MAXBUFS * sizeof(TCHAR));
  626. RtlZeroMemory(ItemRBuf, MAXBUFS * sizeof(BYTE));
  627. bItemIsString = FALSE ; // Assume its a RAW value
  628. ItemRSize = 0 ; // Raw length of zero
  629. switch (pItem->ItemType)
  630. {
  631. case ItemChar:
  632. case ItemUChar:
  633. ItemRSize = sizeof(CHAR);
  634. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  635. _sntprintf(ItemBuf, MAXBUFS, _T("%c"), ItemRBuf);
  636. ptr += ItemRSize;
  637. break;
  638. case ItemCharSign:
  639. ItemRSize = sizeof(CHAR) * 2;
  640. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  641. ItemRBuf[2] = '\0';
  642. _sntprintf(ItemBuf, MAXBUFS,_T("\"%s\""), ItemRBuf);
  643. ptr += ItemRSize;
  644. break;
  645. case ItemCharShort:
  646. ItemRSize = sizeof(CHAR);
  647. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  648. _sntprintf(ItemBuf, MAXBUFS, _T("%d"), * ItemRBuf);
  649. ptr += ItemRSize;
  650. break;
  651. case ItemShort:
  652. ItemRSize = sizeof(USHORT);
  653. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  654. _sntprintf(ItemBuf,MAXBUFS, _T("%6d"), * UShortPtr);
  655. ptr += ItemRSize;
  656. break;
  657. case ItemDouble:
  658. ItemRSize = sizeof(double);
  659. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  660. _sntprintf(ItemBuf,MAXBUFS, _T("%g"), * DoublePtr);
  661. ptr += ItemRSize;
  662. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  663. break;
  664. case ItemUShort:
  665. ItemRSize = sizeof(USHORT);
  666. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  667. _sntprintf(ItemBuf,MAXBUFS, _T("%6u"), * UShortPtr);
  668. ptr += ItemRSize;
  669. break;
  670. case ItemLong:
  671. ItemRSize = sizeof(LONG);
  672. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  673. _sntprintf(ItemBuf,MAXBUFS, _T("%8l"), (LONG) * ULongPtr);
  674. ptr += ItemRSize;
  675. break;
  676. case ItemULong:
  677. ItemRSize = sizeof(ULONG);
  678. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  679. _sntprintf(ItemBuf,MAXBUFS, _T("%8lu"), * ULongPtr);
  680. ptr += ItemRSize;
  681. break;
  682. case ItemULongX:
  683. ItemRSize = sizeof(ULONG);
  684. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  685. _sntprintf(ItemBuf,MAXBUFS, _T("0x%08X"), * ULongPtr);
  686. ptr += ItemRSize;
  687. break;
  688. case ItemPtr :
  689. PtrFmt2 = _T("%08X%08X") ;
  690. PtrFmt1 = _T("%08X") ;
  691. // goto ItemPtrCommon ;
  692. //ItemPtrCommon:
  693. {
  694. ULONG ulongword2;
  695. if (PointerSize == 8) { // 64 bits
  696. RtlCopyMemory(&ulongword,ptr,4);
  697. RtlCopyMemory(&ulongword2,ptr+4,4);
  698. _sntprintf(ItemBuf,MAXBUFS, PtrFmt2 , ulongword2,ulongword);
  699. }
  700. else { // assumes 32 bit otherwise
  701. RtlCopyMemory(&ulongword,ptr,PointerSize);
  702. _sntprintf(ItemBuf,MAXBUFS, PtrFmt1 , ulongword);
  703. }
  704. ItemRSize = 0 ; // Pointers are always co-erced to be strings
  705. ptr += PointerSize;
  706. }
  707. break;
  708. case ItemIPAddr:
  709. ItemRSize = 0; // Only String form exists
  710. memcpy(&ulongword, ptr, sizeof(ULONG));
  711. // Convert it to readable form
  712. //
  713. _sntprintf(
  714. ItemBuf, MAXBUFS,
  715. _T("%03d.%03d.%03d.%03d"),
  716. (ulongword >> 0) & 0xff,
  717. (ulongword >> 8) & 0xff,
  718. (ulongword >> 16) & 0xff,
  719. (ulongword >> 24) & 0xff);
  720. ptr += sizeof (ULONG);
  721. break;
  722. case ItemPort:
  723. ItemRSize = 0; // Only String form exists
  724. _sntprintf(ItemBuf,MAXBUFS, _T("%u"), (UCHAR)ptr[0] * 256 + (UCHAR)ptr[1] * 1);
  725. ptr += sizeof (USHORT);
  726. break;
  727. case ItemLongLong:
  728. ItemRSize = sizeof(LONGLONG);
  729. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  730. _sntprintf(ItemBuf,MAXBUFS, _T("%16I64x"), *LongLongPtr);
  731. ptr += sizeof(LONGLONG);
  732. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  733. break;
  734. case ItemULongLong:
  735. ItemRSize = sizeof(ULONGLONG);
  736. RtlCopyMemory(ItemRBuf, ptr, ItemRSize);
  737. _sntprintf(ItemBuf,MAXBUFS, _T("%16I64x"), *ULongLongPtr);
  738. ptr += sizeof(ULONGLONG);
  739. ItemRSize = 0; // FormatMessage cannot print 8 byte stuff properly on x86
  740. break;
  741. case ItemString:
  742. case ItemRString:
  743. {
  744. SIZE_T pLen = strlen((CHAR *) ptr);
  745. if (pLen > 0)
  746. {
  747. strcpy(str, ptr);
  748. if (pItem->ItemType == ItemRString)
  749. {
  750. reduce(str);
  751. }
  752. #ifdef UNICODE
  753. MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, MAXSTR);
  754. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  755. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), wstr);
  756. #else
  757. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), str);
  758. _stprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s"), str);
  759. #endif /* #ifdef UNICODE */
  760. } else {
  761. _sntprintf(ItemBuf,MAXBUFS,_T("<NULL>"));
  762. _sntprintf((TCHAR *)ItemRBuf,MAXCHARS,_T("<NULL>"));
  763. }
  764. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  765. bItemIsString = TRUE;
  766. ptr += (pLen + 1);
  767. break;
  768. }
  769. case ItemRWString:
  770. case ItemWString:
  771. {
  772. size_t pLen = 0;
  773. size_t i;
  774. if (*(WCHAR *) ptr)
  775. {
  776. if (pItem->ItemType == ItemRWString)
  777. {
  778. reduceW((WCHAR *) ptr);
  779. }
  780. pLen = ((wcslen((WCHAR*)ptr) + 1) * sizeof(WCHAR));
  781. memcpy(wstr, ptr, pLen);
  782. for (i = 0; i < pLen / 2; i++)
  783. {
  784. if (((USHORT) wstr[i] == (USHORT) 0xFFFF))
  785. {
  786. wstr[i] = (USHORT) 0;
  787. }
  788. }
  789. wstr[pLen / 2] = wstr[(pLen / 2) + 1]= '\0';
  790. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  791. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), wstr);
  792. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  793. bItemIsString = TRUE;
  794. }
  795. ptr += pLen; // + sizeof(ULONG);
  796. break;
  797. }
  798. case ItemDSString: // Counted String
  799. {
  800. USHORT pLen = 256 * ((USHORT) * ptr) + ((USHORT) * (ptr + 1));
  801. ptr += sizeof(USHORT);
  802. if (pLen > 0)
  803. {
  804. strcpy(str, ptr);
  805. #ifdef UNICODE
  806. MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, MAXSTR);
  807. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  808. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), wstr);
  809. #else
  810. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), str);
  811. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), str);
  812. #endif
  813. }
  814. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  815. bItemIsString = TRUE;
  816. ptr += (pLen + 1);
  817. break;
  818. }
  819. case ItemPString: // Counted String
  820. {
  821. USHORT pLen = 256 * ((USHORT) * ptr) + ((USHORT) * (ptr + 1));
  822. ptr += sizeof(USHORT);
  823. if (pLen > 0)
  824. {
  825. strcpy(str, ptr);
  826. #ifdef UNICODE
  827. MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, MAXSTR);
  828. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  829. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%ws"), wstr);
  830. #else
  831. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), str);
  832. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), str);
  833. #endif /* #ifdef UNICODE */
  834. } else {
  835. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  836. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  837. }
  838. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  839. bItemIsString = TRUE;
  840. ptr += (pLen + 1);
  841. break;
  842. }
  843. case ItemDSWString: // DS Counted Wide Strings
  844. case ItemPWString: // Counted Wide Strings
  845. {
  846. USHORT pLen = ( pItem->ItemType == ItemDSWString)
  847. ? (256 * ((USHORT) * ptr) + ((USHORT) * (ptr + 1)))
  848. : (* ((USHORT *) ptr));
  849. ptr += sizeof(USHORT);
  850. if (pLen > MAXSTR * sizeof(WCHAR))
  851. {
  852. pLen = MAXSTR * sizeof(WCHAR);
  853. }
  854. if (pLen > 0)
  855. {
  856. memcpy(wstr, ptr, pLen);
  857. wstr[pLen / sizeof(WCHAR)] = L'\0';
  858. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  859. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), wstr);
  860. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  861. bItemIsString = TRUE;
  862. }
  863. ptr += pLen;
  864. break;
  865. }
  866. case ItemNWString: // Non Null Terminated String
  867. {
  868. USHORT Size;
  869. Size = (USHORT) (pEvent->MofLength -
  870. (ULONG) (ptr - iMofPtr));
  871. if (Size > MAXSTR )
  872. {
  873. Size = MAXSTR;
  874. }
  875. if (Size > 0)
  876. {
  877. memcpy(wstr, ptr, Size);
  878. wstr[Size / 2] = '\0';
  879. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  880. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), wstr);
  881. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  882. bItemIsString = TRUE;
  883. }
  884. ptr += Size;
  885. break;
  886. }
  887. case ItemMLString: // Multi Line String
  888. {
  889. SIZE_T pLen;
  890. char * src, * dest;
  891. BOOL inQ = FALSE;
  892. BOOL skip = FALSE;
  893. UINT lineCount = 0;
  894. ptr += sizeof(UCHAR) * 2;
  895. pLen = strlen(ptr);
  896. if (pLen > 0)
  897. {
  898. src = ptr;
  899. dest = str;
  900. while (*src != '\0')
  901. {
  902. if (*src == '\n')
  903. {
  904. if (!lineCount)
  905. {
  906. * dest ++ = ' ';
  907. }
  908. lineCount ++;
  909. }
  910. else if (*src == '\"')
  911. {
  912. if (inQ)
  913. {
  914. char strCount[32];
  915. char * cpy;
  916. sprintf(strCount, "{%dx}", lineCount);
  917. cpy = &strCount[0];
  918. while (*cpy != '\0')
  919. {
  920. * dest ++ = * cpy ++;
  921. }
  922. }
  923. inQ = !inQ;
  924. }
  925. else if (!skip)
  926. {
  927. *dest++ = *src;
  928. }
  929. skip = (lineCount > 1 && inQ);
  930. src++;
  931. }
  932. *dest = '\0';
  933. #ifdef UNICODE
  934. MultiByteToWideChar(CP_ACP, 0, str, -1, wstr, MAXSTR);
  935. _sntprintf(ItemBuf,MAXBUFS, _T("\"%ws\""), wstr);
  936. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%ws"), wstr);
  937. #else
  938. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), str);
  939. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), str);
  940. #endif /* #ifdef UNICODE */
  941. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  942. bItemIsString = TRUE;
  943. }
  944. ptr += (pLen);
  945. break;
  946. }
  947. case ItemSid:
  948. {
  949. TCHAR UserName[64];
  950. TCHAR Domain[64];
  951. TCHAR FullName[256];
  952. ULONG asize = 0;
  953. ULONG bsize = 0;
  954. ULONG Sid[64];
  955. PULONG pSid = &Sid[0];
  956. SID_NAME_USE Se;
  957. ULONG nSidLength;
  958. pSid = (PULONG) ptr;
  959. if (* pSid == 0)
  960. {
  961. ptr += 4;
  962. _sntprintf(ItemBuf,MAXBUFS, _T("<NULL>"));
  963. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("<NULL>"));
  964. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR) ;
  965. bItemIsString = TRUE ;
  966. }
  967. else
  968. {
  969. ptr += 8; // skip the TOKEN_USER structure
  970. nSidLength = 8 + (4 * ptr[1]);
  971. asize = 64;
  972. bsize = 64;
  973. // LookupAccountSid cannot accept asize, bsize as size_t
  974. if (LookupAccountSid(
  975. NULL,
  976. (PSID) ptr,
  977. (LPTSTR) & UserName[0],
  978. &asize,
  979. (LPTSTR) & Domain[0],
  980. & bsize,
  981. & Se))
  982. {
  983. LPTSTR pFullName = & FullName[0];
  984. _tcscpy(pFullName, _T("\\\\"));
  985. _tcscat(pFullName, Domain);
  986. _tcscat(pFullName, _T("\\"));
  987. _tcscat(pFullName, UserName);
  988. asize = (ULONG) _tcslen(pFullName); // Truncate here
  989. if (asize > 0)
  990. {
  991. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), pFullName);
  992. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS, _T("%s"), pFullName);
  993. }
  994. }
  995. else
  996. {
  997. LPTSTR sidStr;
  998. if ( ConvertSidToStringSid(pSid, &sidStr) ) {
  999. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s\""), sidStr); // BUGBUG check size
  1000. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s"), sidStr);
  1001. } else {
  1002. _sntprintf(ItemBuf,MAXBUFS, _T("\"%s(%d)\""), _T("System"), GetLastError() );
  1003. _sntprintf((TCHAR *)ItemRBuf, MAXCHARS,_T("%s(%d)"), _T("System"), GetLastError() );
  1004. }
  1005. }
  1006. SetLastError(ERROR_SUCCESS);
  1007. ItemRSize = _tcslen((TCHAR *)ItemRBuf) * sizeof(TCHAR);
  1008. bItemIsString = TRUE;
  1009. ptr += nSidLength;
  1010. }
  1011. break;
  1012. }
  1013. case ItemChar4:
  1014. ItemRSize = 4 * sizeof(TCHAR);
  1015. _sntprintf(ItemBuf,MAXBUFS,
  1016. _T("%c%c%c%c"),
  1017. ptr[0], ptr[1], ptr[2], ptr[3]);
  1018. ptr += ItemRSize ;
  1019. _tcscpy((LPTSTR)ItemRBuf, ItemBuf);
  1020. bItemIsString = TRUE;
  1021. break;
  1022. case ItemCharHidden:
  1023. ItemRSize = 0 ;
  1024. _stscanf(pItem->ItemList,_T("%d"),&ItemRSize);
  1025. if (ItemRSize > MAXBUFS) {
  1026. ItemRSize = MAXBUFS ;
  1027. }
  1028. _tprintf(_T("size is %d\n"),ItemRSize);
  1029. RtlCopyMemory(ItemBuf,ptr,ItemRSize);
  1030. ptr += ItemRSize ;
  1031. bItemIsString = TRUE ;
  1032. break;
  1033. case ItemSetByte:
  1034. ItemRSize = sizeof(BYTE);
  1035. goto ItemSetCommon;
  1036. case ItemSetShort:
  1037. ItemRSize = sizeof(USHORT);
  1038. goto ItemSetCommon;
  1039. case ItemSetLong:
  1040. ItemRSize = sizeof(ULONG);
  1041. // goto ItemSetCommon;
  1042. ItemSetCommon:
  1043. {
  1044. TCHAR * name;
  1045. ULONG Countr = 0;
  1046. ULONG ItemMask = 0;
  1047. TCHAR iList[MAXBUFS];
  1048. BOOL first = TRUE;
  1049. RtlCopyMemory(&ItemMask, ptr, ItemRSize);
  1050. ptr += ItemRSize;
  1051. _tcscpy(ItemBuf, _T("["));
  1052. _sntprintf(iList, MAXBUFS,_T("%s"), pItem->ItemList);
  1053. name = _tcstok(iList, _T(","));
  1054. while( name != NULL )
  1055. {
  1056. // While there are tokens in "string"
  1057. //
  1058. if (ItemMask & (1 << Countr) )
  1059. {
  1060. if (!first) _tcscat(ItemBuf, _T(","));
  1061. _tcscat(ItemBuf, name); first = FALSE;
  1062. }
  1063. // Get next token:
  1064. //
  1065. name = _tcstok( NULL, _T(","));
  1066. Countr++;
  1067. }
  1068. while (Countr < ItemRSize * 8) {
  1069. if (ItemMask & (1 << Countr) )
  1070. {
  1071. TCHAR smallBuf[20];
  1072. _sntprintf(smallBuf, 20, _T("%d"),Countr);
  1073. if (!first) _tcscat(ItemBuf, _T(","));
  1074. _tcscat(ItemBuf, smallBuf); first = FALSE;
  1075. }
  1076. Countr++;
  1077. }
  1078. _tcscat(ItemBuf, _T("]") );
  1079. ItemRSize = 0; // Strings will be the same Raw and otherwise
  1080. }
  1081. break;
  1082. case ItemListByte:
  1083. ItemRSize = sizeof(BYTE);
  1084. goto ItemListCommon;
  1085. case ItemListShort:
  1086. ItemRSize = sizeof(USHORT);
  1087. goto ItemListCommon;
  1088. case ItemListLong:
  1089. ItemRSize = sizeof(ULONG);
  1090. // goto ItemListCommon;
  1091. ItemListCommon:
  1092. {
  1093. TCHAR * name;
  1094. ULONG Countr = 0;
  1095. ULONG ItemIndex = 0;
  1096. TCHAR iList[MAXBUFS];
  1097. RtlCopyMemory(&ItemIndex, ptr, ItemRSize);
  1098. ptr += ItemRSize;
  1099. ItemRSize = 0; // Strings will be the same Raw and otherwise
  1100. _sntprintf(ItemBuf,MAXBUFS, _T("!%X!"),ItemIndex);
  1101. _sntprintf(iList, MAXBUFS, _T("%s"), pItem->ItemList);
  1102. name = _tcstok(iList, _T(","));
  1103. while( name != NULL )
  1104. {
  1105. // While there are tokens in "string"
  1106. //
  1107. if (ItemIndex == Countr ++)
  1108. {
  1109. _sntprintf(ItemBuf,MAXBUFS, _T("%s"), name);
  1110. break;
  1111. }
  1112. // Get next token:
  1113. //
  1114. name = _tcstok( NULL, _T(","));
  1115. }
  1116. }
  1117. break;
  1118. case ItemNTerror:
  1119. ItemRSize = 0; // Only string form exists
  1120. RtlCopyMemory(ItemRBuf, ptr, sizeof(ULONG));
  1121. ptr += sizeof(ULONG);
  1122. // Translate the NT Error Message
  1123. if ((FormatMessage(
  1124. FORMAT_MESSAGE_FROM_SYSTEM |
  1125. FORMAT_MESSAGE_IGNORE_INSERTS,
  1126. NULL,
  1127. *ULongPtr,
  1128. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1129. ItemBuf,
  1130. MAXBUFS,
  1131. NULL )) == 0) {
  1132. _sntprintf(ItemBuf,MAXBUFS,_T("!NT Error %d unrecognised!"),*ULongPtr);
  1133. }
  1134. break;
  1135. case ItemMerror:
  1136. ItemRSize = 0; // Only string form exists
  1137. RtlCopyMemory(ItemRBuf, ptr, sizeof(ULONG));
  1138. ptr += sizeof(ULONG);
  1139. // Translate the Module Message
  1140. if (pItem->ItemList == NULL) {
  1141. _sntprintf(ItemBuf,MAXBUFS,_T("! Error %d No Module Name!"),*ULongPtr);
  1142. } else {
  1143. if ((hLibrary = LoadLibraryEx(
  1144. pItem->ItemList, // file name of module
  1145. NULL, // reserved, must be NULL
  1146. LOAD_LIBRARY_AS_DATAFILE // entry-point execution flag
  1147. )) == NULL) {
  1148. _sntprintf(ItemBuf,MAXBUFS,_T("!ItemMerror %d : LoadLibrary of %s failed %d!"),
  1149. *ULongPtr,
  1150. pItem->ItemList,
  1151. GetLastError());
  1152. } else {
  1153. if ((FormatMessage(
  1154. FORMAT_MESSAGE_FROM_HMODULE |
  1155. FORMAT_MESSAGE_FROM_SYSTEM |
  1156. FORMAT_MESSAGE_IGNORE_INSERTS,
  1157. hLibrary,
  1158. *ULongPtr,
  1159. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1160. ItemBuf,
  1161. MAXBUFS,
  1162. NULL )) == 0) {
  1163. _sntprintf(ItemBuf,MAXBUFS,_T("!Module Error %d unrecognised!"),*ULongPtr);
  1164. }
  1165. if (!FreeLibrary(hLibrary)) {
  1166. _sntprintf(ItemBuf,MAXBUFS,_T("Failed to free library (%s) handle, err = %d"),
  1167. pItem->ItemList, GetLastError());
  1168. }
  1169. }
  1170. }
  1171. break;
  1172. case ItemHRESULT:
  1173. {
  1174. NTSTATUS TempNTSTATUS, Error ;
  1175. ItemRSize = 0 ;
  1176. RtlCopyMemory(&TempNTSTATUS, ptr, sizeof(NTSTATUS));
  1177. ptr += sizeof(ULONG);
  1178. Error = TempNTSTATUS;
  1179. if (TempNTSTATUS == 0) { // Special case STATUS_SUCCESS just like everyone else!
  1180. _sntprintf(ItemBuf,MAXBUFS,_T("S_OK"));
  1181. } else {
  1182. const ERROR_MAP* map = (ERROR_MAP*)winerrorSymbolicNames;
  1183. _sntprintf(ItemBuf,MAXBUFS,_T("HRESULT=%8X"),TempNTSTATUS);
  1184. if( FACILITY_NT_BIT & TempNTSTATUS ) {
  1185. map = (ERROR_MAP*)ntstatusSymbolicNames;
  1186. Error &= ~FACILITY_NT_BIT;
  1187. } else if (HRESULT_FACILITY(Error) == FACILITY_WIN32) {
  1188. Error &= 0xFFFF;
  1189. }
  1190. while (map->MessageId != 0xFFFFFFFF) {
  1191. if (map->MessageId == Error) {
  1192. _sntprintf(ItemBuf,MAXBUFS,_T("0x%08x(%S)"), TempNTSTATUS, map->SymbolicName);
  1193. break;
  1194. }
  1195. ++map;
  1196. }
  1197. }
  1198. }
  1199. break;
  1200. case ItemNTSTATUS:
  1201. {
  1202. int i = 0 ;
  1203. NTSTATUS TempNTSTATUS ;
  1204. ItemRSize = 0 ;
  1205. RtlCopyMemory(&TempNTSTATUS, ptr, sizeof(NTSTATUS));
  1206. ptr += sizeof(ULONG);
  1207. if (TempNTSTATUS == 0) { // Special case STATUS_SUCCESS just like everyone else!
  1208. _sntprintf(ItemBuf,MAXBUFS,_T("STATUS_SUCCESS"));
  1209. } else {
  1210. _sntprintf(ItemBuf,MAXBUFS,_T("NTSTATUS=%8X"),TempNTSTATUS);
  1211. while (ntstatusSymbolicNames[i].MessageId != 0xFFFFFFFF) {
  1212. if (ntstatusSymbolicNames[i].MessageId == TempNTSTATUS) {
  1213. _sntprintf(ItemBuf,MAXBUFS,_T("0x%08x(%S)"), TempNTSTATUS, ntstatusSymbolicNames[i].SymbolicName);
  1214. break;
  1215. }
  1216. i++ ;
  1217. }
  1218. }
  1219. }
  1220. break;
  1221. case ItemWINERROR:
  1222. {
  1223. int i = 0 ;
  1224. DWORD TempWINERROR ;
  1225. ItemRSize = 0 ;
  1226. RtlCopyMemory(&TempWINERROR, ptr, sizeof(DWORD));
  1227. ptr += sizeof(ULONG);
  1228. _sntprintf(ItemBuf,MAXBUFS,_T("WINERROR=%8X"),TempWINERROR);
  1229. while (winerrorSymbolicNames[i].MessageId != 0xFFFFFFFF) {
  1230. if (winerrorSymbolicNames[i].MessageId == TempWINERROR) {
  1231. _sntprintf(ItemBuf,MAXBUFS,_T("%d(%S)"), TempWINERROR, winerrorSymbolicNames[i].SymbolicName);
  1232. break;
  1233. }
  1234. i++ ;
  1235. }
  1236. }
  1237. break;
  1238. case ItemNETEVENT:
  1239. {
  1240. int i = 0 ;
  1241. DWORD TempNETEVENT ;
  1242. ItemRSize = 0 ;
  1243. RtlCopyMemory(&TempNETEVENT, ptr, sizeof(DWORD));
  1244. ptr += sizeof(ULONG);
  1245. _sntprintf(ItemBuf,MAXBUFS,_T("NETEVENT=%8X"),TempNETEVENT);
  1246. while (neteventSymbolicNames[i].MessageId != 0xFFFFFFFF) {
  1247. if (neteventSymbolicNames[i].MessageId == TempNETEVENT) {
  1248. _sntprintf(ItemBuf,MAXBUFS,_T("%S"), neteventSymbolicNames[i].SymbolicName);
  1249. break;
  1250. }
  1251. i++ ;
  1252. }
  1253. }
  1254. break;
  1255. case ItemGuid:
  1256. GuidToString(ItemBuf, (LPGUID) ptr);
  1257. ItemRSize = 0; // Only string form exists
  1258. ptr += sizeof(GUID);
  1259. break;
  1260. case ItemTimeDelta:
  1261. {
  1262. LONGLONG time;
  1263. RtlCopyMemory(&time, ptr, sizeof(time));
  1264. FormatTimeDelta(ItemBuf, MAXBUFS, time);
  1265. ItemRSize = 0; // Only string form exists
  1266. ptr += sizeof(LONGLONG);
  1267. }
  1268. break;
  1269. case ItemWaitTime:
  1270. {
  1271. LONGLONG time;
  1272. RtlCopyMemory(&time, ptr, sizeof(time));
  1273. if (time <= 0) {
  1274. time = -time;
  1275. ItemBuf[0]='+';
  1276. FormatTimeDelta(ItemBuf+1, MAXBUFS-1, time);
  1277. ItemRSize = 0; // Only string form exists
  1278. ptr += sizeof(LONGLONG);
  1279. break;
  1280. }
  1281. // Fall thru
  1282. }
  1283. case ItemTimestamp:
  1284. {
  1285. LARGE_INTEGER LargeTmp;
  1286. FILETIME stdTime, localTime;
  1287. SYSTEMTIME sysTime;
  1288. RtlCopyMemory(&LargeTmp, ptr, sizeof(ULONGLONG));
  1289. stdTime.dwHighDateTime = LargeTmp.HighPart;
  1290. stdTime.dwLowDateTime = LargeTmp.LowPart;
  1291. if (!FileTimeToLocalFileTime(&stdTime, &localTime)) {
  1292. _sntprintf(ItemBuf,MAXBUFS,_T("FileTimeToLocalFileTime error 0x%8X\n"),GetLastError());
  1293. break;
  1294. }
  1295. if (!FileTimeToSystemTime(&localTime, &sysTime)){
  1296. _sntprintf(ItemBuf,MAXBUFS,_T("FileTimeToSystemTime error 0x%8X\n"),GetLastError());
  1297. break;
  1298. }
  1299. _sntprintf(ItemBuf,MAXBUFS, _T("%02d/%02d/%04d-%02d:%02d:%02d.%03d"),
  1300. sysTime.wMonth,
  1301. sysTime.wDay,
  1302. sysTime.wYear,
  1303. sysTime.wHour,
  1304. sysTime.wMinute,
  1305. sysTime.wSecond,
  1306. sysTime.wMilliseconds);
  1307. }
  1308. ItemRSize = 0; // Only string form exists
  1309. ptr += sizeof(ULONGLONG);
  1310. break;
  1311. default:
  1312. ptr += sizeof (int);
  1313. }
  1314. _sntprintf(pItemBuf[iItemCount],
  1315. MAXBUFS2-ItemsInBuf,
  1316. _T("%s"),
  1317. ItemBuf);
  1318. pItemBuf[iItemCount + 1] =
  1319. pItemBuf[iItemCount] + _tcslen(ItemBuf) + sizeof(TCHAR);
  1320. ItemsInBuf = ItemsInBuf + _tcslen(ItemBuf) + sizeof(TCHAR);
  1321. if (ItemRSize == 0) {
  1322. // Raw and String are the same
  1323. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount];
  1324. }
  1325. else
  1326. {
  1327. if (ItemRSize > MAXBUFS) {
  1328. ItemRSize = MAXBUFS ;
  1329. }
  1330. pItemRBuf[iItemCount] =pItemRBuf[iItemCount+1] = 0 ;
  1331. if (!bItemIsString) {
  1332. RtlCopyMemory(&pItemRBuf[iItemCount],ItemRBuf,ItemRSize);
  1333. } else {
  1334. // Share scratch buffer
  1335. if ((LONG)(ItemsInBuf+ItemRSize+sizeof(TCHAR)) < (LONG)sizeof(ItemBBuf)) {
  1336. (TCHAR *)pItemRBuf[iItemCount] = pItemBuf[iItemCount+1] ;
  1337. RtlCopyMemory(pItemRBuf[iItemCount],ItemRBuf,ItemRSize);
  1338. pItemBuf[iItemCount+1] =(TCHAR *)pItemRBuf[iItemCount] + ItemRSize + sizeof(TCHAR) ;
  1339. ItemsInBuf = ItemsInBuf + ItemRSize + sizeof(TCHAR) ;
  1340. } else {
  1341. pItemRBuf[iItemCount] = (ULONG_PTR *) pItemBuf[iItemCount];
  1342. }
  1343. }
  1344. }
  1345. iItemCount ++;
  1346. Next = Next->Flink;
  1347. }
  1348. // Ok we are finished with the MofData
  1349. //
  1350. free(iMofPtr);
  1351. }
  1352. __except(EXCEPTION_EXECUTE_HANDLER)
  1353. {
  1354. _sntprintf(EventBuf,
  1355. SizeEventBuf,
  1356. _T("\n*****FormatMessage %s of %s, parameter %d raised an exception*****\n"),
  1357. tstrName,
  1358. tstrFormat,
  1359. iItemCount);
  1360. return( (SIZE_T)_tcslen(EventBuf));
  1361. }
  1362. }
  1363. // All argument processing is complete
  1364. // No prepare the final formatting.
  1365. if ((tstrFormat == NULL) || (tstrFormat[0] == 0))
  1366. {
  1367. TCHAR GuidString[32] ;
  1368. // Build a helpful format
  1369. RtlZeroMemory(EventBuf, SizeEventBuf);
  1370. if (!IsEqualGUID(&pEvent->Header.Guid, &EventTraceGuid) ) {
  1371. GuidToString(GuidString, (LPGUID) &pEvent->Header.Guid);
  1372. _sntprintf(EventBuf,
  1373. SizeEventBuf,
  1374. _T("%s(%s): GUID=%s (No Format Information found)."),
  1375. pItemBuf[0], // name if any
  1376. pItemBuf[1], // sub name or number
  1377. GuidString // GUID
  1378. );
  1379. } else {
  1380. // Display nothing for a header for now
  1381. // Might be a good place to display some general info ?
  1382. }
  1383. }
  1384. else
  1385. {
  1386. DWORD dwResult;
  1387. if (tstrTypeOfType == 1)
  1388. {
  1389. __try
  1390. {
  1391. dwResult = FormatMessage(
  1392. FORMAT_MESSAGE_FROM_STRING + FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1393. // source and processing options
  1394. (LPCVOID) tstrFormat, // pointer to message source
  1395. 0, // requested message identifier
  1396. 0, // language identifier
  1397. (LPTSTR) EventBuf, // pointer to message buffer
  1398. SizeEventBuf, // maximum size of message buffer
  1399. (va_list *) pItemBuf); // pointer to array of message inserts
  1400. if (dwResult == 0)
  1401. {
  1402. _sntprintf(
  1403. EventBuf, SizeEventBuf,
  1404. _T("FormatMessage (Type 1) Failed 0x%X (%s/%s) (\n"),
  1405. GetLastError(),
  1406. pItemBuf[0],
  1407. pItemBuf[1]);
  1408. return(GetLastError());
  1409. }
  1410. }
  1411. __except(EXCEPTION_EXECUTE_HANDLER)
  1412. {
  1413. _sntprintf(EventBuf,
  1414. SizeEventBuf,
  1415. _T("\n*****FormatMessage (#Type) of %s, raised an exception*****\n"),
  1416. tstrFormat);
  1417. return( (SIZE_T)_tcslen(EventBuf));
  1418. }
  1419. }
  1420. else if (tstrTypeOfType == 2)
  1421. {
  1422. __try
  1423. {
  1424. #if !defined(STRINGFIXUP)
  1425. dwResult = FormatMessage(
  1426. FORMAT_MESSAGE_FROM_STRING + FORMAT_MESSAGE_ARGUMENT_ARRAY,
  1427. // source and processing options
  1428. (LPCVOID) tstrFormat, // pointer to message source
  1429. 0, // requested message identifier
  1430. 0, // language identifier
  1431. (LPTSTR) EventBuf, // pointer to message buffer
  1432. SizeEventBuf, // maximum size of message buffer
  1433. (va_list *) pItemRBuf); // pointer to array of message inserts
  1434. if (dwResult == 0)
  1435. {
  1436. _sntprintf(
  1437. EventBuf,
  1438. SizeEventBuf,
  1439. _T("FormatMessage (#Typev) Failed 0x%X (%s/%s) (\n"),
  1440. GetLastError(),
  1441. pItemBuf[0],
  1442. pItemBuf[1]);
  1443. return(GetLastError());
  1444. }
  1445. #else // if !defined(STRINGFIXUP)
  1446. ULONG ReturnLength ;
  1447. dwResult = (DWORD)TraceFormatMessage(
  1448. tstrFormat, // message format
  1449. 0,
  1450. FALSE, // Don't ignore inserts,
  1451. #if defined(UNICODE)
  1452. FALSE, // Arguments Are not Ansi,
  1453. #else // #if defined(UNICODE)
  1454. TRUE, // Arguments are Ansi
  1455. #endif // #if defined(UNICODE)
  1456. TRUE, // Arguments Are An Array,
  1457. (va_list *) pItemRBuf, // Arguments,
  1458. EventBuf, // Buffer,
  1459. SizeEventBuf, // maximum size of message buffer
  1460. &ReturnLength // Coutnof Data Returned
  1461. );
  1462. if (ReturnLength == 0)
  1463. {
  1464. _sntprintf(
  1465. EventBuf,
  1466. SizeEventBuf,
  1467. _T("FormatMessage (#Typev) Failed 0x%X (%s/%s) (\n"),
  1468. dwResult,
  1469. pItemBuf[0],
  1470. pItemBuf[1]);
  1471. return(dwResult);
  1472. }
  1473. #endif // if !defined(STRINGFIXUP)
  1474. }
  1475. __except(EXCEPTION_EXECUTE_HANDLER)
  1476. {
  1477. _sntprintf( EventBuf,
  1478. SizeEventBuf,
  1479. _T("\n*****FormatMessage (#Typev) raised an exception (Format = %s) ****\n**** [Check for missing \"!\" Formats]*****\n"), tstrFormat);
  1480. return((SIZE_T)_tcslen(EventBuf));
  1481. }
  1482. }
  1483. else
  1484. {
  1485. return (-12);
  1486. }
  1487. }
  1488. if (pszMask != NULL)
  1489. {
  1490. // Has he imposed a Filter?
  1491. //
  1492. if (_tcsstr(_tcslwr(pszMask), _tcslwr(tstrName)) !=0)
  1493. {
  1494. return( (SIZE_T)_tcslen(EventBuf));
  1495. }
  1496. else
  1497. {
  1498. return(0);
  1499. }
  1500. }
  1501. return ( (SIZE_T)_tcslen(EventBuf));
  1502. }
  1503. PMOF_INFO
  1504. GetMofInfoHead(
  1505. PLIST_ENTRY * HeadEventList,
  1506. LPGUID pGuid,
  1507. LPTSTR strType,
  1508. LONG TypeIndex,
  1509. ULONG TypeOfType,
  1510. LPTSTR TypeFormat,
  1511. BOOL bBestMatch
  1512. )
  1513. {
  1514. PLIST_ENTRY Head, Next;
  1515. PMOF_INFO pMofInfo;
  1516. // Search the eventList for this Guid and find the head
  1517. //
  1518. if (HeadEventList == NULL) {
  1519. return NULL ;
  1520. }
  1521. if (*HeadEventList == NULL)
  1522. {
  1523. if( (*HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY))) == NULL)
  1524. return NULL;
  1525. RtlZeroMemory(*HeadEventList, sizeof(LIST_ENTRY));
  1526. InitializeListHead(*HeadEventList);
  1527. }
  1528. // Traverse the list and look for the Mof info head for this Guid.
  1529. //
  1530. Head = *HeadEventList;
  1531. Next = Head->Flink;
  1532. if (bBestMatch)
  1533. {
  1534. PMOF_INFO pBestMatch = NULL;
  1535. while (Head != Next)
  1536. {
  1537. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  1538. if (IsEqualGUID(&pMofInfo->Guid, pGuid))
  1539. {
  1540. if (pMofInfo->TypeIndex == TypeIndex)
  1541. {
  1542. return pMofInfo;
  1543. }
  1544. else if (pMofInfo->strType == NULL)
  1545. {
  1546. pBestMatch = pMofInfo;
  1547. }
  1548. }
  1549. Next = Next->Flink;
  1550. }
  1551. if(pBestMatch != NULL)
  1552. {
  1553. return pBestMatch;
  1554. }
  1555. }
  1556. else
  1557. {
  1558. while (Head != Next)
  1559. {
  1560. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  1561. if ( (strType != NULL)
  1562. && (pMofInfo->strType != NULL)
  1563. && (IsEqualGUID(&pMofInfo->Guid, pGuid))
  1564. && (!(_tcscmp(strType, pMofInfo->strType))))
  1565. {
  1566. return pMofInfo;
  1567. }
  1568. else if ( (strType == NULL)
  1569. && (pMofInfo->strType == NULL)
  1570. && (IsEqualGUID(&pMofInfo->Guid, pGuid)))
  1571. {
  1572. return pMofInfo;
  1573. }
  1574. Next = Next->Flink;
  1575. }
  1576. }
  1577. // If One does not exist, create one.
  1578. //
  1579. if( (pMofInfo = (PMOF_INFO) malloc(sizeof(MOF_INFO))) == NULL)
  1580. return NULL;
  1581. RtlZeroMemory(pMofInfo, sizeof(MOF_INFO));
  1582. memcpy(&pMofInfo->Guid, pGuid, sizeof(GUID));
  1583. pMofInfo->ItemHeader = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY));
  1584. if( pMofInfo->ItemHeader == NULL){
  1585. free(pMofInfo);
  1586. return NULL;
  1587. }
  1588. RtlZeroMemory(pMofInfo->ItemHeader, sizeof(LIST_ENTRY));
  1589. if (strType != NULL)
  1590. {
  1591. if ((pMofInfo->strType = (LPTSTR) malloc((_tcslen(strType) + 1) * sizeof(TCHAR))) == NULL ) {
  1592. free(pMofInfo);
  1593. return NULL ;
  1594. }
  1595. _tcscpy(pMofInfo->strType,strType);
  1596. }
  1597. if (TypeOfType != 0)
  1598. {
  1599. pMofInfo->TypeOfType = TypeOfType;
  1600. }
  1601. if (TypeFormat != NULL)
  1602. {
  1603. if ((pMofInfo->TypeFormat =
  1604. (LPTSTR) malloc((_tcslen(TypeFormat) + 1) * sizeof(TCHAR)))== NULL) {
  1605. free(pMofInfo->strType);
  1606. free(pMofInfo);
  1607. return NULL ;
  1608. }
  1609. _tcscpy(pMofInfo->TypeFormat,TypeFormat);
  1610. }
  1611. pMofInfo->TypeIndex = bBestMatch ? -1 : TypeIndex;
  1612. InitializeListHead(pMofInfo->ItemHeader);
  1613. InsertTailList(*HeadEventList, &pMofInfo->Entry);
  1614. return pMofInfo;
  1615. }
  1616. void
  1617. MapGuidToName(
  1618. OUT PLIST_ENTRY *HeadEventList,
  1619. IN LPGUID pGuid,
  1620. IN ULONG nType,
  1621. OUT LPTSTR wstr
  1622. )
  1623. {
  1624. if (IsEqualGUID(pGuid, &EventTraceGuid))
  1625. {
  1626. _tcscpy(wstr, GUID_TYPE_HEADER);
  1627. }
  1628. else if (!UserDefinedGuid(*HeadEventList,pGuid, wstr))
  1629. {
  1630. TCHAR filename[MAX_PATH], filepath[MAX_PATH];
  1631. LPTSTR lpFilePart ;
  1632. LPTSTR lppath = NULL; // Path for finding TMF files
  1633. INT len, waslen = 0 ;
  1634. while (((len = GetEnvironmentVariable(TRACE_FORMAT_SEARCH_PATH, lppath, waslen )) - waslen) > 0) {
  1635. if (len - waslen > 0 ) {
  1636. if (lppath != NULL) {
  1637. free(lppath);
  1638. }
  1639. lppath = malloc((len+1) * sizeof(TCHAR)) ;
  1640. waslen = len ;
  1641. }
  1642. }
  1643. if (lppath != NULL) {
  1644. // Try to find it on the path //
  1645. swprintf(filename,L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.tmf",
  1646. pGuid->Data1,pGuid->Data2,pGuid->Data3,
  1647. pGuid->Data4[0],pGuid->Data4[1],pGuid->Data4[2],pGuid->Data4[3],
  1648. pGuid->Data4[4],pGuid->Data4[5],pGuid->Data4[6],pGuid->Data4[7] );
  1649. if ((len = SearchPath(
  1650. lppath, // search path semi-colon seperated
  1651. filename, // file name with extension
  1652. NULL, // file extension (not reqd.)
  1653. MAX_PATH, // size of buffer
  1654. filepath, // found file name buffer
  1655. &lpFilePart // file component
  1656. ) !=0) && (len <= MAX_PATH)) {
  1657. //_tprintf(_T("Opening file %s\n"),filepath);
  1658. if(GetTraceGuidsW(filepath, HeadEventList))
  1659. {
  1660. if (UserDefinedGuid(*HeadEventList,pGuid, wstr)) {
  1661. free(lppath);
  1662. return;
  1663. }
  1664. }
  1665. }
  1666. free(lppath);
  1667. }
  1668. _tcscpy(wstr, GUID_TYPE_UNKNOWN);
  1669. }
  1670. }
  1671. ULONG
  1672. UserDefinedGuid(
  1673. OUT PLIST_ENTRY HeadEventList,
  1674. IN LPGUID pGuid,
  1675. OUT LPTSTR wstr
  1676. )
  1677. {
  1678. PLIST_ENTRY Head, Next;
  1679. PMOF_INFO pMofInfo;
  1680. // Search the eventList for this Guid and find the head
  1681. //
  1682. if (HeadEventList == NULL)
  1683. {
  1684. /*
  1685. HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY));
  1686. if(HeadEventList == NULL)
  1687. return FALSE;
  1688. InitializeListHead(HeadEventList); */
  1689. return FALSE;
  1690. }
  1691. // Traverse the list and look for the Mof info head for this Guid.
  1692. //
  1693. Head = HeadEventList;
  1694. Next = Head->Flink;
  1695. while(Head != Next && Next != NULL){
  1696. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  1697. if (pMofInfo != NULL && IsEqualGUID(&pMofInfo->Guid, pGuid))
  1698. {
  1699. if ( pMofInfo->strDescription == NULL)
  1700. {
  1701. return FALSE;
  1702. }
  1703. else
  1704. {
  1705. _tcscpy(wstr, pMofInfo->strDescription);
  1706. return TRUE;
  1707. }
  1708. }
  1709. Next = Next->Flink;
  1710. }
  1711. return FALSE;
  1712. }
  1713. ULONG
  1714. WINAPI
  1715. GetTraceGuidsW(
  1716. TCHAR * GuidFile,
  1717. PLIST_ENTRY * HeadEventList )
  1718. {
  1719. FILE * f;
  1720. TCHAR line[MAXSTR],
  1721. nextline[MAXSTR],
  1722. arg[MAXSTR],
  1723. strGuid[MAXSTR];
  1724. PMOF_TYPE types;
  1725. LPGUID Guid;
  1726. UINT i,
  1727. n;
  1728. TCHAR * name,
  1729. * s,
  1730. * guidName;
  1731. PMOF_INFO pMofInfo;
  1732. SIZE_T len = 0;
  1733. UINT typeCount = 0;
  1734. BOOL inInfo = FALSE;
  1735. BOOL eof = FALSE ;
  1736. BOOL nextlineF = FALSE ;
  1737. if (HeadEventList == NULL) {
  1738. return 0 ;
  1739. }
  1740. if (*HeadEventList == NULL)
  1741. {
  1742. if( (*HeadEventList = (PLIST_ENTRY) malloc(sizeof(LIST_ENTRY))) == NULL)
  1743. return 0 ;
  1744. RtlZeroMemory(*HeadEventList, sizeof(LIST_ENTRY));
  1745. InitializeListHead(*HeadEventList);
  1746. }
  1747. Guid = (LPGUID) malloc(sizeof(GUID));
  1748. if (Guid == NULL)
  1749. {
  1750. return 0;
  1751. }
  1752. types = (PMOF_TYPE) malloc(MAXTYPE * sizeof(MOF_TYPE));
  1753. if (types == NULL)
  1754. {
  1755. free(Guid);
  1756. return 0;
  1757. }
  1758. RtlZeroMemory(types, MAXTYPE * sizeof(MOF_TYPE));
  1759. RtlZeroMemory(line, MAXSTR * sizeof(TCHAR));
  1760. RtlZeroMemory(strGuid, MAXSTR * sizeof(TCHAR));
  1761. f = _tfopen( GuidFile, _T("r"));
  1762. if (f == NULL)
  1763. {
  1764. free(Guid);
  1765. free(types);
  1766. return 0;
  1767. }
  1768. n = 0;
  1769. while (!eof )
  1770. {
  1771. if (nextlineF) { // Sometimes we read ahead a bit
  1772. _tcscpy(line, nextline);
  1773. nextlineF = FALSE ;
  1774. } else {
  1775. if (_fgetts(line, MAXSTR, f) == NULL) {
  1776. eof = TRUE ;
  1777. break;
  1778. }
  1779. }
  1780. // jump_inside:;
  1781. if (line[0] == '/')
  1782. {
  1783. continue;
  1784. }
  1785. else if (line[0] == '{')
  1786. {
  1787. inInfo = TRUE;
  1788. }
  1789. else if ( line[0] == '}')
  1790. {
  1791. typeCount = 0;
  1792. inInfo = FALSE;
  1793. }
  1794. else if (inInfo)
  1795. {
  1796. ITEM_TYPE type;
  1797. PTCHAR ItemListValue = NULL;
  1798. name = _tcstok(line, _T("\n\t,"));
  1799. s = _tcstok(NULL, _T(" \n\t,("));
  1800. if (s != NULL && name != NULL )
  1801. {
  1802. if (!_tcsicmp(s,STR_ItemChar)) type = ItemChar;
  1803. else if (!_tcsicmp(s,STR_ItemUChar)) type = ItemUChar;
  1804. else if (!_tcsicmp(s,STR_ItemCharShort)) type = ItemCharShort;
  1805. else if (!_tcsicmp(s,STR_ItemCharSign)) type = ItemCharSign;
  1806. else if (!_tcsicmp(s,STR_ItemShort)) type = ItemShort;
  1807. else if (!_tcsicmp(s,STR_ItemHRESULT)) type = ItemHRESULT;
  1808. else if (!_tcsicmp(s,STR_ItemDouble)) type = ItemDouble;
  1809. else if (!_tcsicmp(s,STR_ItemUShort)) type = ItemUShort;
  1810. else if (!_tcsicmp(s,STR_ItemLong)) type = ItemLong;
  1811. else if (!_tcsicmp(s,STR_ItemULong)) type = ItemULong;
  1812. else if (!_tcsicmp(s,STR_ItemULongX)) type = ItemULongX;
  1813. else if (!_tcsicmp(s,STR_ItemLongLong)) type = ItemLongLong;
  1814. else if (!_tcsicmp(s,STR_ItemULongLong)) type = ItemULongLong;
  1815. else if (!_tcsicmp(s,STR_ItemString)) type = ItemString;
  1816. else if (!_tcsicmp(s,STR_ItemWString)) type = ItemWString;
  1817. else if (!_tcsicmp(s,STR_ItemRString)) type = ItemRString;
  1818. else if (!_tcsicmp(s,STR_ItemRWString)) type = ItemRWString;
  1819. else if (!_tcsicmp(s,STR_ItemPString)) type = ItemPString;
  1820. else if (!_tcsicmp(s,STR_ItemMLString)) type = ItemMLString;
  1821. else if (!_tcsicmp(s,STR_ItemNWString)) type = ItemNWString;
  1822. else if (!_tcsicmp(s,STR_ItemPWString)) type = ItemPWString;
  1823. else if (!_tcsicmp(s,STR_ItemDSString)) type = ItemDSString;
  1824. else if (!_tcsicmp(s,STR_ItemDSWString)) type = ItemDSWString;
  1825. else if (!_tcsicmp(s,STR_ItemPtr)) type = ItemPtr;
  1826. else if (!_tcsicmp(s,STR_ItemSid)) type = ItemSid;
  1827. else if (!_tcsicmp(s,STR_ItemChar4)) type = ItemChar4;
  1828. else if (!_tcsicmp(s,STR_ItemIPAddr)) type = ItemIPAddr;
  1829. else if (!_tcsicmp(s,STR_ItemPort)) type = ItemPort;
  1830. else if (!_tcsicmp(s,STR_ItemListLong)) type = ItemListLong;
  1831. else if (!_tcsicmp(s,STR_ItemListShort)) type = ItemListShort;
  1832. else if (!_tcsicmp(s,STR_ItemListByte)) type = ItemListByte;
  1833. else if (!_tcsicmp(s,STR_ItemSetLong)) type = ItemSetLong;
  1834. else if (!_tcsicmp(s,STR_ItemSetShort)) type = ItemSetShort;
  1835. else if (!_tcsicmp(s,STR_ItemSetByte)) type = ItemSetByte;
  1836. else if (!_tcsicmp(s,STR_ItemNTerror)) type = ItemNTerror;
  1837. else if (!_tcsicmp(s,STR_ItemMerror)) type = ItemMerror;
  1838. else if (!_tcsicmp(s,STR_ItemTimestamp)) type = ItemTimestamp;
  1839. else if (!_tcsicmp(s,STR_ItemGuid)) type = ItemGuid;
  1840. else if (!_tcsicmp(s,STR_ItemWaitTime)) type = ItemWaitTime;
  1841. else if (!_tcsicmp(s,STR_ItemTimeDelta)) type = ItemTimeDelta;
  1842. else if (!_tcsicmp(s,STR_ItemNTSTATUS)) type = ItemNTSTATUS;
  1843. else if (!_tcsicmp(s,STR_ItemWINERROR)) type = ItemWINERROR;
  1844. else if (!_tcsicmp(s,STR_ItemNETEVENT)) type = ItemNETEVENT;
  1845. else if (!_tcsicmp(s,STR_ItemCharHidden)) type = ItemCharHidden;
  1846. else type = ItemUnknown;
  1847. // Get List elements
  1848. if ((type == ItemListLong) || (type == ItemListShort) || (type == ItemListByte)
  1849. ||(type == ItemSetLong) || (type == ItemSetShort) || (type == ItemSetByte) )
  1850. {
  1851. s = _tcstok(NULL, _T("()"));
  1852. ItemListValue =
  1853. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  1854. if (ItemListValue == NULL) {
  1855. return 1 ;
  1856. }
  1857. RtlCopyMemory(
  1858. ItemListValue,
  1859. s,
  1860. (_tcslen(s) + 1) * sizeof(TCHAR));
  1861. }
  1862. // Get Module specification for ItemMerror
  1863. if ((type == ItemMerror)) {
  1864. TCHAR * ppos ;
  1865. s = _tcstok(NULL, _T(" \t"));
  1866. ppos = _tcsrchr(s,'\n');
  1867. if (ppos != NULL) {
  1868. *ppos = UNICODE_NULL ;
  1869. ItemListValue =
  1870. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  1871. if (ItemListValue == NULL) {
  1872. return 1 ;
  1873. }
  1874. RtlCopyMemory(
  1875. ItemListValue,
  1876. s,
  1877. (_tcslen(s) + 1) * sizeof(TCHAR));
  1878. }
  1879. }
  1880. // Get size for ItemCharHidden
  1881. if (type == ItemCharHidden) {
  1882. TCHAR * ppos ;
  1883. s = _tcstok(NULL, _T("["));
  1884. ppos = _tcsrchr(s,']');
  1885. if (ppos != NULL) {
  1886. *ppos = UNICODE_NULL ;
  1887. ItemListValue =
  1888. (TCHAR *) malloc((_tcslen(s) + 1) * sizeof(TCHAR));
  1889. if (ItemListValue == NULL) {
  1890. return 1 ;
  1891. }
  1892. RtlCopyMemory(
  1893. ItemListValue,
  1894. s,
  1895. (_tcslen(s) + 1) * sizeof(TCHAR));
  1896. }
  1897. }
  1898. if (typeCount == 0)
  1899. {
  1900. AddMofInfo(
  1901. * HeadEventList,
  1902. Guid,
  1903. NULL,
  1904. -1,
  1905. name,
  1906. type,
  1907. NULL,
  1908. 0,
  1909. NULL);
  1910. }
  1911. else
  1912. {
  1913. for (i = 0; i < typeCount; i ++)
  1914. {
  1915. AddMofInfo(
  1916. * HeadEventList,
  1917. Guid,
  1918. types[i].strType,
  1919. types[i].TypeIndex,
  1920. name,
  1921. type,
  1922. ItemListValue,
  1923. types[i].TypeType,
  1924. types[i].TypeFormat);
  1925. }
  1926. }
  1927. }
  1928. }
  1929. else if (line[0] == '#')
  1930. {
  1931. TCHAR * token, * etoken;
  1932. int Indent ; // Special parameter values(numeric) from comments
  1933. TCHAR FuncName[MAXNAMEARG], // Special parameter values from comment
  1934. LevelName[MAXNAMEARG],
  1935. CompIDName[MAXNAMEARG],
  1936. *v ;
  1937. //This is a workaround to defend against newlines in TMF files.
  1938. while (!nextlineF && !eof) {
  1939. if (_fgetts(nextline,MAXSTR,f) != NULL) {
  1940. if ((nextline[0] != '{') && nextline[0] != '#') {
  1941. TCHAR * eol ;
  1942. if ((eol = _tcsrchr(line,'\n')) != NULL) {
  1943. *eol = 0 ;
  1944. }
  1945. _tcsncat(line,nextline,MAXSTR-_tcslen(line)) ;
  1946. } else {
  1947. nextlineF = TRUE ;
  1948. }
  1949. } else {
  1950. eof = TRUE ;
  1951. }
  1952. }
  1953. // Find any special names in the comments
  1954. // As this gets longer we should make it generic
  1955. Indent = FindIntValue(line,_T("INDENT=")); // Indentaion Level
  1956. v = FindValue(line,_T("FUNC=")); // Function Name
  1957. _tcsncpy(FuncName, v, MAXNAMEARG);
  1958. v = FindValue(line,_T("LEVEL=")); // Tracing level or Flags
  1959. _tcsncpy(LevelName, v, MAXNAMEARG);
  1960. v = FindValue(line,_T("COMPNAME=")); // Component ID
  1961. _tcsncpy(CompIDName, v, MAXNAMEARG);
  1962. token = _tcstok(line,_T(" \t"));
  1963. if (_tcsicmp(token,_T("#type")) == 0)
  1964. {
  1965. types[typeCount].TypeType = 1 ;
  1966. }
  1967. else if (_tcsicmp(token,_T("#typev")) == 0)
  1968. {
  1969. types[typeCount].TypeType = 2 ;
  1970. }
  1971. else
  1972. {
  1973. fclose(f);
  1974. free(Guid);
  1975. free(types);
  1976. return(-10);
  1977. }
  1978. token = _tcstok( NULL, _T(" \t\n")); // Get Type Name
  1979. _tcscpy(types[typeCount].strType,token);
  1980. token =_tcstok( NULL, _T("\"\n,")); // Look for a Format String
  1981. if (token != NULL) {
  1982. types[typeCount].TypeIndex = _ttoi(token); // Get the type Index
  1983. token =_tcstok( NULL, _T("\n"));
  1984. }
  1985. etoken = NULL;
  1986. if (token != NULL)
  1987. {
  1988. etoken = _tcsrchr(token,_T('\"')); // Find the closing quote
  1989. }
  1990. if (etoken !=NULL)
  1991. {
  1992. etoken[0] = 0;
  1993. }
  1994. else
  1995. {
  1996. token = NULL;
  1997. }
  1998. if (token != NULL)
  1999. {
  2000. if (token[0] == '%' && token[1] == '0') {
  2001. // add standard prefix
  2002. if (StdPrefix[0] == 0) {
  2003. // need to initialize it.
  2004. LPTSTR Prefix = NULL ; int len, waslen = 0 ;
  2005. while (((len = GetEnvironmentVariable(TRACE_FORMAT_PREFIX, Prefix, waslen )) - waslen) > 0) {
  2006. if (len - waslen > 0 ) {
  2007. if (Prefix != NULL) {
  2008. free(Prefix);
  2009. }
  2010. Prefix = malloc((len+1) * sizeof(TCHAR)) ;
  2011. if (Prefix == NULL) {
  2012. return -11 ;
  2013. }
  2014. waslen = len ;
  2015. }
  2016. }
  2017. if (Prefix) {
  2018. _tcsncpy(StdPrefix, Prefix, MAXSTR);
  2019. } else {
  2020. _tcscpy(StdPrefix, STD_PREFIX);
  2021. }
  2022. free(Prefix) ;
  2023. }
  2024. _tcscpy(types[typeCount].TypeFormat,StdPrefix);
  2025. _tcscat(types[typeCount].TypeFormat,token + 2);
  2026. } else {
  2027. _tcscpy(types[typeCount].TypeFormat,token);
  2028. }
  2029. // process the special variable names
  2030. // Make generic in future
  2031. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!FUNC!"), FuncName);
  2032. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!LEVEL!"), LevelName);
  2033. ReplaceStringUnsafe(types[typeCount].TypeFormat, _T("%!COMPNAME!"), CompIDName);
  2034. }
  2035. else
  2036. {
  2037. types[typeCount].TypeFormat[0] = types[typeCount].TypeFormat[1]
  2038. = 0;
  2039. }
  2040. if ( types[typeCount].TypeFormat[0] == 0
  2041. && types[typeCount].TypeFormat[1] == 0)
  2042. {
  2043. pMofInfo = GetMofInfoHead(
  2044. HeadEventList,
  2045. Guid,
  2046. types[typeCount].strType,
  2047. types[typeCount].TypeIndex,
  2048. types[typeCount].TypeType,
  2049. NULL,
  2050. FALSE);
  2051. }
  2052. else
  2053. {
  2054. pMofInfo = GetMofInfoHead(
  2055. HeadEventList,
  2056. Guid,
  2057. types[typeCount].strType,
  2058. types[typeCount].TypeIndex,
  2059. types[typeCount].TypeType,
  2060. types[typeCount].TypeFormat,
  2061. FALSE);
  2062. }
  2063. if(pMofInfo == NULL){
  2064. fclose(f);
  2065. free(Guid);
  2066. free(types);
  2067. return 0;
  2068. }
  2069. if (_tcslen(strGuid) > 0)
  2070. {
  2071. pMofInfo->strDescription =
  2072. (PTCHAR) malloc((_tcslen(strGuid) + 1) * sizeof(TCHAR));
  2073. if (pMofInfo->strDescription == NULL) {
  2074. fclose(f);
  2075. free(Guid);
  2076. free(types);
  2077. return 0;
  2078. }
  2079. _tcscpy(pMofInfo->strDescription, strGuid);
  2080. }
  2081. typeCount++;
  2082. if(typeCount >= MAXTYPE)
  2083. {
  2084. fclose(f);
  2085. free(Guid);
  2086. free(types);
  2087. return(-11);
  2088. }
  2089. }
  2090. else if ( (line[0] >= '0' && line[0] <= '9')
  2091. || (line[0] >= 'a' && line[0] <= 'f')
  2092. || (line[0] >= 'A' && line[0] <= 'F'))
  2093. {
  2094. typeCount = 0;
  2095. _tcsncpy(arg, line, 8);
  2096. arg[8] = 0;
  2097. Guid->Data1 = ahextoi(arg);
  2098. _tcsncpy(arg, &line[9], 4);
  2099. arg[4] = 0;
  2100. Guid->Data2 = (USHORT) ahextoi(arg);
  2101. _tcsncpy(arg, &line[14], 4);
  2102. arg[4] = 0;
  2103. Guid->Data3 = (USHORT) ahextoi(arg);
  2104. for (i = 0; i < 2; i ++)
  2105. {
  2106. _tcsncpy(arg, &line[19 + (i * 2)], 2);
  2107. arg[2] = 0;
  2108. Guid->Data4[i] = (UCHAR) ahextoi(arg);
  2109. }
  2110. for (i = 2; i < 8; i ++)
  2111. {
  2112. _tcsncpy(arg, &line[20 + (i * 2)], 2);
  2113. arg[2] = 0;
  2114. Guid->Data4[i] = (UCHAR) ahextoi(arg);
  2115. }
  2116. // Find the next non whitespace character
  2117. //
  2118. guidName = &line[36];
  2119. while (*guidName == ' '|| *guidName == '\t')
  2120. {
  2121. guidName++;
  2122. }
  2123. // cut comment out (if present)
  2124. {
  2125. TCHAR* comment = _tcsstr(guidName, TEXT("//"));
  2126. if (comment) {
  2127. // remove whitespace
  2128. --comment;
  2129. while (comment >= guidName && isspace(*comment)) --comment;
  2130. *++comment = 0;
  2131. }
  2132. }
  2133. len = _tcslen(guidName);
  2134. s = guidName;
  2135. while (len > 0)
  2136. {
  2137. len -= 1;
  2138. if (*s == '\n' || *s == '\0' || *s == '\t')
  2139. {
  2140. *s = '\0';
  2141. break;
  2142. }
  2143. s++;
  2144. }
  2145. pMofInfo = GetMofInfoHead(
  2146. HeadEventList, Guid, NULL, -1, 0, NULL, FALSE);
  2147. if(pMofInfo == NULL){
  2148. fclose(f);
  2149. free(Guid);
  2150. free(types);
  2151. return (ULONG) 0;
  2152. }
  2153. if (pMofInfo->strDescription != NULL)
  2154. {
  2155. free(pMofInfo->strDescription);
  2156. pMofInfo->strDescription = NULL;
  2157. }
  2158. _tcscpy(strGuid, guidName);
  2159. pMofInfo->strDescription =
  2160. (PTCHAR) malloc((_tcslen(guidName) + 1) * sizeof(TCHAR));
  2161. if (pMofInfo->strDescription == NULL ) {
  2162. // We really have problems
  2163. fclose(f);
  2164. free(Guid);
  2165. free(types);
  2166. return (ULONG) 0 ;
  2167. }
  2168. _tcscpy(pMofInfo->strDescription, strGuid);
  2169. n++ ; // Thats one more GUID
  2170. }
  2171. RtlZeroMemory(line, MAXSTR * sizeof(TCHAR));
  2172. }
  2173. fclose(f);
  2174. free(Guid);
  2175. free(types);
  2176. return (ULONG) n;
  2177. }
  2178. ULONG
  2179. ahextoi(
  2180. TCHAR *s
  2181. )
  2182. {
  2183. SSIZE_T len;
  2184. ULONG num, base, hex;
  2185. len = _tcslen(s);
  2186. hex = 0;
  2187. base = 1;
  2188. num = 0;
  2189. while (--len >= 0)
  2190. {
  2191. if ((s[len] == 'x' || s[len] == 'X') && (s[len-1] == '0'))
  2192. {
  2193. break;
  2194. }
  2195. if (s[len] >= '0' && s[len] <= '9')
  2196. {
  2197. num = s[len] - '0';
  2198. }
  2199. else if (s[len] >= 'a' && s[len] <= 'f')
  2200. {
  2201. num = (s[len] - 'a') + 10;
  2202. }
  2203. else if (s[len] >= 'A' && s[len] <= 'F')
  2204. {
  2205. num = (s[len] - 'A') + 10;
  2206. }
  2207. else
  2208. {
  2209. continue;
  2210. }
  2211. hex += num * base;
  2212. base = base * 16;
  2213. }
  2214. return hex;
  2215. }
  2216. void
  2217. WINAPI
  2218. SummaryTraceEventListW(
  2219. TCHAR * SummaryBlock,
  2220. ULONG SizeSummaryBlock,
  2221. PLIST_ENTRY EventListHead
  2222. )
  2223. {
  2224. PLIST_ENTRY Head, Next;
  2225. PMOF_INFO pMofInfo;
  2226. TCHAR strGuid[MAXSTR];
  2227. TCHAR strName[MAXSTR];
  2228. TCHAR strBuffer[MAXSTR];
  2229. if (EventListHead == NULL)
  2230. {
  2231. return;
  2232. }
  2233. if (SummaryBlock == NULL)
  2234. {
  2235. return;
  2236. }
  2237. RtlZeroMemory(SummaryBlock, sizeof(TCHAR) * SizeSummaryBlock);
  2238. Head = EventListHead;
  2239. Next = Head->Flink;
  2240. while (Head != Next)
  2241. {
  2242. RtlZeroMemory(strName, 256);
  2243. RtlZeroMemory(strBuffer, 256);
  2244. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2245. if (pMofInfo->EventCount > 0)
  2246. {
  2247. GuidToString((PTCHAR) strGuid, &pMofInfo->Guid);
  2248. MapGuidToName(&EventListHead, &pMofInfo->Guid, 0, strName);
  2249. _sntprintf(strBuffer,
  2250. MAXSTR,
  2251. _T("|%10d %-20s %-10s %36s|\n"),
  2252. pMofInfo->EventCount,
  2253. strName,
  2254. pMofInfo->strType ? pMofInfo->strType : _T("General"),
  2255. strGuid);
  2256. _tcscat(SummaryBlock, strBuffer);
  2257. if (_tcslen(SummaryBlock) >= SizeSummaryBlock)
  2258. {
  2259. return;
  2260. }
  2261. }
  2262. Next = Next->Flink;
  2263. }
  2264. }
  2265. PTCHAR
  2266. GuidToString(
  2267. PTCHAR s,
  2268. LPGUID piid
  2269. )
  2270. {
  2271. _stprintf(s, _T("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x"),
  2272. piid->Data1,
  2273. piid->Data2,
  2274. piid->Data3,
  2275. piid->Data4[0],
  2276. piid->Data4[1],
  2277. piid->Data4[2],
  2278. piid->Data4[3],
  2279. piid->Data4[4],
  2280. piid->Data4[5],
  2281. piid->Data4[6],
  2282. piid->Data4[7]);
  2283. return(s);
  2284. }
  2285. // This routine is called by the WBEM interface routine for each property
  2286. // found for this Guid. The ITEM_DESC structure is allocted for each Property.
  2287. //
  2288. VOID
  2289. AddMofInfo(
  2290. PLIST_ENTRY HeadEventList,
  2291. const GUID * Guid,
  2292. LPTSTR strType,
  2293. ULONG typeIndex,
  2294. LPTSTR strDesc,
  2295. ITEM_TYPE ItemType,
  2296. TCHAR * ItemList,
  2297. ULONG typeOfType,
  2298. LPTSTR typeFormat
  2299. )
  2300. {
  2301. PITEM_DESC pItem;
  2302. PLIST_ENTRY pListHead;
  2303. PMOF_INFO pMofInfo;
  2304. if (strDesc == NULL)
  2305. {
  2306. return;
  2307. }
  2308. pItem = (PITEM_DESC) malloc(sizeof(ITEM_DESC));
  2309. if(pItem == NULL)return; //silent error
  2310. RtlZeroMemory(pItem, sizeof(ITEM_DESC));
  2311. pItem->strDescription =
  2312. (LPTSTR) malloc((_tcslen(strDesc) + 1) * sizeof(TCHAR));
  2313. if( pItem->strDescription == NULL ){
  2314. free(pItem);
  2315. return;
  2316. }
  2317. _tcscpy(pItem->strDescription, strDesc);
  2318. pItem->ItemType = ItemType;
  2319. pItem->ItemList = ItemList ;
  2320. pMofInfo = GetMofInfoHead(
  2321. (PLIST_ENTRY *) HeadEventList,
  2322. (LPGUID) Guid,
  2323. strType,
  2324. typeIndex,
  2325. typeOfType,
  2326. typeFormat,
  2327. FALSE);
  2328. if (pMofInfo != NULL)
  2329. {
  2330. pListHead = pMofInfo->ItemHeader;
  2331. InsertTailList(pListHead, &pItem->Entry);
  2332. }
  2333. else{
  2334. free(pItem->strDescription);
  2335. free(pItem);
  2336. }
  2337. }
  2338. void
  2339. WINAPI
  2340. CleanupTraceEventList(
  2341. PLIST_ENTRY HeadEventList
  2342. )
  2343. {
  2344. PLIST_ENTRY Head, Next;
  2345. PMOF_INFO pMofInfo;
  2346. if (HeadEventList == NULL)
  2347. {
  2348. return;
  2349. }
  2350. Head = HeadEventList;
  2351. Next = Head->Flink;
  2352. while (Head != Next)
  2353. {
  2354. pMofInfo = CONTAINING_RECORD(Next, MOF_INFO, Entry);
  2355. RemoveEntryList(&pMofInfo->Entry);
  2356. RemoveMofInfo(pMofInfo->ItemHeader);
  2357. free(pMofInfo->ItemHeader);
  2358. free(pMofInfo->strDescription);
  2359. free(pMofInfo->TypeFormat);
  2360. free(pMofInfo->strType);
  2361. Next = Next->Flink;
  2362. free(pMofInfo);
  2363. }
  2364. free(HeadEventList);
  2365. }
  2366. void
  2367. RemoveMofInfo(
  2368. PLIST_ENTRY pMofInfo
  2369. )
  2370. {
  2371. PLIST_ENTRY Head, Next;
  2372. PITEM_DESC pItem;
  2373. Head = pMofInfo;
  2374. Next = Head->Flink;
  2375. while (Head != Next)
  2376. {
  2377. pItem = CONTAINING_RECORD(Next, ITEM_DESC, Entry);
  2378. Next = Next->Flink;
  2379. RemoveEntryList(&pItem->Entry);
  2380. if (pItem->strDescription != NULL)
  2381. free(pItem->strDescription);
  2382. if (pItem->ItemList != NULL)
  2383. free(pItem->ItemList);
  2384. free(pItem);
  2385. }
  2386. }
  2387. // Now for some of the ASCII Stuff
  2388. //
  2389. SIZE_T
  2390. WINAPI
  2391. FormatTraceEventA(
  2392. PLIST_ENTRY HeadEventList,
  2393. PEVENT_TRACE pInEvent,
  2394. CHAR * EventBuf,
  2395. ULONG SizeEventBuf,
  2396. CHAR * pszMask
  2397. )
  2398. {
  2399. SIZE_T Status;
  2400. ULONG uSizeEventBuf;
  2401. TCHAR * EventBufW;
  2402. TCHAR * pszMaskW;
  2403. EventBufW = (TCHAR *) malloc(SizeEventBuf * sizeof(TCHAR) + 2);
  2404. if (EventBufW == (TCHAR *) NULL)
  2405. {
  2406. return -1;
  2407. }
  2408. pszMaskW = (TCHAR *) NULL;
  2409. if (pszMask != NULL && strlen(pszMask) != 0)
  2410. {
  2411. pszMaskW = (TCHAR *) malloc(strlen(pszMask) * sizeof(TCHAR));
  2412. if (pszMaskW == (TCHAR *) NULL)
  2413. {
  2414. free(EventBufW);
  2415. return -1;
  2416. }
  2417. RtlZeroMemory(pszMaskW, strlen(pszMask) * sizeof(TCHAR));
  2418. }
  2419. uSizeEventBuf = SizeEventBuf;
  2420. Status = FormatTraceEventW(
  2421. HeadEventList,
  2422. pInEvent,
  2423. EventBufW,
  2424. SizeEventBuf,
  2425. pszMaskW);
  2426. if (Status == 0)
  2427. {
  2428. free(EventBufW);
  2429. free(pszMaskW);
  2430. return -1;
  2431. }
  2432. RtlZeroMemory(EventBuf,uSizeEventBuf);
  2433. WideCharToMultiByte(
  2434. CP_ACP,
  2435. 0,
  2436. EventBufW,
  2437. (int) _tcslen(EventBufW), // Truncate in Sundown!!
  2438. EventBuf,
  2439. uSizeEventBuf,
  2440. NULL,
  2441. NULL);
  2442. free(EventBufW);
  2443. free(pszMaskW);
  2444. return Status;
  2445. }
  2446. ULONG
  2447. WINAPI
  2448. GetTraceGuidsA(
  2449. CHAR * GuidFile,
  2450. PLIST_ENTRY * HeadEventList
  2451. )
  2452. {
  2453. INT Status;
  2454. TCHAR * GuidFileW;
  2455. SIZE_T len;
  2456. if ((len = strlen(GuidFile)) == 0)
  2457. {
  2458. return 0;
  2459. }
  2460. GuidFileW = malloc(sizeof(TCHAR) * strlen(GuidFile) + 2);
  2461. if (GuidFileW == NULL)
  2462. {
  2463. return 0;
  2464. }
  2465. if ((MultiByteToWideChar(
  2466. CP_ACP,
  2467. 0,
  2468. GuidFile,
  2469. -1,
  2470. GuidFileW,
  2471. (int) strlen(GuidFile) * sizeof(TCHAR))) // Truncate in Sundown!!
  2472. == 0)
  2473. {
  2474. free(GuidFileW);
  2475. return 0;
  2476. }
  2477. Status = GetTraceGuidsW(GuidFileW, HeadEventList);
  2478. free (GuidFileW);
  2479. return Status;
  2480. }
  2481. void
  2482. WINAPI
  2483. SummaryTraceEventListA(
  2484. CHAR * SummaryBlock,
  2485. ULONG SizeSummaryBlock,
  2486. PLIST_ENTRY EventListhead
  2487. )
  2488. {
  2489. TCHAR * SummaryBlockW;
  2490. if(SizeSummaryBlock <= 0 || SizeSummaryBlock * sizeof(TCHAR) <= 0 )return;
  2491. SummaryBlockW = (TCHAR *) malloc(SizeSummaryBlock * sizeof(TCHAR));
  2492. if (SummaryBlockW == (TCHAR *)NULL)
  2493. return;
  2494. //RtlZeroMemory(SummaryBlock, SizeSummaryBlock);
  2495. RtlZeroMemory(SummaryBlockW, SizeSummaryBlock*sizeof(TCHAR));
  2496. SummaryTraceEventListW(
  2497. SummaryBlockW,
  2498. SizeSummaryBlock * ((ULONG)sizeof(TCHAR)),
  2499. EventListhead);
  2500. WideCharToMultiByte(
  2501. CP_ACP,
  2502. 0,
  2503. SummaryBlockW,
  2504. (int)_tcslen(SummaryBlockW), // Truncate in Sundown!!
  2505. SummaryBlock,
  2506. SizeSummaryBlock,
  2507. NULL,
  2508. NULL);
  2509. free(SummaryBlockW);
  2510. }
  2511. void
  2512. WINAPI
  2513. GetTraceElapseTime(
  2514. __int64 * pElapseTime
  2515. )
  2516. {
  2517. * pElapseTime = ElapseTime;
  2518. }
  2519. ULONG
  2520. WINAPI
  2521. SetTraceFormatParameter(
  2522. PARAMETER_TYPE Parameter ,
  2523. PVOID ParameterValue
  2524. )
  2525. {
  2526. switch (Parameter) {
  2527. case ParameterINDENT:
  2528. bIndent = PtrToUlong(ParameterValue);
  2529. break;
  2530. case ParameterSEQUENCE:
  2531. bSequence = PtrToUlong(ParameterValue);
  2532. if (bSequence) {
  2533. STD_PREFIX = STD_PREFIX_SEQ;
  2534. } else {
  2535. STD_PREFIX = STD_PREFIX_NOSEQ;
  2536. }
  2537. break;
  2538. case ParameterGMT:
  2539. bGmt = PtrToUlong(ParameterValue);
  2540. break;
  2541. case ParameterTraceFormatSearchPath:
  2542. break;
  2543. }
  2544. return 0 ;
  2545. }
  2546. ULONG
  2547. WINAPI
  2548. GetTraceFormatParameter(
  2549. PARAMETER_TYPE Parameter ,
  2550. PVOID ParameterValue
  2551. )
  2552. {
  2553. switch (Parameter) {
  2554. case ParameterINDENT: return bIndent;
  2555. case ParameterSEQUENCE: return bSequence;
  2556. case ParameterGMT: return bGmt;
  2557. }
  2558. return 0 ;
  2559. }
  2560. #ifdef __cplusplus
  2561. }
  2562. #endif