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.

658 lines
15 KiB

  1. // StartTrace.cpp : Defines the entry point for the DLL application.
  2. //
  3. //***************************************************************************
  4. //
  5. // judyp May 1999
  6. //
  7. //***************************************************************************
  8. #include "stdafx.h"
  9. #pragma warning (disable : 4786)
  10. #pragma warning (disable : 4275)
  11. #include <iostream>
  12. #include <strstream>
  13. #include <fstream>
  14. #include <fcntl.h>
  15. #include <io.h>
  16. #include <string>
  17. #include <sstream>
  18. #include <map>
  19. #include <list>
  20. using namespace std;
  21. #include <malloc.h>
  22. #include <tchar.h>
  23. #include <windows.h>
  24. #ifdef NONNT5
  25. typedef unsigned long ULONG_PTR;
  26. #endif
  27. #include <wmistr.h>
  28. #include <guiddef.h>
  29. #include <initguid.h>
  30. #include <evntrace.h>
  31. #include <WTYPES.H>
  32. #include "t_string.h"
  33. #include "Persistor.h"
  34. #include "Logger.h"
  35. #include "TCOData.h"
  36. #include "Utilities.h"
  37. #include "StructureWrappers.h"
  38. #include "StructureWapperHelpers.h"
  39. #include "ConstantMap.h"
  40. #include "CollectionControl.h"
  41. extern CConstantMap g_ConstantMap;
  42. #define MAX_LINE 2048
  43. int ParseGuids
  44. (
  45. TCHAR *ptcBuffer,
  46. TCOData *pstructTCOData,
  47. LPTSTR *plptstrErrorDesc
  48. );
  49. int ParseExeData
  50. (
  51. t_string &tsData,
  52. int &nExes,
  53. LPTSTR *&lptstrArray,
  54. LPTSTR *plptstrErrorDesc
  55. );
  56. // If an error occurs you users of this function must delete
  57. // plptstrErrorDesc. It will contain a string describing
  58. // the error.
  59. int GetAllTCOData
  60. (
  61. IN LPCTSTR lpctstrFile,
  62. OUT TCOData **pstructTCOData,
  63. OUT TCOFunctionalData **pstructTCOFunctionalData,
  64. OUT LPTSTR *plptstrErrorDesc, // Any error we had.
  65. IN bool bGetFunctionalData
  66. )
  67. {
  68. *pstructTCOData = (TCOData *) malloc (sizeof(TCOData));
  69. RtlZeroMemory(*pstructTCOData , sizeof(TCOData));
  70. if (bGetFunctionalData)
  71. {
  72. *pstructTCOFunctionalData = (TCOFunctionalData *) malloc(sizeof(TCOFunctionalData));
  73. RtlZeroMemory(*pstructTCOFunctionalData , sizeof(TCOFunctionalData));
  74. }
  75. LPSTR lpstrFile;
  76. #ifdef UNICODE
  77. lpstrFile = NewLPSTR(lpctstrFile);
  78. #else
  79. lpstrFile = NewTCHAR(lpctstrFile);
  80. #endif
  81. CPersistor PersistorIn
  82. (lpstrFile,
  83. ios::in | 0x20, // ios::nocreate = 0x20 - cannot get to compile!!!
  84. true );
  85. HRESULT hr = PersistorIn.Open();
  86. if (FAILED(hr))
  87. {
  88. t_string tsTemp;
  89. tsTemp = _T("TCOData error: Could not open file or file was not in correct character set (Unicode or ANSI) for file ");
  90. t_string tsFile;
  91. #ifdef _UNICODE
  92. LPWSTR lpwstrTemp = NewLPWSTR(lpstrFile);
  93. tsFile = lpwstrTemp;
  94. free(lpwstrTemp);
  95. #else
  96. tsFile = lpstrFile;
  97. #endif
  98. tsTemp += tsFile;
  99. free (lpstrFile);
  100. lpstrFile = NULL;
  101. tsTemp += _T(".");
  102. *plptstrErrorDesc = NewTCHAR(tsTemp.c_str());
  103. return -1;
  104. }
  105. free (lpstrFile);
  106. lpstrFile = NULL;
  107. int nReturn =
  108. GetTCOData
  109. (
  110. PersistorIn,
  111. *pstructTCOData,
  112. plptstrErrorDesc // Any error we had.
  113. );
  114. if (nReturn != ERROR_SUCCESS)
  115. {
  116. PersistorIn.Close();
  117. return nReturn;
  118. }
  119. if (bGetFunctionalData)
  120. {
  121. nReturn =
  122. TCOFunctionalObjects
  123. (
  124. PersistorIn,
  125. *pstructTCOFunctionalData,
  126. plptstrErrorDesc // Describes error this function had.
  127. );
  128. }
  129. PersistorIn.Close();
  130. return nReturn;
  131. }
  132. // If an error occurs you users of this function must delete
  133. // plptstrErrorDesc. It will contain a string describing
  134. // the error.
  135. int GetTCOData
  136. (
  137. IN CPersistor &PersistorIn,
  138. OUT TCOData *pstructTCOData,
  139. OUT LPTSTR *plptstrErrorDesc // Any error we had.
  140. )
  141. {
  142. RtlZeroMemory(pstructTCOData , sizeof(TCOData));
  143. // We are doing line oriented serailization and assume that
  144. // a line in the stream is 1024 or less TCHARS.
  145. TCHAR *ptcBuffer = (TCHAR *) malloc(MAX_LINE * sizeof(TCHAR));
  146. *plptstrErrorDesc = NULL;
  147. // Short description
  148. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  149. pstructTCOData->m_lptstrShortDesc = NewTCHAR(ptcBuffer);
  150. // Long description.
  151. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  152. pstructTCOData->m_lptstrLongDesc = NewTCHAR(ptcBuffer);
  153. // Expected result had better be in the Constant map.
  154. // Constant map is used to map a string to an undsigned int.
  155. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  156. t_string tsTemp;
  157. tsTemp = ptcBuffer;
  158. CONSTMAP::iterator Iterator;
  159. Iterator = g_ConstantMap.m_Map.find(tsTemp);
  160. // If you do not find your value in the map look in
  161. // ConstantMap.cpp. You probably forgot to add it;->
  162. if (Iterator == g_ConstantMap.m_Map.end())
  163. {
  164. *plptstrErrorDesc = NewTCHAR(_T("TCOData error: Expected error is not in map"));
  165. free(ptcBuffer);
  166. return -1;
  167. }
  168. else
  169. {
  170. pstructTCOData->m_lptstrExpectedResult = NewTCHAR(ptcBuffer);
  171. pstructTCOData->m_ulExpectedResult = (*Iterator).second;
  172. }
  173. // TraceHandle values are VALUE_VALID or VALUE_NULL
  174. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  175. tsTemp = ptcBuffer;
  176. if (case_insensitive_compare(tsTemp,_T("VALUE_VALID")) == 0)
  177. {
  178. pstructTCOData->m_pTraceHandle =
  179. (TRACEHANDLE *) malloc (sizeof(TRACEHANDLE));
  180. *pstructTCOData->m_pTraceHandle = NULL;
  181. }
  182. else if (case_insensitive_compare(tsTemp,_T("VALUE_NULL")) == 0)
  183. {
  184. pstructTCOData->m_pTraceHandle = (TRACEHANDLE *) NULL;
  185. }
  186. else
  187. {
  188. *plptstrErrorDesc =
  189. NewTCHAR
  190. (_T("TCOData error: Error in value of TraceHandle. Valid values are \"VALUE_VALID\" or \"VALUE_NULL\"."));
  191. free(ptcBuffer);
  192. return -1;
  193. }
  194. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  195. tsTemp = ptcBuffer;
  196. InitializeTCHARVar(tsTemp , (void *) &pstructTCOData->m_lptstrInstanceName);
  197. // API test - valid values 0 - 6
  198. // OtherTest = 0,
  199. // StartTraceTest = 1,
  200. // StopTraceTest = 2,
  201. // EnableTraceTest = 3,
  202. // QueryTraceTest = 4,
  203. // UpdateTrace = 5,
  204. // QueryAllTraces = 6
  205. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  206. tsTemp = ptcBuffer;
  207. InitializeULONGVar(tsTemp , (void *) &pstructTCOData->m_ulAPITest);
  208. if (pstructTCOData->m_ulAPITest < 0 || pstructTCOData->m_ulAPITest > 6)
  209. {
  210. *plptstrErrorDesc =
  211. NewTCHAR
  212. (_T("TCOData error: Error in value of m_ulAPITest. Valid values are 0 - 6. See enum in TCOData.h"));
  213. free(ptcBuffer);
  214. return -1;
  215. }
  216. // Valid values are KERNEL_LOGGER or PRIVATE_LOGGER
  217. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  218. pstructTCOData->m_lptstrLoggerMode = NewTCHAR(ptcBuffer);
  219. // Enable is used for EnableTrace.
  220. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  221. tsTemp = ptcBuffer;
  222. if (case_insensitive_compare(tsTemp.substr(0,7),_T("ENABLE:")) != 0)
  223. {
  224. *plptstrErrorDesc =
  225. NewTCHAR
  226. (_T("TCOData error: Enable: expected."));
  227. free(ptcBuffer);
  228. return -1;
  229. }
  230. else
  231. {
  232. InitializeULONGVar(tsTemp.substr(7) , &pstructTCOData->m_ulEnable);
  233. }
  234. // EnableFlag is used for EnableTrace and is passed to the provider.
  235. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  236. tsTemp = ptcBuffer;
  237. if (case_insensitive_compare(tsTemp.substr(0,11),_T("ENABLEFLAG:")) != 0)
  238. {
  239. *plptstrErrorDesc =
  240. NewTCHAR
  241. (_T("TCOData error: EnableFlag: expected."));
  242. free(ptcBuffer);
  243. return -1;
  244. }
  245. else
  246. {
  247. InitializeHandleVar(tsTemp.substr(11) , &pstructTCOData->m_ulEnableFlag);
  248. }
  249. // EnableLevel is used for EnableTrace and is passed to the provider.
  250. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  251. tsTemp = ptcBuffer;
  252. if (case_insensitive_compare(tsTemp.substr(0,12),_T("ENABLELEVEL:")) != 0)
  253. {
  254. *plptstrErrorDesc =
  255. NewTCHAR
  256. (_T("TCOData error: EnableLevel: expected."));
  257. free(ptcBuffer);
  258. return -1;
  259. }
  260. else
  261. {
  262. InitializeHandleVar(tsTemp.substr(12) , &pstructTCOData->m_ulEnableLevel);
  263. }
  264. CEventTraceProperties cPropsIn;
  265. // This has to be mofified to allow a NULL strucutre.
  266. cPropsIn.Persist( PersistorIn);
  267. pstructTCOData->m_pProps = cPropsIn.GetEventTracePropertiesInstance();
  268. if (pstructTCOData->m_pProps &&
  269. case_insensitive_compare(tsTemp,_T("PRIVATE_LOGGER")) == 0)
  270. {
  271. pstructTCOData->m_pProps->LogFileMode =
  272. pstructTCOData->m_pProps->LogFileMode | EVENT_TRACE_PRIVATE_LOGGER_MODE;
  273. }
  274. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  275. int nReturn = ParseGuids(ptcBuffer, pstructTCOData, plptstrErrorDesc);
  276. if(nReturn != ERROR_SUCCESS)
  277. {
  278. return nReturn;
  279. }
  280. // Validator
  281. if (PersistorIn.Stream().eof() == false)
  282. {
  283. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  284. pstructTCOData->m_lptstrValidator = NewTCHAR(ptcBuffer);
  285. }
  286. free(ptcBuffer);
  287. return 0;
  288. }
  289. // If an error occurs you users of this function must delete
  290. // plptstrErrorDesc. It will contain a string describing
  291. // the error.
  292. int TCOFunctionalObjects
  293. ( IN CPersistor &PersistorIn,
  294. IN OUT TCOFunctionalData *pstructTCOFunctionalData,
  295. OUT LPTSTR *plptstrErrorDesc // Describes error this function had.
  296. )
  297. {
  298. // We are doing line oriented serailization and assume that
  299. // a line in the stream is 1024 or less TCHARS.
  300. TCHAR *ptcBuffer = (TCHAR *) malloc(MAX_LINE * sizeof(TCHAR));
  301. *plptstrErrorDesc = NULL;
  302. t_string tsTemp;
  303. t_string tsError;
  304. t_string tsSubstr;
  305. if (PersistorIn.Stream().eof() == false)
  306. {
  307. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  308. tsTemp = ptcBuffer;
  309. tsSubstr = tsTemp.substr(0,9);
  310. if (case_insensitive_compare(tsSubstr,_T("provider:")) == 0)
  311. {
  312. tsSubstr = tsTemp.substr(9);
  313. int nReturn =
  314. ParseExeData
  315. (
  316. tsSubstr,
  317. pstructTCOFunctionalData->m_nProviders,
  318. pstructTCOFunctionalData->m_lptstrProviderArray,
  319. plptstrErrorDesc
  320. );
  321. if (nReturn != ERROR_SUCCESS)
  322. {
  323. tsError = _T("Invalid providers argument: ");
  324. tsError += tsTemp;
  325. tsError += _T(".");
  326. *plptstrErrorDesc = NewTCHAR(tsError.c_str());
  327. free(ptcBuffer);
  328. return -1;
  329. }
  330. }
  331. else
  332. {
  333. tsError = _T("Invalid providers argument: ");
  334. tsError += tsTemp;
  335. tsError += _T(".");
  336. *plptstrErrorDesc = NewTCHAR(tsError.c_str());
  337. free(ptcBuffer);
  338. return -1;
  339. }
  340. }
  341. // We may have a DataProvider. If not we us our default.
  342. if (PersistorIn.Stream().eof() == false)
  343. {
  344. GetALine(PersistorIn.Stream(),ptcBuffer, MAX_LINE);
  345. tsTemp = ptcBuffer;
  346. tsSubstr = tsTemp.substr(0,9);
  347. if (case_insensitive_compare(tsSubstr,_T("consumer:")) == 0)
  348. {
  349. tsSubstr = tsTemp.substr(9);
  350. int nReturn =
  351. ParseExeData
  352. (
  353. tsSubstr,
  354. pstructTCOFunctionalData->m_nConsumers,
  355. pstructTCOFunctionalData->m_lptstrConsumerArray,
  356. plptstrErrorDesc
  357. );
  358. if (nReturn != ERROR_SUCCESS)
  359. {
  360. tsError = _T("Invalid consumers argument: ");
  361. tsError += tsTemp;
  362. tsError += _T(".");
  363. *plptstrErrorDesc = NewTCHAR(tsError.c_str());
  364. free(ptcBuffer);
  365. return -1;
  366. }
  367. }
  368. else
  369. {
  370. tsError = _T("Invalid consumers argument: ");
  371. tsError += tsTemp;
  372. tsError += _T(".");
  373. *plptstrErrorDesc = NewTCHAR(tsError.c_str());
  374. free(ptcBuffer);
  375. return -1;
  376. }
  377. }
  378. free(ptcBuffer);
  379. return 0;
  380. }
  381. void FreeTCOData (TCOData *pstructTCOData)
  382. {
  383. if (!pstructTCOData)
  384. {
  385. return;
  386. }
  387. free(pstructTCOData->m_lptstrShortDesc);
  388. free(pstructTCOData->m_lptstrLongDesc);
  389. free(pstructTCOData->m_lptstrExpectedResult);
  390. free(pstructTCOData->m_pTraceHandle);
  391. free(pstructTCOData->m_lptstrInstanceName);
  392. free(pstructTCOData->m_lptstrLoggerMode);
  393. free(pstructTCOData->m_lpguidArray);
  394. if (pstructTCOData->m_pProps)
  395. {
  396. free(pstructTCOData->m_pProps->LoggerName);
  397. free(pstructTCOData->m_pProps->LogFileName);
  398. }
  399. free(pstructTCOData->m_pProps);
  400. free(pstructTCOData->m_lptstrValidator);
  401. free(pstructTCOData);
  402. }
  403. void FreeTCOFunctionalData (TCOFunctionalData *pstructTCOFunctionalData)
  404. {
  405. if (!pstructTCOFunctionalData)
  406. {
  407. return;
  408. }
  409. int i;
  410. TCHAR *pTemp;
  411. for (i = 0; i < pstructTCOFunctionalData->m_nProviders; i++)
  412. {
  413. pTemp = pstructTCOFunctionalData->m_lptstrProviderArray[i];
  414. free (pTemp);
  415. }
  416. free (pstructTCOFunctionalData->m_lptstrProviderArray);
  417. for (i = 0; i < pstructTCOFunctionalData->m_nConsumers; i++)
  418. {
  419. pTemp = pstructTCOFunctionalData->m_lptstrConsumerArray[i];
  420. free (pTemp);
  421. }
  422. free (pstructTCOFunctionalData->m_lptstrConsumerArray);
  423. free(pstructTCOFunctionalData);
  424. }
  425. int ParseExeData
  426. (
  427. t_string &tsData,
  428. int &nExes,
  429. LPTSTR *&lptstrArray,
  430. LPTSTR *plptstrErrorDesc
  431. )
  432. {
  433. // Embedded " are not allowed in the command line. Had to draw
  434. // the line somewhere.
  435. // Tokenize on "," and " at end of line.
  436. list <t_string> listExes;
  437. bool bDone = false;
  438. int nBeg = 0;
  439. int nFind = tsData.find(_T(","), nBeg);
  440. t_string tsExe;
  441. while (!bDone)
  442. {
  443. if (nFind != t_string::npos)
  444. {
  445. tsExe = tsData.substr(nBeg,nFind - nBeg);
  446. listExes.push_back(tsExe);
  447. tsExe.erase();
  448. }
  449. else
  450. {
  451. tsExe = tsData.substr(nBeg,t_string::npos);
  452. listExes.push_back(tsExe);
  453. bDone = true;
  454. tsExe.erase();
  455. }
  456. nBeg = nFind + 1;
  457. nFind = tsData.find(_T(","), nBeg);
  458. }
  459. // Allocate the Exe array
  460. nExes = listExes.size();
  461. lptstrArray =
  462. (TCHAR **) malloc (sizeof(TCHAR *) * nExes);
  463. RtlZeroMemory
  464. (lptstrArray,
  465. sizeof(sizeof(TCHAR *) * nExes));
  466. list<t_string>::iterator pListExes;
  467. int i = 0;
  468. for (pListExes = listExes.begin(); pListExes != listExes.end() ; ++pListExes)
  469. {
  470. tsExe = (*pListExes);
  471. lptstrArray[i++] = NewTCHAR(tsExe.c_str());
  472. }
  473. return ERROR_SUCCESS;
  474. }
  475. int ParseGuids
  476. (
  477. TCHAR *ptcBuffer,
  478. TCOData *pstructTCOData,
  479. LPTSTR *plptstrErrorDesc
  480. )
  481. {
  482. // Is Wnode does not have a GUID put the first one from list in it.
  483. t_string tsTemp;
  484. tsTemp = ptcBuffer;
  485. if (case_insensitive_compare(tsTemp.substr(0,6),_T("guids:")) != 0)
  486. {
  487. tsTemp.erase();
  488. tsTemp = _T("Invalid Guids entry: ");
  489. tsTemp += ptcBuffer;
  490. tsTemp += _T(".");
  491. *plptstrErrorDesc = NewTCHAR(tsTemp.c_str());
  492. return -1;
  493. }
  494. // Count the commas
  495. int nFind = tsTemp.find(_T(','));
  496. t_string tsGuid;
  497. int nBeg = 6;
  498. if(nBeg == tsTemp.length())
  499. {
  500. pstructTCOData->m_nGuids = 0;
  501. pstructTCOData->m_lpguidArray = NULL;
  502. return 0;
  503. }
  504. // We only have one GUID.
  505. if (nFind == t_string::npos)
  506. {
  507. tsGuid = tsTemp.substr(nBeg,nFind - nBeg);
  508. // Allocate the GUID array
  509. pstructTCOData->m_nGuids = 1;
  510. pstructTCOData->m_lpguidArray =
  511. (GUID *) malloc (sizeof(GUID) * pstructTCOData->m_nGuids);
  512. RtlZeroMemory
  513. (pstructTCOData->m_lpguidArray ,
  514. sizeof(sizeof(GUID) * pstructTCOData->m_nGuids));
  515. // Just one GUID, thank you.
  516. wGUIDFromString(tsGuid.c_str(), &pstructTCOData->m_lpguidArray[0]);
  517. return 0;
  518. }
  519. // We have more than one GUID.
  520. bool bDone = false;
  521. list <t_string> listGuids;
  522. while (!bDone)
  523. {
  524. if (nFind != t_string::npos)
  525. {
  526. tsGuid = tsTemp.substr(nBeg,nFind - nBeg);
  527. listGuids.push_back(tsGuid);
  528. tsGuid.erase();
  529. }
  530. else
  531. {
  532. tsGuid = tsTemp.substr(nBeg,t_string::npos);
  533. listGuids.push_back(tsGuid);
  534. bDone = true;
  535. tsGuid.erase();
  536. }
  537. nBeg = nFind + 1;
  538. nFind = tsTemp.find(',', nBeg);
  539. }
  540. // Allocate the GUID array
  541. pstructTCOData->m_nGuids = listGuids.size();
  542. pstructTCOData->m_lpguidArray =
  543. (GUID *) malloc (sizeof(GUID) * pstructTCOData->m_nGuids);
  544. RtlZeroMemory
  545. (pstructTCOData->m_lpguidArray ,
  546. sizeof(sizeof(GUID) * pstructTCOData->m_nGuids));
  547. list<t_string>::iterator pListGuids;
  548. int i = 0;
  549. for (pListGuids = listGuids.begin(); pListGuids != listGuids.end() ; ++pListGuids)
  550. {
  551. tsGuid = (*pListGuids);
  552. wGUIDFromString(tsGuid.c_str(), &pstructTCOData->m_lpguidArray[i++]);
  553. }
  554. return 0;
  555. }