<div dir="ltr"><div>Sorry for joining that late. I'm pretty sure compiler-rt is the wrong place for this tool, and we should move it under llvm/tools/ instead (together with llvm-symbolizer, llvm-cxxdump etc.):</div><div><br></div><div>* we don't include any LLVM headers in compiler-rt libraries (there will be some discussion to allow that for profile library, but there are different reasons).</div><div>actually, we even support standalone build of compiler-rt repository, and there is no guarantee that LLVM source code (or CMake rules definitions) will be available.</div><div>* sancov is a parser/transformer of specific binary format, not a compiler runtime (or resource file that should accompany it).</div><div>* sancov uses LLVM libs, so we want as much LLVM developers as possible to be able to build/test it. Currently it will never be compiled if</div><div>compiler-rt is missing, or sanitizers are not supported on host.</div><div>* LLVM test-suite is more suitable for writing lit tests for sancov: it should be tested on a host architecture like every other tool.</div><div><br></div><div>True, we'll need to update sancov every time we change the sancov output format, but that's fine: I believe that would happen less frequently than notoriously unstable LLVM API will be updated, requiring a mirroring commit to sancov implementation.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 10, 2015 at 5:26 PM, Mike Aizatsky via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: aizatsky<br>
Date: Tue Nov 10 19:26:57 2015<br>
New Revision: 252683<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=252683&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=252683&view=rev</a><br>
Log:<br>
Sancov in C++.<br>
<br>
Summary:<br>
First batch of sancov.py rewrite in C++.<br>
Supports "-print" and "-covered_functions" commands.<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D14356" rel="noreferrer" target="_blank">http://reviews.llvm.org/D14356</a><br>
<br>
Added:<br>
compiler-rt/trunk/lib/sanitizer_common/sancov.cc<br>
Modified:<br>
compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt?rev=252683&r1=252682&r2=252683&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt?rev=252683&r1=252682&r2=252683&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/CMakeLists.txt Tue Nov 10 19:26:57 2015<br>
@@ -156,3 +156,6 @@ add_compiler_rt_object_libraries(RTSanit<br>
if(COMPILER_RT_INCLUDE_TESTS)<br>
add_subdirectory(tests)<br>
endif()<br>
+<br>
+add_llvm_tool(sancov sancov.cc)<br>
+target_link_libraries(sancov LLVMSupport LLVMSymbolize LLVMObject LLVMDebugInfoDWARF LLVMDebugInfoPDB)<br>
<br>
Added: compiler-rt/trunk/lib/sanitizer_common/sancov.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sancov.cc?rev=252683&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sancov.cc?rev=252683&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sancov.cc (added)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sancov.cc Tue Nov 10 19:26:57 2015<br>
@@ -0,0 +1,283 @@<br>
+//===-- sancov.cc --------------------------------------------===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file is a command-line tool for reading and analyzing sanitizer<br>
+// coverage.<br>
+//===----------------------------------------------------------------------===//<br>
+#include "llvm/ADT/STLExtras.h"<br>
+#include "llvm/DebugInfo/Symbolize/Symbolize.h"<br>
+#include "llvm/Support/CommandLine.h"<br>
+#include "llvm/Support/Errc.h"<br>
+#include "llvm/Support/ErrorOr.h"<br>
+#include "llvm/Support/FileSystem.h"<br>
+#include "llvm/Support/LineIterator.h"<br>
+#include "llvm/Support/ManagedStatic.h"<br>
+#include "llvm/Support/MemoryBuffer.h"<br>
+#include "llvm/Support/Path.h"<br>
+#include "llvm/Support/PrettyStackTrace.h"<br>
+#include "llvm/Support/Signals.h"<br>
+#include "llvm/Support/ToolOutputFile.h"<br>
+#include "llvm/Support/raw_ostream.h"<br>
+<br>
+#include <cxxabi.h><br>
+#include <set><br>
+#include <stdio.h><br>
+#include <vector><br>
+<br>
+using namespace llvm;<br>
+<br>
+namespace {<br>
+<br>
+// --------- COMMAND LINE FLAGS ---------<br>
+<br>
+enum ActionType { PrintAction, CoveredFunctionsAction };<br>
+<br>
+cl::opt<ActionType> Action(<br>
+ cl::desc("Action (required)"), cl::Required,<br>
+ cl::values(clEnumValN(PrintAction, "print", "Print coverage addresses"),<br>
+ clEnumValN(CoveredFunctionsAction, "covered_functions",<br>
+ "Print all covered funcions."),<br>
+ clEnumValEnd));<br>
+<br>
+static cl::list<std::string> ClInputFiles(cl::Positional, cl::OneOrMore,<br>
+ cl::desc("<filenames...>"));<br>
+<br>
+static cl::opt<std::string><br>
+ ClBinaryName("obj", cl::Required,<br>
+ cl::desc("Path to object file to be symbolized"));<br>
+<br>
+static cl::opt<bool><br>
+ ClDemangle("demangle", cl::init(true),<br>
+ cl::desc("Print demangled function name."));<br>
+<br>
+// --------- FORMAT SPECIFICATION ---------<br>
+<br>
+struct FileHeader {<br>
+ uint32_t Bitness;<br>
+ uint32_t Magic;<br>
+};<br>
+<br>
+static const uint32_t BinCoverageMagic = 0xC0BFFFFF;<br>
+static const uint32_t Bitness32 = 0xFFFFFF32;<br>
+static const uint32_t Bitness64 = 0xFFFFFF64;<br>
+<br>
+// ---------<br>
+<br>
+template <typename T> static void FailIfError(const ErrorOr<T> &E) {<br>
+ if (E)<br>
+ return;<br>
+<br>
+ auto Error = E.getError();<br>
+ errs() << "Error: " << Error.message() << "(" << Error.value() << ")\n";<br>
+ exit(-2);<br>
+}<br>
+<br>
+template <typename T><br>
+static void readInts(const char *Start, const char *End,<br>
+ std::vector<uint64_t> *V) {<br>
+ const T *S = reinterpret_cast<const T *>(Start);<br>
+ const T *E = reinterpret_cast<const T *>(End);<br>
+ V->reserve(E - S);<br>
+ std::copy(S, E, std::back_inserter(*V));<br>
+}<br>
+<br>
+static std::string CommonPrefix(std::string A, std::string B) {<br>
+ if (A.size() > B.size())<br>
+ return std::string(B.begin(),<br>
+ std::mismatch(B.begin(), B.end(), A.begin()).first);<br>
+ else<br>
+ return std::string(A.begin(),<br>
+ std::mismatch(A.begin(), A.end(), B.begin()).first);<br>
+}<br>
+<br>
+static std::string demangle(std::string Name) {<br>
+ if (Name.substr(0, 2) != "_Z")<br>
+ return Name;<br>
+<br>
+ int status = 0;<br>
+ char *DemangledName =<br>
+ abi::__cxa_demangle(Name.c_str(), nullptr, nullptr, &status);<br>
+ if (status != 0)<br>
+ return Name;<br>
+ std::string Result = DemangledName;<br>
+ free(DemangledName);<br>
+ return Result;<br>
+}<br>
+<br>
+class CoverageData {<br>
+ public:<br>
+ // Read single file coverage data.<br>
+ static ErrorOr<std::unique_ptr<CoverageData>> read(std::string FileName) {<br>
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =<br>
+ MemoryBuffer::getFile(FileName);<br>
+ if (!BufOrErr)<br>
+ return BufOrErr.getError();<br>
+ std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());<br>
+ if (Buf->getBufferSize() < 8) {<br>
+ errs() << "File too small (<8): " << Buf->getBufferSize();<br>
+ return make_error_code(errc::illegal_byte_sequence);<br>
+ }<br>
+ const FileHeader *Header =<br>
+ reinterpret_cast<const FileHeader *>(Buf->getBufferStart());<br>
+<br>
+ if (Header->Magic != BinCoverageMagic) {<br>
+ errs() << "Wrong magic: " << Header->Magic;<br>
+ return make_error_code(errc::illegal_byte_sequence);<br>
+ }<br>
+<br>
+ std::unique_ptr<std::vector<uint64_t>> Addrs(new std::vector<uint64_t>());<br>
+<br>
+ switch (Header->Bitness) {<br>
+ case Bitness64:<br>
+ readInts<uint64_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),<br>
+ Addrs.get());<br>
+ break;<br>
+ case Bitness32:<br>
+ readInts<uint32_t>(Buf->getBufferStart() + 8, Buf->getBufferEnd(),<br>
+ Addrs.get());<br>
+ break;<br>
+ default:<br>
+ errs() << "Unsupported bitness: " << Header->Bitness;<br>
+ return make_error_code(errc::illegal_byte_sequence);<br>
+ }<br>
+<br>
+ return std::unique_ptr<CoverageData>(new CoverageData(std::move(Addrs)));<br>
+ }<br>
+<br>
+ // Merge multiple coverage data together.<br>
+ static std::unique_ptr<CoverageData><br>
+ merge(const std::vector<std::unique_ptr<CoverageData>> &Covs) {<br>
+ std::set<uint64_t> Addrs;<br>
+<br>
+ for (const auto &Cov : Covs)<br>
+ Addrs.insert(Cov->Addrs->begin(), Cov->Addrs->end());<br>
+<br>
+ std::unique_ptr<std::vector<uint64_t>> AddrsVector(<br>
+ new std::vector<uint64_t>(Addrs.begin(), Addrs.end()));<br>
+ return std::unique_ptr<CoverageData>(<br>
+ new CoverageData(std::move(AddrsVector)));<br>
+ }<br>
+<br>
+ // Read list of files and merges their coverage info.<br>
+ static ErrorOr<std::unique_ptr<CoverageData>><br>
+ readAndMerge(const std::vector<std::string> &FileNames) {<br>
+ std::vector<std::unique_ptr<CoverageData>> Covs;<br>
+ for (const auto &FileName : FileNames) {<br>
+ auto Cov = read(FileName);<br>
+ if (!Cov)<br>
+ return Cov.getError();<br>
+ Covs.push_back(std::move(Cov.get()));<br>
+ }<br>
+ return merge(Covs);<br>
+ }<br>
+<br>
+ // Print coverage addresses.<br>
+ void printAddrs(raw_ostream &out) { // NOLINT(runtime/references)<br>
+ for (auto Addr : *Addrs) {<br>
+ out << "0x";<br>
+ out.write_hex(Addr);<br>
+ out << "\n";<br>
+ }<br>
+ }<br>
+<br>
+ // Print list of covered functions.<br>
+ // Line format: <file_name>:<line> <function_name><br>
+ void printCoveredFunctions(raw_ostream &out) { // NOLINT(runtime/references)<br>
+ if (Addrs->empty())<br>
+ return;<br>
+ symbolize::LLVMSymbolizer Symbolizer;<br>
+<br>
+ struct FileLoc {<br>
+ std::string FileName;<br>
+ uint32_t Line;<br>
+ bool operator<(const FileLoc &Rhs) const {<br>
+ return std::tie(FileName, Line) < std::tie(Rhs.FileName, Rhs.Line);<br>
+ }<br>
+ };<br>
+<br>
+ // FileLoc -> FunctionName<br>
+ std::map<FileLoc, std::string> Fns;<br>
+<br>
+ // Fill in Fns map.<br>
+ for (auto Addr : *Addrs) {<br>
+ auto InliningInfo = Symbolizer.symbolizeInlinedCode(ClBinaryName, Addr);<br>
+ FailIfError(InliningInfo);<br>
+ for (uint32_t i = 0; i < InliningInfo->getNumberOfFrames(); ++i) {<br>
+ auto FrameInfo = InliningInfo->getFrame(i);<br>
+ SmallString<256> FileName(FrameInfo.FileName);<br>
+ sys::path::remove_dots(FileName, /* remove_dot_dot */ true);<br>
+ uint32_t Line = FrameInfo.Line;<br>
+ std::string FunctionName = FrameInfo.FunctionName;<br>
+ if (ClDemangle)<br>
+ FunctionName = demangle(FrameInfo.FunctionName);<br>
+<br>
+ FileLoc Loc = { FileName.str(), Line };<br>
+ Fns[Loc] = FunctionName;<br>
+ }<br>
+ }<br>
+<br>
+ // Compute file names common prefix.<br>
+ std::string FilePrefix = Fns.begin()->first.FileName;<br>
+ for (const auto &P : Fns)<br>
+ FilePrefix = CommonPrefix(FilePrefix, P.first.FileName);<br>
+<br>
+ // Print first function occurence in a file.<br>
+ {<br>
+ std::string LastFileName;<br>
+ std::set<std::string> ProcessedFunctions;<br>
+<br>
+ for (const auto &P : Fns) {<br>
+ std::string FileName = P.first.FileName;<br>
+ std::string FunctionName = P.second;<br>
+ uint32_t Line = P.first.Line;<br>
+<br>
+ if (LastFileName != FileName)<br>
+ ProcessedFunctions.clear();<br>
+ LastFileName = FileName;<br>
+<br>
+ if (ProcessedFunctions.find(FunctionName) != ProcessedFunctions.end())<br>
+ continue;<br>
+ ProcessedFunctions.insert(FunctionName);<br>
+<br>
+ out << FileName.substr(FilePrefix.size()) << ":" << Line << " "<br>
+ << FunctionName << "\n";<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
+ private:<br>
+ explicit CoverageData(std::unique_ptr<std::vector<uint64_t>> Addrs)<br>
+ : Addrs(std::move(Addrs)) {}<br>
+<br>
+ std::unique_ptr<std::vector<uint64_t>> Addrs;<br>
+};<br>
+} // namespace<br>
+<br>
+int main(int argc, char **argv) {<br>
+ // Print stack trace if we signal out.<br>
+ sys::PrintStackTraceOnErrorSignal();<br>
+ PrettyStackTraceProgram X(argc, argv);<br>
+ llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.<br>
+<br>
+ cl::ParseCommandLineOptions(argc, argv, "Sanitizer Coverage Processing Tool");<br>
+<br>
+ auto CovData = CoverageData::readAndMerge(ClInputFiles);<br>
+ FailIfError(CovData);<br>
+<br>
+ switch (Action) {<br>
+ case PrintAction: {<br>
+ CovData.get()->printAddrs(outs());<br>
+ return 0;<br>
+ }<br>
+ case CoveredFunctionsAction: {<br>
+ CovData.get()->printCoveredFunctions(outs());<br>
+ return 0;<br>
+ }<br>
+ }<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div>