blob: 8ce93675e4e05a4af1474df766935664a8d811f4 [file] [log] [blame]
Devang Patel8dfb6552011-10-04 17:24:48 +00001//===- GCOVr.cpp - LLVM coverage tool -------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
NAKAMURA Takumi3b551962013-11-14 11:44:58 +000010// GCOV implements the interface to read and write coverage files that use
Devang Patel8dfb6552011-10-04 17:24:48 +000011// 'gcov' format.
Devang Patel37140652011-09-28 18:50:00 +000012//
Devang Patel37140652011-09-28 18:50:00 +000013//===----------------------------------------------------------------------===//
14
Yuchen Wu03678152013-10-25 02:22:24 +000015#include "llvm/Support/Debug.h"
Devang Patel8dfb6552011-10-04 17:24:48 +000016#include "llvm/Support/GCOV.h"
Devang Patel37140652011-09-28 18:50:00 +000017#include "llvm/ADT/OwningPtr.h"
Devang Patela9e8a252011-09-29 16:46:47 +000018#include "llvm/ADT/STLExtras.h"
Bob Wilson3461bed2013-10-22 05:09:41 +000019#include "llvm/Support/Format.h"
Devang Patel37140652011-09-28 18:50:00 +000020#include "llvm/Support/MemoryObject.h"
21#include "llvm/Support/system_error.h"
22using namespace llvm;
23
24//===----------------------------------------------------------------------===//
25// GCOVFile implementation.
26
27/// ~GCOVFile - Delete GCOVFile and its content.
28GCOVFile::~GCOVFile() {
29 DeleteContainerPointers(Functions);
30}
31
Devang Patele5a8f2f92011-09-29 17:06:40 +000032/// isGCDAFile - Return true if Format identifies a .gcda file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000033static bool isGCDAFile(GCOV::GCOVFormat Format) {
34 return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000035}
36
37/// isGCNOFile - Return true if Format identifies a .gcno file.
Bill Wendling6bbe4892012-08-31 17:31:28 +000038static bool isGCNOFile(GCOV::GCOVFormat Format) {
39 return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404;
Devang Patele5a8f2f92011-09-29 17:06:40 +000040}
41
Devang Patel37140652011-09-28 18:50:00 +000042/// read - Read GCOV buffer.
43bool GCOVFile::read(GCOVBuffer &Buffer) {
Bill Wendling6bbe4892012-08-31 17:31:28 +000044 GCOV::GCOVFormat Format = Buffer.readGCOVFormat();
45 if (Format == GCOV::InvalidGCOV)
Devang Patel37140652011-09-28 18:50:00 +000046 return false;
47
Yuchen Wu14ae8e62013-10-25 02:22:21 +000048 if (isGCNOFile(Format)) {
Yuchen Wubabe7492013-11-20 04:15:05 +000049 if (!Buffer.readInt(Checksum)) return false;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000050 while (true) {
Yuchen Wue28da842013-11-14 00:07:15 +000051 if (!Buffer.readFunctionTag()) break;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000052 GCOVFunction *GFun = new GCOVFunction();
Yuchen Wuba718332013-12-03 00:15:49 +000053 if (!GFun->readGCNO(Buffer, Format))
Yuchen Wue28da842013-11-14 00:07:15 +000054 return false;
Devang Patele5a8f2f92011-09-29 17:06:40 +000055 Functions.push_back(GFun);
Devang Patel37140652011-09-28 18:50:00 +000056 }
Yuchen Wu9a74b8c2013-11-21 04:12:10 +000057 } else if (isGCDAFile(Format)) {
Yuchen Wubabe7492013-11-20 04:15:05 +000058 uint32_t Checksum2;
59 if (!Buffer.readInt(Checksum2)) return false;
60 if (Checksum != Checksum2) {
61 errs() << "File checksum does not match.\n";
62 return false;
63 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000064 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +000065 if (!Buffer.readFunctionTag()) {
66 errs() << "Unexpected number of functions.\n";
67 return false;
68 }
Yuchen Wuba718332013-12-03 00:15:49 +000069 if (!Functions[i]->readGCDA(Buffer, Format))
Yuchen Wue28da842013-11-14 00:07:15 +000070 return false;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000071 }
Yuchen Wu30672d92013-11-05 01:11:58 +000072 if (Buffer.readObjectTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +000073 uint32_t Length;
74 uint32_t Dummy;
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;;
Yuchen Wu30672d92013-11-05 01:11:58 +000079 Buffer.advanceCursor(Length-3);
80 }
81 while (Buffer.readProgramTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +000082 uint32_t Length;
83 if (!Buffer.readInt(Length)) return false;
Yuchen Wu30672d92013-11-05 01:11:58 +000084 Buffer.advanceCursor(Length);
Yuchen Wu14ae8e62013-10-25 02:22:21 +000085 ++ProgramCount;
Yuchen Wu30672d92013-11-05 01:11:58 +000086 }
Yuchen Wu14ae8e62013-10-25 02:22:21 +000087 }
88
Devang Patel37140652011-09-28 18:50:00 +000089 return true;
90}
91
Yuchen Wu03678152013-10-25 02:22:24 +000092/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +000093void GCOVFile::dump() const {
94 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000095 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000096 (*I)->dump();
97}
98
99/// collectLineCounts - Collect line counts. This must be used after
100/// reading .gcno and .gcda files.
101void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000102 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
NAKAMURA Takumi3b551962013-11-14 11:44:58 +0000103 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000104 (*I)->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000105 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000106 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000107}
108
109//===----------------------------------------------------------------------===//
110// GCOVFunction implementation.
111
112/// ~GCOVFunction - Delete GCOVFunction and its content.
113GCOVFunction::~GCOVFunction() {
114 DeleteContainerPointers(Blocks);
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000115 DeleteContainerPointers(Edges);
Devang Patel37140652011-09-28 18:50:00 +0000116}
117
Yuchen Wuba718332013-12-03 00:15:49 +0000118/// readGCNO - Read a function from the GCNO buffer. Return false if an error
119/// occurs.
120bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
Yuchen Wue28da842013-11-14 00:07:15 +0000121 uint32_t Dummy;
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
Yuchen Wuba718332013-12-03 00:15:49 +0000125 if (Format != GCOV::GCNO_402)
Yuchen Wue28da842013-11-14 00:07:15 +0000126 if (!Buff.readInt(Dummy)) return false; // Checksum #2
Devang Patel37140652011-09-28 18:50:00 +0000127
Yuchen Wue28da842013-11-14 00:07:15 +0000128 if (!Buff.readString(Name)) return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000129 if (!Buff.readString(Filename)) return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000130 if (!Buff.readInt(LineNumber)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000131
132 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000133 if (!Buff.readBlockTag()) {
134 errs() << "Block tag not found.\n";
135 return false;
136 }
137 uint32_t BlockCount;
138 if (!Buff.readInt(BlockCount)) return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000139 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000140 if (!Buff.readInt(Dummy)) return false; // Block flags;
Yuchen Wud738bee2013-11-14 00:32:00 +0000141 Blocks.push_back(new GCOVBlock(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000142 }
143
144 // read edges.
145 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000146 uint32_t EdgeCount;
147 if (!Buff.readInt(EdgeCount)) return false;
148 EdgeCount = (EdgeCount - 1) / 2;
149 uint32_t BlockNo;
150 if (!Buff.readInt(BlockNo)) return false;
151 if (BlockNo >= BlockCount) {
152 errs() << "Unexpected block number.\n";
153 return false;
154 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000155 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000156 uint32_t Dst;
157 if (!Buff.readInt(Dst)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000158 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
159 Edges.push_back(Edge);
160 Blocks[BlockNo]->addDstEdge(Edge);
161 Blocks[Dst]->addSrcEdge(Edge);
Yuchen Wue28da842013-11-14 00:07:15 +0000162 if (!Buff.readInt(Dummy)) return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000163 }
164 }
165
166 // read line table.
167 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000168 uint32_t LineTableLength;
169 if (!Buff.readInt(LineTableLength)) return false;
Bob Wilson00928bc2013-10-22 19:54:32 +0000170 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Yuchen Wue28da842013-11-14 00:07:15 +0000171 uint32_t BlockNo;
172 if (!Buff.readInt(BlockNo)) return false;
173 if (BlockNo >= BlockCount) {
174 errs() << "Unexpected block number.\n";
175 return false;
Devang Patel37140652011-09-28 18:50:00 +0000176 }
Yuchen Wue28da842013-11-14 00:07:15 +0000177 GCOVBlock *Block = Blocks[BlockNo];
178 if (!Buff.readInt(Dummy)) return false; // flag
179 while (Buff.getCursor() != (EndPos - 4)) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000180 StringRef F;
181 if (!Buff.readString(F)) return false;
182 if (F != Filename) {
183 errs() << "Multiple sources for a single basic block.\n";
184 return false;
185 }
Yuchen Wue28da842013-11-14 00:07:15 +0000186 if (Buff.getCursor() == (EndPos - 4)) break;
187 while (true) {
188 uint32_t Line;
189 if (!Buff.readInt(Line)) return false;
190 if (!Line) break;
Yuchen Wud738bee2013-11-14 00:32:00 +0000191 Block->addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000192 }
193 }
194 if (!Buff.readInt(Dummy)) return false; // flag
Devang Patel37140652011-09-28 18:50:00 +0000195 }
196 return true;
197}
198
Yuchen Wuba718332013-12-03 00:15:49 +0000199/// readGCDA - Read a function from the GCDA buffer. Return false if an error
200/// occurs.
201bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVFormat Format) {
202 uint32_t Dummy;
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
208
209 if (!Buff.readString(Name)) return false;
210
211 if (!Buff.readArcTag()) {
212 errs() << "Arc tag not found.\n";
213 return false;
214 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000215
Yuchen Wuba718332013-12-03 00:15:49 +0000216 uint32_t Count;
217 if (!Buff.readInt(Count)) return false;
218 Count /= 2;
219
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.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000222 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
223 // The last block is always reserved for exit block
224 if (BlockNo >= Blocks.size()-1) {
Yuchen Wuba718332013-12-03 00:15:49 +0000225 errs() << "Unexpected number of edges.\n";
226 return false;
227 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000228 GCOVBlock &Block = *Blocks[BlockNo];
229 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
230 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000231 if (Count == 0) {
232 errs() << "Unexpected number of edges.\n";
233 return false;
234 }
235 uint64_t ArcCount;
236 if (!Buff.readInt64(ArcCount)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000237 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000238 --Count;
239 }
240 }
241 return true;
242}
243
Yuchen Wu03678152013-10-25 02:22:24 +0000244/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000245void GCOVFunction::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000246 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000247 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000248 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000249 (*I)->dump();
250}
251
252/// collectLineCounts - Collect line counts. This must be used after
253/// reading .gcno and .gcda files.
254void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000255 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000256 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000257 (*I)->collectLineCounts(FI);
258}
259
260//===----------------------------------------------------------------------===//
261// GCOVBlock implementation.
262
263/// ~GCOVBlock - Delete GCOVBlock and its content.
264GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000265 SrcEdges.clear();
266 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000267 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000268}
269
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000270/// 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.
272void 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;
275 Counter += N;
276 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
277 DstEdges[DstEdgeNo]->Dst->Counter += N;
278}
279
Devang Patel37140652011-09-28 18:50:00 +0000280/// collectLineCounts - Collect line counts. This must be used after
281/// reading .gcno and .gcda files.
282void GCOVBlock::collectLineCounts(FileInfo &FI) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000283 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000284 E = Lines.end(); I != E; ++I)
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000285 FI.addBlockLine(Parent.getFilename(), *I, this);
Devang Patel37140652011-09-28 18:50:00 +0000286}
287
Yuchen Wu03678152013-10-25 02:22:24 +0000288/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000289void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000290 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000291 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 << "), ";
296 }
297 dbgs() << "\n";
298 }
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 << "), ";
304 }
Yuchen Wu03678152013-10-25 02:22:24 +0000305 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000306 }
307 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000308 dbgs() << "\tLines : ";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000309 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
Yuchen Wud738bee2013-11-14 00:32:00 +0000310 E = Lines.end(); I != E; ++I)
311 dbgs() << (*I) << ",";
312 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000313 }
314}
315
316//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000317// FileInfo implementation.
318
Devang Patel37140652011-09-28 18:50:00 +0000319/// print - Print source files with collected line count information.
Yuchen Wu26326ad2013-12-03 00:57:11 +0000320void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) const {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000321 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
Yuchen Wuef6909d2013-11-19 20:33:32 +0000322 E = LineInfo.end(); I != E; ++I) {
Devang Patel37140652011-09-28 18:50:00 +0000323 StringRef Filename = I->first();
Devang Patel37140652011-09-28 18:50:00 +0000324 OwningPtr<MemoryBuffer> Buff;
325 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
326 errs() << Filename << ": " << ec.message() << "\n";
327 return;
328 }
Benjamin Kramer67421c12013-11-15 09:44:17 +0000329 StringRef AllLines = Buff->getBuffer();
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000330
Yuchen Wu26326ad2013-12-03 00:57:11 +0000331 std::string CovFilename = Filename.str() + ".llcov";
332 std::string ErrorInfo;
333 raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo);
334 if (!ErrorInfo.empty())
335 errs() << ErrorInfo << "\n";
336
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000337 OS << " -: 0:Source:" << Filename << "\n";
338 OS << " -: 0:Graph:" << gcnoFile << "\n";
339 OS << " -: 0:Data:" << gcdaFile << "\n";
340 OS << " -: 0:Runs:" << RunCount << "\n";
341 OS << " -: 0:Programs:" << ProgramCount << "\n";
342
Yuchen Wu1c068162013-12-03 01:35:31 +0000343 const LineData &Line = I->second;
344 for (uint32_t i = 0; !AllLines.empty(); ++i) {
345 LineData::const_iterator BlocksIt = Line.find(i);
346
347 // Add up the block counts to form line counts.
348 if (BlocksIt != Line.end()) {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000349 const BlockVector &Blocks = BlocksIt->second;
350 uint64_t LineCount = 0;
351 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
352 I != E; ++I) {
353 LineCount += (*I)->getCount();
354 }
355 if (LineCount == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000356 OS << " #####:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000357 else
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000358 OS << format("%9" PRIu64 ":", LineCount);
Yuchen Wu48342ee2013-10-23 19:45:03 +0000359 } else {
Yuchen Wudbcf1972013-11-02 00:09:17 +0000360 OS << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000361 }
Devang Patel37140652011-09-28 18:50:00 +0000362 std::pair<StringRef, StringRef> P = AllLines.split('\n');
Yuchen Wu1c068162013-12-03 01:35:31 +0000363 OS << format("%5u:", i+1) << P.first << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000364 AllLines = P.second;
365 }
366 }
367}