CodeCoverage.cpp 34 KB

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