CodeCoverage.cpp 33 KB

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