LLVM  mainline
GCOV.h
Go to the documentation of this file.
00001 //===- GCOV.h - LLVM coverage tool ----------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This header provides the interface to read and write coverage files that
00011 // use 'gcov' format.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_GCOV_H
00016 #define LLVM_SUPPORT_GCOV_H
00017 
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/MapVector.h"
00020 #include "llvm/ADT/SmallVector.h"
00021 #include "llvm/ADT/StringMap.h"
00022 #include "llvm/ADT/iterator.h"
00023 #include "llvm/Support/MemoryBuffer.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 
00026 namespace llvm {
00027 
00028 class GCOVFunction;
00029 class GCOVBlock;
00030 class FileInfo;
00031 
00032 namespace GCOV {
00033 enum GCOVVersion { V402, V404, V704 };
00034 
00035 /// \brief A struct for passing gcov options between functions.
00036 struct Options {
00037   Options(bool A, bool B, bool C, bool F, bool P, bool U, bool L, bool N)
00038       : AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
00039         PreservePaths(P), UncondBranch(U), LongFileNames(L), NoOutput(N) {}
00040 
00041   bool AllBlocks;
00042   bool BranchInfo;
00043   bool BranchCount;
00044   bool FuncCoverage;
00045   bool PreservePaths;
00046   bool UncondBranch;
00047   bool LongFileNames;
00048   bool NoOutput;
00049 };
00050 } // end GCOV namespace
00051 
00052 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
00053 /// read operations.
00054 class GCOVBuffer {
00055 public:
00056   GCOVBuffer(MemoryBuffer *B) : Buffer(B), Cursor(0) {}
00057 
00058   /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
00059   bool readGCNOFormat() {
00060     StringRef File = Buffer->getBuffer().slice(0, 4);
00061     if (File != "oncg") {
00062       errs() << "Unexpected file type: " << File << ".\n";
00063       return false;
00064     }
00065     Cursor = 4;
00066     return true;
00067   }
00068 
00069   /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
00070   bool readGCDAFormat() {
00071     StringRef File = Buffer->getBuffer().slice(0, 4);
00072     if (File != "adcg") {
00073       errs() << "Unexpected file type: " << File << ".\n";
00074       return false;
00075     }
00076     Cursor = 4;
00077     return true;
00078   }
00079 
00080   /// readGCOVVersion - Read GCOV version.
00081   bool readGCOVVersion(GCOV::GCOVVersion &Version) {
00082     StringRef VersionStr = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00083     if (VersionStr == "*204") {
00084       Cursor += 4;
00085       Version = GCOV::V402;
00086       return true;
00087     }
00088     if (VersionStr == "*404") {
00089       Cursor += 4;
00090       Version = GCOV::V404;
00091       return true;
00092     }
00093     if (VersionStr == "*704") {
00094       Cursor += 4;
00095       Version = GCOV::V704;
00096       return true;
00097     }
00098     errs() << "Unexpected version: " << VersionStr << ".\n";
00099     return false;
00100   }
00101 
00102   /// readFunctionTag - If cursor points to a function tag then increment the
00103   /// cursor and return true otherwise return false.
00104   bool readFunctionTag() {
00105     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00106     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
00107         Tag[3] != '\1') {
00108       return false;
00109     }
00110     Cursor += 4;
00111     return true;
00112   }
00113 
00114   /// readBlockTag - If cursor points to a block tag then increment the
00115   /// cursor and return true otherwise return false.
00116   bool readBlockTag() {
00117     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00118     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x41' ||
00119         Tag[3] != '\x01') {
00120       return false;
00121     }
00122     Cursor += 4;
00123     return true;
00124   }
00125 
00126   /// readEdgeTag - If cursor points to an edge tag then increment the
00127   /// cursor and return true otherwise return false.
00128   bool readEdgeTag() {
00129     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00130     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x43' ||
00131         Tag[3] != '\x01') {
00132       return false;
00133     }
00134     Cursor += 4;
00135     return true;
00136   }
00137 
00138   /// readLineTag - If cursor points to a line tag then increment the
00139   /// cursor and return true otherwise return false.
00140   bool readLineTag() {
00141     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00142     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\x45' ||
00143         Tag[3] != '\x01') {
00144       return false;
00145     }
00146     Cursor += 4;
00147     return true;
00148   }
00149 
00150   /// readArcTag - If cursor points to an gcda arc tag then increment the
00151   /// cursor and return true otherwise return false.
00152   bool readArcTag() {
00153     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00154     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\xa1' ||
00155         Tag[3] != '\1') {
00156       return false;
00157     }
00158     Cursor += 4;
00159     return true;
00160   }
00161 
00162   /// readObjectTag - If cursor points to an object summary tag then increment
00163   /// the cursor and return true otherwise return false.
00164   bool readObjectTag() {
00165     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00166     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
00167         Tag[3] != '\xa1') {
00168       return false;
00169     }
00170     Cursor += 4;
00171     return true;
00172   }
00173 
00174   /// readProgramTag - If cursor points to a program summary tag then increment
00175   /// the cursor and return true otherwise return false.
00176   bool readProgramTag() {
00177     StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00178     if (Tag.empty() || Tag[0] != '\0' || Tag[1] != '\0' || Tag[2] != '\0' ||
00179         Tag[3] != '\xa3') {
00180       return false;
00181     }
00182     Cursor += 4;
00183     return true;
00184   }
00185 
00186   bool readInt(uint32_t &Val) {
00187     if (Buffer->getBuffer().size() < Cursor + 4) {
00188       errs() << "Unexpected end of memory buffer: " << Cursor + 4 << ".\n";
00189       return false;
00190     }
00191     StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor + 4);
00192     Cursor += 4;
00193     Val = *(const uint32_t *)(Str.data());
00194     return true;
00195   }
00196 
00197   bool readInt64(uint64_t &Val) {
00198     uint32_t Lo, Hi;
00199     if (!readInt(Lo) || !readInt(Hi))
00200       return false;
00201     Val = ((uint64_t)Hi << 32) | Lo;
00202     return true;
00203   }
00204 
00205   bool readString(StringRef &Str) {
00206     uint32_t Len = 0;
00207     // Keep reading until we find a non-zero length. This emulates gcov's
00208     // behaviour, which appears to do the same.
00209     while (Len == 0)
00210       if (!readInt(Len))
00211         return false;
00212     Len *= 4;
00213     if (Buffer->getBuffer().size() < Cursor + Len) {
00214       errs() << "Unexpected end of memory buffer: " << Cursor + Len << ".\n";
00215       return false;
00216     }
00217     Str = Buffer->getBuffer().slice(Cursor, Cursor + Len).split('\0').first;
00218     Cursor += Len;
00219     return true;
00220   }
00221 
00222   uint64_t getCursor() const { return Cursor; }
00223   void advanceCursor(uint32_t n) { Cursor += n * 4; }
00224 
00225 private:
00226   MemoryBuffer *Buffer;
00227   uint64_t Cursor;
00228 };
00229 
00230 /// GCOVFile - Collects coverage information for one pair of coverage file
00231 /// (.gcno and .gcda).
00232 class GCOVFile {
00233 public:
00234   GCOVFile()
00235       : GCNOInitialized(false), Checksum(0), Functions(), RunCount(0),
00236         ProgramCount(0) {}
00237   bool readGCNO(GCOVBuffer &Buffer);
00238   bool readGCDA(GCOVBuffer &Buffer);
00239   uint32_t getChecksum() const { return Checksum; }
00240   void dump() const;
00241   void collectLineCounts(FileInfo &FI);
00242 
00243 private:
00244   bool GCNOInitialized;
00245   GCOV::GCOVVersion Version;
00246   uint32_t Checksum;
00247   SmallVector<std::unique_ptr<GCOVFunction>, 16> Functions;
00248   uint32_t RunCount;
00249   uint32_t ProgramCount;
00250 };
00251 
00252 /// GCOVEdge - Collects edge information.
00253 struct GCOVEdge {
00254   GCOVEdge(GCOVBlock &S, GCOVBlock &D) : Src(S), Dst(D), Count(0) {}
00255 
00256   GCOVBlock &Src;
00257   GCOVBlock &Dst;
00258   uint64_t Count;
00259 };
00260 
00261 /// GCOVFunction - Collects function information.
00262 class GCOVFunction {
00263 public:
00264   typedef pointee_iterator<SmallVectorImpl<
00265       std::unique_ptr<GCOVBlock>>::const_iterator> BlockIterator;
00266 
00267   GCOVFunction(GCOVFile &P) : Parent(P), Ident(0), LineNumber(0) {}
00268   bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
00269   bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version);
00270   StringRef getName() const { return Name; }
00271   StringRef getFilename() const { return Filename; }
00272   size_t getNumBlocks() const { return Blocks.size(); }
00273   uint64_t getEntryCount() const;
00274   uint64_t getExitCount() const;
00275 
00276   BlockIterator block_begin() const { return Blocks.begin(); }
00277   BlockIterator block_end() const { return Blocks.end(); }
00278   iterator_range<BlockIterator> blocks() const {
00279     return make_range(block_begin(), block_end());
00280   }
00281 
00282   void dump() const;
00283   void collectLineCounts(FileInfo &FI);
00284 
00285 private:
00286   GCOVFile &Parent;
00287   uint32_t Ident;
00288   uint32_t Checksum;
00289   uint32_t LineNumber;
00290   StringRef Name;
00291   StringRef Filename;
00292   SmallVector<std::unique_ptr<GCOVBlock>, 16> Blocks;
00293   SmallVector<std::unique_ptr<GCOVEdge>, 16> Edges;
00294 };
00295 
00296 /// GCOVBlock - Collects block information.
00297 class GCOVBlock {
00298   struct EdgeWeight {
00299     EdgeWeight(GCOVBlock *D) : Dst(D), Count(0) {}
00300 
00301     GCOVBlock *Dst;
00302     uint64_t Count;
00303   };
00304 
00305   struct SortDstEdgesFunctor {
00306     bool operator()(const GCOVEdge *E1, const GCOVEdge *E2) {
00307       return E1->Dst.Number < E2->Dst.Number;
00308     }
00309   };
00310 
00311 public:
00312   typedef SmallVectorImpl<GCOVEdge *>::const_iterator EdgeIterator;
00313 
00314   GCOVBlock(GCOVFunction &P, uint32_t N)
00315       : Parent(P), Number(N), Counter(0), DstEdgesAreSorted(true), SrcEdges(),
00316         DstEdges(), Lines() {}
00317   ~GCOVBlock();
00318   const GCOVFunction &getParent() const { return Parent; }
00319   void addLine(uint32_t N) { Lines.push_back(N); }
00320   uint32_t getLastLine() const { return Lines.back(); }
00321   void addCount(size_t DstEdgeNo, uint64_t N);
00322   uint64_t getCount() const { return Counter; }
00323 
00324   void addSrcEdge(GCOVEdge *Edge) {
00325     assert(&Edge->Dst == this); // up to caller to ensure edge is valid
00326     SrcEdges.push_back(Edge);
00327   }
00328   void addDstEdge(GCOVEdge *Edge) {
00329     assert(&Edge->Src == this); // up to caller to ensure edge is valid
00330     // Check if adding this edge causes list to become unsorted.
00331     if (DstEdges.size() && DstEdges.back()->Dst.Number > Edge->Dst.Number)
00332       DstEdgesAreSorted = false;
00333     DstEdges.push_back(Edge);
00334   }
00335   size_t getNumSrcEdges() const { return SrcEdges.size(); }
00336   size_t getNumDstEdges() const { return DstEdges.size(); }
00337   void sortDstEdges();
00338 
00339   EdgeIterator src_begin() const { return SrcEdges.begin(); }
00340   EdgeIterator src_end() const { return SrcEdges.end(); }
00341   iterator_range<EdgeIterator> srcs() const {
00342     return make_range(src_begin(), src_end());
00343   }
00344 
00345   EdgeIterator dst_begin() const { return DstEdges.begin(); }
00346   EdgeIterator dst_end() const { return DstEdges.end(); }
00347   iterator_range<EdgeIterator> dsts() const {
00348     return make_range(dst_begin(), dst_end());
00349   }
00350 
00351   void dump() const;
00352   void collectLineCounts(FileInfo &FI);
00353 
00354 private:
00355   GCOVFunction &Parent;
00356   uint32_t Number;
00357   uint64_t Counter;
00358   bool DstEdgesAreSorted;
00359   SmallVector<GCOVEdge *, 16> SrcEdges;
00360   SmallVector<GCOVEdge *, 16> DstEdges;
00361   SmallVector<uint32_t, 16> Lines;
00362 };
00363 
00364 class FileInfo {
00365   // It is unlikely--but possible--for multiple functions to be on the same
00366   // line.
00367   // Therefore this typedef allows LineData.Functions to store multiple
00368   // functions
00369   // per instance. This is rare, however, so optimize for the common case.
00370   typedef SmallVector<const GCOVFunction *, 1> FunctionVector;
00371   typedef DenseMap<uint32_t, FunctionVector> FunctionLines;
00372   typedef SmallVector<const GCOVBlock *, 4> BlockVector;
00373   typedef DenseMap<uint32_t, BlockVector> BlockLines;
00374 
00375   struct LineData {
00376     LineData() : LastLine(0) {}
00377     BlockLines Blocks;
00378     FunctionLines Functions;
00379     uint32_t LastLine;
00380   };
00381 
00382   struct GCOVCoverage {
00383     GCOVCoverage(StringRef Name)
00384         : Name(Name), LogicalLines(0), LinesExec(0), Branches(0),
00385           BranchesExec(0), BranchesTaken(0) {}
00386 
00387     StringRef Name;
00388 
00389     uint32_t LogicalLines;
00390     uint32_t LinesExec;
00391 
00392     uint32_t Branches;
00393     uint32_t BranchesExec;
00394     uint32_t BranchesTaken;
00395   };
00396 
00397 public:
00398   FileInfo(const GCOV::Options &Options)
00399       : Options(Options), LineInfo(), RunCount(0), ProgramCount(0) {}
00400 
00401   void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block) {
00402     if (Line > LineInfo[Filename].LastLine)
00403       LineInfo[Filename].LastLine = Line;
00404     LineInfo[Filename].Blocks[Line - 1].push_back(Block);
00405   }
00406   void addFunctionLine(StringRef Filename, uint32_t Line,
00407                        const GCOVFunction *Function) {
00408     if (Line > LineInfo[Filename].LastLine)
00409       LineInfo[Filename].LastLine = Line;
00410     LineInfo[Filename].Functions[Line - 1].push_back(Function);
00411   }
00412   void setRunCount(uint32_t Runs) { RunCount = Runs; }
00413   void setProgramCount(uint32_t Programs) { ProgramCount = Programs; }
00414   void print(raw_ostream &OS, StringRef MainFilename, StringRef GCNOFile,
00415              StringRef GCDAFile);
00416 
00417 private:
00418   std::string getCoveragePath(StringRef Filename, StringRef MainFilename);
00419   std::unique_ptr<raw_ostream> openCoveragePath(StringRef CoveragePath);
00420   void printFunctionSummary(raw_ostream &OS, const FunctionVector &Funcs) const;
00421   void printBlockInfo(raw_ostream &OS, const GCOVBlock &Block,
00422                       uint32_t LineIndex, uint32_t &BlockNo) const;
00423   void printBranchInfo(raw_ostream &OS, const GCOVBlock &Block,
00424                        GCOVCoverage &Coverage, uint32_t &EdgeNo);
00425   void printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo,
00426                              uint64_t Count) const;
00427 
00428   void printCoverage(raw_ostream &OS, const GCOVCoverage &Coverage) const;
00429   void printFuncCoverage(raw_ostream &OS) const;
00430   void printFileCoverage(raw_ostream &OS) const;
00431 
00432   const GCOV::Options &Options;
00433   StringMap<LineData> LineInfo;
00434   uint32_t RunCount;
00435   uint32_t ProgramCount;
00436 
00437   typedef SmallVector<std::pair<std::string, GCOVCoverage>, 4> FileCoverageList;
00438   typedef MapVector<const GCOVFunction *, GCOVCoverage> FuncCoverageMap;
00439 
00440   FileCoverageList FileCoverages;
00441   FuncCoverageMap FuncCoverages;
00442 };
00443 }
00444 
00445 #endif