cmCTestMemCheckHandler.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCTestMemCheckHandler.h"
  14. #include "cmXMLParser.h"
  15. #include "cmCTest.h"
  16. #include "cmake.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include <cmsys/Process.h>
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/Base64.h>
  21. #include "cmMakefile.h"
  22. #include "cmXMLSafe.h"
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <float.h>
  26. struct CatToErrorType
  27. {
  28. const char* ErrorCategory;
  29. int ErrorCode;
  30. };
  31. static CatToErrorType cmCTestMemCheckBoundsChecker[] = {
  32. // Error tags
  33. {"Write Overrun", cmCTestMemCheckHandler::ABW},
  34. {"Read Overrun", cmCTestMemCheckHandler::ABR},
  35. {"Memory Overrun", cmCTestMemCheckHandler::ABW},
  36. {"Allocation Conflict", cmCTestMemCheckHandler::FMM},
  37. {"Bad Pointer Use", cmCTestMemCheckHandler::FMW},
  38. {"Dangling Pointer", cmCTestMemCheckHandler::FMR},
  39. {0,0}
  40. };
  41. // parse the xml file storing the installed version of Xcode on
  42. // the machine
  43. class cmBoundsCheckerParser : public cmXMLParser
  44. {
  45. public:
  46. cmBoundsCheckerParser(cmCTest* c) { this->CTest = c;}
  47. void StartElement(const char* name, const char** atts)
  48. {
  49. if(strcmp(name, "MemoryLeak") == 0)
  50. {
  51. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  52. }
  53. if(strcmp(name, "ResourceLeak") == 0)
  54. {
  55. this->Errors.push_back(cmCTestMemCheckHandler::MLK);
  56. }
  57. if(strcmp(name, "Error") == 0)
  58. {
  59. this->ParseError(atts);
  60. }
  61. if(strcmp(name, "Dangling Pointer") == 0)
  62. {
  63. this->ParseError(atts);
  64. }
  65. // Create the log
  66. cmOStringStream ostr;
  67. ostr << name << ":\n";
  68. int i = 0;
  69. for(; atts[i] != 0; i+=2)
  70. {
  71. ostr << " " << cmXMLSafe(atts[i])
  72. << " - " << cmXMLSafe(atts[i+1]) << "\n";
  73. }
  74. ostr << "\n";
  75. this->Log += ostr.str();
  76. }
  77. void EndElement(const char* )
  78. {
  79. }
  80. const char* GetAttribute(const char* name, const char** atts)
  81. {
  82. int i = 0;
  83. for(; atts[i] != 0; ++i)
  84. {
  85. if(strcmp(name, atts[i]) == 0)
  86. {
  87. return atts[i+1];
  88. }
  89. }
  90. return 0;
  91. }
  92. void ParseError(const char** atts)
  93. {
  94. CatToErrorType* ptr = cmCTestMemCheckBoundsChecker;
  95. const char* cat = this->GetAttribute("ErrorCategory", atts);
  96. if(!cat)
  97. {
  98. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  99. cmCTestLog(this->CTest, ERROR_MESSAGE,
  100. "No Category found in Bounds checker XML\n" );
  101. return;
  102. }
  103. while(ptr->ErrorCategory && cat)
  104. {
  105. if(strcmp(ptr->ErrorCategory, cat) == 0)
  106. {
  107. this->Errors.push_back(ptr->ErrorCode);
  108. return; // found it we are done
  109. }
  110. ptr++;
  111. }
  112. if(ptr->ErrorCategory)
  113. {
  114. this->Errors.push_back(cmCTestMemCheckHandler::ABW); // do not know
  115. cmCTestLog(this->CTest, ERROR_MESSAGE,
  116. "Found unknown Bounds Checker error "
  117. << ptr->ErrorCategory << std::endl);
  118. }
  119. }
  120. cmCTest* CTest;
  121. std::vector<int> Errors;
  122. std::string Log;
  123. };
  124. #define BOUNDS_CHECKER_MARKER \
  125. "******######*****Begin BOUNDS CHECKER XML******######******"
  126. //----------------------------------------------------------------------
  127. static const char* cmCTestMemCheckResultStrings[] = {
  128. "ABR",
  129. "ABW",
  130. "ABWL",
  131. "COR",
  132. "EXU",
  133. "FFM",
  134. "FIM",
  135. "FMM",
  136. "FMR",
  137. "FMW",
  138. "FUM",
  139. "IPR",
  140. "IPW",
  141. "MAF",
  142. "MLK",
  143. "MPK",
  144. "NPR",
  145. "ODS",
  146. "PAR",
  147. "PLK",
  148. "UMC",
  149. "UMR",
  150. 0
  151. };
  152. //----------------------------------------------------------------------
  153. static const char* cmCTestMemCheckResultLongStrings[] = {
  154. "Threading Problem",
  155. "ABW",
  156. "ABWL",
  157. "COR",
  158. "EXU",
  159. "FFM",
  160. "FIM",
  161. "Mismatched deallocation",
  162. "FMR",
  163. "FMW",
  164. "FUM",
  165. "IPR",
  166. "IPW",
  167. "MAF",
  168. "Memory Leak",
  169. "Potential Memory Leak",
  170. "NPR",
  171. "ODS",
  172. "Invalid syscall param",
  173. "PLK",
  174. "Uninitialized Memory Conditional",
  175. "Uninitialized Memory Read",
  176. 0
  177. };
  178. //----------------------------------------------------------------------
  179. cmCTestMemCheckHandler::cmCTestMemCheckHandler()
  180. {
  181. this->MemCheck = true;
  182. this->CustomMaximumPassedTestOutputSize = 0;
  183. this->CustomMaximumFailedTestOutputSize = 0;
  184. }
  185. //----------------------------------------------------------------------
  186. void cmCTestMemCheckHandler::Initialize()
  187. {
  188. this->Superclass::Initialize();
  189. this->CustomMaximumPassedTestOutputSize = 0;
  190. this->CustomMaximumFailedTestOutputSize = 0;
  191. this->MemoryTester = "";
  192. this->MemoryTesterOptionsParsed.clear();
  193. this->MemoryTesterOptions = "";
  194. this->MemoryTesterStyle = UNKNOWN;
  195. this->MemoryTesterOutputFile = "";
  196. int cc;
  197. for ( cc = 0; cc < NO_MEMORY_FAULT; cc ++ )
  198. {
  199. this->MemoryTesterGlobalResults[cc] = 0;
  200. }
  201. }
  202. //----------------------------------------------------------------------
  203. int cmCTestMemCheckHandler::PreProcessHandler()
  204. {
  205. if ( !this->InitializeMemoryChecking() )
  206. {
  207. return 0;
  208. }
  209. if ( !this->ExecuteCommands(this->CustomPreMemCheck) )
  210. {
  211. cmCTestLog(this->CTest, ERROR_MESSAGE,
  212. "Problem executing pre-memcheck command(s)." << std::endl);
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. //----------------------------------------------------------------------
  218. int cmCTestMemCheckHandler::PostProcessHandler()
  219. {
  220. if ( !this->ExecuteCommands(this->CustomPostMemCheck) )
  221. {
  222. cmCTestLog(this->CTest, ERROR_MESSAGE,
  223. "Problem executing post-memcheck command(s)." << std::endl);
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. //----------------------------------------------------------------------
  229. void cmCTestMemCheckHandler::GenerateTestCommand(
  230. std::vector<const char*>& args)
  231. {
  232. std::vector<cmStdString>::size_type pp;
  233. args.push_back(this->MemoryTester.c_str());
  234. std::string memcheckcommand = "";
  235. memcheckcommand = this->MemoryTester;
  236. for ( pp = 0; pp < this->MemoryTesterOptionsParsed.size(); pp ++ )
  237. {
  238. args.push_back(this->MemoryTesterOptionsParsed[pp].c_str());
  239. memcheckcommand += " ";
  240. memcheckcommand += cmSystemTools::EscapeSpaces(
  241. this->MemoryTesterOptionsParsed[pp].c_str());
  242. }
  243. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Memory check command: "
  244. << memcheckcommand << std::endl);
  245. }
  246. //----------------------------------------------------------------------
  247. void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile *mf)
  248. {
  249. this->cmCTestTestHandler::PopulateCustomVectors(mf);
  250. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
  251. this->CustomPreMemCheck);
  252. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
  253. this->CustomPostMemCheck);
  254. this->CTest->PopulateCustomVector(mf,
  255. "CTEST_CUSTOM_MEMCHECK_IGNORE",
  256. this->CustomTestsIgnore);
  257. }
  258. //----------------------------------------------------------------------
  259. void cmCTestMemCheckHandler::GenerateDartOutput(std::ostream& os)
  260. {
  261. if ( !this->CTest->GetProduceXML() )
  262. {
  263. return;
  264. }
  265. this->CTest->StartXML(os, this->AppendXML);
  266. os << "<DynamicAnalysis Checker=\"";
  267. switch ( this->MemoryTesterStyle )
  268. {
  269. case cmCTestMemCheckHandler::VALGRIND:
  270. os << "Valgrind";
  271. break;
  272. case cmCTestMemCheckHandler::PURIFY:
  273. os << "Purify";
  274. break;
  275. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  276. os << "BoundsChecker";
  277. break;
  278. default:
  279. os << "Unknown";
  280. }
  281. os << "\">" << std::endl;
  282. os << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  283. << "\t<StartTestTime>" << this->StartTestTime << "</StartTestTime>\n"
  284. << "\t<TestList>\n";
  285. cmCTestMemCheckHandler::TestResultsVector::size_type cc;
  286. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  287. {
  288. cmCTestTestResult *result = &this->TestResults[cc];
  289. std::string testPath = result->Path + "/" + result->Name;
  290. os << "\t\t<Test>" << cmXMLSafe(
  291. this->CTest->GetShortPathToFile(testPath.c_str()))
  292. << "</Test>" << std::endl;
  293. }
  294. os << "\t</TestList>\n";
  295. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  296. "-- Processing memory checking output: ");
  297. size_t total = this->TestResults.size();
  298. size_t step = total / 10;
  299. size_t current = 0;
  300. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  301. {
  302. cmCTestTestResult *result = &this->TestResults[cc];
  303. std::string memcheckstr;
  304. int memcheckresults[cmCTestMemCheckHandler::NO_MEMORY_FAULT];
  305. int kk;
  306. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  307. memcheckresults);
  308. if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
  309. {
  310. continue;
  311. }
  312. this->CleanTestOutput(memcheckstr,
  313. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  314. this->WriteTestResultHeader(os, result);
  315. os << "\t\t<Results>" << std::endl;
  316. for ( kk = 0; cmCTestMemCheckResultLongStrings[kk]; kk ++ )
  317. {
  318. if ( memcheckresults[kk] )
  319. {
  320. os << "\t\t\t<Defect type=\"" << cmCTestMemCheckResultLongStrings[kk]
  321. << "\">"
  322. << memcheckresults[kk]
  323. << "</Defect>" << std::endl;
  324. }
  325. this->MemoryTesterGlobalResults[kk] += memcheckresults[kk];
  326. }
  327. os
  328. << "\t\t</Results>\n"
  329. << "\t<Log>\n" << memcheckstr << std::endl
  330. << "\t</Log>\n";
  331. this->WriteTestResultFooter(os, result);
  332. if ( current < cc )
  333. {
  334. cmCTestLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush);
  335. current += step;
  336. }
  337. }
  338. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  339. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
  340. << std::endl);
  341. os << "\t<DefectList>" << std::endl;
  342. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  343. {
  344. if ( this->MemoryTesterGlobalResults[cc] )
  345. {
  346. #ifdef cerr
  347. # undef cerr
  348. #endif
  349. std::cerr.width(35);
  350. #define cerr no_cerr
  351. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  352. cmCTestMemCheckResultLongStrings[cc] << " - "
  353. << this->MemoryTesterGlobalResults[cc] << std::endl);
  354. os << "\t\t<Defect Type=\"" << cmCTestMemCheckResultLongStrings[cc]
  355. << "\"/>" << std::endl;
  356. }
  357. }
  358. os << "\t</DefectList>" << std::endl;
  359. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>" << std::endl;
  360. os << "\t<EndTestTime>" << this->EndTestTime
  361. << "</EndTestTime>" << std::endl;
  362. os << "<ElapsedMinutes>"
  363. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  364. << "</ElapsedMinutes>\n";
  365. os << "</DynamicAnalysis>" << std::endl;
  366. this->CTest->EndXML(os);
  367. }
  368. //----------------------------------------------------------------------
  369. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  370. {
  371. // Setup the command
  372. if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  373. "MemoryCheckCommand").c_str()) )
  374. {
  375. this->MemoryTester
  376. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  377. "MemoryCheckCommand").c_str());
  378. }
  379. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  380. "PurifyCommand").c_str()) )
  381. {
  382. this->MemoryTester
  383. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  384. "PurifyCommand").c_str());
  385. }
  386. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  387. "ValgrindCommand").c_str()) )
  388. {
  389. this->MemoryTester
  390. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  391. "ValgrindCommand").c_str());
  392. }
  393. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  394. "BoundsCheckerCommand").c_str()) )
  395. {
  396. this->MemoryTester
  397. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  398. "BoundsCheckerCommand").c_str());
  399. }
  400. else
  401. {
  402. cmCTestLog(this->CTest, WARNING,
  403. "Memory checker (MemoryCheckCommand) "
  404. "not set, or cannot find the specified program."
  405. << std::endl);
  406. return false;
  407. }
  408. if ( this->MemoryTester[0] == '\"' &&
  409. this->MemoryTester[this->MemoryTester.size()-1] == '\"' )
  410. {
  411. this->MemoryTester
  412. = this->MemoryTester.substr(1, this->MemoryTester.size()-2);
  413. }
  414. // Setup the options
  415. if ( this->CTest->GetCTestConfiguration(
  416. "MemoryCheckCommandOptions").size() )
  417. {
  418. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  419. "MemoryCheckCommandOptions");
  420. }
  421. else if ( this->CTest->GetCTestConfiguration(
  422. "ValgrindCommandOptions").size() )
  423. {
  424. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  425. "ValgrindCommandOptions");
  426. }
  427. this->MemoryTesterOutputFile
  428. = this->CTest->GetBinaryDir() + "/Testing/Temporary/MemoryChecker.log";
  429. if ( this->MemoryTester.find("valgrind") != std::string::npos )
  430. {
  431. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  432. if ( !this->MemoryTesterOptions.size() )
  433. {
  434. this->MemoryTesterOptions = "-q --tool=memcheck --leak-check=yes "
  435. "--show-reachable=yes --workaround-gcc296-bugs=yes --num-callers=100";
  436. }
  437. if ( this->CTest->GetCTestConfiguration(
  438. "MemoryCheckSuppressionFile").size() )
  439. {
  440. if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  441. "MemoryCheckSuppressionFile").c_str()) )
  442. {
  443. cmCTestLog(this->CTest, ERROR_MESSAGE,
  444. "Cannot find memory checker suppression file: "
  445. << this->CTest->GetCTestConfiguration(
  446. "MemoryCheckSuppressionFile").c_str() << std::endl);
  447. return false;
  448. }
  449. this->MemoryTesterOptions += " --suppressions=" +
  450. cmSystemTools::EscapeSpaces(this->CTest->GetCTestConfiguration(
  451. "MemoryCheckSuppressionFile").c_str()) + "";
  452. }
  453. }
  454. else if ( this->MemoryTester.find("purify") != std::string::npos )
  455. {
  456. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  457. std::string outputFile =
  458. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  459. #ifdef _WIN32
  460. this->MemoryTesterOptions += " /SAVETEXTDATA=" + outputFile;
  461. #else
  462. this->MemoryTesterOptions += " -log-file=" + outputFile;
  463. #endif
  464. }
  465. else if ( this->MemoryTester.find("BC") != std::string::npos )
  466. {
  467. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  468. std::string outputFile =
  469. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  470. std::string dpbdFile = this->CTest->GetBinaryDir()
  471. + "/Testing/Temporary/MemoryChecker.DPbd";
  472. std::string errorFile = this->CTest->GetBinaryDir()
  473. + "/Testing/Temporary/MemoryChecker.error";
  474. errorFile = cmSystemTools::EscapeSpaces(errorFile.c_str());
  475. this->BoundsCheckerDPBDFile = dpbdFile;
  476. dpbdFile = cmSystemTools::EscapeSpaces(dpbdFile.c_str());
  477. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  478. this->MemoryTesterOptions += " /B " + dpbdFile;
  479. this->MemoryTesterOptions += " /X " + outputFile;
  480. this->MemoryTesterOptions += " /M ";
  481. }
  482. else
  483. {
  484. cmCTestLog(this->CTest, ERROR_MESSAGE,
  485. "Do not understand memory checker: " << this->MemoryTester.c_str()
  486. << std::endl);
  487. return false;
  488. }
  489. this->MemoryTesterOptionsParsed
  490. = cmSystemTools::ParseArguments(this->MemoryTesterOptions.c_str());
  491. std::vector<cmStdString>::size_type cc;
  492. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  493. {
  494. this->MemoryTesterGlobalResults[cc] = 0;
  495. }
  496. return true;
  497. }
  498. //----------------------------------------------------------------------
  499. bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
  500. std::string& log, int* results)
  501. {
  502. std::string::size_type cc;
  503. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  504. {
  505. results[cc] = 0;
  506. }
  507. if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
  508. {
  509. return this->ProcessMemCheckValgrindOutput(str, log, results);
  510. }
  511. else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  512. {
  513. return this->ProcessMemCheckPurifyOutput(str, log, results);
  514. }
  515. else if ( this->MemoryTesterStyle ==
  516. cmCTestMemCheckHandler::BOUNDS_CHECKER )
  517. {
  518. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  519. }
  520. else
  521. {
  522. log.append("\nMemory checking style used was: ");
  523. log.append("None that I know");
  524. log = str;
  525. }
  526. return true;
  527. }
  528. //----------------------------------------------------------------------
  529. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  530. const std::string& str, std::string& log,
  531. int* results)
  532. {
  533. std::vector<cmStdString> lines;
  534. cmSystemTools::Split(str.c_str(), lines);
  535. cmOStringStream ostr;
  536. log = "";
  537. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  538. int defects = 0;
  539. for( std::vector<cmStdString>::iterator i = lines.begin();
  540. i != lines.end(); ++i)
  541. {
  542. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  543. if ( pfW.find(*i) )
  544. {
  545. int cc;
  546. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  547. {
  548. if ( pfW.match(1) == cmCTestMemCheckResultStrings[cc] )
  549. {
  550. failure = cc;
  551. break;
  552. }
  553. }
  554. if ( cc == cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  555. {
  556. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  557. << pfW.match(1) << std::endl);
  558. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  559. << std::endl;
  560. }
  561. }
  562. if ( failure != NO_MEMORY_FAULT )
  563. {
  564. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  565. results[failure] ++;
  566. defects ++;
  567. }
  568. ostr << cmXMLSafe(*i) << std::endl;
  569. }
  570. log = ostr.str();
  571. if ( defects )
  572. {
  573. return false;
  574. }
  575. return true;
  576. }
  577. //----------------------------------------------------------------------
  578. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  579. const std::string& str, std::string& log,
  580. int* results)
  581. {
  582. std::vector<cmStdString> lines;
  583. cmSystemTools::Split(str.c_str(), lines);
  584. bool unlimitedOutput = false;
  585. if(str.find("CTEST_FULL_OUTPUT") != str.npos ||
  586. this->CustomMaximumFailedTestOutputSize == 0)
  587. {
  588. unlimitedOutput = true;
  589. }
  590. std::string::size_type cc;
  591. cmOStringStream ostr;
  592. log = "";
  593. int defects = 0;
  594. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  595. cmsys::RegularExpression vgFIM(
  596. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  597. cmsys::RegularExpression vgFMM(
  598. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  599. cmsys::RegularExpression vgMLK(
  600. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are definitely lost"
  601. " in loss record [0-9][0-9]* of [0-9]");
  602. cmsys::RegularExpression vgPAR(
  603. "== .*Syscall param .* contains unaddressable byte\\(s\\)");
  604. cmsys::RegularExpression vgMPK1(
  605. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are possibly lost in"
  606. " loss record [0-9][0-9]* of [0-9]");
  607. cmsys::RegularExpression vgMPK2(
  608. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are still reachable"
  609. " in loss record [0-9][0-9]* of [0-9]");
  610. cmsys::RegularExpression vgUMC(
  611. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  612. cmsys::RegularExpression vgUMR1(
  613. "== .*Use of uninitialised value of size [0-9][0-9]*");
  614. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9][0-9]*");
  615. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  616. cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
  617. "uninitialised or unaddressable byte\\(s\\)");
  618. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  619. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9]");
  620. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  621. "locked by a different thread");
  622. std::vector<std::string::size_type> nonValGrindOutput;
  623. double sttime = cmSystemTools::GetTime();
  624. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  625. std::string::size_type totalOutputSize = 0;
  626. bool outputFull = false;
  627. for ( cc = 0; cc < lines.size(); cc ++ )
  628. {
  629. cmCTestLog(this->CTest, DEBUG, "test line "
  630. << lines[cc] << std::endl);
  631. if ( valgrindLine.find(lines[cc]) )
  632. {
  633. cmCTestLog(this->CTest, DEBUG, "valgrind line "
  634. << lines[cc] << std::endl);
  635. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  636. if ( vgFIM.find(lines[cc]) )
  637. {
  638. failure = cmCTestMemCheckHandler::FIM;
  639. }
  640. else if ( vgFMM.find(lines[cc]) )
  641. {
  642. failure = cmCTestMemCheckHandler::FMM;
  643. }
  644. else if ( vgMLK.find(lines[cc]) )
  645. {
  646. failure = cmCTestMemCheckHandler::MLK;
  647. }
  648. else if ( vgPAR.find(lines[cc]) )
  649. {
  650. failure = cmCTestMemCheckHandler::PAR;
  651. }
  652. else if ( vgMPK1.find(lines[cc]) )
  653. {
  654. failure = cmCTestMemCheckHandler::MPK;
  655. }
  656. else if ( vgMPK2.find(lines[cc]) )
  657. {
  658. failure = cmCTestMemCheckHandler::MPK;
  659. }
  660. else if ( vgUMC.find(lines[cc]) )
  661. {
  662. failure = cmCTestMemCheckHandler::UMC;
  663. }
  664. else if ( vgUMR1.find(lines[cc]) )
  665. {
  666. failure = cmCTestMemCheckHandler::UMR;
  667. }
  668. else if ( vgUMR2.find(lines[cc]) )
  669. {
  670. failure = cmCTestMemCheckHandler::UMR;
  671. }
  672. else if ( vgUMR3.find(lines[cc]) )
  673. {
  674. failure = cmCTestMemCheckHandler::UMR;
  675. }
  676. else if ( vgUMR4.find(lines[cc]) )
  677. {
  678. failure = cmCTestMemCheckHandler::UMR;
  679. }
  680. else if ( vgUMR5.find(lines[cc]) )
  681. {
  682. failure = cmCTestMemCheckHandler::UMR;
  683. }
  684. else if ( vgIPW.find(lines[cc]) )
  685. {
  686. failure = cmCTestMemCheckHandler::IPW;
  687. }
  688. else if ( vgABR.find(lines[cc]) )
  689. {
  690. failure = cmCTestMemCheckHandler::ABR;
  691. }
  692. if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  693. {
  694. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  695. results[failure] ++;
  696. defects ++;
  697. }
  698. totalOutputSize += lines[cc].size();
  699. ostr << cmXMLSafe(lines[cc]) << std::endl;
  700. }
  701. else
  702. {
  703. nonValGrindOutput.push_back(cc);
  704. }
  705. }
  706. // Now put all all the non valgrind output into the test output
  707. if(!outputFull)
  708. {
  709. for(std::vector<std::string::size_type>::iterator i =
  710. nonValGrindOutput.begin(); i != nonValGrindOutput.end(); ++i)
  711. {
  712. totalOutputSize += lines[*i].size();
  713. cmCTestLog(this->CTest, DEBUG, "before xml safe "
  714. << lines[*i] << std::endl);
  715. cmCTestLog(this->CTest, DEBUG, "after xml safe "
  716. << cmXMLSafe(lines[*i]) << std::endl);
  717. ostr << cmXMLSafe(lines[*i]) << std::endl;
  718. if(!unlimitedOutput && totalOutputSize >
  719. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  720. {
  721. outputFull = true;
  722. ostr << "....\n";
  723. ostr << "Test Output for this test has been truncated see testing"
  724. " machine logs for full output,\n";
  725. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  726. "this test program.\n";
  727. }
  728. }
  729. }
  730. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  731. << (cmSystemTools::GetTime() - sttime) << std::endl);
  732. log = ostr.str();
  733. if ( defects )
  734. {
  735. return false;
  736. }
  737. return true;
  738. }
  739. //----------------------------------------------------------------------
  740. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  741. const std::string& str, std::string& log,
  742. int* results)
  743. {
  744. log = "";
  745. double sttime = cmSystemTools::GetTime();
  746. std::vector<cmStdString> lines;
  747. cmSystemTools::Split(str.c_str(), lines);
  748. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  749. std::vector<cmStdString>::size_type cc;
  750. for ( cc = 0; cc < lines.size(); cc ++ )
  751. {
  752. if(lines[cc] == BOUNDS_CHECKER_MARKER)
  753. {
  754. break;
  755. }
  756. }
  757. cmBoundsCheckerParser parser(this->CTest);
  758. parser.InitializeParser();
  759. if(cc < lines.size())
  760. {
  761. for(cc++; cc < lines.size(); ++cc)
  762. {
  763. std::string& theLine = lines[cc];
  764. // check for command line arguments that are not escaped
  765. // correctly by BC
  766. if(theLine.find("TargetArgs=") != theLine.npos)
  767. {
  768. // skip this because BC gets it wrong and we can't parse it
  769. }
  770. else if(!parser.ParseChunk(theLine.c_str(), theLine.size()))
  771. {
  772. cmCTestLog(this->CTest, ERROR_MESSAGE,
  773. "Error in ParseChunk: " << theLine.c_str()
  774. << std::endl);
  775. }
  776. }
  777. }
  778. int defects = 0;
  779. for(cc =0; cc < parser.Errors.size(); ++cc)
  780. {
  781. results[parser.Errors[cc]]++;
  782. defects++;
  783. }
  784. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  785. << (cmSystemTools::GetTime() - sttime) << std::endl);
  786. if(defects)
  787. {
  788. // only put the output of Bounds Checker if there were
  789. // errors or leaks detected
  790. log = parser.Log;
  791. return false;
  792. }
  793. return true;
  794. }
  795. void
  796. cmCTestMemCheckHandler::ProcessOneTest(cmCTestTestProperties *props,
  797. std::vector<cmStdString> &passed,
  798. std::vector<cmStdString> &failed,
  799. int count, int tmsize)
  800. {
  801. // run parent test
  802. cmCTestTestHandler::ProcessOneTest(props, passed, failed, count, tmsize);
  803. cmCTestTestResult& res = this->TestResults[this->TestResults.size()-1];
  804. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "process test output now: "
  805. << props->Name.c_str() << " " << res.Name.c_str() << std::endl);
  806. if( this->MemoryTesterStyle == cmCTestMemCheckHandler::BOUNDS_CHECKER)
  807. {
  808. this->PostProcessBoundsCheckerTest(res);
  809. }
  810. else if(this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  811. {
  812. this->PostProcessPurifyTest(res);
  813. }
  814. }
  815. // This method puts the bounds checker output file into the output
  816. // for the test
  817. void
  818. cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(cmCTestTestResult& res)
  819. {
  820. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  821. "PostProcessBoundsCheckerTest for : "
  822. << res.Name.c_str() << std::endl);
  823. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  824. {
  825. std::string log = "Cannot find memory tester output file: "
  826. + this->MemoryTesterOutputFile;
  827. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  828. return;
  829. }
  830. // put a scope around this to close ifs so the file can be removed
  831. {
  832. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  833. if ( !ifs )
  834. {
  835. std::string log = "Cannot read memory tester output file: "
  836. + this->MemoryTesterOutputFile;
  837. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  838. return;
  839. }
  840. res.Output += BOUNDS_CHECKER_MARKER;
  841. res.Output += "\n";
  842. std::string line;
  843. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  844. {
  845. res.Output += line;
  846. res.Output += "\n";
  847. }
  848. }
  849. cmSystemTools::Delay(1000);
  850. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile.c_str());
  851. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  852. << this->BoundsCheckerDPBDFile.c_str() << std::endl);
  853. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile.c_str());
  854. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  855. << this->BoundsCheckerXMLFile.c_str() << std::endl);
  856. }
  857. void
  858. cmCTestMemCheckHandler::PostProcessPurifyTest(cmCTestTestResult& res)
  859. {
  860. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  861. "PostProcessPurifyTest for : "
  862. << res.Name.c_str() << std::endl);
  863. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  864. {
  865. std::string log = "Cannot find memory tester output file: "
  866. + this->MemoryTesterOutputFile;
  867. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  868. return;
  869. }
  870. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  871. if ( !ifs )
  872. {
  873. std::string log = "Cannot read memory tester output file: "
  874. + this->MemoryTesterOutputFile;
  875. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  876. return;
  877. }
  878. std::string line;
  879. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  880. {
  881. res.Output += line;
  882. }
  883. }