CodeCoverage.cpp 35 KB

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