cmCTestMemCheckHandler.cxx 29 KB

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