cmCTestMemCheckHandler.cxx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCTestMemCheckHandler.h"
  4. #include "cmCTest.h"
  5. #include "cmSystemTools.h"
  6. #include "cmXMLParser.h"
  7. #include "cmXMLWriter.h"
  8. #include <cmsys/FStream.hxx>
  9. #include <cmsys/Glob.hxx>
  10. #include <cmsys/RegularExpression.hxx>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <string.h>
  14. struct CatToErrorType
  15. {
  16. const char* ErrorCategory;
  17. int ErrorCode;
  18. };
  19. static CatToErrorType cmCTestMemCheckBoundsChecker[] = {
  20. // Error tags
  21. { "Write Overrun", cmCTestMemCheckHandler::ABW },
  22. { "Read Overrun", cmCTestMemCheckHandler::ABR },
  23. { "Memory Overrun", cmCTestMemCheckHandler::ABW },
  24. { "Allocation Conflict", cmCTestMemCheckHandler::FMM },
  25. { "Bad Pointer Use", cmCTestMemCheckHandler::FMW },
  26. { "Dangling Pointer", cmCTestMemCheckHandler::FMR },
  27. { CM_NULLPTR, 0 }
  28. };
  29. static void xmlReportError(int line, const char* msg, void* data)
  30. {
  31. cmCTest* ctest = (cmCTest*)data;
  32. cmCTestLog(ctest, ERROR_MESSAGE, "Error parsing XML in stream at line "
  33. << line << ": " << msg << std::endl);
  34. }
  35. // parse the xml file containing the results of last BoundsChecker run
  36. class cmBoundsCheckerParser : public cmXMLParser
  37. {
  38. public:
  39. cmBoundsCheckerParser(cmCTest* c)
  40. {
  41. this->CTest = c;
  42. this->SetErrorCallback(xmlReportError, (void*)c);
  43. }
  44. void StartElement(const std::string& name, const char** atts) CM_OVERRIDE
  45. {
  46. if (name == "MemoryLeak" || name == "ResourceLeak") {
  47. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  48. } else if (name == "Error" || name == "Dangling Pointer") {
  49. this->ParseError(atts);
  50. }
  51. // Create the log
  52. std::ostringstream ostr;
  53. ostr << name << ":\n";
  54. int i = 0;
  55. for (; atts[i] != CM_NULLPTR; i += 2) {
  56. ostr << " " << atts[i] << " - " << atts[i + 1] << "\n";
  57. }
  58. ostr << "\n";
  59. this->Log += ostr.str();
  60. }
  61. void EndElement(const std::string& /*name*/) CM_OVERRIDE {}
  62. const char* GetAttribute(const char* name, const char** atts)
  63. {
  64. int i = 0;
  65. for (; atts[i] != CM_NULLPTR; ++i) {
  66. if (strcmp(name, atts[i]) == 0) {
  67. return atts[i + 1];
  68. }
  69. }
  70. return CM_NULLPTR;
  71. }
  72. void ParseError(const char** atts)
  73. {
  74. CatToErrorType* ptr = cmCTestMemCheckBoundsChecker;
  75. const char* cat = this->GetAttribute("ErrorCategory", atts);
  76. if (!cat) {
  77. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  78. cmCTestLog(this->CTest, ERROR_MESSAGE,
  79. "No Category found in Bounds checker XML\n");
  80. return;
  81. }
  82. while (ptr->ErrorCategory && cat) {
  83. if (strcmp(ptr->ErrorCategory, cat) == 0) {
  84. this->Errors.push_back(ptr->ErrorCode);
  85. return; // found it we are done
  86. }
  87. ptr++;
  88. }
  89. if (ptr->ErrorCategory) {
  90. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  91. cmCTestLog(this->CTest, ERROR_MESSAGE,
  92. "Found unknown Bounds Checker error " << ptr->ErrorCategory
  93. << std::endl);
  94. }
  95. }
  96. cmCTest* CTest;
  97. std::vector<int> Errors;
  98. std::string Log;
  99. };
  100. #define BOUNDS_CHECKER_MARKER \
  101. "******######*****Begin BOUNDS CHECKER XML******######******"
  102. cmCTestMemCheckHandler::cmCTestMemCheckHandler()
  103. {
  104. this->MemCheck = true;
  105. this->CustomMaximumPassedTestOutputSize = 0;
  106. this->CustomMaximumFailedTestOutputSize = 0;
  107. this->LogWithPID = false;
  108. }
  109. void cmCTestMemCheckHandler::Initialize()
  110. {
  111. this->Superclass::Initialize();
  112. this->LogWithPID = false;
  113. this->CustomMaximumPassedTestOutputSize = 0;
  114. this->CustomMaximumFailedTestOutputSize = 0;
  115. this->MemoryTester = "";
  116. this->MemoryTesterDynamicOptions.clear();
  117. this->MemoryTesterOptions.clear();
  118. this->MemoryTesterStyle = UNKNOWN;
  119. this->MemoryTesterOutputFile = "";
  120. }
  121. int cmCTestMemCheckHandler::PreProcessHandler()
  122. {
  123. if (!this->InitializeMemoryChecking()) {
  124. return 0;
  125. }
  126. if (!this->ExecuteCommands(this->CustomPreMemCheck)) {
  127. cmCTestLog(this->CTest, ERROR_MESSAGE,
  128. "Problem executing pre-memcheck command(s)." << std::endl);
  129. return 0;
  130. }
  131. return 1;
  132. }
  133. int cmCTestMemCheckHandler::PostProcessHandler()
  134. {
  135. if (!this->ExecuteCommands(this->CustomPostMemCheck)) {
  136. cmCTestLog(this->CTest, ERROR_MESSAGE,
  137. "Problem executing post-memcheck command(s)." << std::endl);
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. void cmCTestMemCheckHandler::GenerateTestCommand(
  143. std::vector<std::string>& args, int test)
  144. {
  145. std::vector<std::string>::size_type pp;
  146. std::string index;
  147. std::ostringstream stream;
  148. std::string memcheckcommand =
  149. cmSystemTools::ConvertToOutputPath(this->MemoryTester.c_str());
  150. stream << test;
  151. index = stream.str();
  152. for (pp = 0; pp < this->MemoryTesterDynamicOptions.size(); pp++) {
  153. std::string arg = this->MemoryTesterDynamicOptions[pp];
  154. std::string::size_type pos = arg.find("??");
  155. if (pos != std::string::npos) {
  156. arg.replace(pos, 2, index);
  157. }
  158. args.push_back(arg);
  159. memcheckcommand += " \"";
  160. memcheckcommand += arg;
  161. memcheckcommand += "\"";
  162. }
  163. // Create a copy of the memory tester environment variable.
  164. // This is used for memory testing programs that pass options
  165. // via environment varaibles.
  166. std::string memTesterEnvironmentVariable =
  167. this->MemoryTesterEnvironmentVariable;
  168. for (pp = 0; pp < this->MemoryTesterOptions.size(); pp++) {
  169. if (!memTesterEnvironmentVariable.empty()) {
  170. // If we are using env to pass options, append all the options to
  171. // this string with space separation.
  172. memTesterEnvironmentVariable += " " + this->MemoryTesterOptions[pp];
  173. }
  174. // for regular options just add them to args and memcheckcommand
  175. // which is just used for display
  176. else {
  177. args.push_back(this->MemoryTesterOptions[pp]);
  178. memcheckcommand += " \"";
  179. memcheckcommand += this->MemoryTesterOptions[pp];
  180. memcheckcommand += "\"";
  181. }
  182. }
  183. // if this is an env option type, then add the env string as a single
  184. // argument.
  185. if (!memTesterEnvironmentVariable.empty()) {
  186. std::string::size_type pos = memTesterEnvironmentVariable.find("??");
  187. if (pos != std::string::npos) {
  188. memTesterEnvironmentVariable.replace(pos, 2, index);
  189. }
  190. memcheckcommand += " " + memTesterEnvironmentVariable;
  191. args.push_back(memTesterEnvironmentVariable);
  192. }
  193. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  194. "Memory check command: " << memcheckcommand << std::endl,
  195. this->Quiet);
  196. }
  197. void cmCTestMemCheckHandler::InitializeResultsVectors()
  198. {
  199. // fill these members
  200. // cmsys::vector<std::string> ResultStrings;
  201. // cmsys::vector<std::string> ResultStringsLong;
  202. // cmsys::vector<int> GlobalResults;
  203. this->ResultStringsLong.clear();
  204. this->ResultStrings.clear();
  205. this->GlobalResults.clear();
  206. // If we are working with style checkers that dynamically fill
  207. // the results strings then return.
  208. if (this->MemoryTesterStyle > cmCTestMemCheckHandler::BOUNDS_CHECKER) {
  209. return;
  210. }
  211. // define the standard set of errors
  212. //----------------------------------------------------------------------
  213. static const char* cmCTestMemCheckResultStrings[] = {
  214. "ABR", "ABW", "ABWL", "COR", "EXU", "FFM", "FIM", "FMM",
  215. "FMR", "FMW", "FUM", "IPR", "IPW", "MAF", "MLK", "MPK",
  216. "NPR", "ODS", "PAR", "PLK", "UMC", "UMR", CM_NULLPTR
  217. };
  218. static const char* cmCTestMemCheckResultLongStrings[] = {
  219. "Threading Problem",
  220. "ABW",
  221. "ABWL",
  222. "COR",
  223. "EXU",
  224. "FFM",
  225. "FIM",
  226. "Mismatched deallocation",
  227. "FMR",
  228. "FMW",
  229. "FUM",
  230. "IPR",
  231. "IPW",
  232. "MAF",
  233. "Memory Leak",
  234. "Potential Memory Leak",
  235. "NPR",
  236. "ODS",
  237. "Invalid syscall param",
  238. "PLK",
  239. "Uninitialized Memory Conditional",
  240. "Uninitialized Memory Read",
  241. CM_NULLPTR
  242. };
  243. this->GlobalResults.clear();
  244. for (int i = 0; cmCTestMemCheckResultStrings[i] != CM_NULLPTR; ++i) {
  245. this->ResultStrings.push_back(cmCTestMemCheckResultStrings[i]);
  246. this->ResultStringsLong.push_back(cmCTestMemCheckResultLongStrings[i]);
  247. this->GlobalResults.push_back(0);
  248. }
  249. }
  250. void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile* mf)
  251. {
  252. this->cmCTestTestHandler::PopulateCustomVectors(mf);
  253. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
  254. this->CustomPreMemCheck);
  255. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
  256. this->CustomPostMemCheck);
  257. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_MEMCHECK_IGNORE",
  258. this->CustomTestsIgnore);
  259. std::string cmake = cmSystemTools::GetCMakeCommand();
  260. this->CTest->SetCTestConfiguration("CMakeCommand", cmake.c_str(),
  261. this->Quiet);
  262. }
  263. void cmCTestMemCheckHandler::GenerateDartOutput(cmXMLWriter& xml)
  264. {
  265. if (!this->CTest->GetProduceXML()) {
  266. return;
  267. }
  268. this->CTest->StartXML(xml, this->AppendXML);
  269. xml.StartElement("DynamicAnalysis");
  270. switch (this->MemoryTesterStyle) {
  271. case cmCTestMemCheckHandler::VALGRIND:
  272. xml.Attribute("Checker", "Valgrind");
  273. break;
  274. case cmCTestMemCheckHandler::PURIFY:
  275. xml.Attribute("Checker", "Purify");
  276. break;
  277. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  278. xml.Attribute("Checker", "BoundsChecker");
  279. break;
  280. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  281. xml.Attribute("Checker", "AddressSanitizer");
  282. break;
  283. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  284. xml.Attribute("Checker", "ThreadSanitizer");
  285. break;
  286. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  287. xml.Attribute("Checker", "MemorySanitizer");
  288. break;
  289. case cmCTestMemCheckHandler::UB_SANITIZER:
  290. xml.Attribute("Checker", "UndefinedBehaviorSanitizer");
  291. break;
  292. default:
  293. xml.Attribute("Checker", "Unknown");
  294. }
  295. xml.Element("StartDateTime", this->StartTest);
  296. xml.Element("StartTestTime", this->StartTestTime);
  297. xml.StartElement("TestList");
  298. cmCTestMemCheckHandler::TestResultsVector::size_type cc;
  299. for (cc = 0; cc < this->TestResults.size(); cc++) {
  300. cmCTestTestResult* result = &this->TestResults[cc];
  301. std::string testPath = result->Path + "/" + result->Name;
  302. xml.Element("Test", this->CTest->GetShortPathToFile(testPath.c_str()));
  303. }
  304. xml.EndElement(); // TestList
  305. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  306. "-- Processing memory checking output: ", this->Quiet);
  307. size_t total = this->TestResults.size();
  308. size_t step = total / 10;
  309. size_t current = 0;
  310. for (cc = 0; cc < this->TestResults.size(); cc++) {
  311. cmCTestTestResult* result = &this->TestResults[cc];
  312. std::string memcheckstr;
  313. std::vector<int> memcheckresults(this->ResultStrings.size(), 0);
  314. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  315. memcheckresults);
  316. if (res && result->Status == cmCTestMemCheckHandler::COMPLETED) {
  317. continue;
  318. }
  319. this->CleanTestOutput(
  320. memcheckstr,
  321. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  322. this->WriteTestResultHeader(xml, result);
  323. xml.StartElement("Results");
  324. for (std::vector<int>::size_type kk = 0; kk < memcheckresults.size();
  325. ++kk) {
  326. if (memcheckresults[kk]) {
  327. xml.StartElement("Defect");
  328. xml.Attribute("type", this->ResultStringsLong[kk]);
  329. xml.Content(memcheckresults[kk]);
  330. xml.EndElement(); // Defect
  331. }
  332. this->GlobalResults[kk] += memcheckresults[kk];
  333. }
  334. xml.EndElement(); // Results
  335. xml.StartElement("Log");
  336. if (this->CTest->ShouldCompressMemCheckOutput()) {
  337. this->CTest->CompressString(memcheckstr);
  338. xml.Attribute("compression", "gzip");
  339. xml.Attribute("encoding", "base64");
  340. }
  341. xml.Content(memcheckstr);
  342. xml.EndElement(); // Log
  343. this->WriteTestResultFooter(xml, result);
  344. if (current < cc) {
  345. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush,
  346. this->Quiet);
  347. current += step;
  348. }
  349. }
  350. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, std::endl, this->Quiet);
  351. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  352. "Memory checking results:" << std::endl, this->Quiet);
  353. xml.StartElement("DefectList");
  354. for (cc = 0; cc < this->GlobalResults.size(); cc++) {
  355. if (this->GlobalResults[cc]) {
  356. std::cerr.width(35);
  357. cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT,
  358. this->ResultStringsLong[cc]
  359. << " - " << this->GlobalResults[cc] << std::endl,
  360. this->Quiet);
  361. xml.StartElement("Defect");
  362. xml.Attribute("Type", this->ResultStringsLong[cc]);
  363. xml.EndElement();
  364. }
  365. }
  366. xml.EndElement(); // DefectList
  367. xml.Element("EndDateTime", this->EndTest);
  368. xml.Element("EndTestTime", this->EndTestTime);
  369. xml.Element("ElapsedMinutes",
  370. static_cast<int>(this->ElapsedTestingTime / 6) / 10.0);
  371. xml.EndElement(); // DynamicAnalysis
  372. this->CTest->EndXML(xml);
  373. }
  374. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  375. {
  376. this->MemoryTesterEnvironmentVariable = "";
  377. this->MemoryTester = "";
  378. // Setup the command
  379. if (cmSystemTools::FileExists(
  380. this->CTest->GetCTestConfiguration("MemoryCheckCommand").c_str())) {
  381. this->MemoryTester =
  382. this->CTest->GetCTestConfiguration("MemoryCheckCommand");
  383. std::string testerName =
  384. cmSystemTools::GetFilenameName(this->MemoryTester);
  385. // determine the checker type
  386. if (testerName.find("valgrind") != std::string::npos ||
  387. this->CTest->GetCTestConfiguration("MemoryCheckType") == "Valgrind") {
  388. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  389. } else if (testerName.find("purify") != std::string::npos) {
  390. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  391. } else if (testerName.find("BC") != std::string::npos) {
  392. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  393. } else {
  394. this->MemoryTesterStyle = cmCTestMemCheckHandler::UNKNOWN;
  395. }
  396. } else if (cmSystemTools::FileExists(
  397. this->CTest->GetCTestConfiguration("PurifyCommand").c_str())) {
  398. this->MemoryTester = this->CTest->GetCTestConfiguration("PurifyCommand");
  399. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  400. } else if (cmSystemTools::FileExists(
  401. this->CTest->GetCTestConfiguration("ValgrindCommand")
  402. .c_str())) {
  403. this->MemoryTester = this->CTest->GetCTestConfiguration("ValgrindCommand");
  404. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  405. } else if (cmSystemTools::FileExists(
  406. this->CTest->GetCTestConfiguration("BoundsCheckerCommand")
  407. .c_str())) {
  408. this->MemoryTester =
  409. this->CTest->GetCTestConfiguration("BoundsCheckerCommand");
  410. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  411. }
  412. if (this->CTest->GetCTestConfiguration("MemoryCheckType") ==
  413. "AddressSanitizer") {
  414. this->MemoryTester = this->CTest->GetCTestConfiguration("CMakeCommand");
  415. this->MemoryTesterStyle = cmCTestMemCheckHandler::ADDRESS_SANITIZER;
  416. this->LogWithPID = true; // even if we give the log file the pid is added
  417. }
  418. if (this->CTest->GetCTestConfiguration("MemoryCheckType") ==
  419. "ThreadSanitizer") {
  420. this->MemoryTester = this->CTest->GetCTestConfiguration("CMakeCommand");
  421. this->MemoryTesterStyle = cmCTestMemCheckHandler::THREAD_SANITIZER;
  422. this->LogWithPID = true; // even if we give the log file the pid is added
  423. }
  424. if (this->CTest->GetCTestConfiguration("MemoryCheckType") ==
  425. "MemorySanitizer") {
  426. this->MemoryTester = this->CTest->GetCTestConfiguration("CMakeCommand");
  427. this->MemoryTesterStyle = cmCTestMemCheckHandler::MEMORY_SANITIZER;
  428. this->LogWithPID = true; // even if we give the log file the pid is added
  429. }
  430. if (this->CTest->GetCTestConfiguration("MemoryCheckType") ==
  431. "UndefinedBehaviorSanitizer") {
  432. this->MemoryTester = this->CTest->GetCTestConfiguration("CMakeCommand");
  433. this->MemoryTesterStyle = cmCTestMemCheckHandler::UB_SANITIZER;
  434. this->LogWithPID = true; // even if we give the log file the pid is added
  435. }
  436. // Check the MemoryCheckType
  437. if (this->MemoryTesterStyle == cmCTestMemCheckHandler::UNKNOWN) {
  438. std::string checkType =
  439. this->CTest->GetCTestConfiguration("MemoryCheckType");
  440. if (checkType == "Purify") {
  441. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  442. } else if (checkType == "BoundsChecker") {
  443. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  444. } else if (checkType == "Valgrind") {
  445. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  446. }
  447. }
  448. if (this->MemoryTester.empty()) {
  449. cmCTestOptionalLog(this->CTest, WARNING,
  450. "Memory checker (MemoryCheckCommand) "
  451. "not set, or cannot find the specified program."
  452. << std::endl,
  453. this->Quiet);
  454. return false;
  455. }
  456. // Setup the options
  457. std::string memoryTesterOptions;
  458. if (!this->CTest->GetCTestConfiguration("MemoryCheckCommandOptions")
  459. .empty()) {
  460. memoryTesterOptions =
  461. this->CTest->GetCTestConfiguration("MemoryCheckCommandOptions");
  462. } else if (!this->CTest->GetCTestConfiguration("ValgrindCommandOptions")
  463. .empty()) {
  464. memoryTesterOptions =
  465. this->CTest->GetCTestConfiguration("ValgrindCommandOptions");
  466. }
  467. this->MemoryTesterOptions =
  468. cmSystemTools::ParseArguments(memoryTesterOptions.c_str());
  469. this->MemoryTesterOutputFile =
  470. this->CTest->GetBinaryDir() + "/Testing/Temporary/MemoryChecker.??.log";
  471. switch (this->MemoryTesterStyle) {
  472. case cmCTestMemCheckHandler::VALGRIND: {
  473. if (this->MemoryTesterOptions.empty()) {
  474. this->MemoryTesterOptions.push_back("-q");
  475. this->MemoryTesterOptions.push_back("--tool=memcheck");
  476. this->MemoryTesterOptions.push_back("--leak-check=yes");
  477. this->MemoryTesterOptions.push_back("--show-reachable=yes");
  478. this->MemoryTesterOptions.push_back("--num-callers=50");
  479. }
  480. if (!this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  481. .empty()) {
  482. if (!cmSystemTools::FileExists(
  483. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  484. .c_str())) {
  485. cmCTestLog(this->CTest, ERROR_MESSAGE,
  486. "Cannot find memory checker suppression file: "
  487. << this->CTest->GetCTestConfiguration(
  488. "MemoryCheckSuppressionFile")
  489. << std::endl);
  490. return false;
  491. }
  492. std::string suppressions = "--suppressions=" +
  493. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  494. this->MemoryTesterOptions.push_back(suppressions);
  495. }
  496. std::string outputFile = "--log-file=" + this->MemoryTesterOutputFile;
  497. this->MemoryTesterDynamicOptions.push_back(outputFile);
  498. break;
  499. }
  500. case cmCTestMemCheckHandler::PURIFY: {
  501. std::string outputFile;
  502. #ifdef _WIN32
  503. if (this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  504. .size()) {
  505. if (!cmSystemTools::FileExists(
  506. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile")
  507. .c_str())) {
  508. cmCTestLog(
  509. this->CTest, ERROR_MESSAGE,
  510. "Cannot find memory checker suppression file: "
  511. << this->CTest
  512. ->GetCTestConfiguration("MemoryCheckSuppressionFile")
  513. .c_str()
  514. << std::endl);
  515. return false;
  516. }
  517. std::string filterFiles = "/FilterFiles=" +
  518. this->CTest->GetCTestConfiguration("MemoryCheckSuppressionFile");
  519. this->MemoryTesterOptions.push_back(filterFiles);
  520. }
  521. outputFile = "/SAVETEXTDATA=";
  522. #else
  523. outputFile = "-log-file=";
  524. #endif
  525. outputFile += this->MemoryTesterOutputFile;
  526. this->MemoryTesterDynamicOptions.push_back(outputFile);
  527. break;
  528. }
  529. case cmCTestMemCheckHandler::BOUNDS_CHECKER: {
  530. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  531. std::string dpbdFile = this->CTest->GetBinaryDir() +
  532. "/Testing/Temporary/MemoryChecker.??.DPbd";
  533. this->BoundsCheckerDPBDFile = dpbdFile;
  534. this->MemoryTesterDynamicOptions.push_back("/B");
  535. this->MemoryTesterDynamicOptions.push_back(dpbdFile);
  536. this->MemoryTesterDynamicOptions.push_back("/X");
  537. this->MemoryTesterDynamicOptions.push_back(this->MemoryTesterOutputFile);
  538. this->MemoryTesterOptions.push_back("/M");
  539. break;
  540. }
  541. // these are almost the same but the env var used is different
  542. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  543. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  544. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  545. case cmCTestMemCheckHandler::UB_SANITIZER: {
  546. // To pass arguments to ThreadSanitizer the environment variable
  547. // TSAN_OPTIONS is used. This is done with the cmake -E env command.
  548. // The MemoryTesterDynamicOptions is setup with the -E env
  549. // Then the MemoryTesterEnvironmentVariable gets the
  550. // TSAN_OPTIONS string with the log_path in it.
  551. this->MemoryTesterDynamicOptions.push_back("-E");
  552. this->MemoryTesterDynamicOptions.push_back("env");
  553. std::string envVar;
  554. std::string extraOptions =
  555. this->CTest->GetCTestConfiguration("MemoryCheckSanitizerOptions");
  556. if (this->MemoryTesterStyle ==
  557. cmCTestMemCheckHandler::ADDRESS_SANITIZER) {
  558. envVar = "ASAN_OPTIONS";
  559. extraOptions += " detect_leaks=1";
  560. } else if (this->MemoryTesterStyle ==
  561. cmCTestMemCheckHandler::THREAD_SANITIZER) {
  562. envVar = "TSAN_OPTIONS";
  563. } else if (this->MemoryTesterStyle ==
  564. cmCTestMemCheckHandler::MEMORY_SANITIZER) {
  565. envVar = "MSAN_OPTIONS";
  566. } else if (this->MemoryTesterStyle ==
  567. cmCTestMemCheckHandler::UB_SANITIZER) {
  568. envVar = "UBSAN_OPTIONS";
  569. }
  570. std::string outputFile =
  571. envVar + "=log_path=\"" + this->MemoryTesterOutputFile + "\" ";
  572. this->MemoryTesterEnvironmentVariable = outputFile + extraOptions;
  573. break;
  574. }
  575. default:
  576. cmCTestLog(this->CTest, ERROR_MESSAGE,
  577. "Do not understand memory checker: " << this->MemoryTester
  578. << std::endl);
  579. return false;
  580. }
  581. this->InitializeResultsVectors();
  582. // std::vector<std::string>::size_type cc;
  583. // for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  584. // {
  585. // this->MemoryTesterGlobalResults[cc] = 0;
  586. // }
  587. return true;
  588. }
  589. bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
  590. std::string& log,
  591. std::vector<int>& results)
  592. {
  593. switch (this->MemoryTesterStyle) {
  594. case cmCTestMemCheckHandler::VALGRIND:
  595. return this->ProcessMemCheckValgrindOutput(str, log, results);
  596. case cmCTestMemCheckHandler::PURIFY:
  597. return this->ProcessMemCheckPurifyOutput(str, log, results);
  598. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  599. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  600. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  601. case cmCTestMemCheckHandler::UB_SANITIZER:
  602. return this->ProcessMemCheckSanitizerOutput(str, log, results);
  603. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  604. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  605. default:
  606. log.append("\nMemory checking style used was: ");
  607. log.append("None that I know");
  608. log = str;
  609. return true;
  610. }
  611. }
  612. std::vector<int>::size_type cmCTestMemCheckHandler::FindOrAddWarning(
  613. const std::string& warning)
  614. {
  615. for (std::vector<std::string>::size_type i = 0;
  616. i < this->ResultStrings.size(); ++i) {
  617. if (this->ResultStrings[i] == warning) {
  618. return i;
  619. }
  620. }
  621. this->GlobalResults.push_back(0); // this must stay the same size
  622. this->ResultStrings.push_back(warning);
  623. this->ResultStringsLong.push_back(warning);
  624. return this->ResultStrings.size() - 1;
  625. }
  626. bool cmCTestMemCheckHandler::ProcessMemCheckSanitizerOutput(
  627. const std::string& str, std::string& log, std::vector<int>& result)
  628. {
  629. std::string regex;
  630. switch (this->MemoryTesterStyle) {
  631. case cmCTestMemCheckHandler::ADDRESS_SANITIZER:
  632. regex = "ERROR: AddressSanitizer: (.*) on.*";
  633. break;
  634. case cmCTestMemCheckHandler::THREAD_SANITIZER:
  635. regex = "WARNING: ThreadSanitizer: (.*) \\(pid=.*\\)";
  636. break;
  637. case cmCTestMemCheckHandler::MEMORY_SANITIZER:
  638. regex = "WARNING: MemorySanitizer: (.*)";
  639. break;
  640. case cmCTestMemCheckHandler::UB_SANITIZER:
  641. regex = "runtime error: (.*)";
  642. break;
  643. default:
  644. break;
  645. }
  646. cmsys::RegularExpression sanitizerWarning(regex);
  647. cmsys::RegularExpression leakWarning("(Direct|Indirect) leak of .*");
  648. int defects = 0;
  649. std::vector<std::string> lines;
  650. cmSystemTools::Split(str.c_str(), lines);
  651. std::ostringstream ostr;
  652. log = "";
  653. for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end();
  654. ++i) {
  655. std::string resultFound;
  656. if (leakWarning.find(*i)) {
  657. resultFound = leakWarning.match(1) + " leak";
  658. } else if (sanitizerWarning.find(*i)) {
  659. resultFound = sanitizerWarning.match(1);
  660. }
  661. if (!resultFound.empty()) {
  662. std::vector<int>::size_type idx = this->FindOrAddWarning(resultFound);
  663. if (result.empty() || idx > result.size() - 1) {
  664. result.push_back(1);
  665. } else {
  666. result[idx]++;
  667. }
  668. defects++;
  669. ostr << "<b>" << this->ResultStrings[idx] << "</b> ";
  670. }
  671. ostr << *i << std::endl;
  672. }
  673. log = ostr.str();
  674. return defects == 0;
  675. }
  676. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  677. const std::string& str, std::string& log, std::vector<int>& results)
  678. {
  679. std::vector<std::string> lines;
  680. cmSystemTools::Split(str.c_str(), lines);
  681. std::ostringstream ostr;
  682. log = "";
  683. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  684. int defects = 0;
  685. for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end();
  686. ++i) {
  687. std::vector<int>::size_type failure = this->ResultStrings.size();
  688. if (pfW.find(*i)) {
  689. std::vector<int>::size_type cc;
  690. for (cc = 0; cc < this->ResultStrings.size(); cc++) {
  691. if (pfW.match(1) == this->ResultStrings[cc]) {
  692. failure = cc;
  693. break;
  694. }
  695. }
  696. if (cc == this->ResultStrings.size()) {
  697. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  698. << pfW.match(1) << std::endl);
  699. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  700. << std::endl;
  701. }
  702. }
  703. if (failure != this->ResultStrings.size()) {
  704. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  705. results[failure]++;
  706. defects++;
  707. }
  708. ostr << *i << std::endl;
  709. }
  710. log = ostr.str();
  711. return defects == 0;
  712. }
  713. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  714. const std::string& str, std::string& log, std::vector<int>& results)
  715. {
  716. std::vector<std::string> lines;
  717. cmSystemTools::Split(str.c_str(), lines);
  718. bool unlimitedOutput = false;
  719. if (str.find("CTEST_FULL_OUTPUT") != str.npos ||
  720. this->CustomMaximumFailedTestOutputSize == 0) {
  721. unlimitedOutput = true;
  722. }
  723. std::string::size_type cc;
  724. std::ostringstream ostr;
  725. log = "";
  726. int defects = 0;
  727. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  728. cmsys::RegularExpression vgFIM(
  729. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  730. cmsys::RegularExpression vgFMM(
  731. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  732. cmsys::RegularExpression vgMLK1(
  733. "== .*[0-9,]+ bytes in [0-9,]+ blocks are definitely lost"
  734. " in loss record [0-9,]+ of [0-9,]+");
  735. cmsys::RegularExpression vgMLK2(
  736. "== .*[0-9,]+ \\([0-9,]+ direct, [0-9,]+ indirect\\)"
  737. " bytes in [0-9,]+ blocks are definitely lost"
  738. " in loss record [0-9,]+ of [0-9,]+");
  739. cmsys::RegularExpression vgPAR(
  740. "== .*Syscall param .* (contains|points to) unaddressable byte\\(s\\)");
  741. cmsys::RegularExpression vgMPK1(
  742. "== .*[0-9,]+ bytes in [0-9,]+ blocks are possibly lost in"
  743. " loss record [0-9,]+ of [0-9,]+");
  744. cmsys::RegularExpression vgMPK2(
  745. "== .*[0-9,]+ bytes in [0-9,]+ blocks are still reachable"
  746. " in loss record [0-9,]+ of [0-9,]+");
  747. cmsys::RegularExpression vgUMC(
  748. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  749. cmsys::RegularExpression vgUMR1(
  750. "== .*Use of uninitialised value of size [0-9,]+");
  751. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9,]+");
  752. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  753. cmsys::RegularExpression vgUMR4(
  754. "== .*Syscall param .* contains "
  755. "uninitialised or unaddressable byte\\(s\\)");
  756. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  757. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9,]+");
  758. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  759. "locked by a different thread");
  760. std::vector<std::string::size_type> nonValGrindOutput;
  761. double sttime = cmSystemTools::GetTime();
  762. cmCTestOptionalLog(this->CTest, DEBUG,
  763. "Start test: " << lines.size() << std::endl, this->Quiet);
  764. std::string::size_type totalOutputSize = 0;
  765. for (cc = 0; cc < lines.size(); cc++) {
  766. cmCTestOptionalLog(this->CTest, DEBUG,
  767. "test line " << lines[cc] << std::endl, this->Quiet);
  768. if (valgrindLine.find(lines[cc])) {
  769. cmCTestOptionalLog(this->CTest, DEBUG,
  770. "valgrind line " << lines[cc] << std::endl,
  771. this->Quiet);
  772. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  773. if (vgFIM.find(lines[cc])) {
  774. failure = cmCTestMemCheckHandler::FIM;
  775. } else if (vgFMM.find(lines[cc])) {
  776. failure = cmCTestMemCheckHandler::FMM;
  777. } else if (vgMLK1.find(lines[cc])) {
  778. failure = cmCTestMemCheckHandler::MLK;
  779. } else if (vgMLK2.find(lines[cc])) {
  780. failure = cmCTestMemCheckHandler::MLK;
  781. } else if (vgPAR.find(lines[cc])) {
  782. failure = cmCTestMemCheckHandler::PAR;
  783. } else if (vgMPK1.find(lines[cc])) {
  784. failure = cmCTestMemCheckHandler::MPK;
  785. } else if (vgMPK2.find(lines[cc])) {
  786. failure = cmCTestMemCheckHandler::MPK;
  787. } else if (vgUMC.find(lines[cc])) {
  788. failure = cmCTestMemCheckHandler::UMC;
  789. } else if (vgUMR1.find(lines[cc])) {
  790. failure = cmCTestMemCheckHandler::UMR;
  791. } else if (vgUMR2.find(lines[cc])) {
  792. failure = cmCTestMemCheckHandler::UMR;
  793. } else if (vgUMR3.find(lines[cc])) {
  794. failure = cmCTestMemCheckHandler::UMR;
  795. } else if (vgUMR4.find(lines[cc])) {
  796. failure = cmCTestMemCheckHandler::UMR;
  797. } else if (vgUMR5.find(lines[cc])) {
  798. failure = cmCTestMemCheckHandler::UMR;
  799. } else if (vgIPW.find(lines[cc])) {
  800. failure = cmCTestMemCheckHandler::IPW;
  801. } else if (vgABR.find(lines[cc])) {
  802. failure = cmCTestMemCheckHandler::ABR;
  803. }
  804. if (failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT) {
  805. ostr << "<b>" << this->ResultStrings[failure] << "</b> ";
  806. results[failure]++;
  807. defects++;
  808. }
  809. totalOutputSize += lines[cc].size();
  810. ostr << lines[cc] << std::endl;
  811. } else {
  812. nonValGrindOutput.push_back(cc);
  813. }
  814. }
  815. // Now put all all the non valgrind output into the test output
  816. // This should be last in case it gets truncated by the output
  817. // limiting code
  818. for (std::vector<std::string::size_type>::iterator i =
  819. nonValGrindOutput.begin();
  820. i != nonValGrindOutput.end(); ++i) {
  821. totalOutputSize += lines[*i].size();
  822. ostr << lines[*i] << std::endl;
  823. if (!unlimitedOutput &&
  824. totalOutputSize >
  825. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize)) {
  826. ostr << "....\n";
  827. ostr << "Test Output for this test has been truncated see testing"
  828. " machine logs for full output,\n";
  829. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  830. "this test program.\n";
  831. break; // stop the copy of output if we are full
  832. }
  833. }
  834. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  835. << (cmSystemTools::GetTime() - sttime) << std::endl,
  836. this->Quiet);
  837. log = ostr.str();
  838. return defects == 0;
  839. }
  840. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  841. const std::string& str, std::string& log, std::vector<int>& results)
  842. {
  843. log = "";
  844. double sttime = cmSystemTools::GetTime();
  845. std::vector<std::string> lines;
  846. cmSystemTools::Split(str.c_str(), lines);
  847. cmCTestOptionalLog(this->CTest, DEBUG,
  848. "Start test: " << lines.size() << std::endl, this->Quiet);
  849. std::vector<std::string>::size_type cc;
  850. for (cc = 0; cc < lines.size(); cc++) {
  851. if (lines[cc] == BOUNDS_CHECKER_MARKER) {
  852. break;
  853. }
  854. }
  855. cmBoundsCheckerParser parser(this->CTest);
  856. parser.InitializeParser();
  857. if (cc < lines.size()) {
  858. for (cc++; cc < lines.size(); ++cc) {
  859. std::string& theLine = lines[cc];
  860. // check for command line arguments that are not escaped
  861. // correctly by BC
  862. if (theLine.find("TargetArgs=") != theLine.npos) {
  863. // skip this because BC gets it wrong and we can't parse it
  864. } else if (!parser.ParseChunk(theLine.c_str(), theLine.size())) {
  865. cmCTestLog(this->CTest, ERROR_MESSAGE,
  866. "Error in ParseChunk: " << theLine << std::endl);
  867. }
  868. }
  869. }
  870. int defects = 0;
  871. for (cc = 0; cc < parser.Errors.size(); ++cc) {
  872. results[parser.Errors[cc]]++;
  873. defects++;
  874. }
  875. cmCTestOptionalLog(this->CTest, DEBUG, "End test (elapsed: "
  876. << (cmSystemTools::GetTime() - sttime) << std::endl,
  877. this->Quiet);
  878. if (defects) {
  879. // only put the output of Bounds Checker if there were
  880. // errors or leaks detected
  881. log = parser.Log;
  882. return false;
  883. }
  884. return true;
  885. }
  886. // PostProcessTest memcheck results
  887. void cmCTestMemCheckHandler::PostProcessTest(cmCTestTestResult& res, int test)
  888. {
  889. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  890. "PostProcessTest memcheck results for : " << res.Name
  891. << std::endl,
  892. this->Quiet);
  893. if (this->MemoryTesterStyle == cmCTestMemCheckHandler::BOUNDS_CHECKER) {
  894. this->PostProcessBoundsCheckerTest(res, test);
  895. } else {
  896. std::vector<std::string> files;
  897. this->TestOutputFileNames(test, files);
  898. for (std::vector<std::string>::iterator i = files.begin();
  899. i != files.end(); ++i) {
  900. this->AppendMemTesterOutput(res, *i);
  901. }
  902. }
  903. }
  904. // This method puts the bounds checker output file into the output
  905. // for the test
  906. void cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(
  907. cmCTestTestResult& res, int test)
  908. {
  909. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  910. "PostProcessBoundsCheckerTest for : " << res.Name
  911. << std::endl,
  912. this->Quiet);
  913. std::vector<std::string> files;
  914. this->TestOutputFileNames(test, files);
  915. if (files.empty()) {
  916. return;
  917. }
  918. std::string ofile = files[0];
  919. if (ofile.empty()) {
  920. return;
  921. }
  922. // put a scope around this to close ifs so the file can be removed
  923. {
  924. cmsys::ifstream ifs(ofile.c_str());
  925. if (!ifs) {
  926. std::string log = "Cannot read memory tester output file: " + ofile;
  927. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  928. return;
  929. }
  930. res.Output += BOUNDS_CHECKER_MARKER;
  931. res.Output += "\n";
  932. std::string line;
  933. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  934. res.Output += line;
  935. res.Output += "\n";
  936. }
  937. }
  938. cmSystemTools::Delay(1000);
  939. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile);
  940. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  941. "Remove: " << this->BoundsCheckerDPBDFile << std::endl,
  942. this->Quiet);
  943. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile);
  944. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  945. "Remove: " << this->BoundsCheckerXMLFile << std::endl,
  946. this->Quiet);
  947. }
  948. void cmCTestMemCheckHandler::AppendMemTesterOutput(cmCTestTestResult& res,
  949. std::string const& ofile)
  950. {
  951. if (ofile.empty()) {
  952. return;
  953. }
  954. // put ifs in scope so file can be deleted if needed
  955. {
  956. cmsys::ifstream ifs(ofile.c_str());
  957. if (!ifs) {
  958. std::string log = "Cannot read memory tester output file: " + ofile;
  959. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  960. return;
  961. }
  962. std::string line;
  963. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  964. res.Output += line;
  965. res.Output += "\n";
  966. }
  967. }
  968. if (this->LogWithPID) {
  969. cmSystemTools::RemoveFile(ofile);
  970. cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  971. "Remove: " << ofile << "\n", this->Quiet);
  972. }
  973. }
  974. void cmCTestMemCheckHandler::TestOutputFileNames(
  975. int test, std::vector<std::string>& files)
  976. {
  977. std::string index;
  978. std::ostringstream stream;
  979. stream << test;
  980. index = stream.str();
  981. std::string ofile = this->MemoryTesterOutputFile;
  982. std::string::size_type pos = ofile.find("??");
  983. ofile.replace(pos, 2, index);
  984. if (this->LogWithPID) {
  985. ofile += ".*";
  986. cmsys::Glob g;
  987. g.FindFiles(ofile);
  988. if (g.GetFiles().empty()) {
  989. std::string log = "Cannot find memory tester output file: " + ofile;
  990. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  991. ofile = "";
  992. } else {
  993. files = g.GetFiles();
  994. return;
  995. }
  996. } else if (!cmSystemTools::FileExists(ofile.c_str())) {
  997. std::string log = "Cannot find memory tester output file: " + ofile;
  998. cmCTestLog(this->CTest, ERROR_MESSAGE, log << std::endl);
  999. ofile = "";
  1000. }
  1001. files.push_back(ofile);
  1002. }