cmCTestMemCheckHandler.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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<std::string>& args)
  231. {
  232. std::vector<cmStdString>::size_type pp;
  233. std::string memcheckcommand = "";
  234. memcheckcommand = this->MemoryTester;
  235. for ( pp = 0; pp < this->MemoryTesterOptionsParsed.size(); pp ++ )
  236. {
  237. args.push_back(this->MemoryTesterOptionsParsed[pp]);
  238. memcheckcommand += " ";
  239. memcheckcommand += cmSystemTools::EscapeSpaces(
  240. this->MemoryTesterOptionsParsed[pp].c_str());
  241. }
  242. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Memory check command: "
  243. << memcheckcommand << std::endl);
  244. }
  245. //----------------------------------------------------------------------
  246. void cmCTestMemCheckHandler::PopulateCustomVectors(cmMakefile *mf)
  247. {
  248. this->cmCTestTestHandler::PopulateCustomVectors(mf);
  249. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_PRE_MEMCHECK",
  250. this->CustomPreMemCheck);
  251. this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_POST_MEMCHECK",
  252. this->CustomPostMemCheck);
  253. this->CTest->PopulateCustomVector(mf,
  254. "CTEST_CUSTOM_MEMCHECK_IGNORE",
  255. this->CustomTestsIgnore);
  256. }
  257. //----------------------------------------------------------------------
  258. void cmCTestMemCheckHandler::GenerateDartOutput(std::ostream& os)
  259. {
  260. if ( !this->CTest->GetProduceXML() )
  261. {
  262. return;
  263. }
  264. this->CTest->StartXML(os, this->AppendXML);
  265. os << "<DynamicAnalysis Checker=\"";
  266. switch ( this->MemoryTesterStyle )
  267. {
  268. case cmCTestMemCheckHandler::VALGRIND:
  269. os << "Valgrind";
  270. break;
  271. case cmCTestMemCheckHandler::PURIFY:
  272. os << "Purify";
  273. break;
  274. case cmCTestMemCheckHandler::BOUNDS_CHECKER:
  275. os << "BoundsChecker";
  276. break;
  277. default:
  278. os << "Unknown";
  279. }
  280. os << "\">" << std::endl;
  281. os << "\t<StartDateTime>" << this->StartTest << "</StartDateTime>\n"
  282. << "\t<StartTestTime>" << this->StartTestTime << "</StartTestTime>\n"
  283. << "\t<TestList>\n";
  284. cmCTestMemCheckHandler::TestResultsVector::size_type cc;
  285. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  286. {
  287. cmCTestTestResult *result = &this->TestResults[cc];
  288. std::string testPath = result->Path + "/" + result->Name;
  289. os << "\t\t<Test>" << cmXMLSafe(
  290. this->CTest->GetShortPathToFile(testPath.c_str()))
  291. << "</Test>" << std::endl;
  292. }
  293. os << "\t</TestList>\n";
  294. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  295. "-- Processing memory checking output: ");
  296. size_t total = this->TestResults.size();
  297. size_t step = total / 10;
  298. size_t current = 0;
  299. for ( cc = 0; cc < this->TestResults.size(); cc ++ )
  300. {
  301. cmCTestTestResult *result = &this->TestResults[cc];
  302. std::string memcheckstr;
  303. int memcheckresults[cmCTestMemCheckHandler::NO_MEMORY_FAULT];
  304. int kk;
  305. bool res = this->ProcessMemCheckOutput(result->Output, memcheckstr,
  306. memcheckresults);
  307. if ( res && result->Status == cmCTestMemCheckHandler::COMPLETED )
  308. {
  309. continue;
  310. }
  311. this->CleanTestOutput(memcheckstr,
  312. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize));
  313. this->WriteTestResultHeader(os, result);
  314. os << "\t\t<Results>" << std::endl;
  315. for ( kk = 0; cmCTestMemCheckResultLongStrings[kk]; kk ++ )
  316. {
  317. if ( memcheckresults[kk] )
  318. {
  319. os << "\t\t\t<Defect type=\"" << cmCTestMemCheckResultLongStrings[kk]
  320. << "\">"
  321. << memcheckresults[kk]
  322. << "</Defect>" << std::endl;
  323. }
  324. this->MemoryTesterGlobalResults[kk] += memcheckresults[kk];
  325. }
  326. os
  327. << "\t\t</Results>\n"
  328. << "\t<Log>\n" << memcheckstr << std::endl
  329. << "\t</Log>\n";
  330. this->WriteTestResultFooter(os, result);
  331. if ( current < cc )
  332. {
  333. cmCTestLog(this->CTest, HANDLER_OUTPUT, "#" << std::flush);
  334. current += step;
  335. }
  336. }
  337. cmCTestLog(this->CTest, HANDLER_OUTPUT, std::endl);
  338. cmCTestLog(this->CTest, HANDLER_OUTPUT, "Memory checking results:"
  339. << std::endl);
  340. os << "\t<DefectList>" << std::endl;
  341. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  342. {
  343. if ( this->MemoryTesterGlobalResults[cc] )
  344. {
  345. #ifdef cerr
  346. # undef cerr
  347. #endif
  348. std::cerr.width(35);
  349. #define cerr no_cerr
  350. cmCTestLog(this->CTest, HANDLER_OUTPUT,
  351. cmCTestMemCheckResultLongStrings[cc] << " - "
  352. << this->MemoryTesterGlobalResults[cc] << std::endl);
  353. os << "\t\t<Defect Type=\"" << cmCTestMemCheckResultLongStrings[cc]
  354. << "\"/>" << std::endl;
  355. }
  356. }
  357. os << "\t</DefectList>" << std::endl;
  358. os << "\t<EndDateTime>" << this->EndTest << "</EndDateTime>" << std::endl;
  359. os << "\t<EndTestTime>" << this->EndTestTime
  360. << "</EndTestTime>" << std::endl;
  361. os << "<ElapsedMinutes>"
  362. << static_cast<int>(this->ElapsedTestingTime/6)/10.0
  363. << "</ElapsedMinutes>\n";
  364. os << "</DynamicAnalysis>" << std::endl;
  365. this->CTest->EndXML(os);
  366. }
  367. //----------------------------------------------------------------------
  368. bool cmCTestMemCheckHandler::InitializeMemoryChecking()
  369. {
  370. // Setup the command
  371. if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  372. "MemoryCheckCommand").c_str()) )
  373. {
  374. this->MemoryTester
  375. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  376. "MemoryCheckCommand").c_str());
  377. }
  378. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  379. "PurifyCommand").c_str()) )
  380. {
  381. this->MemoryTester
  382. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  383. "PurifyCommand").c_str());
  384. }
  385. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  386. "ValgrindCommand").c_str()) )
  387. {
  388. this->MemoryTester
  389. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  390. "ValgrindCommand").c_str());
  391. }
  392. else if ( cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  393. "BoundsCheckerCommand").c_str()) )
  394. {
  395. this->MemoryTester
  396. = cmSystemTools::ConvertToOutputPath(this->CTest->GetCTestConfiguration(
  397. "BoundsCheckerCommand").c_str());
  398. }
  399. else
  400. {
  401. cmCTestLog(this->CTest, WARNING,
  402. "Memory checker (MemoryCheckCommand) "
  403. "not set, or cannot find the specified program."
  404. << std::endl);
  405. return false;
  406. }
  407. if ( this->MemoryTester[0] == '\"' &&
  408. this->MemoryTester[this->MemoryTester.size()-1] == '\"' )
  409. {
  410. this->MemoryTester
  411. = this->MemoryTester.substr(1, this->MemoryTester.size()-2);
  412. }
  413. // Setup the options
  414. if ( this->CTest->GetCTestConfiguration(
  415. "MemoryCheckCommandOptions").size() )
  416. {
  417. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  418. "MemoryCheckCommandOptions");
  419. }
  420. else if ( this->CTest->GetCTestConfiguration(
  421. "ValgrindCommandOptions").size() )
  422. {
  423. this->MemoryTesterOptions = this->CTest->GetCTestConfiguration(
  424. "ValgrindCommandOptions");
  425. }
  426. this->MemoryTesterOutputFile
  427. = this->CTest->GetBinaryDir() + "/Testing/Temporary/MemoryChecker.log";
  428. if ( this->MemoryTester.find("valgrind") != std::string::npos )
  429. {
  430. this->MemoryTesterStyle = cmCTestMemCheckHandler::VALGRIND;
  431. if ( !this->MemoryTesterOptions.size() )
  432. {
  433. this->MemoryTesterOptions = "-q --tool=memcheck --leak-check=yes "
  434. "--show-reachable=yes --workaround-gcc296-bugs=yes --num-callers=100";
  435. }
  436. if ( this->CTest->GetCTestConfiguration(
  437. "MemoryCheckSuppressionFile").size() )
  438. {
  439. if ( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  440. "MemoryCheckSuppressionFile").c_str()) )
  441. {
  442. cmCTestLog(this->CTest, ERROR_MESSAGE,
  443. "Cannot find memory checker suppression file: "
  444. << this->CTest->GetCTestConfiguration(
  445. "MemoryCheckSuppressionFile").c_str() << std::endl);
  446. return false;
  447. }
  448. this->MemoryTesterOptions += " --suppressions=" +
  449. cmSystemTools::EscapeSpaces(this->CTest->GetCTestConfiguration(
  450. "MemoryCheckSuppressionFile").c_str()) + "";
  451. }
  452. }
  453. else if ( this->MemoryTester.find("purify") != std::string::npos )
  454. {
  455. this->MemoryTesterStyle = cmCTestMemCheckHandler::PURIFY;
  456. std::string outputFile =
  457. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  458. #ifdef _WIN32
  459. if( this->CTest->GetCTestConfiguration(
  460. "MemoryCheckSuppressionFile").size() )
  461. {
  462. if( !cmSystemTools::FileExists(this->CTest->GetCTestConfiguration(
  463. "MemoryCheckSuppressionFile").c_str()) )
  464. {
  465. cmCTestLog(this->CTest, ERROR_MESSAGE,
  466. "Cannot find memory checker suppression file: "
  467. << this->CTest->GetCTestConfiguration(
  468. "MemoryCheckSuppressionFile").c_str() << std::endl);
  469. return false;
  470. }
  471. this->MemoryTesterOptions += " /FilterFiles=" +
  472. cmSystemTools::EscapeSpaces(this->CTest->GetCTestConfiguration(
  473. "MemoryCheckSuppressionFile").c_str());
  474. }
  475. this->MemoryTesterOptions += " /SAVETEXTDATA=" + outputFile;
  476. #else
  477. this->MemoryTesterOptions += " -log-file=" + outputFile;
  478. #endif
  479. }
  480. else if ( this->MemoryTester.find("BC") != std::string::npos )
  481. {
  482. this->BoundsCheckerXMLFile = this->MemoryTesterOutputFile;
  483. std::string outputFile =
  484. cmSystemTools::EscapeSpaces(this->MemoryTesterOutputFile.c_str());
  485. std::string dpbdFile = this->CTest->GetBinaryDir()
  486. + "/Testing/Temporary/MemoryChecker.DPbd";
  487. std::string errorFile = this->CTest->GetBinaryDir()
  488. + "/Testing/Temporary/MemoryChecker.error";
  489. errorFile = cmSystemTools::EscapeSpaces(errorFile.c_str());
  490. this->BoundsCheckerDPBDFile = dpbdFile;
  491. dpbdFile = cmSystemTools::EscapeSpaces(dpbdFile.c_str());
  492. this->MemoryTesterStyle = cmCTestMemCheckHandler::BOUNDS_CHECKER;
  493. this->MemoryTesterOptions += " /B " + dpbdFile;
  494. this->MemoryTesterOptions += " /X " + outputFile;
  495. this->MemoryTesterOptions += " /M ";
  496. }
  497. else
  498. {
  499. cmCTestLog(this->CTest, ERROR_MESSAGE,
  500. "Do not understand memory checker: " << this->MemoryTester.c_str()
  501. << std::endl);
  502. return false;
  503. }
  504. this->MemoryTesterOptionsParsed
  505. = cmSystemTools::ParseArguments(this->MemoryTesterOptions.c_str());
  506. std::vector<cmStdString>::size_type cc;
  507. for ( cc = 0; cmCTestMemCheckResultStrings[cc]; cc ++ )
  508. {
  509. this->MemoryTesterGlobalResults[cc] = 0;
  510. }
  511. return true;
  512. }
  513. //----------------------------------------------------------------------
  514. bool cmCTestMemCheckHandler::ProcessMemCheckOutput(const std::string& str,
  515. std::string& log, int* results)
  516. {
  517. std::string::size_type cc;
  518. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  519. {
  520. results[cc] = 0;
  521. }
  522. if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::VALGRIND )
  523. {
  524. return this->ProcessMemCheckValgrindOutput(str, log, results);
  525. }
  526. else if ( this->MemoryTesterStyle == cmCTestMemCheckHandler::PURIFY )
  527. {
  528. return this->ProcessMemCheckPurifyOutput(str, log, results);
  529. }
  530. else if ( this->MemoryTesterStyle ==
  531. cmCTestMemCheckHandler::BOUNDS_CHECKER )
  532. {
  533. return this->ProcessMemCheckBoundsCheckerOutput(str, log, results);
  534. }
  535. else
  536. {
  537. log.append("\nMemory checking style used was: ");
  538. log.append("None that I know");
  539. log = str;
  540. }
  541. return true;
  542. }
  543. //----------------------------------------------------------------------
  544. bool cmCTestMemCheckHandler::ProcessMemCheckPurifyOutput(
  545. const std::string& str, std::string& log,
  546. int* results)
  547. {
  548. std::vector<cmStdString> lines;
  549. cmSystemTools::Split(str.c_str(), lines);
  550. cmOStringStream ostr;
  551. log = "";
  552. cmsys::RegularExpression pfW("^\\[[WEI]\\] ([A-Z][A-Z][A-Z][A-Z]*): ");
  553. int defects = 0;
  554. for( std::vector<cmStdString>::iterator i = lines.begin();
  555. i != lines.end(); ++i)
  556. {
  557. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  558. if ( pfW.find(*i) )
  559. {
  560. int cc;
  561. for ( cc = 0; cc < cmCTestMemCheckHandler::NO_MEMORY_FAULT; cc ++ )
  562. {
  563. if ( pfW.match(1) == cmCTestMemCheckResultStrings[cc] )
  564. {
  565. failure = cc;
  566. break;
  567. }
  568. }
  569. if ( cc == cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  570. {
  571. cmCTestLog(this->CTest, ERROR_MESSAGE, "Unknown Purify memory fault: "
  572. << pfW.match(1) << std::endl);
  573. ostr << "*** Unknown Purify memory fault: " << pfW.match(1)
  574. << std::endl;
  575. }
  576. }
  577. if ( failure != NO_MEMORY_FAULT )
  578. {
  579. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  580. results[failure] ++;
  581. defects ++;
  582. }
  583. ostr << cmXMLSafe(*i) << std::endl;
  584. }
  585. log = ostr.str();
  586. if ( defects )
  587. {
  588. return false;
  589. }
  590. return true;
  591. }
  592. //----------------------------------------------------------------------
  593. bool cmCTestMemCheckHandler::ProcessMemCheckValgrindOutput(
  594. const std::string& str, std::string& log,
  595. int* results)
  596. {
  597. std::vector<cmStdString> lines;
  598. cmSystemTools::Split(str.c_str(), lines);
  599. bool unlimitedOutput = false;
  600. if(str.find("CTEST_FULL_OUTPUT") != str.npos ||
  601. this->CustomMaximumFailedTestOutputSize == 0)
  602. {
  603. unlimitedOutput = true;
  604. }
  605. std::string::size_type cc;
  606. cmOStringStream ostr;
  607. log = "";
  608. int defects = 0;
  609. cmsys::RegularExpression valgrindLine("^==[0-9][0-9]*==");
  610. cmsys::RegularExpression vgFIM(
  611. "== .*Invalid free\\(\\) / delete / delete\\[\\]");
  612. cmsys::RegularExpression vgFMM(
  613. "== .*Mismatched free\\(\\) / delete / delete \\[\\]");
  614. cmsys::RegularExpression vgMLK(
  615. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are definitely lost"
  616. " in loss record [0-9][0-9]* of [0-9]");
  617. cmsys::RegularExpression vgPAR(
  618. "== .*Syscall param .* contains unaddressable byte\\(s\\)");
  619. cmsys::RegularExpression vgMPK1(
  620. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are possibly lost in"
  621. " loss record [0-9][0-9]* of [0-9]");
  622. cmsys::RegularExpression vgMPK2(
  623. "== .*[0-9][0-9]* bytes in [0-9][0-9]* blocks are still reachable"
  624. " in loss record [0-9][0-9]* of [0-9]");
  625. cmsys::RegularExpression vgUMC(
  626. "== .*Conditional jump or move depends on uninitialised value\\(s\\)");
  627. cmsys::RegularExpression vgUMR1(
  628. "== .*Use of uninitialised value of size [0-9][0-9]*");
  629. cmsys::RegularExpression vgUMR2("== .*Invalid read of size [0-9][0-9]*");
  630. cmsys::RegularExpression vgUMR3("== .*Jump to the invalid address ");
  631. cmsys::RegularExpression vgUMR4("== .*Syscall param .* contains "
  632. "uninitialised or unaddressable byte\\(s\\)");
  633. cmsys::RegularExpression vgUMR5("== .*Syscall param .* uninitialised");
  634. cmsys::RegularExpression vgIPW("== .*Invalid write of size [0-9]");
  635. cmsys::RegularExpression vgABR("== .*pthread_mutex_unlock: mutex is "
  636. "locked by a different thread");
  637. std::vector<std::string::size_type> nonValGrindOutput;
  638. double sttime = cmSystemTools::GetTime();
  639. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  640. std::string::size_type totalOutputSize = 0;
  641. bool outputFull = false;
  642. for ( cc = 0; cc < lines.size(); cc ++ )
  643. {
  644. cmCTestLog(this->CTest, DEBUG, "test line "
  645. << lines[cc] << std::endl);
  646. if ( valgrindLine.find(lines[cc]) )
  647. {
  648. cmCTestLog(this->CTest, DEBUG, "valgrind line "
  649. << lines[cc] << std::endl);
  650. int failure = cmCTestMemCheckHandler::NO_MEMORY_FAULT;
  651. if ( vgFIM.find(lines[cc]) )
  652. {
  653. failure = cmCTestMemCheckHandler::FIM;
  654. }
  655. else if ( vgFMM.find(lines[cc]) )
  656. {
  657. failure = cmCTestMemCheckHandler::FMM;
  658. }
  659. else if ( vgMLK.find(lines[cc]) )
  660. {
  661. failure = cmCTestMemCheckHandler::MLK;
  662. }
  663. else if ( vgPAR.find(lines[cc]) )
  664. {
  665. failure = cmCTestMemCheckHandler::PAR;
  666. }
  667. else if ( vgMPK1.find(lines[cc]) )
  668. {
  669. failure = cmCTestMemCheckHandler::MPK;
  670. }
  671. else if ( vgMPK2.find(lines[cc]) )
  672. {
  673. failure = cmCTestMemCheckHandler::MPK;
  674. }
  675. else if ( vgUMC.find(lines[cc]) )
  676. {
  677. failure = cmCTestMemCheckHandler::UMC;
  678. }
  679. else if ( vgUMR1.find(lines[cc]) )
  680. {
  681. failure = cmCTestMemCheckHandler::UMR;
  682. }
  683. else if ( vgUMR2.find(lines[cc]) )
  684. {
  685. failure = cmCTestMemCheckHandler::UMR;
  686. }
  687. else if ( vgUMR3.find(lines[cc]) )
  688. {
  689. failure = cmCTestMemCheckHandler::UMR;
  690. }
  691. else if ( vgUMR4.find(lines[cc]) )
  692. {
  693. failure = cmCTestMemCheckHandler::UMR;
  694. }
  695. else if ( vgUMR5.find(lines[cc]) )
  696. {
  697. failure = cmCTestMemCheckHandler::UMR;
  698. }
  699. else if ( vgIPW.find(lines[cc]) )
  700. {
  701. failure = cmCTestMemCheckHandler::IPW;
  702. }
  703. else if ( vgABR.find(lines[cc]) )
  704. {
  705. failure = cmCTestMemCheckHandler::ABR;
  706. }
  707. if ( failure != cmCTestMemCheckHandler::NO_MEMORY_FAULT )
  708. {
  709. ostr << "<b>" << cmCTestMemCheckResultStrings[failure] << "</b> ";
  710. results[failure] ++;
  711. defects ++;
  712. }
  713. totalOutputSize += lines[cc].size();
  714. ostr << cmXMLSafe(lines[cc]) << std::endl;
  715. }
  716. else
  717. {
  718. nonValGrindOutput.push_back(cc);
  719. }
  720. }
  721. // Now put all all the non valgrind output into the test output
  722. if(!outputFull)
  723. {
  724. for(std::vector<std::string::size_type>::iterator i =
  725. nonValGrindOutput.begin(); i != nonValGrindOutput.end(); ++i)
  726. {
  727. totalOutputSize += lines[*i].size();
  728. cmCTestLog(this->CTest, DEBUG, "before xml safe "
  729. << lines[*i] << std::endl);
  730. cmCTestLog(this->CTest, DEBUG, "after xml safe "
  731. << cmXMLSafe(lines[*i]) << std::endl);
  732. ostr << cmXMLSafe(lines[*i]) << std::endl;
  733. if(!unlimitedOutput && totalOutputSize >
  734. static_cast<size_t>(this->CustomMaximumFailedTestOutputSize))
  735. {
  736. outputFull = true;
  737. ostr << "....\n";
  738. ostr << "Test Output for this test has been truncated see testing"
  739. " machine logs for full output,\n";
  740. ostr << "or put CTEST_FULL_OUTPUT in the output of "
  741. "this test program.\n";
  742. }
  743. }
  744. }
  745. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  746. << (cmSystemTools::GetTime() - sttime) << std::endl);
  747. log = ostr.str();
  748. if ( defects )
  749. {
  750. return false;
  751. }
  752. return true;
  753. }
  754. //----------------------------------------------------------------------
  755. bool cmCTestMemCheckHandler::ProcessMemCheckBoundsCheckerOutput(
  756. const std::string& str, std::string& log,
  757. int* results)
  758. {
  759. log = "";
  760. double sttime = cmSystemTools::GetTime();
  761. std::vector<cmStdString> lines;
  762. cmSystemTools::Split(str.c_str(), lines);
  763. cmCTestLog(this->CTest, DEBUG, "Start test: " << lines.size() << std::endl);
  764. std::vector<cmStdString>::size_type cc;
  765. for ( cc = 0; cc < lines.size(); cc ++ )
  766. {
  767. if(lines[cc] == BOUNDS_CHECKER_MARKER)
  768. {
  769. break;
  770. }
  771. }
  772. cmBoundsCheckerParser parser(this->CTest);
  773. parser.InitializeParser();
  774. if(cc < lines.size())
  775. {
  776. for(cc++; cc < lines.size(); ++cc)
  777. {
  778. std::string& theLine = lines[cc];
  779. // check for command line arguments that are not escaped
  780. // correctly by BC
  781. if(theLine.find("TargetArgs=") != theLine.npos)
  782. {
  783. // skip this because BC gets it wrong and we can't parse it
  784. }
  785. else if(!parser.ParseChunk(theLine.c_str(), theLine.size()))
  786. {
  787. cmCTestLog(this->CTest, ERROR_MESSAGE,
  788. "Error in ParseChunk: " << theLine.c_str()
  789. << std::endl);
  790. }
  791. }
  792. }
  793. int defects = 0;
  794. for(cc =0; cc < parser.Errors.size(); ++cc)
  795. {
  796. results[parser.Errors[cc]]++;
  797. defects++;
  798. }
  799. cmCTestLog(this->CTest, DEBUG, "End test (elapsed: "
  800. << (cmSystemTools::GetTime() - sttime) << std::endl);
  801. if(defects)
  802. {
  803. // only put the output of Bounds Checker if there were
  804. // errors or leaks detected
  805. log = parser.Log;
  806. return false;
  807. }
  808. return true;
  809. }
  810. // This method puts the bounds checker output file into the output
  811. // for the test
  812. void
  813. cmCTestMemCheckHandler::PostProcessBoundsCheckerTest(cmCTestTestResult& res)
  814. {
  815. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  816. "PostProcessBoundsCheckerTest for : "
  817. << res.Name.c_str() << std::endl);
  818. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  819. {
  820. std::string log = "Cannot find memory tester output file: "
  821. + this->MemoryTesterOutputFile;
  822. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  823. return;
  824. }
  825. // put a scope around this to close ifs so the file can be removed
  826. {
  827. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  828. if ( !ifs )
  829. {
  830. std::string log = "Cannot read memory tester output file: "
  831. + this->MemoryTesterOutputFile;
  832. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  833. return;
  834. }
  835. res.Output += BOUNDS_CHECKER_MARKER;
  836. res.Output += "\n";
  837. std::string line;
  838. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  839. {
  840. res.Output += line;
  841. res.Output += "\n";
  842. }
  843. }
  844. cmSystemTools::Delay(1000);
  845. cmSystemTools::RemoveFile(this->BoundsCheckerDPBDFile.c_str());
  846. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  847. << this->BoundsCheckerDPBDFile.c_str() << std::endl);
  848. cmSystemTools::RemoveFile(this->BoundsCheckerXMLFile.c_str());
  849. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT, "Remove: "
  850. << this->BoundsCheckerXMLFile.c_str() << std::endl);
  851. }
  852. void
  853. cmCTestMemCheckHandler::PostProcessPurifyTest(cmCTestTestResult& res)
  854. {
  855. cmCTestLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
  856. "PostProcessPurifyTest for : "
  857. << res.Name.c_str() << std::endl);
  858. if ( !cmSystemTools::FileExists(this->MemoryTesterOutputFile.c_str()) )
  859. {
  860. std::string log = "Cannot find memory tester output file: "
  861. + this->MemoryTesterOutputFile;
  862. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  863. return;
  864. }
  865. std::ifstream ifs(this->MemoryTesterOutputFile.c_str());
  866. if ( !ifs )
  867. {
  868. std::string log = "Cannot read memory tester output file: "
  869. + this->MemoryTesterOutputFile;
  870. cmCTestLog(this->CTest, ERROR_MESSAGE, log.c_str() << std::endl);
  871. return;
  872. }
  873. std::string line;
  874. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  875. {
  876. res.Output += line;
  877. res.Output += "\n";
  878. }
  879. }