LLVM 17.0.0git
GCOV.h
Go to the documentation of this file.
1//===- GCOV.h - LLVM coverage tool ------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This header provides the interface to read and write coverage files that
10// use 'gcov' format.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_PROFILEDATA_GCOV_H
15#define LLVM_PROFILEDATA_GCOV_H
16
17#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/iterator.h"
26#include <algorithm>
27#include <cstddef>
28#include <cstdint>
29#include <map>
30#include <memory>
31#include <string>
32#include <utility>
33
34namespace llvm {
35
36class GCOVFunction;
37class GCOVBlock;
38
39namespace GCOV {
40
42
43/// A struct for passing gcov options between functions.
44struct Options {
45 Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L,
46 bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
51
65 std::string SourcePrefix;
66};
67
68} // end namespace GCOV
69
70/// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
71/// read operations.
73public:
74 GCOVBuffer(MemoryBuffer *B) : Buffer(B) {}
76
77 /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
79 StringRef buf = Buffer->getBuffer();
80 StringRef magic = buf.substr(0, 4);
81 if (magic == "gcno") {
82 de = DataExtractor(buf.substr(4), false, 0);
83 } else if (magic == "oncg") {
84 de = DataExtractor(buf.substr(4), true, 0);
85 } else {
86 errs() << "unexpected magic: " << magic << "\n";
87 return false;
88 }
89 return true;
90 }
91
92 /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
94 StringRef buf = Buffer->getBuffer();
95 StringRef magic = buf.substr(0, 4);
96 if (magic == "gcda") {
97 de = DataExtractor(buf.substr(4), false, 0);
98 } else if (magic == "adcg") {
99 de = DataExtractor(buf.substr(4), true, 0);
100 } else {
101 return false;
102 }
103 return true;
104 }
105
106 /// readGCOVVersion - Read GCOV version.
108 std::string str(de.getBytes(cursor, 4));
109 if (str.size() != 4)
110 return false;
111 if (de.isLittleEndian())
112 std::reverse(str.begin(), str.end());
113 int ver = str[0] >= 'A'
114 ? (str[0] - 'A') * 100 + (str[1] - '0') * 10 + str[2] - '0'
115 : (str[0] - '0') * 10 + str[2] - '0';
116 if (ver >= 120) {
117 this->version = version = GCOV::V1200;
118 return true;
119 } else if (ver >= 90) {
120 // PR gcov-profile/84846, r269678
121 this->version = version = GCOV::V900;
122 return true;
123 } else if (ver >= 80) {
124 // PR gcov-profile/48463
125 this->version = version = GCOV::V800;
126 return true;
127 } else if (ver >= 48) {
128 // r189778: the exit block moved from the last to the second.
129 this->version = version = GCOV::V408;
130 return true;
131 } else if (ver >= 47) {
132 // r173147: split checksum into cfg checksum and line checksum.
133 this->version = version = GCOV::V407;
134 return true;
135 } else if (ver >= 34) {
136 this->version = version = GCOV::V304;
137 return true;
138 }
139 errs() << "unexpected version: " << str << "\n";
140 return false;
141 }
142
145 uint32_t len;
146 if (!readInt(len) || len == 0)
147 return {};
148 return de.getBytes(cursor, len * 4).split('\0').first;
149 }
150
151 bool readInt(uint32_t &Val) {
152 if (cursor.tell() + 4 > de.size()) {
153 Val = 0;
154 errs() << "unexpected end of memory buffer: " << cursor.tell() << "\n";
155 return false;
156 }
157 Val = de.getU32(cursor);
158 return true;
159 }
160
161 bool readInt64(uint64_t &Val) {
162 uint32_t Lo, Hi;
163 if (!readInt(Lo) || !readInt(Hi))
164 return false;
165 Val = ((uint64_t)Hi << 32) | Lo;
166 return true;
167 }
168
170 uint32_t len;
171 if (!readInt(len) || len == 0)
172 return false;
173 if (version >= GCOV::V1200)
174 str = de.getBytes(cursor, len).drop_back();
175 else
176 str = de.getBytes(cursor, len * 4).split('\0').first;
177 return bool(cursor);
178 }
179
182
183private:
184 MemoryBuffer *Buffer;
185 GCOV::GCOVVersion version{};
186};
187
188/// GCOVFile - Collects coverage information for one pair of coverage file
189/// (.gcno and .gcda).
190class GCOVFile {
191public:
192 GCOVFile() = default;
193
194 bool readGCNO(GCOVBuffer &Buffer);
195 bool readGCDA(GCOVBuffer &Buffer);
197 void print(raw_ostream &OS) const;
198 void dump() const;
199
200 std::vector<std::string> filenames;
202
203public:
204 bool GCNOInitialized = false;
209 std::map<uint32_t, GCOVFunction *> identToFunction;
212
215 iterator begin() const { return iterator(functions.begin()); }
216 iterator end() const { return iterator(functions.end()); }
217};
218
219struct GCOVArc {
221 : src(src), dst(dst), flags(flags) {}
222 bool onTree() const;
223
229};
230
231/// GCOVFunction - Collects function information.
233public:
236
238
239 StringRef getName(bool demangle) const;
240 StringRef getFilename() const;
241 uint64_t getEntryCount() const;
242 GCOVBlock &getExitBlock() const;
243
245 return make_range(blocks.begin(), blocks.end());
246 }
247
249 void print(raw_ostream &OS) const;
250 void dump() const;
251
260 uint8_t artificial = 0;
263 unsigned srcIdx;
267};
268
269/// GCOVBlock - Collects block information.
271public:
276
278
280 uint32_t getLastLine() const { return lines.back(); }
281 uint64_t getCount() const { return count; }
282
283 void addSrcEdge(GCOVArc *Edge) { pred.push_back(Edge); }
284
285 void addDstEdge(GCOVArc *Edge) { succ.push_back(Edge); }
286
288 return make_range(pred.begin(), pred.end());
289 }
290
292 return make_range(succ.begin(), succ.end());
293 }
294
295 void print(raw_ostream &OS) const;
296 void dump() const;
297
298 static uint64_t
300 std::vector<std::pair<GCOVBlock *, size_t>> &stack);
301 static uint64_t getCyclesCount(const BlockVector &blocks);
303
304public:
310 bool traversable = false;
311 GCOVArc *incoming = nullptr;
312};
313
314void gcovOneInput(const GCOV::Options &options, StringRef filename,
315 StringRef gcno, StringRef gcda, GCOVFile &file);
316
317} // end namespace llvm
318
319#endif // LLVM_PROFILEDATA_GCOV_H
This file defines the StringMap class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines the DenseSet and SmallDenseSet classes.
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:491
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
hexagon gen pred
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
dot regions Print regions of function to dot file(with no function bodies)"
raw_pwrite_stream & OS
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition: DataExtractor.h:54
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:71
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:78
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
size_t size() const
Return the number of bytes in the underlying buffer.
bool isLittleEndian() const
Get the endianness for this extractor.
Definition: DataExtractor.h:97
StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
GCOVBlock - Collects block information.
Definition: GCOV.h:270
void addLine(uint32_t N)
Definition: GCOV.h:279
iterator_range< EdgeIterator > dsts() const
Definition: GCOV.h:291
static uint64_t getCyclesCount(const BlockVector &blocks)
Definition: GCOV.cpp:488
iterator_range< EdgeIterator > srcs() const
Definition: GCOV.h:287
SmallVector< uint32_t, 4 > lines
Definition: GCOV.h:309
uint64_t getCount() const
Definition: GCOV.h:281
GCOVBlock(uint32_t N)
Definition: GCOV.h:277
static uint64_t getLineCount(const BlockVector &Blocks)
void addDstEdge(GCOVArc *Edge)
Definition: GCOV.h:285
uint32_t getLastLine() const
Definition: GCOV.h:280
uint32_t number
Definition: GCOV.h:305
void addSrcEdge(GCOVArc *Edge)
Definition: GCOV.h:283
SmallVectorImpl< GCOVArc * >::const_iterator EdgeIterator
Definition: GCOV.h:272
bool traversable
Definition: GCOV.h:310
void print(raw_ostream &OS) const
collectLineCounts - Collect line counts.
Definition: GCOV.cpp:406
SmallVector< const GCOVBlock *, 1 > BlockVector
Definition: GCOV.h:273
GCOVArc * incoming
Definition: GCOV.h:311
static uint64_t augmentOneCycle(GCOVBlock *src, std::vector< std::pair< GCOVBlock *, size_t > > &stack)
Definition: GCOV.cpp:437
SmallVector< GCOVArc *, 2 > pred
Definition: GCOV.h:307
void dump() const
dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Definition: GCOV.cpp:433
uint64_t count
Definition: GCOV.h:306
SmallVector< GCOVArc *, 2 > succ
Definition: GCOV.h:308
GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific read operations.
Definition: GCOV.h:72
bool readInt(uint32_t &Val)
Definition: GCOV.h:151
DataExtractor::Cursor cursor
Definition: GCOV.h:181
bool readInt64(uint64_t &Val)
Definition: GCOV.h:161
GCOVBuffer(MemoryBuffer *B)
Definition: GCOV.h:74
bool readGCOVVersion(GCOV::GCOVVersion &version)
readGCOVVersion - Read GCOV version.
Definition: GCOV.h:107
bool readString(StringRef &str)
Definition: GCOV.h:169
DataExtractor de
Definition: GCOV.h:180
uint32_t getWord()
Definition: GCOV.h:143
bool readGCNOFormat()
readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
Definition: GCOV.h:78
StringRef getString()
Definition: GCOV.h:144
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
Definition: GCOV.h:93
GCOVFile - Collects coverage information for one pair of coverage file (.gcno and ....
Definition: GCOV.h:190
SmallVector< std::unique_ptr< GCOVFunction >, 16 > functions
Definition: GCOV.h:208
uint32_t checksum
Definition: GCOV.h:206
iterator end() const
Definition: GCOV.h:216
void print(raw_ostream &OS) const
Definition: GCOV.cpp:319
iterator begin() const
Definition: GCOV.h:215
uint32_t programCount
Definition: GCOV.h:211
uint32_t runCount
Definition: GCOV.h:210
GCOV::GCOVVersion version
Definition: GCOV.h:205
pointee_iterator< SmallVectorImpl< std::unique_ptr< GCOVFunction > >::const_iterator > iterator
Definition: GCOV.h:214
std::vector< std::string > filenames
Definition: GCOV.h:200
GCOV::GCOVVersion getVersion() const
Definition: GCOV.h:196
GCOVFile()=default
void dump() const
dump - Dump GCOVFile content to dbgs() for debugging purposes.
Definition: GCOV.cpp:326
std::map< uint32_t, GCOVFunction * > identToFunction
Definition: GCOV.h:209
StringRef cwd
Definition: GCOV.h:207
bool GCNOInitialized
Definition: GCOV.h:204
StringMap< unsigned > filenameToIdx
Definition: GCOV.h:201
bool readGCNO(GCOVBuffer &Buffer)
readGCNO - Read GCNO buffer.
Definition: GCOV.cpp:102
bool readGCDA(GCOVBuffer &Buffer)
readGCDA - Read GCDA buffer.
Definition: GCOV.cpp:213
GCOVFunction - Collects function information.
Definition: GCOV.h:232
uint64_t getEntryCount() const
getEntryCount - Get the number of times the function was called by retrieving the entry block's count...
Definition: GCOV.cpp:356
uint32_t endColumn
Definition: GCOV.h:259
SmallVector< std::unique_ptr< GCOVArc >, 0 > treeArcs
Definition: GCOV.h:265
StringRef getName(bool demangle) const
Definition: GCOV.cpp:334
uint32_t cfgChecksum
Definition: GCOV.h:255
GCOVFunction(GCOVFile &file)
Definition: GCOV.h:237
void dump() const
dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Definition: GCOV.cpp:397
StringRef Name
Definition: GCOV.h:261
uint8_t artificial
Definition: GCOV.h:260
uint64_t propagateCounts(const GCOVBlock &v, GCOVArc *pred)
Definition: GCOV.cpp:368
uint32_t endLine
Definition: GCOV.h:258
SmallVector< std::unique_ptr< GCOVBlock >, 0 > blocks
Definition: GCOV.h:264
GCOVBlock & getExitBlock() const
Definition: GCOV.cpp:360
StringRef getFilename() const
Definition: GCOV.cpp:352
DenseSet< const GCOVBlock * > visited
Definition: GCOV.h:266
uint32_t startLine
Definition: GCOV.h:256
SmallString< 0 > demangled
Definition: GCOV.h:262
void print(raw_ostream &OS) const
Definition: GCOV.cpp:388
uint32_t startColumn
Definition: GCOV.h:257
uint32_t linenoChecksum
Definition: GCOV.h:254
iterator_range< BlockIterator > blocksRange() const
Definition: GCOV.h:244
SmallVector< std::unique_ptr< GCOVArc >, 0 > arcs
Definition: GCOV.h:265
uint32_t ident
Definition: GCOV.h:253
unsigned srcIdx
Definition: GCOV.h:263
GCOVFile & file
Definition: GCOV.h:252
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
StringRef getBuffer() const
Definition: MemoryBuffer.h:70
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:582
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:111
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:698
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:569
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:614
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
GCOVVersion
Definition: GCOV.h:41
@ V407
Definition: GCOV.h:41
@ V800
Definition: GCOV.h:41
@ V408
Definition: GCOV.h:41
@ V900
Definition: GCOV.h:41
@ V1200
Definition: GCOV.h:41
@ V304
Definition: GCOV.h:41
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::string demangle(const std::string &MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:29
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1946
void gcovOneInput(const GCOV::Options &options, StringRef filename, StringRef gcno, StringRef gcda, GCOVFile &file)
Definition: GCOV.cpp:956
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
Definition: BitVector.h:858
#define N
uint64_t count
Definition: GCOV.h:227
bool onTree() const
Definition: GCOV.cpp:329
GCOVBlock & src
Definition: GCOV.h:224
GCOVBlock & dst
Definition: GCOV.h:225
GCOVArc(GCOVBlock &src, GCOVBlock &dst, uint32_t flags)
Definition: GCOV.h:220
uint64_t cycleCount
Definition: GCOV.h:228
uint32_t flags
Definition: GCOV.h:226
A struct for passing gcov options between functions.
Definition: GCOV.h:44
bool HashFilenames
Definition: GCOV.h:64
bool PreservePaths
Definition: GCOV.h:56
std::string SourcePrefix
Definition: GCOV.h:65
bool BranchInfo
Definition: GCOV.h:53
bool RelativeOnly
Definition: GCOV.h:62
bool FuncCoverage
Definition: GCOV.h:55
Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L, bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
Definition: GCOV.h:45
bool UncondBranch
Definition: GCOV.h:57
bool BranchCount
Definition: GCOV.h:54
bool LongFileNames
Definition: GCOV.h:59
bool UseStdout
Definition: GCOV.h:63
bool AllBlocks
Definition: GCOV.h:52
bool Intermediate
Definition: GCOV.h:58
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324