2016-04-06 04:19:49 +08:00
|
|
|
//===-- sancov.cc ---------------------------------------------------------===//
|
2015-11-12 04:58:20 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a command-line tool for reading and analyzing sanitizer
|
|
|
|
// coverage.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2016-04-06 04:19:49 +08:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-04-06 04:19:49 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-02-12 08:29:07 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/DebugInfo/Symbolize/Symbolize.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-01-27 00:44:37 +08:00
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
|
|
|
#include "llvm/MC/MCInstrAnalysis.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2015-12-19 06:02:08 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
2016-02-19 08:26:20 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-02-19 08:26:20 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Errc.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/LineIterator.h"
|
2016-03-12 07:28:28 +08:00
|
|
|
#include "llvm/Support/MD5.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2016-02-12 08:29:07 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2015-12-16 08:31:48 +08:00
|
|
|
#include "llvm/Support/SpecialCaseList.h"
|
2015-12-05 02:35:37 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2015-11-12 04:58:20 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2016-03-25 04:41:18 +08:00
|
|
|
#include <algorithm>
|
2016-04-06 04:19:49 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2015-11-12 04:58:20 +08:00
|
|
|
#include <set>
|
2015-12-05 02:35:37 +08:00
|
|
|
#include <string>
|
2016-04-06 04:19:49 +08:00
|
|
|
#include <tuple>
|
2016-02-12 08:29:07 +08:00
|
|
|
#include <utility>
|
2015-11-12 04:58:20 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// --------- COMMAND LINE FLAGS ---------
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
enum ActionType {
|
|
|
|
PrintAction,
|
2016-02-27 10:21:44 +08:00
|
|
|
PrintCovPointsAction,
|
2015-12-05 02:35:37 +08:00
|
|
|
CoveredFunctionsAction,
|
2016-01-15 06:34:11 +08:00
|
|
|
NotCoveredFunctionsAction,
|
2016-03-24 08:00:08 +08:00
|
|
|
HtmlReportAction,
|
|
|
|
StatsAction
|
2015-12-05 02:35:37 +08:00
|
|
|
};
|
2015-11-12 04:58:20 +08:00
|
|
|
|
|
|
|
cl::opt<ActionType> Action(
|
|
|
|
cl::desc("Action (required)"), cl::Required,
|
|
|
|
cl::values(clEnumValN(PrintAction, "print", "Print coverage addresses"),
|
2016-02-27 10:21:44 +08:00
|
|
|
clEnumValN(PrintCovPointsAction, "print-coverage-pcs",
|
|
|
|
"Print coverage instrumentation points addresses."),
|
2015-12-05 02:35:37 +08:00
|
|
|
clEnumValN(CoveredFunctionsAction, "covered-functions",
|
2015-11-12 04:58:20 +08:00
|
|
|
"Print all covered funcions."),
|
2015-12-05 02:35:37 +08:00
|
|
|
clEnumValN(NotCoveredFunctionsAction, "not-covered-functions",
|
|
|
|
"Print all not covered funcions."),
|
2016-01-15 06:34:11 +08:00
|
|
|
clEnumValN(HtmlReportAction, "html-report",
|
|
|
|
"Print HTML coverage report."),
|
2016-03-24 08:00:08 +08:00
|
|
|
clEnumValN(StatsAction, "print-coverage-stats",
|
|
|
|
"Print coverage statistics."),
|
2015-11-12 04:58:20 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
cl::list<std::string>
|
2016-02-12 08:29:07 +08:00
|
|
|
ClInputFiles(cl::Positional, cl::OneOrMore,
|
|
|
|
cl::desc("(<binary file>|<.sancov file>)..."));
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
cl::opt<bool> ClDemangle("demangle", cl::init(true),
|
|
|
|
cl::desc("Print demangled function name."));
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
cl::opt<std::string> ClStripPathPrefix(
|
2015-12-05 02:35:37 +08:00
|
|
|
"strip_path_prefix", cl::init(""),
|
|
|
|
cl::desc("Strip this prefix from file paths in reports."));
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
cl::opt<std::string>
|
2015-12-16 08:31:48 +08:00
|
|
|
ClBlacklist("blacklist", cl::init(""),
|
|
|
|
cl::desc("Blacklist file (sanitizer blacklist format)."));
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
cl::opt<bool> ClUseDefaultBlacklist(
|
2015-12-16 08:31:48 +08:00
|
|
|
"use_default_blacklist", cl::init(true), cl::Hidden,
|
|
|
|
cl::desc("Controls if default blacklist should be used."));
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
const char *const DefaultBlacklistStr = "fun:__sanitizer_.*\n"
|
|
|
|
"src:/usr/include/.*\n"
|
|
|
|
"src:.*/libc\\+\\+/.*\n";
|
2015-12-16 08:31:48 +08:00
|
|
|
|
2015-11-12 04:58:20 +08:00
|
|
|
// --------- FORMAT SPECIFICATION ---------
|
|
|
|
|
|
|
|
struct FileHeader {
|
|
|
|
uint32_t Bitness;
|
|
|
|
uint32_t Magic;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
const uint32_t BinCoverageMagic = 0xC0BFFFFF;
|
|
|
|
const uint32_t Bitness32 = 0xFFFFFF32;
|
|
|
|
const uint32_t Bitness64 = 0xFFFFFF64;
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// --------- ERROR HANDLING ---------
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void Fail(const llvm::Twine &E) {
|
2016-02-12 08:29:07 +08:00
|
|
|
errs() << "Error: " << E << "\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void FailIfError(std::error_code Error) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (!Error)
|
|
|
|
return;
|
|
|
|
errs() << "Error: " << Error.message() << "(" << Error.value() << ")\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
template <typename T>
|
|
|
|
void FailIfError(const ErrorOr<T> &E) {
|
2015-12-05 02:35:37 +08:00
|
|
|
FailIfError(E.getError());
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void FailIfNotEmpty(const llvm::Twine &E) {
|
2016-02-12 08:29:07 +08:00
|
|
|
if (E.str().empty())
|
2015-11-12 04:58:20 +08:00
|
|
|
return;
|
2016-02-12 08:29:07 +08:00
|
|
|
Fail(E);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
template <typename T>
|
2016-04-06 04:19:49 +08:00
|
|
|
void FailIfEmpty(const std::unique_ptr<T> &Ptr, const std::string &Message) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (Ptr.get())
|
|
|
|
return;
|
2016-02-12 08:29:07 +08:00
|
|
|
Fail(Message);
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// ---------
|
|
|
|
|
|
|
|
// Produces std::map<K, std::vector<E>> grouping input
|
|
|
|
// elements by FuncTy result.
|
|
|
|
template <class RangeTy, class FuncTy>
|
2016-04-06 04:19:49 +08:00
|
|
|
inline auto group_by(const RangeTy &R, FuncTy F)
|
2016-02-19 08:26:20 +08:00
|
|
|
-> std::map<typename std::decay<decltype(F(*R.begin()))>::type,
|
|
|
|
std::vector<typename std::decay<decltype(*R.begin())>::type>> {
|
|
|
|
std::map<typename std::decay<decltype(F(*R.begin()))>::type,
|
|
|
|
std::vector<typename std::decay<decltype(*R.begin())>::type>>
|
|
|
|
Result;
|
|
|
|
for (const auto &E : R) {
|
|
|
|
Result[F(E)].push_back(E);
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-11-12 04:58:20 +08:00
|
|
|
template <typename T>
|
2016-04-06 04:19:49 +08:00
|
|
|
void readInts(const char *Start, const char *End, std::set<uint64_t> *Ints) {
|
2015-11-12 04:58:20 +08:00
|
|
|
const T *S = reinterpret_cast<const T *>(Start);
|
|
|
|
const T *E = reinterpret_cast<const T *>(End);
|
2015-12-05 02:35:37 +08:00
|
|
|
std::copy(S, E, std::inserter(*Ints, Ints->end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FileLoc {
|
|
|
|
bool operator<(const FileLoc &RHS) const {
|
|
|
|
return std::tie(FileName, Line) < std::tie(RHS.FileName, RHS.Line);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileName;
|
|
|
|
uint32_t Line;
|
|
|
|
};
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
struct FileFn {
|
|
|
|
bool operator<(const FileFn &RHS) const {
|
|
|
|
return std::tie(FileName, FunctionName) <
|
|
|
|
std::tie(RHS.FileName, RHS.FunctionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileName;
|
|
|
|
std::string FunctionName;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FnLoc {
|
|
|
|
bool operator<(const FnLoc &RHS) const {
|
2015-12-05 02:35:37 +08:00
|
|
|
return std::tie(Loc, FunctionName) < std::tie(RHS.Loc, RHS.FunctionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLoc Loc;
|
|
|
|
std::string FunctionName;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string stripPathPrefix(std::string Path) {
|
|
|
|
if (ClStripPathPrefix.empty())
|
|
|
|
return Path;
|
|
|
|
size_t Pos = Path.find(ClStripPathPrefix);
|
|
|
|
if (Pos == std::string::npos)
|
|
|
|
return Path;
|
|
|
|
return Path.substr(Pos + ClStripPathPrefix.size());
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
|
2016-01-15 06:34:11 +08:00
|
|
|
symbolize::LLVMSymbolizer::Options SymbolizerOptions;
|
|
|
|
SymbolizerOptions.Demangle = ClDemangle;
|
|
|
|
SymbolizerOptions.UseSymbolTable = true;
|
|
|
|
return std::unique_ptr<symbolize::LLVMSymbolizer>(
|
|
|
|
new symbolize::LLVMSymbolizer(SymbolizerOptions));
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// A DILineInfo with address.
|
|
|
|
struct AddrInfo : public DILineInfo {
|
|
|
|
uint64_t Addr;
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
AddrInfo(const DILineInfo &DI, uint64_t Addr) : DILineInfo(DI), Addr(Addr) {
|
|
|
|
FileName = normalizeFilename(FileName);
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
private:
|
|
|
|
static std::string normalizeFilename(std::string FileName) {
|
|
|
|
SmallString<256> S(FileName);
|
|
|
|
sys::path::remove_dots(S, /* remove_dot_dot */ true);
|
|
|
|
return S.str().str();
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
};
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
class Blacklists {
|
|
|
|
public:
|
|
|
|
Blacklists()
|
|
|
|
: DefaultBlacklist(createDefaultBlacklist()),
|
|
|
|
UserBlacklist(createUserBlacklist()) {}
|
|
|
|
|
|
|
|
bool isBlacklisted(const DILineInfo &DI) {
|
|
|
|
if (DefaultBlacklist && DefaultBlacklist->inSection("fun", DI.FunctionName))
|
|
|
|
return true;
|
|
|
|
if (DefaultBlacklist && DefaultBlacklist->inSection("src", DI.FileName))
|
|
|
|
return true;
|
|
|
|
if (UserBlacklist && UserBlacklist->inSection("fun", DI.FunctionName))
|
|
|
|
return true;
|
|
|
|
if (UserBlacklist && UserBlacklist->inSection("src", DI.FileName))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
private:
|
|
|
|
static std::unique_ptr<SpecialCaseList> createDefaultBlacklist() {
|
|
|
|
if (!ClUseDefaultBlacklist)
|
|
|
|
return std::unique_ptr<SpecialCaseList>();
|
|
|
|
std::unique_ptr<MemoryBuffer> MB =
|
|
|
|
MemoryBuffer::getMemBuffer(DefaultBlacklistStr);
|
|
|
|
std::string Error;
|
|
|
|
auto Blacklist = SpecialCaseList::create(MB.get(), Error);
|
|
|
|
FailIfNotEmpty(Error);
|
|
|
|
return Blacklist;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
static std::unique_ptr<SpecialCaseList> createUserBlacklist() {
|
|
|
|
if (ClBlacklist.empty())
|
|
|
|
return std::unique_ptr<SpecialCaseList>();
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
return SpecialCaseList::createOrDie({{ClBlacklist}});
|
|
|
|
}
|
|
|
|
std::unique_ptr<SpecialCaseList> DefaultBlacklist;
|
|
|
|
std::unique_ptr<SpecialCaseList> UserBlacklist;
|
|
|
|
};
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Collect all debug info for given addresses.
|
2016-04-06 04:19:49 +08:00
|
|
|
std::vector<AddrInfo> getAddrInfo(std::string ObjectFile,
|
|
|
|
const std::set<uint64_t> &Addrs,
|
|
|
|
bool InlinedCode) {
|
2016-02-19 08:26:20 +08:00
|
|
|
std::vector<AddrInfo> Result;
|
|
|
|
auto Symbolizer(createSymbolizer());
|
|
|
|
Blacklists B;
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
for (auto Addr : Addrs) {
|
|
|
|
auto LineInfo = Symbolizer->symbolizeCode(ObjectFile, Addr);
|
|
|
|
FailIfError(LineInfo);
|
|
|
|
if (B.isBlacklisted(*LineInfo))
|
2015-12-05 02:35:37 +08:00
|
|
|
continue;
|
2016-02-19 08:26:20 +08:00
|
|
|
Result.push_back(AddrInfo(*LineInfo, Addr));
|
|
|
|
if (InlinedCode) {
|
|
|
|
auto InliningInfo = Symbolizer->symbolizeInlinedCode(ObjectFile, Addr);
|
|
|
|
FailIfError(InliningInfo);
|
|
|
|
for (uint32_t I = 0; I < InliningInfo->getNumberOfFrames(); ++I) {
|
|
|
|
auto FrameInfo = InliningInfo->getFrame(I);
|
|
|
|
if (B.isBlacklisted(FrameInfo))
|
|
|
|
continue;
|
|
|
|
Result.push_back(AddrInfo(FrameInfo, Addr));
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 08:31:48 +08:00
|
|
|
return Result;
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
// Locate __sanitizer_cov* function addresses that are used for coverage
|
|
|
|
// reporting.
|
2016-04-06 04:19:49 +08:00
|
|
|
std::set<uint64_t>
|
2015-12-15 07:55:04 +08:00
|
|
|
findSanitizerCovFunctions(const object::ObjectFile &O) {
|
|
|
|
std::set<uint64_t> Result;
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
for (const object::SymbolRef &Symbol : O.symbols()) {
|
|
|
|
ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
|
|
|
|
FailIfError(AddressOrErr);
|
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = Symbol.getName();
|
|
|
|
FailIfError(NameOrErr);
|
|
|
|
StringRef Name = NameOrErr.get();
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
if (Name == "__sanitizer_cov" || Name == "__sanitizer_cov_with_check" ||
|
|
|
|
Name == "__sanitizer_cov_trace_func_enter") {
|
2016-02-19 08:26:20 +08:00
|
|
|
if (!(Symbol.getFlags() & object::BasicSymbolRef::SF_Undefined))
|
|
|
|
Result.insert(AddressOrErr.get());
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-15 07:55:04 +08:00
|
|
|
|
|
|
|
return Result;
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Locate addresses of all coverage points in a file. Coverage point
|
|
|
|
// is defined as the 'address of instruction following __sanitizer_cov
|
|
|
|
// call - 1'.
|
2016-04-06 04:19:49 +08:00
|
|
|
void getObjectCoveragePoints(const object::ObjectFile &O,
|
|
|
|
std::set<uint64_t> *Addrs) {
|
2015-12-05 02:35:37 +08:00
|
|
|
Triple TheTriple("unknown-unknown-unknown");
|
|
|
|
TheTriple.setArch(Triple::ArchType(O.getArch()));
|
|
|
|
auto TripleName = TheTriple.getTriple();
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
|
|
|
|
FailIfNotEmpty(Error);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCSubtargetInfo> STI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, "", ""));
|
|
|
|
FailIfEmpty(STI, "no subtarget info for target " + TripleName);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCRegisterInfo> MRI(
|
|
|
|
TheTarget->createMCRegInfo(TripleName));
|
|
|
|
FailIfEmpty(MRI, "no register info for target " + TripleName);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCAsmInfo> AsmInfo(
|
|
|
|
TheTarget->createMCAsmInfo(*MRI, TripleName));
|
|
|
|
FailIfEmpty(AsmInfo, "no asm info for target " + TripleName);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
|
|
|
|
MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
|
|
|
|
std::unique_ptr<MCDisassembler> DisAsm(
|
|
|
|
TheTarget->createMCDisassembler(*STI, Ctx));
|
|
|
|
FailIfEmpty(DisAsm, "no disassembler info for target " + TripleName);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
|
|
|
FailIfEmpty(MII, "no instruction info for target " + TripleName);
|
|
|
|
|
|
|
|
std::unique_ptr<const MCInstrAnalysis> MIA(
|
|
|
|
TheTarget->createMCInstrAnalysis(MII.get()));
|
|
|
|
FailIfEmpty(MIA, "no instruction analysis info for target " + TripleName);
|
|
|
|
|
2015-12-15 07:55:04 +08:00
|
|
|
auto SanCovAddrs = findSanitizerCovFunctions(O);
|
2016-02-19 08:26:20 +08:00
|
|
|
if (SanCovAddrs.empty())
|
|
|
|
Fail("__sanitizer_cov* functions not found");
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-22 21:11:58 +08:00
|
|
|
for (object::SectionRef Section : O.sections()) {
|
2015-12-05 02:35:37 +08:00
|
|
|
if (Section.isVirtual() || !Section.isText()) // llvm-objdump does the same.
|
|
|
|
continue;
|
|
|
|
uint64_t SectionAddr = Section.getAddress();
|
|
|
|
uint64_t SectSize = Section.getSize();
|
|
|
|
if (!SectSize)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
StringRef BytesStr;
|
|
|
|
FailIfError(Section.getContents(BytesStr));
|
|
|
|
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
|
|
|
|
BytesStr.size());
|
|
|
|
|
|
|
|
for (uint64_t Index = 0, Size = 0; Index < Section.getSize();
|
|
|
|
Index += Size) {
|
|
|
|
MCInst Inst;
|
|
|
|
if (!DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
|
|
|
|
SectionAddr + Index, nulls(), nulls())) {
|
|
|
|
if (Size == 0)
|
|
|
|
Size = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
uint64_t Addr = Index + SectionAddr;
|
|
|
|
// Sanitizer coverage uses the address of the next instruction - 1.
|
|
|
|
uint64_t CovPoint = Addr + Size - 1;
|
2015-12-05 02:35:37 +08:00
|
|
|
uint64_t Target;
|
|
|
|
if (MIA->isCall(Inst) &&
|
2016-02-19 08:26:20 +08:00
|
|
|
MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target) &&
|
|
|
|
SanCovAddrs.find(Target) != SanCovAddrs.end())
|
|
|
|
Addrs->insert(CovPoint);
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void
|
2016-02-19 08:26:20 +08:00
|
|
|
visitObjectFiles(const object::Archive &A,
|
|
|
|
std::function<void(const object::ObjectFile &)> Fn) {
|
2015-12-05 02:35:37 +08:00
|
|
|
for (auto &ErrorOrChild : A.children()) {
|
|
|
|
FailIfError(ErrorOrChild);
|
|
|
|
const object::Archive::Child &C = *ErrorOrChild;
|
|
|
|
ErrorOr<std::unique_ptr<object::Binary>> ChildOrErr = C.getAsBinary();
|
|
|
|
FailIfError(ChildOrErr);
|
2016-02-19 08:26:20 +08:00
|
|
|
if (auto *O = dyn_cast<object::ObjectFile>(&*ChildOrErr.get()))
|
|
|
|
Fn(*O);
|
2015-12-05 02:35:37 +08:00
|
|
|
else
|
|
|
|
FailIfError(object::object_error::invalid_file_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void
|
2016-02-19 08:26:20 +08:00
|
|
|
visitObjectFiles(std::string FileName,
|
|
|
|
std::function<void(const object::ObjectFile &)> Fn) {
|
2015-12-05 02:35:37 +08:00
|
|
|
ErrorOr<object::OwningBinary<object::Binary>> BinaryOrErr =
|
|
|
|
object::createBinary(FileName);
|
|
|
|
FailIfError(BinaryOrErr);
|
|
|
|
|
|
|
|
object::Binary &Binary = *BinaryOrErr.get().getBinary();
|
|
|
|
if (object::Archive *A = dyn_cast<object::Archive>(&Binary))
|
2016-02-19 08:26:20 +08:00
|
|
|
visitObjectFiles(*A, Fn);
|
2015-12-05 02:35:37 +08:00
|
|
|
else if (object::ObjectFile *O = dyn_cast<object::ObjectFile>(&Binary))
|
2016-02-19 08:26:20 +08:00
|
|
|
Fn(*O);
|
2015-11-12 04:58:20 +08:00
|
|
|
else
|
2015-12-05 02:35:37 +08:00
|
|
|
FailIfError(object::object_error::invalid_file_type);
|
2015-12-16 08:31:48 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
std::set<uint64_t> findSanitizerCovFunctions(std::string FileName) {
|
|
|
|
std::set<uint64_t> Result;
|
|
|
|
visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
|
|
|
|
auto Addrs = findSanitizerCovFunctions(O);
|
|
|
|
Result.insert(Addrs.begin(), Addrs.end());
|
|
|
|
});
|
|
|
|
return Result;
|
2015-12-16 08:31:48 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Locate addresses of all coverage points in a file. Coverage point
|
|
|
|
// is defined as the 'address of instruction following __sanitizer_cov
|
|
|
|
// call - 1'.
|
|
|
|
std::set<uint64_t> getCoveragePoints(std::string FileName) {
|
|
|
|
std::set<uint64_t> Result;
|
|
|
|
visitObjectFiles(FileName, [&](const object::ObjectFile &O) {
|
|
|
|
getObjectCoveragePoints(O, &Result);
|
|
|
|
});
|
|
|
|
return Result;
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void printCovPoints(std::string ObjFile, raw_ostream &OS) {
|
2016-02-27 10:21:44 +08:00
|
|
|
for (uint64_t Addr : getCoveragePoints(ObjFile)) {
|
|
|
|
OS << "0x";
|
|
|
|
OS.write_hex(Addr);
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
std::string escapeHtml(const std::string &S) {
|
2016-01-15 06:34:11 +08:00
|
|
|
std::string Result;
|
|
|
|
Result.reserve(S.size());
|
|
|
|
for (char Ch : S) {
|
|
|
|
switch (Ch) {
|
|
|
|
case '&':
|
|
|
|
Result.append("&");
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
Result.append("'");
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
Result.append(""");
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
Result.append("<");
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
Result.append(">");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Result.push_back(Ch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-03-25 04:41:18 +08:00
|
|
|
// Adds leading zeroes wrapped in 'lz' style.
|
|
|
|
// Leading zeroes help locate 000% coverage.
|
2016-04-06 04:19:49 +08:00
|
|
|
std::string formatHtmlPct(size_t Pct) {
|
2016-03-25 04:41:18 +08:00
|
|
|
Pct = std::max(std::size_t{0}, std::min(std::size_t{100}, Pct));
|
|
|
|
|
|
|
|
std::string Num = std::to_string(Pct);
|
|
|
|
std::string Zeroes(3 - Num.size(), '0');
|
|
|
|
if (!Zeroes.empty())
|
|
|
|
Zeroes = "<span class='lz'>" + Zeroes + "</span>";
|
|
|
|
|
|
|
|
return Zeroes + Num;
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
std::string anchorName(std::string Anchor) {
|
2016-03-12 07:28:28 +08:00
|
|
|
llvm::MD5 Hasher;
|
|
|
|
llvm::MD5::MD5Result Hash;
|
|
|
|
Hasher.update(Anchor);
|
|
|
|
Hasher.final(Hash);
|
|
|
|
|
|
|
|
SmallString<32> HexString;
|
|
|
|
llvm::MD5::stringifyResult(Hash, HexString);
|
|
|
|
return HexString.str().str();
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
ErrorOr<bool> isCoverageFile(std::string FileName) {
|
2016-02-12 08:29:07 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(FileName);
|
2016-03-12 07:26:41 +08:00
|
|
|
if (!BufOrErr) {
|
|
|
|
errs() << "Warning: " << BufOrErr.getError().message() << "("
|
|
|
|
<< BufOrErr.getError().value()
|
|
|
|
<< "), filename: " << llvm::sys::path::filename(FileName) << "\n";
|
2016-02-12 08:29:07 +08:00
|
|
|
return BufOrErr.getError();
|
2016-03-12 07:26:41 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
|
|
|
|
if (Buf->getBufferSize() < 8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const FileHeader *Header =
|
|
|
|
reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
|
|
|
|
return Header->Magic == BinCoverageMagic;
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:00:08 +08:00
|
|
|
struct CoverageStats {
|
|
|
|
CoverageStats() : AllPoints(0), CovPoints(0), AllFns(0), CovFns(0) {}
|
|
|
|
|
|
|
|
size_t AllPoints;
|
|
|
|
size_t CovPoints;
|
|
|
|
size_t AllFns;
|
|
|
|
size_t CovFns;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const CoverageStats &Stats) {
|
2016-03-25 05:49:55 +08:00
|
|
|
OS << "all-edges: " << Stats.AllPoints << "\n";
|
|
|
|
OS << "cov-edges: " << Stats.CovPoints << "\n";
|
|
|
|
OS << "all-functions: " << Stats.AllFns << "\n";
|
|
|
|
OS << "cov-functions: " << Stats.CovFns << "\n";
|
2016-03-24 08:00:08 +08:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2015-11-12 04:58:20 +08:00
|
|
|
class CoverageData {
|
2016-03-19 06:40:52 +08:00
|
|
|
public:
|
2015-11-12 04:58:20 +08:00
|
|
|
// Read single file coverage data.
|
2016-03-19 06:40:52 +08:00
|
|
|
static ErrorOr<std::unique_ptr<CoverageData>> read(std::string FileName) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(FileName);
|
|
|
|
if (!BufOrErr)
|
|
|
|
return BufOrErr.getError();
|
|
|
|
std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
|
|
|
|
if (Buf->getBufferSize() < 8) {
|
|
|
|
errs() << "File too small (<8): " << Buf->getBufferSize();
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
|
|
|
}
|
|
|
|
const FileHeader *Header =
|
|
|
|
reinterpret_cast<const FileHeader *>(Buf->getBufferStart());
|
|
|
|
|
|
|
|
if (Header->Magic != BinCoverageMagic) {
|
|
|
|
errs() << "Wrong magic: " << Header->Magic;
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Addrs = llvm::make_unique<std::set<uint64_t>>();
|
|
|
|
|
|
|
|
switch (Header->Bitness) {
|
|
|
|
case Bitness64:
|
|
|
|
readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
|
|
|
|
Addrs.get());
|
|
|
|
break;
|
|
|
|
case Bitness32:
|
|
|
|
readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),
|
|
|
|
Addrs.get());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errs() << "Unsupported bitness: " << Header->Bitness;
|
|
|
|
return make_error_code(errc::illegal_byte_sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_ptr<CoverageData>(new CoverageData(std::move(Addrs)));
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge multiple coverage data together.
|
|
|
|
static std::unique_ptr<CoverageData>
|
|
|
|
merge(const std::vector<std::unique_ptr<CoverageData>> &Covs) {
|
2015-12-05 02:35:37 +08:00
|
|
|
auto Addrs = llvm::make_unique<std::set<uint64_t>>();
|
2015-11-12 04:58:20 +08:00
|
|
|
|
|
|
|
for (const auto &Cov : Covs)
|
2015-12-05 02:35:37 +08:00
|
|
|
Addrs->insert(Cov->Addrs->begin(), Cov->Addrs->end());
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
return std::unique_ptr<CoverageData>(new CoverageData(std::move(Addrs)));
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read list of files and merges their coverage info.
|
|
|
|
static ErrorOr<std::unique_ptr<CoverageData>>
|
|
|
|
readAndMerge(const std::vector<std::string> &FileNames) {
|
|
|
|
std::vector<std::unique_ptr<CoverageData>> Covs;
|
|
|
|
for (const auto &FileName : FileNames) {
|
|
|
|
auto Cov = read(FileName);
|
|
|
|
if (!Cov)
|
|
|
|
return Cov.getError();
|
|
|
|
Covs.push_back(std::move(Cov.get()));
|
|
|
|
}
|
|
|
|
return merge(Covs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print coverage addresses.
|
2015-12-05 02:35:37 +08:00
|
|
|
void printAddrs(raw_ostream &OS) {
|
2015-11-12 04:58:20 +08:00
|
|
|
for (auto Addr : *Addrs) {
|
2015-12-05 02:35:37 +08:00
|
|
|
OS << "0x";
|
|
|
|
OS.write_hex(Addr);
|
|
|
|
OS << "\n";
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
protected:
|
|
|
|
explicit CoverageData(std::unique_ptr<std::set<uint64_t>> Addrs)
|
|
|
|
: Addrs(std::move(Addrs)) {}
|
2016-01-15 06:34:11 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
friend class CoverageDataWithObjectFile;
|
|
|
|
|
|
|
|
std::unique_ptr<std::set<uint64_t>> Addrs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Coverage data translated into source code line-level information.
|
|
|
|
// Fetches debug info in constructor and calculates various information per
|
|
|
|
// request.
|
|
|
|
class SourceCoverageData {
|
|
|
|
public:
|
|
|
|
enum LineStatus {
|
|
|
|
// coverage information for the line is not available.
|
|
|
|
// default value in maps.
|
|
|
|
UNKNOWN = 0,
|
|
|
|
// the line is fully covered.
|
|
|
|
COVERED = 1,
|
|
|
|
// the line is fully uncovered.
|
|
|
|
NOT_COVERED = 2,
|
|
|
|
// some points in the line a covered, some are not.
|
|
|
|
MIXED = 3
|
|
|
|
};
|
|
|
|
|
2016-03-24 08:00:08 +08:00
|
|
|
SourceCoverageData(std::string ObjectFile, const std::set<uint64_t> &Addrs)
|
|
|
|
: AllCovPoints(getCoveragePoints(ObjectFile)) {
|
2016-02-19 08:26:20 +08:00
|
|
|
if (!std::includes(AllCovPoints.begin(), AllCovPoints.end(), Addrs.begin(),
|
|
|
|
Addrs.end())) {
|
|
|
|
Fail("Coverage points in binary and .sancov file do not match.");
|
2016-01-15 06:34:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
AllAddrInfo = getAddrInfo(ObjectFile, AllCovPoints, true);
|
|
|
|
CovAddrInfo = getAddrInfo(ObjectFile, Addrs, true);
|
|
|
|
}
|
2016-01-15 06:34:11 +08:00
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
// Compute number of coverage points hit/total in a file.
|
|
|
|
// file_name -> <coverage, all_coverage>
|
|
|
|
std::map<std::string, std::pair<size_t, size_t>> computeFileCoverage() {
|
2016-02-19 08:26:20 +08:00
|
|
|
std::map<std::string, std::pair<size_t, size_t>> FileCoverage;
|
|
|
|
auto AllCovPointsByFile =
|
|
|
|
group_by(AllAddrInfo, [](const AddrInfo &AI) { return AI.FileName; });
|
2016-03-18 08:12:14 +08:00
|
|
|
auto CovPointsByFile =
|
2016-02-19 08:26:20 +08:00
|
|
|
group_by(CovAddrInfo, [](const AddrInfo &AI) { return AI.FileName; });
|
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
for (const auto &P : AllCovPointsByFile) {
|
2016-02-19 08:26:20 +08:00
|
|
|
const std::string &FileName = P.first;
|
2016-03-18 08:12:14 +08:00
|
|
|
|
|
|
|
FileCoverage[FileName] =
|
|
|
|
std::make_pair(CovPointsByFile[FileName].size(),
|
|
|
|
AllCovPointsByFile[FileName].size());
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
return FileCoverage;
|
|
|
|
}
|
2016-01-15 06:34:11 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// line_number -> line_status.
|
|
|
|
typedef std::map<int, LineStatus> LineStatusMap;
|
|
|
|
// file_name -> LineStatusMap
|
|
|
|
typedef std::map<std::string, LineStatusMap> FileLineStatusMap;
|
|
|
|
|
|
|
|
// fills in the {file_name -> {line_no -> status}} map.
|
|
|
|
FileLineStatusMap computeLineStatusMap() {
|
|
|
|
FileLineStatusMap StatusMap;
|
|
|
|
|
|
|
|
auto AllLocs = group_by(AllAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileLoc{AI.FileName, AI.Line};
|
|
|
|
});
|
|
|
|
auto CovLocs = group_by(CovAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileLoc{AI.FileName, AI.Line};
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &P : AllLocs) {
|
|
|
|
const FileLoc &Loc = P.first;
|
|
|
|
auto I = CovLocs.find(Loc);
|
|
|
|
|
|
|
|
if (I == CovLocs.end()) {
|
|
|
|
StatusMap[Loc.FileName][Loc.Line] = NOT_COVERED;
|
|
|
|
} else {
|
|
|
|
StatusMap[Loc.FileName][Loc.Line] =
|
|
|
|
(I->second.size() == P.second.size()) ? COVERED : MIXED;
|
2016-01-15 06:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
return StatusMap;
|
2016-01-15 06:34:11 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
std::set<FileFn> computeAllFunctions() const {
|
|
|
|
std::set<FileFn> Fns;
|
|
|
|
for (const auto &AI : AllAddrInfo) {
|
|
|
|
Fns.insert(FileFn{AI.FileName, AI.FunctionName});
|
|
|
|
}
|
|
|
|
return Fns;
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
std::set<FileFn> computeCoveredFunctions() const {
|
|
|
|
std::set<FileFn> Fns;
|
|
|
|
auto CovFns = group_by(CovAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileFn{AI.FileName, AI.FunctionName};
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &P : CovFns) {
|
|
|
|
Fns.insert(P.first);
|
|
|
|
}
|
|
|
|
return Fns;
|
2015-12-05 02:35:37 +08:00
|
|
|
}
|
2015-11-13 03:34:21 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
std::set<FileFn> computeNotCoveredFunctions() const {
|
|
|
|
std::set<FileFn> Fns;
|
2015-12-05 02:35:37 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
auto AllFns = group_by(AllAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileFn{AI.FileName, AI.FunctionName};
|
|
|
|
});
|
|
|
|
auto CovFns = group_by(CovAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileFn{AI.FileName, AI.FunctionName};
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &P : AllFns) {
|
|
|
|
if (CovFns.find(P.first) == CovFns.end()) {
|
|
|
|
Fns.insert(P.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Fns;
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
// Compute % coverage for each function.
|
|
|
|
std::map<FileFn, int> computeFunctionsCoverage() const {
|
|
|
|
std::map<FileFn, int> FnCoverage;
|
|
|
|
auto AllFns = group_by(AllAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileFn{AI.FileName, AI.FunctionName};
|
|
|
|
});
|
|
|
|
|
|
|
|
auto CovFns = group_by(CovAddrInfo, [](const AddrInfo &AI) {
|
|
|
|
return FileFn{AI.FileName, AI.FunctionName};
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &P : AllFns) {
|
|
|
|
FileFn F = P.first;
|
|
|
|
FnCoverage[F] = CovFns[F].size() * 100 / P.second.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return FnCoverage;
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
typedef std::map<FileLoc, std::set<std::string>> FunctionLocs;
|
|
|
|
// finds first line number in a file for each function.
|
|
|
|
FunctionLocs resolveFunctions(const std::set<FileFn> &Fns) const {
|
|
|
|
std::vector<AddrInfo> FnAddrs;
|
|
|
|
for (const auto &AI : AllAddrInfo) {
|
|
|
|
if (Fns.find(FileFn{AI.FileName, AI.FunctionName}) != Fns.end())
|
|
|
|
FnAddrs.push_back(AI);
|
|
|
|
}
|
2015-11-12 04:58:20 +08:00
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
auto GroupedAddrs = group_by(FnAddrs, [](const AddrInfo &AI) {
|
|
|
|
return FnLoc{FileLoc{AI.FileName, AI.Line}, AI.FunctionName};
|
|
|
|
});
|
|
|
|
|
|
|
|
FunctionLocs Result;
|
|
|
|
std::string LastFileName;
|
|
|
|
std::set<std::string> ProcessedFunctions;
|
|
|
|
|
|
|
|
for (const auto &P : GroupedAddrs) {
|
|
|
|
const FnLoc &Loc = P.first;
|
|
|
|
std::string FileName = Loc.Loc.FileName;
|
|
|
|
std::string FunctionName = Loc.FunctionName;
|
|
|
|
|
|
|
|
if (LastFileName != FileName)
|
|
|
|
ProcessedFunctions.clear();
|
|
|
|
LastFileName = FileName;
|
|
|
|
|
|
|
|
if (!ProcessedFunctions.insert(FunctionName).second)
|
|
|
|
continue;
|
|
|
|
|
2016-02-20 10:11:49 +08:00
|
|
|
auto FLoc = FileLoc{FileName, Loc.Loc.Line};
|
|
|
|
Result[FLoc].insert(FunctionName);
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<std::string> files() const {
|
|
|
|
std::set<std::string> Files;
|
|
|
|
for (const auto &AI : AllAddrInfo) {
|
|
|
|
Files.insert(AI.FileName);
|
|
|
|
}
|
|
|
|
return Files;
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:00:08 +08:00
|
|
|
void collectStats(CoverageStats *Stats) const {
|
|
|
|
Stats->AllPoints += AllCovPoints.size();
|
|
|
|
Stats->AllFns += computeAllFunctions().size();
|
|
|
|
Stats->CovFns += computeCoveredFunctions().size();
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
private:
|
2016-03-24 08:00:08 +08:00
|
|
|
const std::set<uint64_t> AllCovPoints;
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
std::vector<AddrInfo> AllAddrInfo;
|
|
|
|
std::vector<AddrInfo> CovAddrInfo;
|
2015-11-12 04:58:20 +08:00
|
|
|
};
|
2016-02-12 08:29:07 +08:00
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
void printFunctionLocs(const SourceCoverageData::FunctionLocs &FnLocs,
|
|
|
|
raw_ostream &OS) {
|
2016-02-19 08:26:20 +08:00
|
|
|
for (const auto &Fns : FnLocs) {
|
|
|
|
for (const auto &Fn : Fns.second) {
|
|
|
|
OS << stripPathPrefix(Fns.first.FileName) << ":" << Fns.first.Line << " "
|
|
|
|
<< Fn << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
// Holder for coverage data + filename of corresponding object file.
|
2016-02-19 08:26:20 +08:00
|
|
|
class CoverageDataWithObjectFile : public CoverageData {
|
2016-02-12 08:29:07 +08:00
|
|
|
public:
|
|
|
|
static ErrorOr<std::unique_ptr<CoverageDataWithObjectFile>>
|
|
|
|
readAndMerge(std::string ObjectFile,
|
|
|
|
const std::vector<std::string> &FileNames) {
|
|
|
|
auto MergedDataOrError = CoverageData::readAndMerge(FileNames);
|
|
|
|
if (!MergedDataOrError)
|
|
|
|
return MergedDataOrError.getError();
|
|
|
|
return std::unique_ptr<CoverageDataWithObjectFile>(
|
|
|
|
new CoverageDataWithObjectFile(ObjectFile,
|
|
|
|
std::move(MergedDataOrError.get())));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string object_file() const { return ObjectFile; }
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Print list of covered functions.
|
|
|
|
// Line format: <file_name>:<line> <function_name>
|
2016-02-12 08:29:07 +08:00
|
|
|
void printCoveredFunctions(raw_ostream &OS) const {
|
2016-02-19 08:26:20 +08:00
|
|
|
SourceCoverageData SCovData(ObjectFile, *Addrs);
|
|
|
|
auto CoveredFns = SCovData.computeCoveredFunctions();
|
|
|
|
printFunctionLocs(SCovData.resolveFunctions(CoveredFns), OS);
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Print list of not covered functions.
|
|
|
|
// Line format: <file_name>:<line> <function_name>
|
2016-02-12 08:29:07 +08:00
|
|
|
void printNotCoveredFunctions(raw_ostream &OS) const {
|
2016-02-19 08:26:20 +08:00
|
|
|
SourceCoverageData SCovData(ObjectFile, *Addrs);
|
|
|
|
auto NotCoveredFns = SCovData.computeNotCoveredFunctions();
|
|
|
|
printFunctionLocs(SCovData.resolveFunctions(NotCoveredFns), OS);
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void printReport(raw_ostream &OS) const {
|
2016-02-19 08:26:20 +08:00
|
|
|
SourceCoverageData SCovData(ObjectFile, *Addrs);
|
|
|
|
auto LineStatusMap = SCovData.computeLineStatusMap();
|
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
std::set<FileFn> AllFns = SCovData.computeAllFunctions();
|
2016-02-19 08:26:20 +08:00
|
|
|
// file_loc -> set[function_name]
|
2016-03-18 08:12:14 +08:00
|
|
|
auto AllFnsByLoc = SCovData.resolveFunctions(AllFns);
|
|
|
|
auto FileCoverage = SCovData.computeFileCoverage();
|
|
|
|
|
|
|
|
auto FnCoverage = SCovData.computeFunctionsCoverage();
|
|
|
|
auto FnCoverageByFile =
|
|
|
|
group_by(FnCoverage, [](const std::pair<FileFn, int> &FileFn) {
|
|
|
|
return FileFn.first.FileName;
|
|
|
|
});
|
2016-02-19 08:26:20 +08:00
|
|
|
|
|
|
|
// TOC
|
|
|
|
|
2016-02-20 06:55:08 +08:00
|
|
|
size_t NotCoveredFilesCount = 0;
|
|
|
|
std::set<std::string> Files = SCovData.files();
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Covered Files.
|
2016-02-20 06:55:08 +08:00
|
|
|
OS << "<details open><summary>Touched Files</summary>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
OS << "<table>\n";
|
2016-03-18 08:12:14 +08:00
|
|
|
OS << "<tr><th>File</th><th>Coverage %</th>";
|
2016-02-19 08:26:20 +08:00
|
|
|
OS << "<th>Hit (Total) Fns</th></tr>\n";
|
2016-02-20 06:55:08 +08:00
|
|
|
for (auto FileName : Files) {
|
2016-03-18 08:12:14 +08:00
|
|
|
std::pair<size_t, size_t> FC = FileCoverage[FileName];
|
2016-02-20 06:55:08 +08:00
|
|
|
if (FC.first == 0) {
|
|
|
|
NotCoveredFilesCount++;
|
2016-02-19 08:26:20 +08:00
|
|
|
continue;
|
2016-02-20 06:55:08 +08:00
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
size_t CovPct = FC.second == 0 ? 100 : 100 * FC.first / FC.second;
|
|
|
|
|
2016-03-12 07:28:28 +08:00
|
|
|
OS << "<tr><td><a href=\"#" << anchorName(FileName) << "\">"
|
2016-02-19 08:26:20 +08:00
|
|
|
<< stripPathPrefix(FileName) << "</a></td>"
|
2016-03-25 04:41:18 +08:00
|
|
|
<< "<td>" << formatHtmlPct(CovPct) << "%</td>"
|
2016-02-19 08:26:20 +08:00
|
|
|
<< "<td>" << FC.first << " (" << FC.second << ")"
|
|
|
|
<< "</tr>\n";
|
|
|
|
}
|
|
|
|
OS << "</table>\n";
|
|
|
|
OS << "</details>\n";
|
|
|
|
|
|
|
|
// Not covered files.
|
2016-02-20 06:55:08 +08:00
|
|
|
if (NotCoveredFilesCount) {
|
|
|
|
OS << "<details><summary>Not Touched Files</summary>\n";
|
|
|
|
OS << "<table>\n";
|
|
|
|
for (auto FileName : Files) {
|
2016-03-18 08:12:14 +08:00
|
|
|
std::pair<size_t, size_t> FC = FileCoverage[FileName];
|
2016-02-20 06:55:08 +08:00
|
|
|
if (FC.first == 0)
|
|
|
|
OS << "<tr><td>" << stripPathPrefix(FileName) << "</td>\n";
|
|
|
|
}
|
|
|
|
OS << "</table>\n";
|
|
|
|
OS << "</details>\n";
|
|
|
|
} else {
|
|
|
|
OS << "<p>Congratulations! All source files are touched.</p>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Source
|
2016-02-20 06:55:08 +08:00
|
|
|
for (auto FileName : Files) {
|
2016-03-18 08:12:14 +08:00
|
|
|
std::pair<size_t, size_t> FC = FileCoverage[FileName];
|
2016-02-19 08:26:20 +08:00
|
|
|
if (FC.first == 0)
|
|
|
|
continue;
|
2016-03-12 07:28:28 +08:00
|
|
|
OS << "<a name=\"" << anchorName(FileName) << "\"></a>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
OS << "<h2>" << stripPathPrefix(FileName) << "</h2>\n";
|
2016-03-18 08:12:14 +08:00
|
|
|
OS << "<details open><summary>Function Coverage</summary>";
|
|
|
|
OS << "<div class='fnlist'>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
|
2016-03-18 08:12:14 +08:00
|
|
|
auto &FileFnCoverage = FnCoverageByFile[FileName];
|
|
|
|
|
|
|
|
for (const auto &P : FileFnCoverage) {
|
|
|
|
std::string FunctionName = P.first.FunctionName;
|
|
|
|
|
|
|
|
OS << "<div class='fn' style='order: " << P.second << "'>";
|
2016-03-25 04:41:18 +08:00
|
|
|
OS << "<span class='pct'>" << formatHtmlPct(P.second)
|
|
|
|
<< "%</span> ";
|
2016-03-18 08:12:14 +08:00
|
|
|
OS << "<span class='name'><a href=\"#"
|
|
|
|
<< anchorName(FileName + "::" + FunctionName) << "\">";
|
|
|
|
OS << escapeHtml(FunctionName) << "</a></span>";
|
|
|
|
OS << "</div>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
}
|
2016-03-18 08:12:14 +08:00
|
|
|
OS << "</div></details>\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
|
|
|
|
MemoryBuffer::getFile(FileName);
|
|
|
|
if (!BufOrErr) {
|
|
|
|
OS << "Error reading file: " << FileName << " : "
|
|
|
|
<< BufOrErr.getError().message() << "("
|
|
|
|
<< BufOrErr.getError().value() << ")\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "<pre>\n";
|
|
|
|
const auto &LineStatuses = LineStatusMap[FileName];
|
|
|
|
for (line_iterator I = line_iterator(*BufOrErr.get(), false);
|
|
|
|
!I.is_at_eof(); ++I) {
|
|
|
|
uint32_t Line = I.line_number();
|
|
|
|
{ // generate anchors (if any);
|
|
|
|
FileLoc Loc = FileLoc{FileName, Line};
|
2016-03-18 08:12:14 +08:00
|
|
|
auto It = AllFnsByLoc.find(Loc);
|
|
|
|
if (It != AllFnsByLoc.end()) {
|
2016-02-19 08:26:20 +08:00
|
|
|
for (std::string Fn : It->second) {
|
2016-03-12 07:28:28 +08:00
|
|
|
OS << "<a name=\"" << anchorName(FileName + "::" + Fn)
|
2016-02-19 08:26:20 +08:00
|
|
|
<< "\"></a>";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << "<span ";
|
|
|
|
auto LIT = LineStatuses.find(I.line_number());
|
|
|
|
auto Status = (LIT != LineStatuses.end()) ? LIT->second
|
|
|
|
: SourceCoverageData::UNKNOWN;
|
|
|
|
switch (Status) {
|
|
|
|
case SourceCoverageData::UNKNOWN:
|
|
|
|
OS << "class=unknown";
|
|
|
|
break;
|
|
|
|
case SourceCoverageData::COVERED:
|
|
|
|
OS << "class=covered";
|
|
|
|
break;
|
|
|
|
case SourceCoverageData::NOT_COVERED:
|
|
|
|
OS << "class=notcovered";
|
|
|
|
break;
|
|
|
|
case SourceCoverageData::MIXED:
|
|
|
|
OS << "class=mixed";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
OS << ">";
|
|
|
|
OS << escapeHtml(*I) << "</span>\n";
|
|
|
|
}
|
|
|
|
OS << "</pre>\n";
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:00:08 +08:00
|
|
|
void collectStats(CoverageStats *Stats) const {
|
|
|
|
Stats->CovPoints += Addrs->size();
|
|
|
|
|
|
|
|
SourceCoverageData SCovData(ObjectFile, *Addrs);
|
|
|
|
SCovData.collectStats(Stats);
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
private:
|
|
|
|
CoverageDataWithObjectFile(std::string ObjectFile,
|
|
|
|
std::unique_ptr<CoverageData> Coverage)
|
2016-02-19 08:26:20 +08:00
|
|
|
: CoverageData(std::move(Coverage->Addrs)),
|
|
|
|
ObjectFile(std::move(ObjectFile)) {}
|
2016-02-12 08:29:07 +08:00
|
|
|
const std::string ObjectFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Multiple coverage files data organized by object file.
|
|
|
|
class CoverageDataSet {
|
|
|
|
public:
|
|
|
|
static ErrorOr<std::unique_ptr<CoverageDataSet>>
|
|
|
|
readCmdArguments(std::vector<std::string> FileNames) {
|
|
|
|
// Short name => file name.
|
|
|
|
std::map<std::string, std::string> ObjFiles;
|
|
|
|
std::string FirstObjFile;
|
|
|
|
std::set<std::string> CovFiles;
|
|
|
|
|
|
|
|
// Partition input values into coverage/object files.
|
|
|
|
for (const auto &FileName : FileNames) {
|
|
|
|
auto ErrorOrIsCoverage = isCoverageFile(FileName);
|
2016-03-12 07:26:41 +08:00
|
|
|
if (!ErrorOrIsCoverage)
|
|
|
|
continue;
|
2016-02-12 08:29:07 +08:00
|
|
|
if (ErrorOrIsCoverage.get()) {
|
|
|
|
CovFiles.insert(FileName);
|
|
|
|
} else {
|
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
|
|
|
if (ObjFiles.find(ShortFileName) != ObjFiles.end()) {
|
|
|
|
Fail("Duplicate binary file with a short name: " + ShortFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjFiles[ShortFileName] = FileName;
|
|
|
|
if (FirstObjFile.empty())
|
|
|
|
FirstObjFile = FileName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Regex SancovRegex("(.*)\\.[0-9]+\\.sancov");
|
|
|
|
SmallVector<StringRef, 2> Components;
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// Object file => list of corresponding coverage file names.
|
|
|
|
auto CoverageByObjFile = group_by(CovFiles, [&](std::string FileName) {
|
2016-02-12 08:29:07 +08:00
|
|
|
auto ShortFileName = llvm::sys::path::filename(FileName);
|
|
|
|
auto Ok = SancovRegex.match(ShortFileName, &Components);
|
|
|
|
if (!Ok) {
|
|
|
|
Fail("Can't match coverage file name against "
|
|
|
|
"<module_name>.<pid>.sancov pattern: " +
|
|
|
|
FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Iter = ObjFiles.find(Components[1]);
|
|
|
|
if (Iter == ObjFiles.end()) {
|
|
|
|
Fail("Object file for coverage not found: " + FileName);
|
|
|
|
}
|
2016-02-19 08:26:20 +08:00
|
|
|
return Iter->second;
|
|
|
|
});
|
2016-02-12 08:29:07 +08:00
|
|
|
|
|
|
|
// Read coverage.
|
|
|
|
std::vector<std::unique_ptr<CoverageDataWithObjectFile>> MergedCoverage;
|
|
|
|
for (const auto &Pair : CoverageByObjFile) {
|
2016-02-19 08:26:20 +08:00
|
|
|
if (findSanitizerCovFunctions(Pair.first).empty()) {
|
|
|
|
for (auto FileName : Pair.second) {
|
|
|
|
CovFiles.erase(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
errs()
|
|
|
|
<< "Ignoring " << Pair.first
|
|
|
|
<< " and its coverage because __sanitizer_cov* functions were not "
|
|
|
|
"found.\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
auto DataOrError =
|
|
|
|
CoverageDataWithObjectFile::readAndMerge(Pair.first, Pair.second);
|
|
|
|
FailIfError(DataOrError);
|
|
|
|
MergedCoverage.push_back(std::move(DataOrError.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_ptr<CoverageDataSet>(
|
|
|
|
new CoverageDataSet(FirstObjFile, &MergedCoverage, CovFiles));
|
|
|
|
}
|
|
|
|
|
|
|
|
void printCoveredFunctions(raw_ostream &OS) const {
|
|
|
|
for (const auto &Cov : Coverage) {
|
|
|
|
Cov->printCoveredFunctions(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printNotCoveredFunctions(raw_ostream &OS) const {
|
|
|
|
for (const auto &Cov : Coverage) {
|
|
|
|
Cov->printNotCoveredFunctions(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 08:00:08 +08:00
|
|
|
void printStats(raw_ostream &OS) const {
|
|
|
|
CoverageStats Stats;
|
|
|
|
for (const auto &Cov : Coverage) {
|
|
|
|
Cov->collectStats(&Stats);
|
|
|
|
}
|
|
|
|
OS << Stats;
|
|
|
|
}
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
void printReport(raw_ostream &OS) const {
|
2016-03-19 06:46:10 +08:00
|
|
|
auto Title =
|
|
|
|
(llvm::sys::path::filename(MainObjFile) + " Coverage Report").str();
|
2016-02-12 08:29:07 +08:00
|
|
|
|
|
|
|
OS << "<html>\n";
|
|
|
|
OS << "<head>\n";
|
|
|
|
|
|
|
|
// Stylesheet
|
|
|
|
OS << "<style>\n";
|
|
|
|
OS << ".covered { background: #7F7; }\n";
|
|
|
|
OS << ".notcovered { background: #F77; }\n";
|
2016-02-20 06:55:08 +08:00
|
|
|
OS << ".mixed { background: #FF7; }\n";
|
2016-02-19 08:26:20 +08:00
|
|
|
OS << "summary { font-weight: bold; }\n";
|
|
|
|
OS << "details > summary + * { margin-left: 1em; }\n";
|
2016-03-18 08:12:14 +08:00
|
|
|
OS << ".fnlist { display: flex; flex-flow: column nowrap; }\n";
|
|
|
|
OS << ".fn { display: flex; flex-flow: row nowrap; }\n";
|
|
|
|
OS << ".pct { width: 3em; text-align: right; margin-right: 1em; }\n";
|
|
|
|
OS << ".name { flex: 2; }\n";
|
2016-03-25 04:41:18 +08:00
|
|
|
OS << ".lz { color: lightgray; }\n";
|
2016-02-12 08:29:07 +08:00
|
|
|
OS << "</style>\n";
|
|
|
|
OS << "<title>" << Title << "</title>\n";
|
|
|
|
OS << "</head>\n";
|
|
|
|
OS << "<body>\n";
|
|
|
|
|
|
|
|
// Title
|
|
|
|
OS << "<h1>" << Title << "</h1>\n";
|
|
|
|
|
|
|
|
// Modules TOC.
|
|
|
|
if (Coverage.size() > 1) {
|
|
|
|
for (const auto &CovData : Coverage) {
|
2016-03-12 07:28:28 +08:00
|
|
|
OS << "<li><a href=\"#module_" << anchorName(CovData->object_file())
|
2016-02-12 08:29:07 +08:00
|
|
|
<< "\">" << llvm::sys::path::filename(CovData->object_file())
|
|
|
|
<< "</a></li>\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &CovData : Coverage) {
|
|
|
|
if (Coverage.size() > 1) {
|
|
|
|
OS << "<h2>" << llvm::sys::path::filename(CovData->object_file())
|
|
|
|
<< "</h2>\n";
|
|
|
|
}
|
2016-03-12 07:28:28 +08:00
|
|
|
OS << "<a name=\"module_" << anchorName(CovData->object_file())
|
2016-02-12 08:29:07 +08:00
|
|
|
<< "\"></a>\n";
|
|
|
|
CovData->printReport(OS);
|
|
|
|
}
|
|
|
|
|
2016-02-19 08:26:20 +08:00
|
|
|
// About
|
|
|
|
OS << "<details><summary>About</summary>\n";
|
|
|
|
OS << "Coverage files:<ul>";
|
|
|
|
for (auto InputFile : CoverageFiles) {
|
|
|
|
llvm::sys::fs::file_status Status;
|
|
|
|
llvm::sys::fs::status(InputFile, Status);
|
|
|
|
OS << "<li>" << stripPathPrefix(InputFile) << " ("
|
|
|
|
<< Status.getLastModificationTime().str() << ")</li>\n";
|
|
|
|
}
|
|
|
|
OS << "</ul></details>\n";
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
OS << "</body>\n";
|
|
|
|
OS << "</html>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const { return Coverage.empty(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit CoverageDataSet(
|
|
|
|
const std::string &MainObjFile,
|
|
|
|
std::vector<std::unique_ptr<CoverageDataWithObjectFile>> *Data,
|
|
|
|
const std::set<std::string> &CoverageFiles)
|
|
|
|
: MainObjFile(MainObjFile), CoverageFiles(CoverageFiles) {
|
|
|
|
Data->swap(this->Coverage);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string MainObjFile;
|
|
|
|
std::vector<std::unique_ptr<CoverageDataWithObjectFile>> Coverage;
|
|
|
|
const std::set<std::string> CoverageFiles;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:19:49 +08:00
|
|
|
} // end anonymous namespace
|
2015-11-12 04:58:20 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Print stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
2015-12-05 02:35:37 +08:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllDisassemblers();
|
|
|
|
|
2015-11-12 04:58:20 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "Sanitizer Coverage Processing Tool");
|
|
|
|
|
2016-02-12 08:29:07 +08:00
|
|
|
// -print doesn't need object files.
|
|
|
|
if (Action == PrintAction) {
|
|
|
|
auto CovData = CoverageData::readAndMerge(ClInputFiles);
|
|
|
|
FailIfError(CovData);
|
2015-11-12 04:58:20 +08:00
|
|
|
CovData.get()->printAddrs(outs());
|
|
|
|
return 0;
|
2016-02-27 10:21:44 +08:00
|
|
|
} else if (Action == PrintCovPointsAction) {
|
|
|
|
// -print-coverage-points doesn't need coverage files.
|
|
|
|
for (std::string ObjFile : ClInputFiles) {
|
|
|
|
printCovPoints(ObjFile, outs());
|
|
|
|
}
|
|
|
|
return 0;
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
|
|
|
|
auto CovDataSet = CoverageDataSet::readCmdArguments(ClInputFiles);
|
|
|
|
FailIfError(CovDataSet);
|
|
|
|
|
|
|
|
if (CovDataSet.get()->empty()) {
|
|
|
|
Fail("No coverage files specified.");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Action) {
|
2015-11-12 04:58:20 +08:00
|
|
|
case CoveredFunctionsAction: {
|
2016-02-12 08:29:07 +08:00
|
|
|
CovDataSet.get()->printCoveredFunctions(outs());
|
2015-11-12 04:58:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-05 02:35:37 +08:00
|
|
|
case NotCoveredFunctionsAction: {
|
2016-02-12 08:29:07 +08:00
|
|
|
CovDataSet.get()->printNotCoveredFunctions(outs());
|
2015-12-05 02:35:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-15 06:34:11 +08:00
|
|
|
case HtmlReportAction: {
|
2016-02-12 08:29:07 +08:00
|
|
|
CovDataSet.get()->printReport(outs());
|
2016-01-15 06:34:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-24 08:00:08 +08:00
|
|
|
case StatsAction: {
|
|
|
|
CovDataSet.get()->printStats(outs());
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-12 08:29:07 +08:00
|
|
|
case PrintAction:
|
2016-02-27 10:21:44 +08:00
|
|
|
case PrintCovPointsAction:
|
2016-02-12 08:29:07 +08:00
|
|
|
llvm_unreachable("unsupported action");
|
2015-11-12 04:58:20 +08:00
|
|
|
}
|
|
|
|
}
|