LLVM API Documentation

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