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.

513 lines
12 KiB

  1. // StructureWrappers.cpp: implementation of the CStructureWrappers class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. //***************************************************************************
  5. //
  6. // judyp May 1999
  7. //
  8. //***************************************************************************
  9. #include "stdafx.h"
  10. #pragma warning (disable : 4786)
  11. #pragma warning (disable : 4275)
  12. #include <iosfwd>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <string>
  16. #include <map>
  17. #include <list>
  18. using namespace std;
  19. #include <malloc.h>
  20. #include <tchar.h>
  21. #include <windows.h>
  22. #ifdef NONNT5
  23. typedef unsigned long ULONG_PTR;
  24. #endif
  25. #include <wmistr.h>
  26. #include <guiddef.h>
  27. #include <initguid.h>
  28. #include <evntrace.h>
  29. #include <WTYPES.H>
  30. #include "t_string.h"
  31. #include "Persistor.h"
  32. #include "Logger.h"
  33. #include "TCOData.h"
  34. #include "Utilities.h"
  35. #include "StructureWrappers.h"
  36. #include "StructureWapperHelpers.h"
  37. #include "ConstantMap.h"
  38. //////////////////////////////////////////////////////////////////////
  39. // Wrappers that serialize and de-serialize Event Tracing Data
  40. // Structures.
  41. //////////////////////////////////////////////////////////////////////
  42. #define MAX_LINE 1024
  43. static TCHAR g_tcNl = _T('\n');
  44. static TCHAR g_tcCR = 0x0d;
  45. static TCHAR g_tcLF = 0x0a;
  46. #ifdef _UNICODE
  47. static TCHAR g_tcDQuote[] = _T("\"");
  48. static TCHAR g_atcNL[] = {0x0d, 0x0a, 0x00};
  49. #else
  50. static TCHAR g_atcNL[] = {g_tcNl};
  51. static TCHAR g_tcDQuote = _T('"');
  52. #endif
  53. CConstantMap g_ConstantMap;
  54. //////////////////////////////////////////////////////////////////////
  55. // _EVENT_TRACE_PROPERTIES
  56. //////////////////////////////////////////////////////////////////////
  57. CEventTraceProperties::CEventTraceProperties()
  58. {
  59. m_pProps = NULL;
  60. m_bIsNULL = true;
  61. }
  62. CEventTraceProperties::CEventTraceProperties
  63. (PEVENT_TRACE_PROPERTIES pProps)
  64. {
  65. Initialize(pProps);
  66. }
  67. CEventTraceProperties::~CEventTraceProperties()
  68. {
  69. if (m_pProps)
  70. {
  71. free(m_pProps->LogFileName);
  72. m_pProps->LogFileName = NULL;
  73. free(m_pProps->LoggerName);
  74. m_pProps->LoggerName = NULL;
  75. free(m_pProps);
  76. m_pProps = NULL;
  77. }
  78. m_bIsNULL = true;
  79. }
  80. CEventTraceProperties::CEventTraceProperties(CEventTraceProperties &rhs)
  81. {
  82. Initialize(rhs.m_pProps);
  83. }
  84. CEventTraceProperties &CEventTraceProperties::operator=
  85. (CEventTraceProperties &rhs)
  86. {
  87. if ( this != &rhs )
  88. {
  89. delete this;
  90. Initialize(rhs.m_pProps);
  91. }
  92. return *this;
  93. }
  94. #if 0
  95. "_EVENT_TRACE_PROPERTIES Instance Begin"
  96. "BufferSize:ULONG:32"
  97. "MinimunBuffers:ULONG:2"
  98. "MaximunBuffers:ULONG:3"
  99. "MaximunFileSize:ULONG:4"
  100. "LogFileMode:@#$ENUM:EVENT_TRACE_FILE_MODE_NEWFILE|EVENT_TRACE_REAL_TIME_MODE|@#$UNKNOWNVALUE:0x20"
  101. "FlushTimer:ULONG:6"
  102. "EnableFlags:@#$ENUM:EVENT_TRACE_FLAG_IMAGE_LOAD|EVENT_TRACE_FLAG_DISK_IO|@#$UNKNOWNVALUE:20"
  103. "NumberOfBuffers:ULONG:8"
  104. "FreeBuffers:ULONG:9"
  105. "EventsLost:ULONG:10"
  106. "BuffersWritten:ULONG:11"
  107. "LogBuffersLost:ULONG:12"
  108. "RealTimeBuffersLost:ULONG:13"
  109. "AgeLimit:LONG:-14"
  110. "LoggerThreadId:HANDLE:0000000F"
  111. "LogFileName:TCHAR*:Log file name"
  112. "LoggerName:TCHAR*:Logger name"
  113. "_EVENT_TRACE_PROPERTIES Instance End"
  114. #endif
  115. void CEventTraceProperties::InitializeMemberVar(TCHAR *tcBuffer, int nVar)
  116. {
  117. int nDebug = 0;
  118. if (nVar == 17 || nVar == 18)
  119. {
  120. nDebug = nVar;
  121. }
  122. t_string tsTemp;
  123. tsTemp = tcBuffer;
  124. int nPos = tsTemp.find(_T(":"), 0);
  125. // The first character in the type.
  126. int nPosType = nPos + 1;
  127. if (nPos == t_string::npos)
  128. {
  129. m_bDeSerializationOK = false;
  130. return;
  131. }
  132. nPos = tsTemp.find(_T(":"), nPos + 1);
  133. t_string tsType;
  134. tsType = tsTemp.substr(nPosType,nPos - nPosType);
  135. // The first chatacter in the value.
  136. ++nPos;
  137. t_string tsValue;
  138. tsValue = tsTemp.substr(nPos, (tsTemp.length() - nPos) - 1);
  139. // a TCHAR * value
  140. if (tsType.compare(_T("TCHAR*")) == 0)
  141. {
  142. InitializeTCHARVar(tsValue ,m_pVarArray[nVar]);
  143. }
  144. // A #define value
  145. else if (tsType.compare(_T("@#$ENUM")) == 0)
  146. {
  147. InitializeEnumVar(tsValue , m_pVarArray[nVar]);
  148. }
  149. // A HEX
  150. else if (tsType.compare(_T("HANDLE")) == 0)
  151. {
  152. InitializeHandleVar(tsValue , m_pVarArray[nVar]);
  153. }
  154. // An unsigned value
  155. else if (tsType.compare(_T("ULONG")) == 0)
  156. {
  157. InitializeULONGVar(tsValue , m_pVarArray[nVar]);
  158. }
  159. // A long value
  160. else if (tsType.compare(_T("LONG")) == 0)
  161. {
  162. InitializeLONGVar(tsValue , m_pVarArray[nVar]);
  163. }
  164. // A long value
  165. else if (tsType.compare(_T("GUID")) == 0)
  166. {
  167. InitializeGUIDVar(tsValue , m_pVarArray[nVar]);
  168. }
  169. }
  170. void CEventTraceProperties::Initialize
  171. (PEVENT_TRACE_PROPERTIES pProps)
  172. {
  173. m_bDeSerializationOK = true;
  174. if (pProps == NULL)
  175. {
  176. m_pProps = NULL;
  177. m_bIsNULL = true;
  178. }
  179. m_pProps = (EVENT_TRACE_PROPERTIES *) malloc (sizeof(EVENT_TRACE_PROPERTIES));
  180. RtlZeroMemory(m_pProps, sizeof(EVENT_TRACE_PROPERTIES));
  181. *m_pProps = *pProps;
  182. m_pProps -> LogFileName = pProps -> LogFileName ?
  183. NewTCHAR(pProps -> LogFileName) :
  184. NULL;
  185. m_pProps -> LoggerName = pProps -> LoggerName ?
  186. NewTCHAR(pProps -> LoggerName) :
  187. NULL;
  188. m_pProps -> Wnode.BufferSize = sizeof(*m_pProps);
  189. m_bIsNULL = false;
  190. }
  191. PEVENT_TRACE_PROPERTIES
  192. CEventTraceProperties::GetEventTracePropertiesInstance()
  193. {
  194. if (m_pProps == NULL)
  195. {
  196. return NULL;
  197. }
  198. EVENT_TRACE_PROPERTIES *pProps =
  199. (EVENT_TRACE_PROPERTIES *) malloc (sizeof(EVENT_TRACE_PROPERTIES));
  200. RtlZeroMemory(pProps, sizeof(EVENT_TRACE_PROPERTIES));
  201. *pProps = *m_pProps;
  202. pProps -> LogFileName = m_pProps -> LogFileName ?
  203. NewTCHAR(m_pProps -> LogFileName) :
  204. NULL;
  205. pProps -> LoggerName = m_pProps -> LoggerName ?
  206. NewTCHAR(m_pProps -> LoggerName) :
  207. NULL;
  208. int n1 = sizeof(*pProps);
  209. int n2 = sizeof(EVENT_TRACE_PROPERTIES);
  210. pProps -> Wnode.BufferSize = sizeof(*pProps);
  211. return pProps;
  212. }
  213. HRESULT CEventTraceProperties::Persist (CPersistor &rPersistor)
  214. {
  215. if (rPersistor.IsLoading())
  216. {
  217. rPersistor.Stream() >> *this;
  218. }
  219. else
  220. {
  221. rPersistor.Stream() << *this;
  222. }
  223. return S_OK;
  224. }
  225. t_ostream& operator<<(t_ostream &ros, const CEventTraceProperties &r)
  226. {
  227. t_string tsOut;
  228. tsOut = _T("\"_EVENT_TRACE_PROPERTIES Instance Begin\"\n");
  229. PutALine(ros, tsOut.c_str());
  230. // "Wnode.Guid:GUID:{0000cbd1-0011-11d0-0d00-00aa006d010a}"
  231. // "Wnode.Flags:@#$ENUM:WNODE_FLAG_ALL_DATA"
  232. tsOut = _T("\"Wnode.Guid:GUID:");
  233. PutALine(ros, tsOut.c_str());
  234. GUIDOut(ros, r.m_pProps -> Wnode.Guid);
  235. tsOut = g_tcDQuote;
  236. tsOut += g_atcNL;
  237. PutALine(ros, tsOut.c_str());
  238. WnodeFlagsOut(ros, r.m_pProps -> Wnode.Flags);
  239. tsOut = _T("\"BufferSize:ULONG:");
  240. PutALine(ros, tsOut.c_str());
  241. PutAULONGVar(ros, r.m_pProps -> BufferSize);
  242. tsOut = g_tcDQuote;
  243. tsOut += g_atcNL;
  244. PutALine(ros, tsOut.c_str());
  245. tsOut = _T("\"MinimunBuffers:ULONG:");
  246. PutALine(ros, tsOut.c_str());
  247. PutAULONGVar(ros, r.m_pProps -> MinimumBuffers);
  248. tsOut = g_tcDQuote;
  249. tsOut += g_atcNL;
  250. PutALine(ros, tsOut.c_str());
  251. tsOut = _T("\"MaximunBuffers:ULONG:");
  252. PutALine(ros, tsOut.c_str());
  253. PutAULONGVar(ros, r.m_pProps -> MaximumBuffers);
  254. tsOut = g_tcDQuote;
  255. tsOut += g_atcNL;
  256. PutALine(ros, tsOut.c_str());
  257. tsOut = _T("\"MaximunFileSize:ULONG:");
  258. PutALine(ros, tsOut.c_str());
  259. PutAULONGVar(ros, r.m_pProps -> MaximumFileSize);
  260. tsOut = g_tcDQuote;
  261. tsOut += g_atcNL;
  262. PutALine(ros, tsOut.c_str());
  263. LogFileModeOut(ros, r.m_pProps -> LogFileMode );
  264. tsOut = _T("\"FlushTimer:ULONG:");
  265. PutALine(ros, tsOut.c_str());
  266. PutAULONGVar(ros, r.m_pProps -> FlushTimer);
  267. tsOut = g_tcDQuote;
  268. tsOut += g_atcNL;
  269. PutALine(ros, tsOut.c_str());
  270. EnableFlagsOut(ros,r.m_pProps -> EnableFlags);
  271. // ros << _T("\"NumberOfBuffers:ULONG:") << r.m_pProps -> NumberOfBuffers << g_tcDQuote << g_atcNL;
  272. tsOut = _T("\"NumberOfBuffers:ULONG:");
  273. PutALine(ros, tsOut.c_str());
  274. PutAULONGVar(ros, r.m_pProps -> NumberOfBuffers);
  275. tsOut = g_tcDQuote;
  276. tsOut += g_atcNL;
  277. PutALine(ros, tsOut.c_str());
  278. // ros << _T("\"FreeBuffers:ULONG:") << r.m_pProps -> FreeBuffers << g_tcDQuote << g_atcNL;
  279. tsOut = _T("\"FreeBuffers:ULONG:");
  280. PutALine(ros, tsOut.c_str());
  281. PutAULONGVar(ros, r.m_pProps -> FreeBuffers);
  282. tsOut = g_tcDQuote;
  283. tsOut += g_atcNL;
  284. PutALine(ros, tsOut.c_str());
  285. // ros << _T("\"EventsLost:ULONG:") << r.m_pProps -> EventsLost << g_tcDQuote << g_atcNL;
  286. tsOut = _T("\"EventsLost:ULONG:");
  287. PutALine(ros, tsOut.c_str());
  288. PutAULONGVar(ros, r.m_pProps -> EventsLost);
  289. tsOut = g_tcDQuote;
  290. tsOut += g_atcNL;
  291. PutALine(ros, tsOut.c_str());
  292. // ros << _T("\"BuffersWritten:ULONG:") << r.m_pProps -> BuffersWritten << g_tcDQuote << g_atcNL;
  293. tsOut = _T("\"BuffersWritten:ULONG:");
  294. PutALine(ros, tsOut.c_str());
  295. PutAULONGVar(ros, r.m_pProps -> BuffersWritten);
  296. tsOut = g_tcDQuote;
  297. tsOut += g_atcNL;
  298. PutALine(ros, tsOut.c_str());
  299. // ros << _T("\"LogBuffersLost:ULONG:") << r.m_pProps -> LogBuffersLost << g_tcDQuote << g_atcNL;
  300. tsOut = _T("\"LogBuffersLost:ULONG:");
  301. PutALine(ros, tsOut.c_str());
  302. PutAULONGVar(ros, r.m_pProps -> LogBuffersLost);
  303. tsOut = g_tcDQuote;
  304. tsOut += g_atcNL;
  305. PutALine(ros, tsOut.c_str());
  306. // ros << _T("\"RealTimeBuffersLost:ULONG:") << r.m_pProps -> RealTimeBuffersLost << g_tcDQuote <<g_atcNL;
  307. tsOut = _T("\"RealTimeBuffersLost:ULONG:");
  308. PutALine(ros, tsOut.c_str());
  309. PutAULONGVar(ros, r.m_pProps -> RealTimeBuffersLost);
  310. tsOut = g_tcDQuote;
  311. tsOut += g_atcNL;
  312. PutALine(ros, tsOut.c_str());
  313. // ros << _T("\"AgeLimit:LONG:") << r.m_pProps -> AgeLimit << g_tcDQuote <<g_atcNL;
  314. tsOut = _T("\"AgeLimit:ULONG:");
  315. PutALine(ros, tsOut.c_str());
  316. PutALONGVar(ros, r.m_pProps -> AgeLimit);
  317. tsOut = g_tcDQuote;
  318. tsOut += g_atcNL;
  319. PutALine(ros, tsOut.c_str());
  320. // Handles are in hex.
  321. // ros << _T("\"LoggerThreadId:HANDLE:0x") << r.m_pProps -> LoggerThreadId << g_tcDQuote <<g_atcNL;
  322. tsOut = _T("\"LoggerThreadId:HANDLE:0x");
  323. PutALine(ros, tsOut.c_str());
  324. PutAULONGVar(ros, (ULONG) r.m_pProps -> LoggerThreadId, true);
  325. tsOut = g_tcDQuote;
  326. tsOut += g_atcNL;
  327. PutALine(ros, tsOut.c_str());
  328. // ros << _T("\"LogFileName:TCHAR*:") << r.m_pProps -> LogFileName << g_tcDQuote <<g_atcNL;
  329. tsOut = _T("\"LogFileName:TCHAR*:");
  330. if (r.m_pProps -> LogFileName)
  331. {
  332. tsOut += r.m_pProps -> LogFileName;
  333. }
  334. tsOut += g_tcDQuote;
  335. tsOut += g_atcNL;
  336. PutALine(ros, tsOut.c_str());
  337. // ros << _T("\"LoggerName:TCHAR*:") << r.m_pProps -> LoggerName << g_tcDQuote <<g_atcNL;
  338. tsOut = _T("\"LoggerName:TCHAR*:");
  339. if (r.m_pProps -> LoggerName)
  340. {
  341. tsOut += r.m_pProps -> LoggerName;
  342. }
  343. tsOut += g_tcDQuote;
  344. tsOut += g_atcNL;
  345. PutALine(ros, tsOut.c_str());
  346. tsOut = _T("\"_EVENT_TRACE_PROPERTIES Instance End\"\n");
  347. PutALine(ros, tsOut.c_str());
  348. return ros;
  349. }
  350. t_istream& operator>>(t_istream &ris,CEventTraceProperties &r)
  351. {
  352. r.m_bDeSerializationOK = true;
  353. // We are doing line oriented serailization and assume that
  354. // a line in the stream is 1024 or less TCHARS.
  355. TCHAR *ptcBuffer = (TCHAR *) malloc(MAX_LINE * sizeof(TCHAR));
  356. GetALine(ris, ptcBuffer,MAX_LINE);
  357. if (_tcscmp(ptcBuffer,_T("\"_EVENT_TRACE_PROPERTIES Instance NULL\"")) == 0)
  358. {
  359. r.~CEventTraceProperties();
  360. r.m_bDeSerializationOK = false;
  361. free(ptcBuffer);
  362. return ris;
  363. }
  364. if (_tcscmp(ptcBuffer,_T("\"_EVENT_TRACE_PROPERTIES Instance Begin\"")) != 0)
  365. {
  366. r.m_bDeSerializationOK = false;
  367. free(ptcBuffer);
  368. return ris;
  369. }
  370. r.~CEventTraceProperties();
  371. r.m_pProps = (EVENT_TRACE_PROPERTIES *) malloc (sizeof(EVENT_TRACE_PROPERTIES));
  372. RtlZeroMemory(r.m_pProps, sizeof(EVENT_TRACE_PROPERTIES));
  373. r.m_pProps->Wnode.BufferSize = sizeof(EVENT_TRACE_PROPERTIES);
  374. // "Wnode.Guid:GUID:{0000cbd1-0011-11d0-0d00-00aa006d010a}"
  375. // "Wnode.Flags:@#$ENUM:WNODE_FLAG_ALL_DATA"
  376. r.m_pVarArray[0] = &r.m_pProps->Wnode.Guid;
  377. r.m_pVarArray[1] = &r.m_pProps->Wnode.Flags;
  378. r.m_pVarArray[2] = &r.m_pProps->BufferSize;
  379. r.m_pVarArray[3] = &r.m_pProps->MinimumBuffers;
  380. r.m_pVarArray[4] = &r.m_pProps->MaximumBuffers;
  381. r.m_pVarArray[5] = &r.m_pProps->MaximumFileSize;
  382. r.m_pVarArray[6] = &r.m_pProps->LogFileMode;
  383. r.m_pVarArray[7] = &r.m_pProps->FlushTimer;
  384. r.m_pVarArray[8] = &r.m_pProps->EnableFlags;
  385. r.m_pVarArray[9] = &r.m_pProps->NumberOfBuffers;
  386. r.m_pVarArray[10] = &r.m_pProps->FreeBuffers;
  387. r.m_pVarArray[11] = &r.m_pProps->EventsLost;
  388. r.m_pVarArray[12] = &r.m_pProps->BuffersWritten;
  389. r.m_pVarArray[13] = &r.m_pProps->LogBuffersLost;
  390. r.m_pVarArray[14] = &r.m_pProps->RealTimeBuffersLost;
  391. r.m_pVarArray[15] = &r.m_pProps->AgeLimit;
  392. r.m_pVarArray[16] = &r.m_pProps->LoggerThreadId;
  393. r.m_pVarArray[17] = &r.m_pProps->LogFileName;
  394. r.m_pVarArray[18] = &r.m_pProps->LoggerName;
  395. int n = 0;
  396. while (n < 19 && GetALine(ris,ptcBuffer,MAX_LINE))
  397. {
  398. r.InitializeMemberVar(ptcBuffer,n++);
  399. }
  400. // Consume end of Props
  401. GetALine(ris,ptcBuffer,MAX_LINE);
  402. free(ptcBuffer);
  403. BOOL bHeapGood = HeapValidate(GetProcessHeap(), 0, NULL);
  404. return ris;
  405. }