Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
13 KiB

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