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