From e82d89cc37ceb69bcb35b911e2ca2119aa51a5e5 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Fri, 22 Aug 2014 22:56:03 +0000 Subject: llvm-cov: add code coverage tool that's based on coverage mapping format and clang's pgo. This commit expands llvm-cov's functionality by adding support for a new code coverage tool that uses LLVM's coverage mapping format and clang's instrumentation based profiling. The gcov compatible tool can be invoked by supplying the 'gcov' command as the first argument, or by modifying the tool's name to end with 'gcov'. Differential Revision: http://reviews.llvm.org/D4445 llvm-svn: 216300 --- llvm/lib/ProfileData/CoverageMappingReader.cpp | 97 +++++++++++++++++++------- 1 file changed, 71 insertions(+), 26 deletions(-) (limited to 'llvm/lib/ProfileData/CoverageMappingReader.cpp') diff --git a/llvm/lib/ProfileData/CoverageMappingReader.cpp b/llvm/lib/ProfileData/CoverageMappingReader.cpp index f10ae05..8d1508b 100644 --- a/llvm/lib/ProfileData/CoverageMappingReader.cpp +++ b/llvm/lib/ProfileData/CoverageMappingReader.cpp @@ -289,18 +289,6 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader( Object = std::move(File.get()); } -ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader( - std::unique_ptr &ObjectBuffer, sys::fs::file_magic Type) - : CurrentRecord(0) { - auto File = object::ObjectFile::createObjectFile( - ObjectBuffer->getMemBufferRef(), Type); - if (!File) - error(File.getError()); - else - Object = OwningBinary(std::move(File.get()), - std::move(ObjectBuffer)); -} - namespace { /// \brief The coverage mapping data for a single function. /// It points to the function's name. @@ -347,19 +335,11 @@ struct SectionData { template std::error_code readCoverageMappingData( - SectionRef &ProfileNames, SectionRef &CoverageMapping, + SectionData &ProfileNames, StringRef Data, std::vector &Records, std::vector &Filenames) { llvm::DenseSet UniqueFunctionMappingData; - // Get the contents of the given sections. - StringRef Data; - if (auto Err = CoverageMapping.getContents(Data)) - return Err; - SectionData ProfileNamesData; - if (auto Err = ProfileNamesData.load(ProfileNames)) - return Err; - // Read the records in the coverage data section. while (!Data.empty()) { if (Data.size() < sizeof(CoverageMappingTURecord)) @@ -418,9 +398,9 @@ std::error_code readCoverageMappingData( continue; UniqueFunctionMappingData.insert(MappingRecord.FunctionNamePtr); StringRef FunctionName; - if (auto Err = ProfileNamesData.get(MappingRecord.FunctionNamePtr, - MappingRecord.FunctionNameSize, - FunctionName)) + if (auto Err = + ProfileNames.get(MappingRecord.FunctionNamePtr, + MappingRecord.FunctionNameSize, FunctionName)) return Err; Records.push_back(ObjectFileCoverageMappingReader::ProfileMappingRecord( Version, FunctionName, MappingRecord.FunctionHash, Mapping, @@ -431,6 +411,63 @@ std::error_code readCoverageMappingData( return instrprof_error::success; } +static const char *TestingFormatMagic = "llvmcovmtestdata"; + +static std::error_code decodeTestingFormat(StringRef Data, + SectionData &ProfileNames, + StringRef &CoverageMapping) { + Data = Data.substr(StringRef(TestingFormatMagic).size()); + if (Data.size() < 1) + return instrprof_error::truncated; + unsigned N = 0; + auto ProfileNamesSize = + decodeULEB128(reinterpret_cast(Data.data()), &N); + if (N > Data.size()) + return instrprof_error::malformed; + Data = Data.substr(N); + if (Data.size() < 1) + return instrprof_error::truncated; + N = 0; + ProfileNames.Address = + decodeULEB128(reinterpret_cast(Data.data()), &N); + if (N > Data.size()) + return instrprof_error::malformed; + Data = Data.substr(N); + if (Data.size() < ProfileNamesSize) + return instrprof_error::malformed; + ProfileNames.Data = Data.substr(0, ProfileNamesSize); + CoverageMapping = Data.substr(ProfileNamesSize); + return instrprof_error::success; +} + +ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader( + std::unique_ptr &ObjectBuffer, sys::fs::file_magic Type) + : CurrentRecord(0) { + if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) { + // This is a special format used for testing. + SectionData ProfileNames; + StringRef CoverageMapping; + if (auto Err = decodeTestingFormat(ObjectBuffer->getBuffer(), ProfileNames, + CoverageMapping)) { + error(Err); + return; + } + error(readCoverageMappingData(ProfileNames, CoverageMapping, + MappingRecords, Filenames)); + Object = OwningBinary(std::unique_ptr(), + std::move(ObjectBuffer)); + return; + } + + auto File = object::ObjectFile::createObjectFile( + ObjectBuffer->getMemBufferRef(), Type); + if (!File) + error(File.getError()); + else + Object = OwningBinary(std::move(File.get()), + std::move(ObjectBuffer)); +} + std::error_code ObjectFileCoverageMappingReader::readHeader() { ObjectFile *OF = Object.getBinary().get(); if (!OF) @@ -457,13 +494,21 @@ std::error_code ObjectFileCoverageMappingReader::readHeader() { if (FoundSectionCount != 2) return error(instrprof_error::bad_header); + // Get the contents of the given sections. + StringRef Data; + if (auto Err = CoverageMapping.getContents(Data)) + return Err; + SectionData ProfileNamesData; + if (auto Err = ProfileNamesData.load(ProfileNames)) + return Err; + // Load the data from the found sections. std::error_code Err; if (BytesInAddress == 4) - Err = readCoverageMappingData(ProfileNames, CoverageMapping, + Err = readCoverageMappingData(ProfileNamesData, Data, MappingRecords, Filenames); else - Err = readCoverageMappingData(ProfileNames, CoverageMapping, + Err = readCoverageMappingData(ProfileNamesData, Data, MappingRecords, Filenames); if (Err) return error(Err); -- cgit v1.1