LLVM 22.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"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/ADT/iterator.h"
28#include <algorithm>
29#include <cstddef>
30#include <cstdint>
31#include <map>
32#include <memory>
33#include <string>
34#include <utility>
35
36namespace llvm {
37
38class GCOVFunction;
39class GCOVBlock;
40
41namespace GCOV {
42
44
45/// A struct for passing gcov options between functions.
46struct Options {
47 Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L,
48 bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
53
67 std::string SourcePrefix;
68};
69
70} // end namespace GCOV
71
72/// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
73/// read operations.
75public:
76 GCOVBuffer(MemoryBuffer *B) : Buffer(B) {}
78
79 /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
81 StringRef buf = Buffer->getBuffer();
82 StringRef magic = buf.substr(0, 4);
83 if (magic == "gcno") {
84 de = DataExtractor(buf.substr(4), false, 0);
85 } else if (magic == "oncg") {
86 de = DataExtractor(buf.substr(4), true, 0);
87 } else {
88 errs() << "unexpected magic: " << magic << "\n";
89 return false;
90 }
91 return true;
92 }
93
94 /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
96 StringRef buf = Buffer->getBuffer();
97 StringRef magic = buf.substr(0, 4);
98 if (magic == "gcda") {
99 de = DataExtractor(buf.substr(4), false, 0);
100 } else if (magic == "adcg") {
101 de = DataExtractor(buf.substr(4), true, 0);
102 } else {
103 return false;
104 }
105 return true;
106 }
107
108 /// readGCOVVersion - Read GCOV version.
110 std::string str(de.getBytes(cursor, 4));
111 if (str.size() != 4)
112 return false;
113 if (de.isLittleEndian())
114 std::reverse(str.begin(), str.end());
115 int ver = str[0] >= 'A'
116 ? (str[0] - 'A') * 100 + (str[1] - '0') * 10 + str[2] - '0'
117 : (str[0] - '0') * 10 + str[2] - '0';
118 if (ver >= 120) {
119 this->version = version = GCOV::V1200;
120 return true;
121 } else if (ver >= 90) {
122 // PR gcov-profile/84846, r269678
123 this->version = version = GCOV::V900;
124 return true;
125 } else if (ver >= 80) {
126 // PR gcov-profile/48463
127 this->version = version = GCOV::V800;
128 return true;
129 } else if (ver >= 48) {
130 // r189778: the exit block moved from the last to the second.
131 this->version = version = GCOV::V408;
132 return true;
133 } else if (ver >= 47) {
134 // r173147: split checksum into cfg checksum and line checksum.
135 this->version = version = GCOV::V407;
136 return true;
137 } else if (ver >= 34) {
138 this->version = version = GCOV::V304;
139 return true;
140 }
141 errs() << "unexpected version: " << str << "\n";
142 return false;
143 }
144
147 uint32_t len;
148 if (!readInt(len) || len == 0)
149 return {};
150 return de.getBytes(cursor, len * 4).split('\0').first;
151 }
152
153 bool readInt(uint32_t &Val) {
154 if (cursor.tell() + 4 > de.size()) {
155 Val = 0;
156 errs() << "unexpected end of memory buffer: " << cursor.tell() << "\n";
157 return false;
158 }
159 Val = de.getU32(cursor);
160 return true;
161 }
162
163 bool readInt64(uint64_t &Val) {
164 uint32_t Lo, Hi;
165 if (!readInt(Lo) || !readInt(Hi))
166 return false;
167 Val = ((uint64_t)Hi << 32) | Lo;
168 return true;
169 }
170
172 uint32_t len;
173 if (!readInt(len) || len == 0)
174 return false;
175 if (version >= GCOV::V1200)
176 str = de.getBytes(cursor, len).drop_back();
177 else
178 str = de.getBytes(cursor, len * 4).split('\0').first;
179 return bool(cursor);
180 }
181
184
185private:
186 MemoryBuffer *Buffer;
187 GCOV::GCOVVersion version{};
188};
189
190/// GCOVFile - Collects coverage information for one pair of coverage file
191/// (.gcno and .gcda).
192class GCOVFile {
193public:
194 GCOVFile() = default;
195
196 LLVM_ABI bool readGCNO(GCOVBuffer &Buffer);
197 LLVM_ABI bool readGCDA(GCOVBuffer &Buffer);
199 LLVM_ABI void print(raw_ostream &OS) const;
200 LLVM_ABI void dump() const;
201
202 std::vector<std::string> filenames;
204
205public:
206 bool GCNOInitialized = false;
211 std::map<uint32_t, GCOVFunction *> identToFunction;
214
217 iterator begin() const { return iterator(functions.begin()); }
218 iterator end() const { return iterator(functions.end()); }
219
220private:
221 unsigned addNormalizedPathToMap(StringRef filename);
222};
223
224struct GCOVArc {
226 : src(src), dst(dst), flags(flags) {}
227 LLVM_ABI bool onTree() const;
228
234};
235
236/// GCOVFunction - Collects function information.
238public:
241
243
248
250 return make_range(blocks.begin(), blocks.end());
251 }
252
253 LLVM_ABI void propagateCounts(const GCOVBlock &v, GCOVArc *pred);
254 LLVM_ABI void print(raw_ostream &OS) const;
255 LLVM_ABI void dump() const;
256
268 unsigned srcIdx;
272};
273
274/// Represent file of lines same with block_location_info in gcc.
276 GCOVBlockLocation(unsigned idx) : srcIdx(idx) {}
277
278 unsigned srcIdx;
280};
281
282/// GCOVBlock - Collects block information.
284public:
289
291
293 locations.back().lines.push_back(N);
294 lastLine = N;
295 }
296 void addFile(unsigned fileIdx) { locations.emplace_back(fileIdx); }
297
298 uint32_t getLastLine() const { return lastLine; }
299 uint64_t getCount() const { return count; }
300
301 void addSrcEdge(GCOVArc *Edge) { pred.push_back(Edge); }
302
303 void addDstEdge(GCOVArc *Edge) { succ.push_back(Edge); }
304
306 return make_range(pred.begin(), pred.end());
307 }
308
310 return make_range(succ.begin(), succ.end());
311 }
312
313 LLVM_ABI void print(raw_ostream &OS) const;
314 LLVM_ABI void dump() const;
315
316 LLVM_ABI static uint64_t
318 std::vector<std::pair<GCOVBlock *, size_t>> &stack);
321
322public:
329 bool traversable = false;
330 GCOVArc *incoming = nullptr;
331};
332
333LLVM_ABI void gcovOneInput(const GCOV::Options &options, StringRef filename,
334 StringRef gcno, StringRef gcda, GCOVFile &file);
335
336} // end namespace llvm
337
338#endif // LLVM_PROFILEDATA_GCOV_H
This file defines the StringMap class.
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseSet and SmallDenseSet classes.
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
#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)"
std::pair< BasicBlock *, BasicBlock * > Edge
raw_pwrite_stream & OS
This file defines the SmallString class.
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:55
uint64_t tell() const
Return the current position of this Cursor.
Definition: DataExtractor.h:72
Error takeError()
Return error contained inside this Cursor, if any.
Definition: DataExtractor.h:79
LLVM_ABI 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:98
LLVM_ABI 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:263
GCOVBlock - Collects block information.
Definition: GCOV.h:283
void addLine(uint32_t N)
Definition: GCOV.h:292
iterator_range< EdgeIterator > dsts() const
Definition: GCOV.h:309
static LLVM_ABI uint64_t getCyclesCount(const BlockVector &blocks)
Definition: GCOV.cpp:526
iterator_range< EdgeIterator > srcs() const
Definition: GCOV.h:305
uint64_t getCount() const
Definition: GCOV.h:299
GCOVBlock(uint32_t N)
Definition: GCOV.h:290
void addDstEdge(GCOVArc *Edge)
Definition: GCOV.h:303
uint32_t getLastLine() const
Definition: GCOV.h:298
SmallVector< GCOVBlockLocation > locations
Definition: GCOV.h:327
uint32_t number
Definition: GCOV.h:323
void addSrcEdge(GCOVArc *Edge)
Definition: GCOV.h:301
SmallVectorImpl< GCOVArc * >::const_iterator EdgeIterator
Definition: GCOV.h:285
bool traversable
Definition: GCOV.h:329
LLVM_ABI void print(raw_ostream &OS) const
collectLineCounts - Collect line counts.
Definition: GCOV.cpp:442
uint32_t lastLine
Definition: GCOV.h:328
static LLVM_ABI uint64_t getLineCount(const BlockVector &Blocks)
SmallVector< const GCOVBlock *, 1 > BlockVector
Definition: GCOV.h:286
GCOVArc * incoming
Definition: GCOV.h:330
static LLVM_ABI uint64_t augmentOneCycle(GCOVBlock *src, std::vector< std::pair< GCOVBlock *, size_t > > &stack)
Definition: GCOV.cpp:475
SmallVector< GCOVArc *, 2 > pred
Definition: GCOV.h:325
LLVM_ABI void dump() const
dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Definition: GCOV.cpp:471
uint64_t count
Definition: GCOV.h:324
void addFile(unsigned fileIdx)
Definition: GCOV.h:296
SmallVector< GCOVArc *, 2 > succ
Definition: GCOV.h:326
GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific read operations.
Definition: GCOV.h:74
bool readInt(uint32_t &Val)
Definition: GCOV.h:153
DataExtractor::Cursor cursor
Definition: GCOV.h:183
bool readInt64(uint64_t &Val)
Definition: GCOV.h:163
GCOVBuffer(MemoryBuffer *B)
Definition: GCOV.h:76
bool readGCOVVersion(GCOV::GCOVVersion &version)
readGCOVVersion - Read GCOV version.
Definition: GCOV.h:109
bool readString(StringRef &str)
Definition: GCOV.h:171
DataExtractor de
Definition: GCOV.h:182
uint32_t getWord()
Definition: GCOV.h:145
bool readGCNOFormat()
readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
Definition: GCOV.h:80
StringRef getString()
Definition: GCOV.h:146
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
Definition: GCOV.h:95
GCOVFile - Collects coverage information for one pair of coverage file (.gcno and ....
Definition: GCOV.h:192
SmallVector< std::unique_ptr< GCOVFunction >, 16 > functions
Definition: GCOV.h:210
uint32_t checksum
Definition: GCOV.h:208
iterator end() const
Definition: GCOV.h:218
LLVM_ABI void print(raw_ostream &OS) const
Definition: GCOV.cpp:307
iterator begin() const
Definition: GCOV.h:217
uint32_t programCount
Definition: GCOV.h:213
uint32_t runCount
Definition: GCOV.h:212
GCOV::GCOVVersion version
Definition: GCOV.h:207
pointee_iterator< SmallVectorImpl< std::unique_ptr< GCOVFunction > >::const_iterator > iterator
Definition: GCOV.h:216
std::vector< std::string > filenames
Definition: GCOV.h:202
GCOV::GCOVVersion getVersion() const
Definition: GCOV.h:198
GCOVFile()=default
LLVM_ABI void dump() const
dump - Dump GCOVFile content to dbgs() for debugging purposes.
Definition: GCOV.cpp:314
std::map< uint32_t, GCOVFunction * > identToFunction
Definition: GCOV.h:211
StringRef cwd
Definition: GCOV.h:209
bool GCNOInitialized
Definition: GCOV.h:206
StringMap< unsigned > filenameToIdx
Definition: GCOV.h:203
LLVM_ABI bool readGCNO(GCOVBuffer &Buffer)
readGCNO - Read GCNO buffer.
Definition: GCOV.cpp:102
LLVM_ABI bool readGCDA(GCOVBuffer &Buffer)
readGCDA - Read GCDA buffer.
Definition: GCOV.cpp:210
GCOVFunction - Collects function information.
Definition: GCOV.h:237
LLVM_ABI uint64_t getEntryCount() const
getEntryCount - Get the number of times the function was called by retrieving the entry block's count...
Definition: GCOV.cpp:357
uint32_t endColumn
Definition: GCOV.h:264
SmallVector< std::unique_ptr< GCOVArc >, 0 > treeArcs
Definition: GCOV.h:270
LLVM_ABI StringRef getName(bool demangle) const
Definition: GCOV.cpp:335
uint32_t cfgChecksum
Definition: GCOV.h:260
GCOVFunction(GCOVFile &file)
Definition: GCOV.h:242
LLVM_ABI void dump() const
dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Definition: GCOV.cpp:433
StringRef Name
Definition: GCOV.h:266
uint8_t artificial
Definition: GCOV.h:265
LLVM_ABI void propagateCounts(const GCOVBlock &v, GCOVArc *pred)
Definition: GCOV.cpp:372
uint32_t endLine
Definition: GCOV.h:263
SmallVector< std::unique_ptr< GCOVBlock >, 0 > blocks
Definition: GCOV.h:269
LLVM_ABI GCOVBlock & getExitBlock() const
Definition: GCOV.cpp:361
LLVM_ABI StringRef getFilename() const
Definition: GCOV.cpp:353
DenseSet< const GCOVBlock * > visited
Definition: GCOV.h:271
uint32_t startLine
Definition: GCOV.h:261
SmallString< 0 > demangled
Definition: GCOV.h:267
LLVM_ABI void print(raw_ostream &OS) const
Definition: GCOV.cpp:424
uint32_t startColumn
Definition: GCOV.h:262
uint32_t linenoChecksum
Definition: GCOV.h:259
iterator_range< BlockIterator > blocksRange() const
Definition: GCOV.h:249
SmallVector< std::unique_ptr< GCOVArc >, 0 > arcs
Definition: GCOV.h:270
uint32_t ident
Definition: GCOV.h:258
unsigned srcIdx
Definition: GCOV.h:268
GCOVFile & file
Definition: GCOV.h:257
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
StringRef getBuffer() const
Definition: MemoryBuffer.h:71
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:574
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:579
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:710
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:581
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:626
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:53
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:43
@ V407
Definition: GCOV.h:43
@ V800
Definition: GCOV.h:43
@ V408
Definition: GCOV.h:43
@ V900
Definition: GCOV.h:43
@ V1200
Definition: GCOV.h:43
@ V304
Definition: GCOV.h:43
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI 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:1886
LLVM_ABI void gcovOneInput(const GCOV::Options &options, StringRef filename, StringRef gcno, StringRef gcda, GCOVFile &file)
Definition: GCOV.cpp:999
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1083
DEMANGLE_ABI std::string demangle(std::string_view MangledName)
Attempt to demangle a string using different demangling schemes.
Definition: Demangle.cpp:20
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
#define N
uint64_t count
Definition: GCOV.h:232
LLVM_ABI bool onTree() const
Definition: GCOV.cpp:330
GCOVBlock & src
Definition: GCOV.h:229
GCOVBlock & dst
Definition: GCOV.h:230
GCOVArc(GCOVBlock &src, GCOVBlock &dst, uint32_t flags)
Definition: GCOV.h:225
uint64_t cycleCount
Definition: GCOV.h:233
uint32_t flags
Definition: GCOV.h:231
Represent file of lines same with block_location_info in gcc.
Definition: GCOV.h:275
GCOVBlockLocation(unsigned idx)
Definition: GCOV.h:276
SmallVector< uint32_t, 4 > lines
Definition: GCOV.h:279
A struct for passing gcov options between functions.
Definition: GCOV.h:46
bool HashFilenames
Definition: GCOV.h:66
bool PreservePaths
Definition: GCOV.h:58
std::string SourcePrefix
Definition: GCOV.h:67
bool BranchInfo
Definition: GCOV.h:55
bool RelativeOnly
Definition: GCOV.h:64
bool FuncCoverage
Definition: GCOV.h:57
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:47
bool UncondBranch
Definition: GCOV.h:59
bool BranchCount
Definition: GCOV.h:56
bool LongFileNames
Definition: GCOV.h:61
bool UseStdout
Definition: GCOV.h:65
bool AllBlocks
Definition: GCOV.h:54
bool Intermediate
Definition: GCOV.h:60
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324