CodeCoverage.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. //===- CodeCoverage.cpp - Coverage tool based on profiling instrumentation-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // The 'CodeCoverageTool' class implements a command line tool to analyze and
  11. // report coverage information using the profiling instrumentation and code
  12. // coverage mapping.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "CoverageExporterJson.h"
  16. #include "CoverageFilters.h"
  17. #include "CoverageReport.h"
  18. #include "CoverageSummaryInfo.h"
  19. #include "CoverageViewOptions.h"
  20. #include "RenderingSupport.h"
  21. #include "SourceCoverageView.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/Triple.h"
  25. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  26. #include "llvm/ProfileData/InstrProfReader.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/FileSystem.h"
  29. #include "llvm/Support/Format.h"
  30. #include "llvm/Support/MemoryBuffer.h"
  31. #include "llvm/Support/Path.h"
  32. #include "llvm/Support/Process.h"
  33. #include "llvm/Support/Program.h"
  34. #include "llvm/Support/ScopedPrinter.h"
  35. #include "llvm/Support/ThreadPool.h"
  36. #include "llvm/Support/Threading.h"
  37. #include "llvm/Support/ToolOutputFile.h"
  38. #include <functional>
  39. #include <map>
  40. #include <system_error>
  41. using namespace llvm;
  42. using namespace coverage;
  43. void exportCoverageDataToJson(const coverage::CoverageMapping &CoverageMapping,
  44. const CoverageViewOptions &Options,
  45. raw_ostream &OS);
  46. namespace {
  47. /// The implementation of the coverage tool.
  48. class CodeCoverageTool {
  49. public:
  50. enum Command {
  51. /// The show command.
  52. Show,
  53. /// The report command.
  54. Report,
  55. /// The export command.
  56. Export
  57. };
  58. int run(Command Cmd, int argc, const char **argv);
  59. private:
  60. /// Print the error message to the error output stream.
  61. void error(const Twine &Message, StringRef Whence = "");
  62. /// Print the warning message to the error output stream.
  63. void warning(const Twine &Message, StringRef Whence = "");
  64. /// Convert \p Path into an absolute path and append it to the list
  65. /// of collected paths.
  66. void addCollectedPath(const std::string &Path);
  67. /// If \p Path is a regular file, collect the path. If it's a
  68. /// directory, recursively collect all of the paths within the directory.
  69. void collectPaths(const std::string &Path);
  70. /// Return a memory buffer for the given source file.
  71. ErrorOr<const MemoryBuffer &> getSourceFile(StringRef SourceFile);
  72. /// Create source views for the expansions of the view.
  73. void attachExpansionSubViews(SourceCoverageView &View,
  74. ArrayRef<ExpansionRecord> Expansions,
  75. const CoverageMapping &Coverage);
  76. /// Create the source view of a particular function.
  77. std::unique_ptr<SourceCoverageView>
  78. createFunctionView(const FunctionRecord &Function,
  79. const CoverageMapping &Coverage);
  80. /// Create the main source view of a particular source file.
  81. std::unique_ptr<SourceCoverageView>
  82. createSourceFileView(StringRef SourceFile, const CoverageMapping &Coverage);
  83. /// Load the coverage mapping data. Return nullptr if an error occurred.
  84. std::unique_ptr<CoverageMapping> load();
  85. /// Create a mapping from files in the Coverage data to local copies
  86. /// (path-equivalence).
  87. void remapPathNames(const CoverageMapping &Coverage);
  88. /// Remove input source files which aren't mapped by \p Coverage.
  89. void removeUnmappedInputs(const CoverageMapping &Coverage);
  90. /// If a demangler is available, demangle all symbol names.
  91. void demangleSymbols(const CoverageMapping &Coverage);
  92. /// Write out a source file view to the filesystem.
  93. void writeSourceFileView(StringRef SourceFile, CoverageMapping *Coverage,
  94. CoveragePrinter *Printer, bool ShowFilenames);
  95. typedef llvm::function_ref<int(int, const char **)> CommandLineParserType;
  96. int doShow(int argc, const char **argv,
  97. CommandLineParserType commandLineParser);
  98. int doReport(int argc, const char **argv,
  99. CommandLineParserType commandLineParser);
  100. int doExport(int argc, const char **argv,
  101. CommandLineParserType commandLineParser);
  102. std::vector<StringRef> ObjectFilenames;
  103. CoverageViewOptions ViewOpts;
  104. CoverageFiltersMatchAll Filters;
  105. CoverageFilters IgnoreFilenameFilters;
  106. /// The path to the indexed profile.
  107. std::string PGOFilename;
  108. /// A list of input source files.
  109. std::vector<std::string> SourceFiles;
  110. /// In -path-equivalence mode, this maps the absolute paths from the coverage
  111. /// mapping data to the input source files.
  112. StringMap<std::string> RemappedFilenames;
  113. /// The coverage data path to be remapped from, and the source path to be
  114. /// remapped to, when using -path-equivalence.
  115. Optional<std::pair<std::string, std::string>> PathRemapping;
  116. /// The architecture the coverage mapping data targets.
  117. std::vector<StringRef> CoverageArches;
  118. /// A cache for demangled symbols.
  119. DemangleCache DC;
  120. /// A lock which guards printing to stderr.
  121. std::mutex ErrsLock;
  122. /// A container for input source file buffers.
  123. std::mutex LoadedSourceFilesLock;
  124. std::vector<std::pair<std::string, std::unique_ptr<MemoryBuffer>>>
  125. LoadedSourceFiles;
  126. /// Whitelist from -name-whitelist to be used for filtering.
  127. std::unique_ptr<SpecialCaseList> NameWhitelist;
  128. };
  129. }
  130. static std::string getErrorString(const Twine &Message, StringRef Whence,
  131. bool Warning) {
  132. std::string Str = (Warning ? "warning" : "error");
  133. Str += ": ";
  134. if (!Whence.empty())
  135. Str += Whence.str() + ": ";
  136. Str += Message.str() + "\n";
  137. return Str;
  138. }
  139. void CodeCoverageTool::error(const Twine &Message, StringRef Whence) {
  140. std::unique_lock<std::mutex> Guard{ErrsLock};
  141. ViewOpts.colored_ostream(errs(), raw_ostream::RED)
  142. << getErrorString(Message, Whence, false);
  143. }
  144. void CodeCoverageTool::warning(const Twine &Message, StringRef Whence) {
  145. std::unique_lock<std::mutex> Guard{ErrsLock};
  146. ViewOpts.colored_ostream(errs(), raw_ostream::RED)
  147. << getErrorString(Message, Whence, true);
  148. }
  149. void CodeCoverageTool::addCollectedPath(const std::string &Path) {
  150. SmallString<128> EffectivePath(Path);
  151. if (std::error_code EC = sys::fs::make_absolute(EffectivePath)) {
  152. error(EC.message(), Path);
  153. return;
  154. }
  155. sys::path::remove_dots(EffectivePath, /*remove_dot_dots=*/true);
  156. if (!IgnoreFilenameFilters.matchesFilename(EffectivePath))
  157. SourceFiles.emplace_back(EffectivePath.str());
  158. }
  159. void CodeCoverageTool::collectPaths(const std::string &Path) {
  160. llvm::sys::fs::file_status Status;
  161. llvm::sys::fs::status(Path, Status);
  162. if (!llvm::sys::fs::exists(Status)) {
  163. if (PathRemapping)
  164. addCollectedPath(Path);
  165. else
  166. warning("Source file doesn't exist, proceeded by ignoring it.", Path);
  167. return;
  168. }
  169. if (llvm::sys::fs::is_regular_file(Status)) {
  170. addCollectedPath(Path);
  171. return;
  172. }
  173. if (llvm::sys::fs::is_directory(Status)) {
  174. std::error_code EC;
  175. for (llvm::sys::fs::recursive_directory_iterator F(Path, EC), E;
  176. F != E; F.increment(EC)) {
  177. if (EC) {
  178. warning(EC.message(), F->path());
  179. continue;
  180. }
  181. if (llvm::sys::fs::is_regular_file(F->path()))
  182. addCollectedPath(F->path());
  183. }
  184. }
  185. }
  186. ErrorOr<const MemoryBuffer &>
  187. CodeCoverageTool::getSourceFile(StringRef SourceFile) {
  188. // If we've remapped filenames, look up the real location for this file.
  189. std::unique_lock<std::mutex> Guard{LoadedSourceFilesLock};
  190. if (!RemappedFilenames.empty()) {
  191. auto Loc = RemappedFilenames.find(SourceFile);
  192. if (Loc != RemappedFilenames.end())
  193. SourceFile = Loc->second;
  194. }
  195. for (const auto &Files : LoadedSourceFiles)
  196. if (sys::fs::equivalent(SourceFile, Files.first))
  197. return *Files.second;
  198. auto Buffer = MemoryBuffer::getFile(SourceFile);
  199. if (auto EC = Buffer.getError()) {
  200. error(EC.message(), SourceFile);
  201. return EC;
  202. }
  203. LoadedSourceFiles.emplace_back(SourceFile, std::move(Buffer.get()));
  204. return *LoadedSourceFiles.back().second;
  205. }
  206. void CodeCoverageTool::attachExpansionSubViews(
  207. SourceCoverageView &View, ArrayRef<ExpansionRecord> Expansions,
  208. const CoverageMapping &Coverage) {
  209. if (!ViewOpts.ShowExpandedRegions)
  210. return;
  211. for (const auto &Expansion : Expansions) {
  212. auto ExpansionCoverage = Coverage.getCoverageForExpansion(Expansion);
  213. if (ExpansionCoverage.empty())
  214. continue;
  215. auto SourceBuffer = getSourceFile(ExpansionCoverage.getFilename());
  216. if (!SourceBuffer)
  217. continue;
  218. auto SubViewExpansions = ExpansionCoverage.getExpansions();
  219. auto SubView =
  220. SourceCoverageView::create(Expansion.Function.Name, SourceBuffer.get(),
  221. ViewOpts, std::move(ExpansionCoverage));
  222. attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
  223. View.addExpansion(Expansion.Region, std::move(SubView));
  224. }
  225. }
  226. std::unique_ptr<SourceCoverageView>
  227. CodeCoverageTool::createFunctionView(const FunctionRecord &Function,
  228. const CoverageMapping &Coverage) {
  229. auto FunctionCoverage = Coverage.getCoverageForFunction(Function);
  230. if (FunctionCoverage.empty())
  231. return nullptr;
  232. auto SourceBuffer = getSourceFile(FunctionCoverage.getFilename());
  233. if (!SourceBuffer)
  234. return nullptr;
  235. auto Expansions = FunctionCoverage.getExpansions();
  236. auto View = SourceCoverageView::create(DC.demangle(Function.Name),
  237. SourceBuffer.get(), ViewOpts,
  238. std::move(FunctionCoverage));
  239. attachExpansionSubViews(*View, Expansions, Coverage);
  240. return View;
  241. }
  242. std::unique_ptr<SourceCoverageView>
  243. CodeCoverageTool::createSourceFileView(StringRef SourceFile,
  244. const CoverageMapping &Coverage) {
  245. auto SourceBuffer = getSourceFile(SourceFile);
  246. if (!SourceBuffer)
  247. return nullptr;
  248. auto FileCoverage = Coverage.getCoverageForFile(SourceFile);
  249. if (FileCoverage.empty())
  250. return nullptr;
  251. auto Expansions = FileCoverage.getExpansions();
  252. auto View = SourceCoverageView::create(SourceFile, SourceBuffer.get(),
  253. ViewOpts, std::move(FileCoverage));
  254. attachExpansionSubViews(*View, Expansions, Coverage);
  255. if (!ViewOpts.ShowFunctionInstantiations)
  256. return View;
  257. for (const auto &Group : Coverage.getInstantiationGroups(SourceFile)) {
  258. // Skip functions which have a single instantiation.
  259. if (Group.size() < 2)
  260. continue;
  261. for (const FunctionRecord *Function : Group.getInstantiations()) {
  262. std::unique_ptr<SourceCoverageView> SubView{nullptr};
  263. StringRef Funcname = DC.demangle(Function->Name);
  264. if (Function->ExecutionCount > 0) {
  265. auto SubViewCoverage = Coverage.getCoverageForFunction(*Function);
  266. auto SubViewExpansions = SubViewCoverage.getExpansions();
  267. SubView = SourceCoverageView::create(
  268. Funcname, SourceBuffer.get(), ViewOpts, std::move(SubViewCoverage));
  269. attachExpansionSubViews(*SubView, SubViewExpansions, Coverage);
  270. }
  271. unsigned FileID = Function->CountedRegions.front().FileID;
  272. unsigned Line = 0;
  273. for (const auto &CR : Function->CountedRegions)
  274. if (CR.FileID == FileID)
  275. Line = std::max(CR.LineEnd, Line);
  276. View->addInstantiation(Funcname, Line, std::move(SubView));
  277. }
  278. }
  279. return View;
  280. }
  281. static bool modifiedTimeGT(StringRef LHS, StringRef RHS) {
  282. sys::fs::file_status Status;
  283. if (sys::fs::status(LHS, Status))
  284. return false;
  285. auto LHSTime = Status.getLastModificationTime();
  286. if (sys::fs::status(RHS, Status))
  287. return false;
  288. auto RHSTime = Status.getLastModificationTime();
  289. return LHSTime > RHSTime;
  290. }
  291. std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
  292. for (StringRef ObjectFilename : ObjectFilenames)
  293. if (modifiedTimeGT(ObjectFilename, PGOFilename))
  294. warning("profile data may be out of date - object is newer",
  295. ObjectFilename);
  296. auto CoverageOrErr =
  297. CoverageMapping::load(ObjectFilenames, PGOFilename, CoverageArches);
  298. if (Error E = CoverageOrErr.takeError()) {
  299. error("Failed to load coverage: " + toString(std::move(E)),
  300. join(ObjectFilenames.begin(), ObjectFilenames.end(), ", "));
  301. return nullptr;
  302. }
  303. auto Coverage = std::move(CoverageOrErr.get());
  304. unsigned Mismatched = Coverage->getMismatchedCount();
  305. if (Mismatched) {
  306. warning(Twine(Mismatched) + " functions have mismatched data");
  307. if (ViewOpts.Debug) {
  308. for (const auto &HashMismatch : Coverage->getHashMismatches())
  309. errs() << "hash-mismatch: "
  310. << "No profile record found for '" << HashMismatch.first << "'"
  311. << " with hash = 0x" << Twine::utohexstr(HashMismatch.second)
  312. << '\n';
  313. for (const auto &CounterMismatch : Coverage->getCounterMismatches())
  314. errs() << "counter-mismatch: "
  315. << "Coverage mapping for " << CounterMismatch.first
  316. << " only has " << CounterMismatch.second
  317. << " valid counter expressions\n";
  318. }
  319. }
  320. remapPathNames(*Coverage);
  321. if (!SourceFiles.empty())
  322. removeUnmappedInputs(*Coverage);
  323. demangleSymbols(*Coverage);
  324. return Coverage;
  325. }
  326. void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
  327. if (!PathRemapping)
  328. return;
  329. // Convert remapping paths to native paths with trailing seperators.
  330. auto nativeWithTrailing = [](StringRef Path) -> std::string {
  331. if (Path.empty())
  332. return "";
  333. SmallString<128> NativePath;
  334. sys::path::native(Path, NativePath);
  335. if (!sys::path::is_separator(NativePath.back()))
  336. NativePath += sys::path::get_separator();
  337. return NativePath.c_str();
  338. };
  339. std::string RemapFrom = nativeWithTrailing(PathRemapping->first);
  340. std::string RemapTo = nativeWithTrailing(PathRemapping->second);
  341. // Create a mapping from coverage data file paths to local paths.
  342. for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
  343. SmallString<128> NativeFilename;
  344. sys::path::native(Filename, NativeFilename);
  345. if (NativeFilename.startswith(RemapFrom)) {
  346. RemappedFilenames[Filename] =
  347. RemapTo + NativeFilename.substr(RemapFrom.size()).str();
  348. }
  349. }
  350. // Convert input files from local paths to coverage data file paths.
  351. StringMap<std::string> InvRemappedFilenames;
  352. for (const auto &RemappedFilename : RemappedFilenames)
  353. InvRemappedFilenames[RemappedFilename.getValue()] = RemappedFilename.getKey();
  354. for (std::string &Filename : SourceFiles) {
  355. SmallString<128> NativeFilename;
  356. sys::path::native(Filename, NativeFilename);
  357. auto CovFileName = InvRemappedFilenames.find(NativeFilename);
  358. if (CovFileName != InvRemappedFilenames.end())
  359. Filename = CovFileName->second;
  360. }
  361. }
  362. void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
  363. std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
  364. auto UncoveredFilesIt = SourceFiles.end();
  365. // The user may have specified source files which aren't in the coverage
  366. // mapping. Filter these files away.
  367. UncoveredFilesIt = std::remove_if(
  368. SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
  369. return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
  370. SF);
  371. });
  372. SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
  373. }
  374. void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
  375. if (!ViewOpts.hasDemangler())
  376. return;
  377. // Pass function names to the demangler in a temporary file.
  378. int InputFD;
  379. SmallString<256> InputPath;
  380. std::error_code EC =
  381. sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
  382. if (EC) {
  383. error(InputPath, EC.message());
  384. return;
  385. }
  386. ToolOutputFile InputTOF{InputPath, InputFD};
  387. unsigned NumSymbols = 0;
  388. for (const auto &Function : Coverage.getCoveredFunctions()) {
  389. InputTOF.os() << Function.Name << '\n';
  390. ++NumSymbols;
  391. }
  392. InputTOF.os().close();
  393. // Use another temporary file to store the demangler's output.
  394. int OutputFD;
  395. SmallString<256> OutputPath;
  396. EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
  397. OutputPath);
  398. if (EC) {
  399. error(OutputPath, EC.message());
  400. return;
  401. }
  402. ToolOutputFile OutputTOF{OutputPath, OutputFD};
  403. OutputTOF.os().close();
  404. // Invoke the demangler.
  405. std::vector<StringRef> ArgsV;
  406. for (StringRef Arg : ViewOpts.DemanglerOpts)
  407. ArgsV.push_back(Arg);
  408. Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}};
  409. std::string ErrMsg;
  410. int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV,
  411. /*env=*/None, Redirects, /*secondsToWait=*/0,
  412. /*memoryLimit=*/0, &ErrMsg);
  413. if (RC) {
  414. error(ErrMsg, ViewOpts.DemanglerOpts[0]);
  415. return;
  416. }
  417. // Parse the demangler's output.
  418. auto BufOrError = MemoryBuffer::getFile(OutputPath);
  419. if (!BufOrError) {
  420. error(OutputPath, BufOrError.getError().message());
  421. return;
  422. }
  423. std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
  424. SmallVector<StringRef, 8> Symbols;
  425. StringRef DemanglerData = DemanglerBuf->getBuffer();
  426. DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
  427. /*KeepEmpty=*/false);
  428. if (Symbols.size() != NumSymbols) {
  429. error("Demangler did not provide expected number of symbols");
  430. return;
  431. }
  432. // Cache the demangled names.
  433. unsigned I = 0;
  434. for (const auto &Function : Coverage.getCoveredFunctions())
  435. // On Windows, lines in the demangler's output file end with "\r\n".
  436. // Splitting by '\n' keeps '\r's, so cut them now.
  437. DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
  438. }
  439. void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
  440. CoverageMapping *Coverage,
  441. CoveragePrinter *Printer,
  442. bool ShowFilenames) {
  443. auto View = createSourceFileView(SourceFile, *Coverage);
  444. if (!View) {
  445. warning("The file '" + SourceFile + "' isn't covered.");
  446. return;
  447. }
  448. auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
  449. if (Error E = OSOrErr.takeError()) {
  450. error("Could not create view file!", toString(std::move(E)));
  451. return;
  452. }
  453. auto OS = std::move(OSOrErr.get());
  454. View->print(*OS.get(), /*Wholefile=*/true,
  455. /*ShowSourceName=*/ShowFilenames,
  456. /*ShowTitle=*/ViewOpts.hasOutputDirectory());
  457. Printer->closeViewFile(std::move(OS));
  458. }
  459. int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
  460. cl::opt<std::string> CovFilename(
  461. cl::Positional, cl::desc("Covered executable or object file."));
  462. cl::list<std::string> CovFilenames(
  463. "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
  464. cl::CommaSeparated);
  465. cl::list<std::string> InputSourceFiles(
  466. cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
  467. cl::opt<bool> DebugDumpCollectedPaths(
  468. "dump-collected-paths", cl::Optional, cl::Hidden,
  469. cl::desc("Show the collected paths to source files"));
  470. cl::opt<std::string, true> PGOFilename(
  471. "instr-profile", cl::Required, cl::location(this->PGOFilename),
  472. cl::desc(
  473. "File with the profile data obtained after an instrumented run"));
  474. cl::list<std::string> Arches(
  475. "arch", cl::desc("architectures of the coverage mapping binaries"));
  476. cl::opt<bool> DebugDump("dump", cl::Optional,
  477. cl::desc("Show internal debug dump"));
  478. cl::opt<CoverageViewOptions::OutputFormat> Format(
  479. "format", cl::desc("Output format for line-based coverage reports"),
  480. cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
  481. "Text output"),
  482. clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
  483. "HTML output")),
  484. cl::init(CoverageViewOptions::OutputFormat::Text));
  485. cl::opt<std::string> PathRemap(
  486. "path-equivalence", cl::Optional,
  487. cl::desc("<from>,<to> Map coverage data paths to local source file "
  488. "paths"));
  489. cl::OptionCategory FilteringCategory("Function filtering options");
  490. cl::list<std::string> NameFilters(
  491. "name", cl::Optional,
  492. cl::desc("Show code coverage only for functions with the given name"),
  493. cl::ZeroOrMore, cl::cat(FilteringCategory));
  494. cl::list<std::string> NameFilterFiles(
  495. "name-whitelist", cl::Optional,
  496. cl::desc("Show code coverage only for functions listed in the given "
  497. "file"),
  498. cl::ZeroOrMore, cl::cat(FilteringCategory));
  499. cl::list<std::string> NameRegexFilters(
  500. "name-regex", cl::Optional,
  501. cl::desc("Show code coverage only for functions that match the given "
  502. "regular expression"),
  503. cl::ZeroOrMore, cl::cat(FilteringCategory));
  504. cl::list<std::string> IgnoreFilenameRegexFilters(
  505. "ignore-filename-regex", cl::Optional,
  506. cl::desc("Skip source code files with file paths that match the given "
  507. "regular expression"),
  508. cl::ZeroOrMore, cl::cat(FilteringCategory));
  509. cl::opt<double> RegionCoverageLtFilter(
  510. "region-coverage-lt", cl::Optional,
  511. cl::desc("Show code coverage only for functions with region coverage "
  512. "less than the given threshold"),
  513. cl::cat(FilteringCategory));
  514. cl::opt<double> RegionCoverageGtFilter(
  515. "region-coverage-gt", cl::Optional,
  516. cl::desc("Show code coverage only for functions with region coverage "
  517. "greater than the given threshold"),
  518. cl::cat(FilteringCategory));
  519. cl::opt<double> LineCoverageLtFilter(
  520. "line-coverage-lt", cl::Optional,
  521. cl::desc("Show code coverage only for functions with line coverage less "
  522. "than the given threshold"),
  523. cl::cat(FilteringCategory));
  524. cl::opt<double> LineCoverageGtFilter(
  525. "line-coverage-gt", cl::Optional,
  526. cl::desc("Show code coverage only for functions with line coverage "
  527. "greater than the given threshold"),
  528. cl::cat(FilteringCategory));
  529. cl::opt<cl::boolOrDefault> UseColor(
  530. "use-color", cl::desc("Emit colored output (default=autodetect)"),
  531. cl::init(cl::BOU_UNSET));
  532. cl::list<std::string> DemanglerOpts(
  533. "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
  534. cl::opt<bool> RegionSummary(
  535. "show-region-summary", cl::Optional,
  536. cl::desc("Show region statistics in summary table"),
  537. cl::init(true));
  538. cl::opt<bool> InstantiationSummary(
  539. "show-instantiation-summary", cl::Optional,
  540. cl::desc("Show instantiation statistics in summary table"));
  541. cl::opt<bool> SummaryOnly(
  542. "summary-only", cl::Optional,
  543. cl::desc("Export only summary information for each source file"));
  544. cl::opt<unsigned> NumThreads(
  545. "num-threads", cl::init(0),
  546. cl::desc("Number of merge threads to use (default: autodetect)"));
  547. cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
  548. cl::aliasopt(NumThreads));
  549. auto commandLineParser = [&, this](int argc, const char **argv) -> int {
  550. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  551. ViewOpts.Debug = DebugDump;
  552. if (!CovFilename.empty())
  553. ObjectFilenames.emplace_back(CovFilename);
  554. for (const std::string &Filename : CovFilenames)
  555. ObjectFilenames.emplace_back(Filename);
  556. if (ObjectFilenames.empty()) {
  557. errs() << "No filenames specified!\n";
  558. ::exit(1);
  559. }
  560. ViewOpts.Format = Format;
  561. switch (ViewOpts.Format) {
  562. case CoverageViewOptions::OutputFormat::Text:
  563. ViewOpts.Colors = UseColor == cl::BOU_UNSET
  564. ? sys::Process::StandardOutHasColors()
  565. : UseColor == cl::BOU_TRUE;
  566. break;
  567. case CoverageViewOptions::OutputFormat::HTML:
  568. if (UseColor == cl::BOU_FALSE)
  569. errs() << "Color output cannot be disabled when generating html.\n";
  570. ViewOpts.Colors = true;
  571. break;
  572. }
  573. // If path-equivalence was given and is a comma seperated pair then set
  574. // PathRemapping.
  575. auto EquivPair = StringRef(PathRemap).split(',');
  576. if (!(EquivPair.first.empty() && EquivPair.second.empty()))
  577. PathRemapping = EquivPair;
  578. // If a demangler is supplied, check if it exists and register it.
  579. if (DemanglerOpts.size()) {
  580. auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
  581. if (!DemanglerPathOrErr) {
  582. error("Could not find the demangler!",
  583. DemanglerPathOrErr.getError().message());
  584. return 1;
  585. }
  586. DemanglerOpts[0] = *DemanglerPathOrErr;
  587. ViewOpts.DemanglerOpts.swap(DemanglerOpts);
  588. }
  589. // Read in -name-whitelist files.
  590. if (!NameFilterFiles.empty()) {
  591. std::string SpecialCaseListErr;
  592. NameWhitelist =
  593. SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr);
  594. if (!NameWhitelist)
  595. error(SpecialCaseListErr);
  596. }
  597. // Create the function filters
  598. if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) {
  599. auto NameFilterer = llvm::make_unique<CoverageFilters>();
  600. for (const auto &Name : NameFilters)
  601. NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
  602. if (NameWhitelist)
  603. NameFilterer->push_back(
  604. llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
  605. for (const auto &Regex : NameRegexFilters)
  606. NameFilterer->push_back(
  607. llvm::make_unique<NameRegexCoverageFilter>(Regex));
  608. Filters.push_back(std::move(NameFilterer));
  609. }
  610. if (RegionCoverageLtFilter.getNumOccurrences() ||
  611. RegionCoverageGtFilter.getNumOccurrences() ||
  612. LineCoverageLtFilter.getNumOccurrences() ||
  613. LineCoverageGtFilter.getNumOccurrences()) {
  614. auto StatFilterer = llvm::make_unique<CoverageFilters>();
  615. if (RegionCoverageLtFilter.getNumOccurrences())
  616. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  617. RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
  618. if (RegionCoverageGtFilter.getNumOccurrences())
  619. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  620. RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
  621. if (LineCoverageLtFilter.getNumOccurrences())
  622. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  623. LineCoverageFilter::LessThan, LineCoverageLtFilter));
  624. if (LineCoverageGtFilter.getNumOccurrences())
  625. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  626. RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
  627. Filters.push_back(std::move(StatFilterer));
  628. }
  629. // Create the ignore filename filters.
  630. for (const auto &RE : IgnoreFilenameRegexFilters)
  631. IgnoreFilenameFilters.push_back(
  632. llvm::make_unique<NameRegexCoverageFilter>(RE));
  633. if (!Arches.empty()) {
  634. for (const std::string &Arch : Arches) {
  635. if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
  636. error("Unknown architecture: " + Arch);
  637. return 1;
  638. }
  639. CoverageArches.emplace_back(Arch);
  640. }
  641. if (CoverageArches.size() != ObjectFilenames.size()) {
  642. error("Number of architectures doesn't match the number of objects");
  643. return 1;
  644. }
  645. }
  646. // IgnoreFilenameFilters are applied even when InputSourceFiles specified.
  647. for (const std::string &File : InputSourceFiles)
  648. collectPaths(File);
  649. if (DebugDumpCollectedPaths) {
  650. for (const std::string &SF : SourceFiles)
  651. outs() << SF << '\n';
  652. ::exit(0);
  653. }
  654. ViewOpts.ShowRegionSummary = RegionSummary;
  655. ViewOpts.ShowInstantiationSummary = InstantiationSummary;
  656. ViewOpts.ExportSummaryOnly = SummaryOnly;
  657. ViewOpts.NumThreads = NumThreads;
  658. return 0;
  659. };
  660. switch (Cmd) {
  661. case Show:
  662. return doShow(argc, argv, commandLineParser);
  663. case Report:
  664. return doReport(argc, argv, commandLineParser);
  665. case Export:
  666. return doExport(argc, argv, commandLineParser);
  667. }
  668. return 0;
  669. }
  670. int CodeCoverageTool::doShow(int argc, const char **argv,
  671. CommandLineParserType commandLineParser) {
  672. cl::OptionCategory ViewCategory("Viewing options");
  673. cl::opt<bool> ShowLineExecutionCounts(
  674. "show-line-counts", cl::Optional,
  675. cl::desc("Show the execution counts for each line"), cl::init(true),
  676. cl::cat(ViewCategory));
  677. cl::opt<bool> ShowRegions(
  678. "show-regions", cl::Optional,
  679. cl::desc("Show the execution counts for each region"),
  680. cl::cat(ViewCategory));
  681. cl::opt<bool> ShowBestLineRegionsCounts(
  682. "show-line-counts-or-regions", cl::Optional,
  683. cl::desc("Show the execution counts for each line, or the execution "
  684. "counts for each region on lines that have multiple regions"),
  685. cl::cat(ViewCategory));
  686. cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
  687. cl::desc("Show expanded source regions"),
  688. cl::cat(ViewCategory));
  689. cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
  690. cl::desc("Show function instantiations"),
  691. cl::init(true), cl::cat(ViewCategory));
  692. cl::opt<std::string> ShowOutputDirectory(
  693. "output-dir", cl::init(""),
  694. cl::desc("Directory in which coverage information is written out"));
  695. cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
  696. cl::aliasopt(ShowOutputDirectory));
  697. cl::opt<uint32_t> TabSize(
  698. "tab-size", cl::init(2),
  699. cl::desc(
  700. "Set tab expansion size for html coverage reports (default = 2)"));
  701. cl::opt<std::string> ProjectTitle(
  702. "project-title", cl::Optional,
  703. cl::desc("Set project title for the coverage report"));
  704. auto Err = commandLineParser(argc, argv);
  705. if (Err)
  706. return Err;
  707. ViewOpts.ShowLineNumbers = true;
  708. ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
  709. !ShowRegions || ShowBestLineRegionsCounts;
  710. ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
  711. ViewOpts.ShowExpandedRegions = ShowExpansions;
  712. ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
  713. ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
  714. ViewOpts.TabSize = TabSize;
  715. ViewOpts.ProjectTitle = ProjectTitle;
  716. if (ViewOpts.hasOutputDirectory()) {
  717. if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
  718. error("Could not create output directory!", E.message());
  719. return 1;
  720. }
  721. }
  722. sys::fs::file_status Status;
  723. if (sys::fs::status(PGOFilename, Status)) {
  724. error("profdata file error: can not get the file status. \n");
  725. return 1;
  726. }
  727. auto ModifiedTime = Status.getLastModificationTime();
  728. std::string ModifiedTimeStr = to_string(ModifiedTime);
  729. size_t found = ModifiedTimeStr.rfind(':');
  730. ViewOpts.CreatedTimeStr = (found != std::string::npos)
  731. ? "Created: " + ModifiedTimeStr.substr(0, found)
  732. : "Created: " + ModifiedTimeStr;
  733. auto Coverage = load();
  734. if (!Coverage)
  735. return 1;
  736. auto Printer = CoveragePrinter::create(ViewOpts);
  737. if (SourceFiles.empty())
  738. // Get the source files from the function coverage mapping.
  739. for (StringRef Filename : Coverage->getUniqueSourceFiles()) {
  740. if (!IgnoreFilenameFilters.matchesFilename(Filename))
  741. SourceFiles.push_back(Filename);
  742. }
  743. // Create an index out of the source files.
  744. if (ViewOpts.hasOutputDirectory()) {
  745. if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
  746. error("Could not create index file!", toString(std::move(E)));
  747. return 1;
  748. }
  749. }
  750. if (!Filters.empty()) {
  751. // Build the map of filenames to functions.
  752. std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
  753. FilenameFunctionMap;
  754. for (const auto &SourceFile : SourceFiles)
  755. for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
  756. if (Filters.matches(*Coverage.get(), Function))
  757. FilenameFunctionMap[SourceFile].push_back(&Function);
  758. // Only print filter matching functions for each file.
  759. for (const auto &FileFunc : FilenameFunctionMap) {
  760. StringRef File = FileFunc.first;
  761. const auto &Functions = FileFunc.second;
  762. auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
  763. if (Error E = OSOrErr.takeError()) {
  764. error("Could not create view file!", toString(std::move(E)));
  765. return 1;
  766. }
  767. auto OS = std::move(OSOrErr.get());
  768. bool ShowTitle = ViewOpts.hasOutputDirectory();
  769. for (const auto *Function : Functions) {
  770. auto FunctionView = createFunctionView(*Function, *Coverage);
  771. if (!FunctionView) {
  772. warning("Could not read coverage for '" + Function->Name + "'.");
  773. continue;
  774. }
  775. FunctionView->print(*OS.get(), /*WholeFile=*/false,
  776. /*ShowSourceName=*/true, ShowTitle);
  777. ShowTitle = false;
  778. }
  779. Printer->closeViewFile(std::move(OS));
  780. }
  781. return 0;
  782. }
  783. // Show files
  784. bool ShowFilenames =
  785. (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
  786. (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
  787. auto NumThreads = ViewOpts.NumThreads;
  788. // If NumThreads is not specified, auto-detect a good default.
  789. if (NumThreads == 0)
  790. NumThreads =
  791. std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
  792. unsigned(SourceFiles.size())));
  793. if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
  794. for (const std::string &SourceFile : SourceFiles)
  795. writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
  796. ShowFilenames);
  797. } else {
  798. // In -output-dir mode, it's safe to use multiple threads to print files.
  799. ThreadPool Pool(NumThreads);
  800. for (const std::string &SourceFile : SourceFiles)
  801. Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
  802. Coverage.get(), Printer.get(), ShowFilenames);
  803. Pool.wait();
  804. }
  805. return 0;
  806. }
  807. int CodeCoverageTool::doReport(int argc, const char **argv,
  808. CommandLineParserType commandLineParser) {
  809. cl::opt<bool> ShowFunctionSummaries(
  810. "show-functions", cl::Optional, cl::init(false),
  811. cl::desc("Show coverage summaries for each function"));
  812. auto Err = commandLineParser(argc, argv);
  813. if (Err)
  814. return Err;
  815. if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
  816. error("HTML output for summary reports is not yet supported.");
  817. return 1;
  818. }
  819. auto Coverage = load();
  820. if (!Coverage)
  821. return 1;
  822. CoverageReport Report(ViewOpts, *Coverage.get());
  823. if (!ShowFunctionSummaries) {
  824. if (SourceFiles.empty())
  825. Report.renderFileReports(llvm::outs(), IgnoreFilenameFilters);
  826. else
  827. Report.renderFileReports(llvm::outs(), SourceFiles);
  828. } else {
  829. if (SourceFiles.empty()) {
  830. error("Source files must be specified when -show-functions=true is "
  831. "specified");
  832. return 1;
  833. }
  834. Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
  835. }
  836. return 0;
  837. }
  838. int CodeCoverageTool::doExport(int argc, const char **argv,
  839. CommandLineParserType commandLineParser) {
  840. auto Err = commandLineParser(argc, argv);
  841. if (Err)
  842. return Err;
  843. if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
  844. error("Coverage data can only be exported as textual JSON.");
  845. return 1;
  846. }
  847. auto Coverage = load();
  848. if (!Coverage) {
  849. error("Could not load coverage information");
  850. return 1;
  851. }
  852. auto Exporter = CoverageExporterJson(*Coverage.get(), ViewOpts, outs());
  853. if (SourceFiles.empty())
  854. Exporter.renderRoot(IgnoreFilenameFilters);
  855. else
  856. Exporter.renderRoot(SourceFiles);
  857. return 0;
  858. }
  859. int showMain(int argc, const char *argv[]) {
  860. CodeCoverageTool Tool;
  861. return Tool.run(CodeCoverageTool::Show, argc, argv);
  862. }
  863. int reportMain(int argc, const char *argv[]) {
  864. CodeCoverageTool Tool;
  865. return Tool.run(CodeCoverageTool::Report, argc, argv);
  866. }
  867. int exportMain(int argc, const char *argv[]) {
  868. CodeCoverageTool Tool;
  869. return Tool.run(CodeCoverageTool::Export, argc, argv);
  870. }