CodeCoverage.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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(utostr(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" << utohexstr(HashMismatch.second) << "\n";
  307. for (const auto &CounterMismatch : Coverage->getCounterMismatches())
  308. errs() << "counter-mismatch: "
  309. << "Coverage mapping for " << CounterMismatch.first
  310. << " only has " << CounterMismatch.second
  311. << " valid counter expressions\n";
  312. }
  313. }
  314. remapPathNames(*Coverage);
  315. if (!SourceFiles.empty())
  316. removeUnmappedInputs(*Coverage);
  317. demangleSymbols(*Coverage);
  318. return Coverage;
  319. }
  320. void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
  321. if (!PathRemapping)
  322. return;
  323. // Convert remapping paths to native paths with trailing seperators.
  324. auto nativeWithTrailing = [](StringRef Path) -> std::string {
  325. if (Path.empty())
  326. return "";
  327. SmallString<128> NativePath;
  328. sys::path::native(Path, NativePath);
  329. if (!sys::path::is_separator(NativePath.back()))
  330. NativePath += sys::path::get_separator();
  331. return NativePath.c_str();
  332. };
  333. std::string RemapFrom = nativeWithTrailing(PathRemapping->first);
  334. std::string RemapTo = nativeWithTrailing(PathRemapping->second);
  335. // Create a mapping from coverage data file paths to local paths.
  336. for (StringRef Filename : Coverage.getUniqueSourceFiles()) {
  337. SmallString<128> NativeFilename;
  338. sys::path::native(Filename, NativeFilename);
  339. if (NativeFilename.startswith(RemapFrom)) {
  340. RemappedFilenames[Filename] =
  341. RemapTo + NativeFilename.substr(RemapFrom.size()).str();
  342. }
  343. }
  344. // Convert input files from local paths to coverage data file paths.
  345. StringMap<std::string> InvRemappedFilenames;
  346. for (const auto &RemappedFilename : RemappedFilenames)
  347. InvRemappedFilenames[RemappedFilename.getValue()] = RemappedFilename.getKey();
  348. for (std::string &Filename : SourceFiles) {
  349. SmallString<128> NativeFilename;
  350. sys::path::native(Filename, NativeFilename);
  351. auto CovFileName = InvRemappedFilenames.find(NativeFilename);
  352. if (CovFileName != InvRemappedFilenames.end())
  353. Filename = CovFileName->second;
  354. }
  355. }
  356. void CodeCoverageTool::removeUnmappedInputs(const CoverageMapping &Coverage) {
  357. std::vector<StringRef> CoveredFiles = Coverage.getUniqueSourceFiles();
  358. auto UncoveredFilesIt = SourceFiles.end();
  359. // The user may have specified source files which aren't in the coverage
  360. // mapping. Filter these files away.
  361. UncoveredFilesIt = std::remove_if(
  362. SourceFiles.begin(), SourceFiles.end(), [&](const std::string &SF) {
  363. return !std::binary_search(CoveredFiles.begin(), CoveredFiles.end(),
  364. SF);
  365. });
  366. SourceFiles.erase(UncoveredFilesIt, SourceFiles.end());
  367. }
  368. void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
  369. if (!ViewOpts.hasDemangler())
  370. return;
  371. // Pass function names to the demangler in a temporary file.
  372. int InputFD;
  373. SmallString<256> InputPath;
  374. std::error_code EC =
  375. sys::fs::createTemporaryFile("demangle-in", "list", InputFD, InputPath);
  376. if (EC) {
  377. error(InputPath, EC.message());
  378. return;
  379. }
  380. ToolOutputFile InputTOF{InputPath, InputFD};
  381. unsigned NumSymbols = 0;
  382. for (const auto &Function : Coverage.getCoveredFunctions()) {
  383. InputTOF.os() << Function.Name << '\n';
  384. ++NumSymbols;
  385. }
  386. InputTOF.os().close();
  387. // Use another temporary file to store the demangler's output.
  388. int OutputFD;
  389. SmallString<256> OutputPath;
  390. EC = sys::fs::createTemporaryFile("demangle-out", "list", OutputFD,
  391. OutputPath);
  392. if (EC) {
  393. error(OutputPath, EC.message());
  394. return;
  395. }
  396. ToolOutputFile OutputTOF{OutputPath, OutputFD};
  397. OutputTOF.os().close();
  398. // Invoke the demangler.
  399. std::vector<const char *> ArgsV;
  400. for (const std::string &Arg : ViewOpts.DemanglerOpts)
  401. ArgsV.push_back(Arg.c_str());
  402. ArgsV.push_back(nullptr);
  403. Optional<StringRef> Redirects[] = {InputPath.str(), OutputPath.str(), {""}};
  404. std::string ErrMsg;
  405. int RC = sys::ExecuteAndWait(ViewOpts.DemanglerOpts[0], ArgsV.data(),
  406. /*env=*/nullptr, Redirects, /*secondsToWait=*/0,
  407. /*memoryLimit=*/0, &ErrMsg);
  408. if (RC) {
  409. error(ErrMsg, ViewOpts.DemanglerOpts[0]);
  410. return;
  411. }
  412. // Parse the demangler's output.
  413. auto BufOrError = MemoryBuffer::getFile(OutputPath);
  414. if (!BufOrError) {
  415. error(OutputPath, BufOrError.getError().message());
  416. return;
  417. }
  418. std::unique_ptr<MemoryBuffer> DemanglerBuf = std::move(*BufOrError);
  419. SmallVector<StringRef, 8> Symbols;
  420. StringRef DemanglerData = DemanglerBuf->getBuffer();
  421. DemanglerData.split(Symbols, '\n', /*MaxSplit=*/NumSymbols,
  422. /*KeepEmpty=*/false);
  423. if (Symbols.size() != NumSymbols) {
  424. error("Demangler did not provide expected number of symbols");
  425. return;
  426. }
  427. // Cache the demangled names.
  428. unsigned I = 0;
  429. for (const auto &Function : Coverage.getCoveredFunctions())
  430. // On Windows, lines in the demangler's output file end with "\r\n".
  431. // Splitting by '\n' keeps '\r's, so cut them now.
  432. DC.DemangledNames[Function.Name] = Symbols[I++].rtrim();
  433. }
  434. void CodeCoverageTool::writeSourceFileView(StringRef SourceFile,
  435. CoverageMapping *Coverage,
  436. CoveragePrinter *Printer,
  437. bool ShowFilenames) {
  438. auto View = createSourceFileView(SourceFile, *Coverage);
  439. if (!View) {
  440. warning("The file '" + SourceFile + "' isn't covered.");
  441. return;
  442. }
  443. auto OSOrErr = Printer->createViewFile(SourceFile, /*InToplevel=*/false);
  444. if (Error E = OSOrErr.takeError()) {
  445. error("Could not create view file!", toString(std::move(E)));
  446. return;
  447. }
  448. auto OS = std::move(OSOrErr.get());
  449. View->print(*OS.get(), /*Wholefile=*/true,
  450. /*ShowSourceName=*/ShowFilenames,
  451. /*ShowTitle=*/ViewOpts.hasOutputDirectory());
  452. Printer->closeViewFile(std::move(OS));
  453. }
  454. int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
  455. cl::opt<std::string> CovFilename(
  456. cl::Positional, cl::desc("Covered executable or object file."));
  457. cl::list<std::string> CovFilenames(
  458. "object", cl::desc("Coverage executable or object file"), cl::ZeroOrMore,
  459. cl::CommaSeparated);
  460. cl::list<std::string> InputSourceFiles(
  461. cl::Positional, cl::desc("<Source files>"), cl::ZeroOrMore);
  462. cl::opt<bool> DebugDumpCollectedPaths(
  463. "dump-collected-paths", cl::Optional, cl::Hidden,
  464. cl::desc("Show the collected paths to source files"));
  465. cl::opt<std::string, true> PGOFilename(
  466. "instr-profile", cl::Required, cl::location(this->PGOFilename),
  467. cl::desc(
  468. "File with the profile data obtained after an instrumented run"));
  469. cl::list<std::string> Arches(
  470. "arch", cl::desc("architectures of the coverage mapping binaries"));
  471. cl::opt<bool> DebugDump("dump", cl::Optional,
  472. cl::desc("Show internal debug dump"));
  473. cl::opt<CoverageViewOptions::OutputFormat> Format(
  474. "format", cl::desc("Output format for line-based coverage reports"),
  475. cl::values(clEnumValN(CoverageViewOptions::OutputFormat::Text, "text",
  476. "Text output"),
  477. clEnumValN(CoverageViewOptions::OutputFormat::HTML, "html",
  478. "HTML output")),
  479. cl::init(CoverageViewOptions::OutputFormat::Text));
  480. cl::opt<std::string> PathRemap(
  481. "path-equivalence", cl::Optional,
  482. cl::desc("<from>,<to> Map coverage data paths to local source file "
  483. "paths"));
  484. cl::OptionCategory FilteringCategory("Function filtering options");
  485. cl::list<std::string> NameFilters(
  486. "name", cl::Optional,
  487. cl::desc("Show code coverage only for functions with the given name"),
  488. cl::ZeroOrMore, cl::cat(FilteringCategory));
  489. cl::list<std::string> NameFilterFiles(
  490. "name-whitelist", cl::Optional,
  491. cl::desc("Show code coverage only for functions listed in the given "
  492. "file"),
  493. cl::ZeroOrMore, cl::cat(FilteringCategory));
  494. cl::list<std::string> NameRegexFilters(
  495. "name-regex", cl::Optional,
  496. cl::desc("Show code coverage only for functions that match the given "
  497. "regular expression"),
  498. cl::ZeroOrMore, cl::cat(FilteringCategory));
  499. cl::opt<double> RegionCoverageLtFilter(
  500. "region-coverage-lt", cl::Optional,
  501. cl::desc("Show code coverage only for functions with region coverage "
  502. "less than the given threshold"),
  503. cl::cat(FilteringCategory));
  504. cl::opt<double> RegionCoverageGtFilter(
  505. "region-coverage-gt", cl::Optional,
  506. cl::desc("Show code coverage only for functions with region coverage "
  507. "greater than the given threshold"),
  508. cl::cat(FilteringCategory));
  509. cl::opt<double> LineCoverageLtFilter(
  510. "line-coverage-lt", cl::Optional,
  511. cl::desc("Show code coverage only for functions with line coverage less "
  512. "than the given threshold"),
  513. cl::cat(FilteringCategory));
  514. cl::opt<double> LineCoverageGtFilter(
  515. "line-coverage-gt", cl::Optional,
  516. cl::desc("Show code coverage only for functions with line coverage "
  517. "greater than the given threshold"),
  518. cl::cat(FilteringCategory));
  519. cl::opt<cl::boolOrDefault> UseColor(
  520. "use-color", cl::desc("Emit colored output (default=autodetect)"),
  521. cl::init(cl::BOU_UNSET));
  522. cl::list<std::string> DemanglerOpts(
  523. "Xdemangler", cl::desc("<demangler-path>|<demangler-option>"));
  524. cl::opt<bool> RegionSummary(
  525. "show-region-summary", cl::Optional,
  526. cl::desc("Show region statistics in summary table"),
  527. cl::init(true));
  528. cl::opt<bool> InstantiationSummary(
  529. "show-instantiation-summary", cl::Optional,
  530. cl::desc("Show instantiation statistics in summary table"));
  531. auto commandLineParser = [&, this](int argc, const char **argv) -> int {
  532. cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
  533. ViewOpts.Debug = DebugDump;
  534. if (!CovFilename.empty())
  535. ObjectFilenames.emplace_back(CovFilename);
  536. for (const std::string &Filename : CovFilenames)
  537. ObjectFilenames.emplace_back(Filename);
  538. if (ObjectFilenames.empty()) {
  539. errs() << "No filenames specified!\n";
  540. ::exit(1);
  541. }
  542. ViewOpts.Format = Format;
  543. switch (ViewOpts.Format) {
  544. case CoverageViewOptions::OutputFormat::Text:
  545. ViewOpts.Colors = UseColor == cl::BOU_UNSET
  546. ? sys::Process::StandardOutHasColors()
  547. : UseColor == cl::BOU_TRUE;
  548. break;
  549. case CoverageViewOptions::OutputFormat::HTML:
  550. if (UseColor == cl::BOU_FALSE)
  551. errs() << "Color output cannot be disabled when generating html.\n";
  552. ViewOpts.Colors = true;
  553. break;
  554. }
  555. // If path-equivalence was given and is a comma seperated pair then set
  556. // PathRemapping.
  557. auto EquivPair = StringRef(PathRemap).split(',');
  558. if (!(EquivPair.first.empty() && EquivPair.second.empty()))
  559. PathRemapping = EquivPair;
  560. // If a demangler is supplied, check if it exists and register it.
  561. if (DemanglerOpts.size()) {
  562. auto DemanglerPathOrErr = sys::findProgramByName(DemanglerOpts[0]);
  563. if (!DemanglerPathOrErr) {
  564. error("Could not find the demangler!",
  565. DemanglerPathOrErr.getError().message());
  566. return 1;
  567. }
  568. DemanglerOpts[0] = *DemanglerPathOrErr;
  569. ViewOpts.DemanglerOpts.swap(DemanglerOpts);
  570. }
  571. // Read in -name-whitelist files.
  572. if (!NameFilterFiles.empty()) {
  573. std::string SpecialCaseListErr;
  574. NameWhitelist =
  575. SpecialCaseList::create(NameFilterFiles, SpecialCaseListErr);
  576. if (!NameWhitelist)
  577. error(SpecialCaseListErr);
  578. }
  579. // Create the function filters
  580. if (!NameFilters.empty() || NameWhitelist || !NameRegexFilters.empty()) {
  581. auto NameFilterer = llvm::make_unique<CoverageFilters>();
  582. for (const auto &Name : NameFilters)
  583. NameFilterer->push_back(llvm::make_unique<NameCoverageFilter>(Name));
  584. if (NameWhitelist)
  585. NameFilterer->push_back(
  586. llvm::make_unique<NameWhitelistCoverageFilter>(*NameWhitelist));
  587. for (const auto &Regex : NameRegexFilters)
  588. NameFilterer->push_back(
  589. llvm::make_unique<NameRegexCoverageFilter>(Regex));
  590. Filters.push_back(std::move(NameFilterer));
  591. }
  592. if (RegionCoverageLtFilter.getNumOccurrences() ||
  593. RegionCoverageGtFilter.getNumOccurrences() ||
  594. LineCoverageLtFilter.getNumOccurrences() ||
  595. LineCoverageGtFilter.getNumOccurrences()) {
  596. auto StatFilterer = llvm::make_unique<CoverageFilters>();
  597. if (RegionCoverageLtFilter.getNumOccurrences())
  598. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  599. RegionCoverageFilter::LessThan, RegionCoverageLtFilter));
  600. if (RegionCoverageGtFilter.getNumOccurrences())
  601. StatFilterer->push_back(llvm::make_unique<RegionCoverageFilter>(
  602. RegionCoverageFilter::GreaterThan, RegionCoverageGtFilter));
  603. if (LineCoverageLtFilter.getNumOccurrences())
  604. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  605. LineCoverageFilter::LessThan, LineCoverageLtFilter));
  606. if (LineCoverageGtFilter.getNumOccurrences())
  607. StatFilterer->push_back(llvm::make_unique<LineCoverageFilter>(
  608. RegionCoverageFilter::GreaterThan, LineCoverageGtFilter));
  609. Filters.push_back(std::move(StatFilterer));
  610. }
  611. if (!Arches.empty()) {
  612. for (const std::string &Arch : Arches) {
  613. if (Triple(Arch).getArch() == llvm::Triple::ArchType::UnknownArch) {
  614. error("Unknown architecture: " + Arch);
  615. return 1;
  616. }
  617. CoverageArches.emplace_back(Arch);
  618. }
  619. if (CoverageArches.size() != ObjectFilenames.size()) {
  620. error("Number of architectures doesn't match the number of objects");
  621. return 1;
  622. }
  623. }
  624. for (const std::string &File : InputSourceFiles)
  625. collectPaths(File);
  626. if (DebugDumpCollectedPaths) {
  627. for (const std::string &SF : SourceFiles)
  628. outs() << SF << '\n';
  629. ::exit(0);
  630. }
  631. ViewOpts.ShowRegionSummary = RegionSummary;
  632. ViewOpts.ShowInstantiationSummary = InstantiationSummary;
  633. return 0;
  634. };
  635. switch (Cmd) {
  636. case Show:
  637. return show(argc, argv, commandLineParser);
  638. case Report:
  639. return report(argc, argv, commandLineParser);
  640. case Export:
  641. return export_(argc, argv, commandLineParser);
  642. }
  643. return 0;
  644. }
  645. int CodeCoverageTool::show(int argc, const char **argv,
  646. CommandLineParserType commandLineParser) {
  647. cl::OptionCategory ViewCategory("Viewing options");
  648. cl::opt<bool> ShowLineExecutionCounts(
  649. "show-line-counts", cl::Optional,
  650. cl::desc("Show the execution counts for each line"), cl::init(true),
  651. cl::cat(ViewCategory));
  652. cl::opt<bool> ShowRegions(
  653. "show-regions", cl::Optional,
  654. cl::desc("Show the execution counts for each region"),
  655. cl::cat(ViewCategory));
  656. cl::opt<bool> ShowBestLineRegionsCounts(
  657. "show-line-counts-or-regions", cl::Optional,
  658. cl::desc("Show the execution counts for each line, or the execution "
  659. "counts for each region on lines that have multiple regions"),
  660. cl::cat(ViewCategory));
  661. cl::opt<bool> ShowExpansions("show-expansions", cl::Optional,
  662. cl::desc("Show expanded source regions"),
  663. cl::cat(ViewCategory));
  664. cl::opt<bool> ShowInstantiations("show-instantiations", cl::Optional,
  665. cl::desc("Show function instantiations"),
  666. cl::init(true), cl::cat(ViewCategory));
  667. cl::opt<std::string> ShowOutputDirectory(
  668. "output-dir", cl::init(""),
  669. cl::desc("Directory in which coverage information is written out"));
  670. cl::alias ShowOutputDirectoryA("o", cl::desc("Alias for --output-dir"),
  671. cl::aliasopt(ShowOutputDirectory));
  672. cl::opt<uint32_t> TabSize(
  673. "tab-size", cl::init(2),
  674. cl::desc(
  675. "Set tab expansion size for html coverage reports (default = 2)"));
  676. cl::opt<std::string> ProjectTitle(
  677. "project-title", cl::Optional,
  678. cl::desc("Set project title for the coverage report"));
  679. cl::opt<unsigned> NumThreads(
  680. "num-threads", cl::init(0),
  681. cl::desc("Number of merge threads to use (default: autodetect)"));
  682. cl::alias NumThreadsA("j", cl::desc("Alias for --num-threads"),
  683. cl::aliasopt(NumThreads));
  684. auto Err = commandLineParser(argc, argv);
  685. if (Err)
  686. return Err;
  687. ViewOpts.ShowLineNumbers = true;
  688. ViewOpts.ShowLineStats = ShowLineExecutionCounts.getNumOccurrences() != 0 ||
  689. !ShowRegions || ShowBestLineRegionsCounts;
  690. ViewOpts.ShowRegionMarkers = ShowRegions || ShowBestLineRegionsCounts;
  691. ViewOpts.ShowExpandedRegions = ShowExpansions;
  692. ViewOpts.ShowFunctionInstantiations = ShowInstantiations;
  693. ViewOpts.ShowOutputDirectory = ShowOutputDirectory;
  694. ViewOpts.TabSize = TabSize;
  695. ViewOpts.ProjectTitle = ProjectTitle;
  696. if (ViewOpts.hasOutputDirectory()) {
  697. if (auto E = sys::fs::create_directories(ViewOpts.ShowOutputDirectory)) {
  698. error("Could not create output directory!", E.message());
  699. return 1;
  700. }
  701. }
  702. sys::fs::file_status Status;
  703. if (sys::fs::status(PGOFilename, Status)) {
  704. error("profdata file error: can not get the file status. \n");
  705. return 1;
  706. }
  707. auto ModifiedTime = Status.getLastModificationTime();
  708. std::string ModifiedTimeStr = to_string(ModifiedTime);
  709. size_t found = ModifiedTimeStr.rfind(':');
  710. ViewOpts.CreatedTimeStr = (found != std::string::npos)
  711. ? "Created: " + ModifiedTimeStr.substr(0, found)
  712. : "Created: " + ModifiedTimeStr;
  713. auto Coverage = load();
  714. if (!Coverage)
  715. return 1;
  716. auto Printer = CoveragePrinter::create(ViewOpts);
  717. if (SourceFiles.empty())
  718. // Get the source files from the function coverage mapping.
  719. for (StringRef Filename : Coverage->getUniqueSourceFiles())
  720. SourceFiles.push_back(Filename);
  721. // Create an index out of the source files.
  722. if (ViewOpts.hasOutputDirectory()) {
  723. if (Error E = Printer->createIndexFile(SourceFiles, *Coverage, Filters)) {
  724. error("Could not create index file!", toString(std::move(E)));
  725. return 1;
  726. }
  727. }
  728. if (!Filters.empty()) {
  729. // Build the map of filenames to functions.
  730. std::map<llvm::StringRef, std::vector<const FunctionRecord *>>
  731. FilenameFunctionMap;
  732. for (const auto &SourceFile : SourceFiles)
  733. for (const auto &Function : Coverage->getCoveredFunctions(SourceFile))
  734. if (Filters.matches(*Coverage.get(), Function))
  735. FilenameFunctionMap[SourceFile].push_back(&Function);
  736. // Only print filter matching functions for each file.
  737. for (const auto &FileFunc : FilenameFunctionMap) {
  738. const StringRef &File = FileFunc.first;
  739. const auto &Functions = FileFunc.second;
  740. auto OSOrErr = Printer->createViewFile(File, /*InToplevel=*/false);
  741. if (Error E = OSOrErr.takeError()) {
  742. error("Could not create view file!", toString(std::move(E)));
  743. return 1;
  744. }
  745. auto OS = std::move(OSOrErr.get());
  746. bool ShowTitle = true;
  747. for (const auto *Function : Functions) {
  748. auto FunctionView = createFunctionView(*Function, *Coverage);
  749. if (!FunctionView) {
  750. warning("Could not read coverage for '" + Function->Name + "'.");
  751. continue;
  752. }
  753. FunctionView->print(*OS.get(), /*WholeFile=*/false,
  754. /*ShowSourceName=*/true, ShowTitle);
  755. ShowTitle = false;
  756. }
  757. Printer->closeViewFile(std::move(OS));
  758. }
  759. return 0;
  760. }
  761. // Show files
  762. bool ShowFilenames =
  763. (SourceFiles.size() != 1) || ViewOpts.hasOutputDirectory() ||
  764. (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML);
  765. // If NumThreads is not specified, auto-detect a good default.
  766. if (NumThreads == 0)
  767. NumThreads =
  768. std::max(1U, std::min(llvm::heavyweight_hardware_concurrency(),
  769. unsigned(SourceFiles.size())));
  770. if (!ViewOpts.hasOutputDirectory() || NumThreads == 1) {
  771. for (const std::string &SourceFile : SourceFiles)
  772. writeSourceFileView(SourceFile, Coverage.get(), Printer.get(),
  773. ShowFilenames);
  774. } else {
  775. // In -output-dir mode, it's safe to use multiple threads to print files.
  776. ThreadPool Pool(NumThreads);
  777. for (const std::string &SourceFile : SourceFiles)
  778. Pool.async(&CodeCoverageTool::writeSourceFileView, this, SourceFile,
  779. Coverage.get(), Printer.get(), ShowFilenames);
  780. Pool.wait();
  781. }
  782. return 0;
  783. }
  784. int CodeCoverageTool::report(int argc, const char **argv,
  785. CommandLineParserType commandLineParser) {
  786. cl::opt<bool> ShowFunctionSummaries(
  787. "show-functions", cl::Optional, cl::init(false),
  788. cl::desc("Show coverage summaries for each function"));
  789. auto Err = commandLineParser(argc, argv);
  790. if (Err)
  791. return Err;
  792. if (ViewOpts.Format == CoverageViewOptions::OutputFormat::HTML) {
  793. error("HTML output for summary reports is not yet supported.");
  794. return 1;
  795. }
  796. auto Coverage = load();
  797. if (!Coverage)
  798. return 1;
  799. CoverageReport Report(ViewOpts, *Coverage.get());
  800. if (!ShowFunctionSummaries) {
  801. Report.renderFileReports(llvm::outs());
  802. } else {
  803. if (SourceFiles.empty()) {
  804. error("Source files must be specified when -show-functions=true is "
  805. "specified");
  806. return 1;
  807. }
  808. Report.renderFunctionReports(SourceFiles, DC, llvm::outs());
  809. }
  810. return 0;
  811. }
  812. int CodeCoverageTool::export_(int argc, const char **argv,
  813. CommandLineParserType commandLineParser) {
  814. auto Err = commandLineParser(argc, argv);
  815. if (Err)
  816. return Err;
  817. if (ViewOpts.Format != CoverageViewOptions::OutputFormat::Text) {
  818. error("Coverage data can only be exported as textual JSON.");
  819. return 1;
  820. }
  821. auto Coverage = load();
  822. if (!Coverage) {
  823. error("Could not load coverage information");
  824. return 1;
  825. }
  826. exportCoverageDataToJson(*Coverage.get(), ViewOpts, outs());
  827. return 0;
  828. }
  829. int showMain(int argc, const char *argv[]) {
  830. CodeCoverageTool Tool;
  831. return Tool.run(CodeCoverageTool::Show, argc, argv);
  832. }
  833. int reportMain(int argc, const char *argv[]) {
  834. CodeCoverageTool Tool;
  835. return Tool.run(CodeCoverageTool::Report, argc, argv);
  836. }
  837. int exportMain(int argc, const char *argv[]) {
  838. CodeCoverageTool Tool;
  839. return Tool.run(CodeCoverageTool::Export, argc, argv);
  840. }