blob: 3b870f3471f78f8887e7ccef603743b1dda05940 [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
Yuchen Wubec4e902013-12-04 04:49:23 +000032/// readGCNO - Read GCNO buffer.
33bool GCOVFile::readGCNO(GCOVBuffer &Buffer) {
34 if (!Buffer.readGCNOFormat()) return false;
35 if (!Buffer.readGCOVVersion(Version)) return false;
36
37 if (!Buffer.readInt(Checksum)) return false;
38 while (true) {
39 if (!Buffer.readFunctionTag()) break;
40 GCOVFunction *GFun = new GCOVFunction();
41 if (!GFun->readGCNO(Buffer, Version))
42 return false;
43 Functions.push_back(GFun);
44 }
45
46 gcnoInitialized = true;
47 return true;
Devang Patele5a8f2f92011-09-29 17:06:40 +000048}
49
Yuchen Wubec4e902013-12-04 04:49:23 +000050/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
51/// called after readGCNO().
52bool GCOVFile::readGCDA(GCOVBuffer &Buffer) {
53 assert(gcnoInitialized && "readGCDA() can only be called after readGCNO()");
54 if (!Buffer.readGCDAFormat()) return false;
55 GCOV::GCOVVersion GCDAVersion;
56 if (!Buffer.readGCOVVersion(GCDAVersion)) return false;
57 if (Version != GCDAVersion) {
58 errs() << "GCOV versions do not match.\n";
Devang Patel37140652011-09-28 18:50:00 +000059 return false;
Yuchen Wubec4e902013-12-04 04:49:23 +000060 }
Devang Patel37140652011-09-28 18:50:00 +000061
Yuchen Wubec4e902013-12-04 04:49:23 +000062 uint32_t GCDAChecksum;
63 if (!Buffer.readInt(GCDAChecksum)) return false;
64 if (Checksum != GCDAChecksum) {
65 errs() << "File checksum does not match.\n";
66 return false;
67 }
68 for (size_t i = 0, e = Functions.size(); i < e; ++i) {
69 if (!Buffer.readFunctionTag()) {
70 errs() << "Unexpected number of functions.\n";
Yuchen Wubabe7492013-11-20 04:15:05 +000071 return false;
72 }
Yuchen Wubec4e902013-12-04 04:49:23 +000073 if (!Functions[i]->readGCDA(Buffer, Version))
74 return false;
75 }
76 if (Buffer.readObjectTag()) {
77 uint32_t Length;
78 uint32_t Dummy;
79 if (!Buffer.readInt(Length)) return false;
80 if (!Buffer.readInt(Dummy)) return false; // checksum
81 if (!Buffer.readInt(Dummy)) return false; // num
82 if (!Buffer.readInt(RunCount)) return false;;
83 Buffer.advanceCursor(Length-3);
84 }
85 while (Buffer.readProgramTag()) {
86 uint32_t Length;
87 if (!Buffer.readInt(Length)) return false;
88 Buffer.advanceCursor(Length);
89 ++ProgramCount;
Yuchen Wu14ae8e62013-10-25 02:22:21 +000090 }
91
Devang Patel37140652011-09-28 18:50:00 +000092 return true;
93}
94
Yuchen Wu03678152013-10-25 02:22:24 +000095/// dump - Dump GCOVFile content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +000096void GCOVFile::dump() const {
97 for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +000098 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +000099 (*I)->dump();
100}
101
102/// collectLineCounts - Collect line counts. This must be used after
103/// reading .gcno and .gcda files.
104void GCOVFile::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000105 for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
NAKAMURA Takumi3b551962013-11-14 11:44:58 +0000106 E = Functions.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000107 (*I)->collectLineCounts(FI);
Yuchen Wu30672d92013-11-05 01:11:58 +0000108 FI.setRunCount(RunCount);
Yuchen Wu14ae8e62013-10-25 02:22:21 +0000109 FI.setProgramCount(ProgramCount);
Devang Patel37140652011-09-28 18:50:00 +0000110}
111
112//===----------------------------------------------------------------------===//
113// GCOVFunction implementation.
114
115/// ~GCOVFunction - Delete GCOVFunction and its content.
116GCOVFunction::~GCOVFunction() {
117 DeleteContainerPointers(Blocks);
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000118 DeleteContainerPointers(Edges);
Devang Patel37140652011-09-28 18:50:00 +0000119}
120
Yuchen Wuba718332013-12-03 00:15:49 +0000121/// readGCNO - Read a function from the GCNO buffer. Return false if an error
122/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000123bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wue28da842013-11-14 00:07:15 +0000124 uint32_t Dummy;
125 if (!Buff.readInt(Dummy)) return false; // Function header length
126 if (!Buff.readInt(Ident)) return false;
127 if (!Buff.readInt(Dummy)) return false; // Checksum #1
Yuchen Wubec4e902013-12-04 04:49:23 +0000128 if (Version != GCOV::V402)
Yuchen Wue28da842013-11-14 00:07:15 +0000129 if (!Buff.readInt(Dummy)) return false; // Checksum #2
Devang Patel37140652011-09-28 18:50:00 +0000130
Yuchen Wue28da842013-11-14 00:07:15 +0000131 if (!Buff.readString(Name)) return false;
Yuchen Wuba718332013-12-03 00:15:49 +0000132 if (!Buff.readString(Filename)) return false;
Yuchen Wue28da842013-11-14 00:07:15 +0000133 if (!Buff.readInt(LineNumber)) return false;
Devang Patel37140652011-09-28 18:50:00 +0000134
135 // read blocks.
Yuchen Wue28da842013-11-14 00:07:15 +0000136 if (!Buff.readBlockTag()) {
137 errs() << "Block tag not found.\n";
138 return false;
139 }
140 uint32_t BlockCount;
141 if (!Buff.readInt(BlockCount)) return false;
Bob Wilson868e6e32013-10-22 20:02:36 +0000142 for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000143 if (!Buff.readInt(Dummy)) return false; // Block flags;
Yuchen Wud738bee2013-11-14 00:32:00 +0000144 Blocks.push_back(new GCOVBlock(*this, i));
Devang Patel37140652011-09-28 18:50:00 +0000145 }
146
147 // read edges.
148 while (Buff.readEdgeTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000149 uint32_t EdgeCount;
150 if (!Buff.readInt(EdgeCount)) return false;
151 EdgeCount = (EdgeCount - 1) / 2;
152 uint32_t BlockNo;
153 if (!Buff.readInt(BlockNo)) return false;
154 if (BlockNo >= BlockCount) {
155 errs() << "Unexpected block number.\n";
156 return false;
157 }
Bob Wilson868e6e32013-10-22 20:02:36 +0000158 for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
Yuchen Wue28da842013-11-14 00:07:15 +0000159 uint32_t Dst;
160 if (!Buff.readInt(Dst)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000161 GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]);
162 Edges.push_back(Edge);
163 Blocks[BlockNo]->addDstEdge(Edge);
164 Blocks[Dst]->addSrcEdge(Edge);
Yuchen Wue28da842013-11-14 00:07:15 +0000165 if (!Buff.readInt(Dummy)) return false; // Edge flag
Devang Patel37140652011-09-28 18:50:00 +0000166 }
167 }
168
169 // read line table.
170 while (Buff.readLineTag()) {
Yuchen Wue28da842013-11-14 00:07:15 +0000171 uint32_t LineTableLength;
172 if (!Buff.readInt(LineTableLength)) return false;
Bob Wilson00928bc2013-10-22 19:54:32 +0000173 uint32_t EndPos = Buff.getCursor() + LineTableLength*4;
Yuchen Wue28da842013-11-14 00:07:15 +0000174 uint32_t BlockNo;
175 if (!Buff.readInt(BlockNo)) return false;
176 if (BlockNo >= BlockCount) {
177 errs() << "Unexpected block number.\n";
178 return false;
Devang Patel37140652011-09-28 18:50:00 +0000179 }
Yuchen Wue28da842013-11-14 00:07:15 +0000180 GCOVBlock *Block = Blocks[BlockNo];
181 if (!Buff.readInt(Dummy)) return false; // flag
182 while (Buff.getCursor() != (EndPos - 4)) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000183 StringRef F;
184 if (!Buff.readString(F)) return false;
185 if (F != Filename) {
186 errs() << "Multiple sources for a single basic block.\n";
187 return false;
188 }
Yuchen Wue28da842013-11-14 00:07:15 +0000189 if (Buff.getCursor() == (EndPos - 4)) break;
190 while (true) {
191 uint32_t Line;
192 if (!Buff.readInt(Line)) return false;
193 if (!Line) break;
Yuchen Wud738bee2013-11-14 00:32:00 +0000194 Block->addLine(Line);
Yuchen Wue28da842013-11-14 00:07:15 +0000195 }
196 }
197 if (!Buff.readInt(Dummy)) return false; // flag
Devang Patel37140652011-09-28 18:50:00 +0000198 }
199 return true;
200}
201
Yuchen Wuba718332013-12-03 00:15:49 +0000202/// readGCDA - Read a function from the GCDA buffer. Return false if an error
203/// occurs.
Yuchen Wubec4e902013-12-04 04:49:23 +0000204bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) {
Yuchen Wuba718332013-12-03 00:15:49 +0000205 uint32_t Dummy;
206 if (!Buff.readInt(Dummy)) return false; // Function header length
207 if (!Buff.readInt(Ident)) return false;
208 if (!Buff.readInt(Dummy)) return false; // Checksum #1
Yuchen Wubec4e902013-12-04 04:49:23 +0000209 if (Version != GCOV::V402)
Yuchen Wuba718332013-12-03 00:15:49 +0000210 if (!Buff.readInt(Dummy)) return false; // Checksum #2
211
212 if (!Buff.readString(Name)) return false;
213
214 if (!Buff.readArcTag()) {
215 errs() << "Arc tag not found.\n";
216 return false;
217 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000218
Yuchen Wuba718332013-12-03 00:15:49 +0000219 uint32_t Count;
220 if (!Buff.readInt(Count)) return false;
221 Count /= 2;
222
223 // This for loop adds the counts for each block. A second nested loop is
224 // required to combine the edge counts that are contained in the GCDA file.
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000225 for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
226 // The last block is always reserved for exit block
227 if (BlockNo >= Blocks.size()-1) {
Yuchen Wuba718332013-12-03 00:15:49 +0000228 errs() << "Unexpected number of edges.\n";
229 return false;
230 }
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000231 GCOVBlock &Block = *Blocks[BlockNo];
232 for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
233 ++EdgeNo) {
Yuchen Wuba718332013-12-03 00:15:49 +0000234 if (Count == 0) {
235 errs() << "Unexpected number of edges.\n";
236 return false;
237 }
238 uint64_t ArcCount;
239 if (!Buff.readInt64(ArcCount)) return false;
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000240 Block.addCount(EdgeNo, ArcCount);
Yuchen Wuba718332013-12-03 00:15:49 +0000241 --Count;
242 }
243 }
244 return true;
245}
246
Yuchen Wu03678152013-10-25 02:22:24 +0000247/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000248void GCOVFunction::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000249 dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000250 for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000251 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000252 (*I)->dump();
253}
254
255/// collectLineCounts - Collect line counts. This must be used after
256/// reading .gcno and .gcda files.
257void GCOVFunction::collectLineCounts(FileInfo &FI) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000258 for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000259 E = Blocks.end(); I != E; ++I)
Devang Patel37140652011-09-28 18:50:00 +0000260 (*I)->collectLineCounts(FI);
261}
262
263//===----------------------------------------------------------------------===//
264// GCOVBlock implementation.
265
266/// ~GCOVBlock - Delete GCOVBlock and its content.
267GCOVBlock::~GCOVBlock() {
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000268 SrcEdges.clear();
269 DstEdges.clear();
Yuchen Wud738bee2013-11-14 00:32:00 +0000270 Lines.clear();
Devang Patel37140652011-09-28 18:50:00 +0000271}
272
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000273/// addCount - Add to block counter while storing the edge count. If the
274/// destination has no outgoing edges, also update that block's count too.
275void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
276 assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
277 DstEdges[DstEdgeNo]->Count = N;
278 Counter += N;
279 if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges())
280 DstEdges[DstEdgeNo]->Dst->Counter += N;
281}
282
Devang Patel37140652011-09-28 18:50:00 +0000283/// collectLineCounts - Collect line counts. This must be used after
284/// reading .gcno and .gcda files.
285void GCOVBlock::collectLineCounts(FileInfo &FI) {
Yuchen Wud738bee2013-11-14 00:32:00 +0000286 for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000287 E = Lines.end(); I != E; ++I)
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000288 FI.addBlockLine(Parent.getFilename(), *I, this);
Devang Patel37140652011-09-28 18:50:00 +0000289}
290
Yuchen Wu03678152013-10-25 02:22:24 +0000291/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Yuchen Wuef6909d2013-11-19 20:33:32 +0000292void GCOVBlock::dump() const {
Yuchen Wu03678152013-10-25 02:22:24 +0000293 dbgs() << "Block : " << Number << " Counter : " << Counter << "\n";
Yuchen Wu8ad9b042013-12-03 00:24:44 +0000294 if (!SrcEdges.empty()) {
295 dbgs() << "\tSource Edges : ";
296 for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) {
297 const GCOVEdge *Edge = *I;
298 dbgs() << Edge->Src->Number << " (" << Edge->Count << "), ";
299 }
300 dbgs() << "\n";
301 }
302 if (!DstEdges.empty()) {
303 dbgs() << "\tDestination Edges : ";
304 for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) {
305 const GCOVEdge *Edge = *I;
306 dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), ";
307 }
Yuchen Wu03678152013-10-25 02:22:24 +0000308 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000309 }
310 if (!Lines.empty()) {
Yuchen Wu03678152013-10-25 02:22:24 +0000311 dbgs() << "\tLines : ";
Yuchen Wuef6909d2013-11-19 20:33:32 +0000312 for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(),
Yuchen Wud738bee2013-11-14 00:32:00 +0000313 E = Lines.end(); I != E; ++I)
314 dbgs() << (*I) << ",";
315 dbgs() << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000316 }
317}
318
319//===----------------------------------------------------------------------===//
Devang Patel37140652011-09-28 18:50:00 +0000320// FileInfo implementation.
321
Devang Patel37140652011-09-28 18:50:00 +0000322/// print - Print source files with collected line count information.
Yuchen Wu26326ad2013-12-03 00:57:11 +0000323void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) const {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000324 for (StringMap<LineData>::const_iterator I = LineInfo.begin(),
Yuchen Wuef6909d2013-11-19 20:33:32 +0000325 E = LineInfo.end(); I != E; ++I) {
Devang Patel37140652011-09-28 18:50:00 +0000326 StringRef Filename = I->first();
Devang Patel37140652011-09-28 18:50:00 +0000327 OwningPtr<MemoryBuffer> Buff;
328 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
329 errs() << Filename << ": " << ec.message() << "\n";
330 return;
331 }
Benjamin Kramer67421c12013-11-15 09:44:17 +0000332 StringRef AllLines = Buff->getBuffer();
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000333
Yuchen Wu26326ad2013-12-03 00:57:11 +0000334 std::string CovFilename = Filename.str() + ".llcov";
335 std::string ErrorInfo;
336 raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo);
337 if (!ErrorInfo.empty())
338 errs() << ErrorInfo << "\n";
339
Yuchen Wu8aac4f62013-11-19 20:57:20 +0000340 OS << " -: 0:Source:" << Filename << "\n";
341 OS << " -: 0:Graph:" << gcnoFile << "\n";
342 OS << " -: 0:Data:" << gcdaFile << "\n";
343 OS << " -: 0:Runs:" << RunCount << "\n";
344 OS << " -: 0:Programs:" << ProgramCount << "\n";
345
Yuchen Wu1c068162013-12-03 01:35:31 +0000346 const LineData &Line = I->second;
347 for (uint32_t i = 0; !AllLines.empty(); ++i) {
348 LineData::const_iterator BlocksIt = Line.find(i);
349
350 // Add up the block counts to form line counts.
351 if (BlocksIt != Line.end()) {
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000352 const BlockVector &Blocks = BlocksIt->second;
353 uint64_t LineCount = 0;
354 for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end();
355 I != E; ++I) {
356 LineCount += (*I)->getCount();
357 }
358 if (LineCount == 0)
Yuchen Wudbcf1972013-11-02 00:09:17 +0000359 OS << " #####:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000360 else
Yuchen Wu8f1c8812013-12-03 00:38:21 +0000361 OS << format("%9" PRIu64 ":", LineCount);
Yuchen Wu48342ee2013-10-23 19:45:03 +0000362 } else {
Yuchen Wudbcf1972013-11-02 00:09:17 +0000363 OS << " -:";
Yuchen Wu48342ee2013-10-23 19:45:03 +0000364 }
Devang Patel37140652011-09-28 18:50:00 +0000365 std::pair<StringRef, StringRef> P = AllLines.split('\n');
Yuchen Wu1c068162013-12-03 01:35:31 +0000366 OS << format("%5u:", i+1) << P.first << "\n";
Devang Patel37140652011-09-28 18:50:00 +0000367 AllLines = P.second;
368 }
369 }
370}