1 //===- GCOVr.cpp - LLVM coverage tool -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // GCOV implements the interface to read and write coverage files that use
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/GCOV.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MemoryObject.h"
21 #include "llvm/Support/system_error.h"
24 //===----------------------------------------------------------------------===//
25 // GCOVFile implementation.
27 /// ~GCOVFile - Delete GCOVFile and its content.
28 GCOVFile::~GCOVFile() {
29 DeleteContainerPointers(Functions);
32 /// isGCDAFile - Return true if Format identifies a .gcda file.
33 static bool isGCDAFile(GCOV::GCOVFormat Format) {
34 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
37 /// isGCNOFile - Return true if Format identifies a .gcno file.
38 static bool isGCNOFile(GCOV::GCOVFormat Format) {
39 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
42 /// read - Read GCOV buffer.
43 bool GCOVFile::read(GCOVBuffer &Buffer) {
44 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
45 if (Format == GCOV::InvalidGCOV)
48 if (isGCNOFile(Format)) {
49 if (!Buffer.readInt(Checksum)) return false;
51 if (!Buffer.readFunctionTag()) break;
52 GCOVFunction *GFun = new GCOVFunction();
53 if (!GFun->readGCNO(Buffer, Format))
55 Functions.push_back(GFun);
57 } else if (isGCDAFile(Format)) {
59 if (!Buffer.readInt(Checksum2)) return false;
60 if (Checksum != Checksum2) {
61 errs() << "File checksum does not match.\n";
64 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
65 if (!Buffer.readFunctionTag()) {
66 errs() << "Unexpected number of functions.\n";
69 if (!Functions[i]->readGCDA(Buffer, Format))
72 if (Buffer.readObjectTag()) {
75 if (!Buffer.readInt(Length)) return false;
76 if (!Buffer.readInt(Dummy)) return false; // checksum
77 if (!Buffer.readInt(Dummy)) return false; // num
78 if (!Buffer.readInt(RunCount)) return false;;
79 Buffer.advanceCursor(Length-3);
81 while (Buffer.readProgramTag()) {
83 if (!Buffer.readInt(Length)) return false;
84 Buffer.advanceCursor(Length);
92 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
93 void GCOVFile::dump() const {
94 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
95 E = Functions.end(); I != E; ++I)
99 /// collectLineCounts - Collect line counts. This must be used after
100 /// reading .gcno and .gcda files.
101 void GCOVFile::collectLineCounts(FileInfo &FI) {
102 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
103 E = Functions.end(); I != E; ++I)
104 (*I)->collectLineCounts(FI);
105 FI.setRunCount(RunCount);
106 FI.setProgramCount(ProgramCount);
109 //===----------------------------------------------------------------------===//
110 // GCOVFunction implementation.
112 /// ~GCOVFunction - Delete GCOVFunction and its content.
113 GCOVFunction::~GCOVFunction() {
114 DeleteContainerPointers(Blocks);
115 DeleteContainerPointers(Edges);
118 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
120 bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
122 if (!Buff.readInt(Dummy)) return false; // Function header length
123 if (!Buff.readInt(Ident)) return false;
124 if (!Buff.readInt(Dummy)) return false; // Checksum #1
125 if (Format != GCOV::GCNO_402)
126 if (!Buff.readInt(Dummy)) return false; // Checksum #2
128 if (!Buff.readString(Name)) return false;
129 if (!Buff.readString(Filename)) return false;
130 if (!Buff.readInt(LineNumber)) return false;
133 if (!Buff.readBlockTag()) {
134 errs() << "Block tag not found.\n";
138 if (!Buff.readInt(BlockCount)) return false;
139 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
140 if (!Buff.readInt(Dummy)) return false; // Block flags;
141 Blocks.push_back(new GCOVBlock(*this, i));
145 while (Buff.readEdgeTag()) {
147 if (!Buff.readInt(EdgeCount)) return false;
148 EdgeCount = (EdgeCount - 1) / 2;
150 if (!Buff.readInt(BlockNo)) return false;
151 if (BlockNo >= BlockCount) {
152 errs() << "Unexpected block number.\n";
155 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
157 if (!Buff.readInt(Dst)) return false;
158 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
159 Edges.push_back(Edge);
160 Blocks[BlockNo]->addDstEdge(Edge);
161 Blocks[Dst]->addSrcEdge(Edge);
162 if (!Buff.readInt(Dummy)) return false; // Edge flag
167 while (Buff.readLineTag()) {
168 uint32_t LineTableLength;
169 if (!Buff.readInt(LineTableLength)) return false;
170 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
172 if (!Buff.readInt(BlockNo)) return false;
173 if (BlockNo >= BlockCount) {
174 errs() << "Unexpected block number.\n";
177 GCOVBlock *Block = Blocks[BlockNo];
178 if (!Buff.readInt(Dummy)) return false; // flag
179 while (Buff.getCursor() != (EndPos - 4)) {
181 if (!Buff.readString(F)) return false;
183 errs() << "Multiple sources for a single basic block.\n";
186 if (Buff.getCursor() == (EndPos - 4)) break;
189 if (!Buff.readInt(Line)) return false;
191 Block->addLine(Line);
194 if (!Buff.readInt(Dummy)) return false; // flag
199 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
201 bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
203 if (!Buff.readInt(Dummy)) return false; // Function header length
204 if (!Buff.readInt(Ident)) return false;
205 if (!Buff.readInt(Dummy)) return false; // Checksum #1
206 if (Format != GCOV::GCDA_402)
207 if (!Buff.readInt(Dummy)) return false; // Checksum #2
209 if (!Buff.readString(Name)) return false;
211 if (!Buff.readArcTag()) {
212 errs() << "Arc tag not found.\n";
217 if (!Buff.readInt(Count)) return false;
220 // This for loop adds the counts for each block. A second nested loop is
221 // required to combine the edge counts that are contained in the GCDA file.
222 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
223 // The last block is always reserved for exit block
224 if (BlockNo >= Blocks.size()-1) {
225 errs() << "Unexpected number of edges.\n";
228 GCOVBlock &Block = *Blocks[BlockNo];
229 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
232 errs() << "Unexpected number of edges.\n";
236 if (!Buff.readInt64(ArcCount)) return false;
237 Block.addCount(EdgeNo, ArcCount);
244 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
245 void GCOVFunction::dump() const {
246 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
247 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
248 E = Blocks.end(); I != E; ++I)
252 /// collectLineCounts - Collect line counts. This must be used after
253 /// reading .gcno and .gcda files.
254 void GCOVFunction::collectLineCounts(FileInfo &FI) {
255 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
256 E = Blocks.end(); I != E; ++I)
257 (*I)->collectLineCounts(FI);
260 //===----------------------------------------------------------------------===//
261 // GCOVBlock implementation.
263 /// ~GCOVBlock - Delete GCOVBlock and its content.
264 GCOVBlock::~GCOVBlock() {
270 /// addCount - Add to block counter while storing the edge count. If the
271 /// destination has no outgoing edges, also update that block's count too.
272 void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
273 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
274 DstEdges[DstEdgeNo]->Count = N;
276 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
277 DstEdges[DstEdgeNo]->Dst->Counter += N;
280 /// collectLineCounts - Collect line counts. This must be used after
281 /// reading .gcno and .gcda files.
282 void GCOVBlock::collectLineCounts(FileInfo &FI) {
283 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
284 E = Lines.end(); I != E; ++I)
285 FI.addBlockLine(Parent.getFilename(), *I, this);
288 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
289 void GCOVBlock::dump() const {
290 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
291 if (!SrcEdges.empty()) {
292 dbgs() << "\tSource Edges : ";
293 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
294 const GCOVEdge *Edge = *I;
295 dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
299 if (!DstEdges.empty()) {
300 dbgs() << "\tDestination Edges : ";
301 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
302 const GCOVEdge *Edge = *I;
303 dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
307 if (!Lines.empty()) {
308 dbgs() << "\tLines : ";
309 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
310 E = Lines.end(); I != E; ++I)
311 dbgs() << (*I) << ",";
316 //===----------------------------------------------------------------------===//
317 // FileInfo implementation.
319 /// print - Print source files with collected line count information.
320 void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile,
321 StringRef gcdaFile) const {
322 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
323 E = LineInfo.end(); I != E; ++I) {
324 StringRef Filename = I->first();
325 OwningPtr<MemoryBuffer> Buff;
326 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
327 errs() << Filename << ": " << ec.message() << "\n";
330 StringRef AllLines = Buff->getBuffer();
332 OS << " -: 0:Source:" << Filename << "\n";
333 OS << " -: 0:Graph:" << gcnoFile << "\n";
334 OS << " -: 0:Data:" << gcdaFile << "\n";
335 OS << " -: 0:Runs:" << RunCount << "\n";
336 OS << " -: 0:Programs:" << ProgramCount << "\n";
338 const LineData &L = I->second;
340 while (!AllLines.empty()) {
341 LineData::const_iterator BlocksIt = L.find(i);
342 if (BlocksIt != L.end()) {
343 const BlockVector &Blocks = BlocksIt->second;
344 uint64_t LineCount = 0;
345 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
347 LineCount += (*I)->getCount();
352 OS << format("%9" PRIu64 ":", LineCount);
356 std::pair<StringRef, StringRef> P = AllLines.split('\n');
357 if (AllLines != P.first)
358 OS << format("%5u:", i+1) << P.first;