GCOV.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #ifndef LLVM_PROFILEDATA_GCOV_H
  14. #define LLVM_PROFILEDATA_GCOV_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/DenseSet.h"
  17. #include "llvm/ADT/MapVector.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/iterator.h"
  22. #include "llvm/ADT/iterator_range.h"
  23. #include "llvm/Support/DataExtractor.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <algorithm>
  27. #include <cassert>
  28. #include <cstddef>
  29. #include <cstdint>
  30. #include <limits>
  31. #include <map>
  32. #include <memory>
  33. #include <string>
  34. #include <utility>
  35. namespace llvm {
  36. class GCOVFunction;
  37. class GCOVBlock;
  38. namespace GCOV {
  39. enum GCOVVersion { V304, V407, V408, V800, V900 };
  40. /// A struct for passing gcov options between functions.
  41. struct Options {
  42. Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L,
  43. bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
  44. : AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
  45. PreservePaths(P), UncondBranch(U), Intermediate(I), LongFileNames(L),
  46. Demangle(M), NoOutput(N), RelativeOnly(R), UseStdout(T),
  47. HashFilenames(X), SourcePrefix(std::move(SourcePrefix)) {}
  48. bool AllBlocks;
  49. bool BranchInfo;
  50. bool BranchCount;
  51. bool FuncCoverage;
  52. bool PreservePaths;
  53. bool UncondBranch;
  54. bool Intermediate;
  55. bool LongFileNames;
  56. bool Demangle;
  57. bool NoOutput;
  58. bool RelativeOnly;
  59. bool UseStdout;
  60. bool HashFilenames;
  61. std::string SourcePrefix;
  62. };
  63. } // end namespace GCOV
  64. /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
  65. /// read operations.
  66. class GCOVBuffer {
  67. public:
  68. GCOVBuffer(MemoryBuffer *B) : Buffer(B) {}
  69. ~GCOVBuffer() { consumeError(cursor.takeError()); }
  70. /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
  71. bool readGCNOFormat() {
  72. StringRef buf = Buffer->getBuffer();
  73. StringRef magic = buf.substr(0, 4);
  74. if (magic == "gcno") {
  75. de = DataExtractor(buf.substr(4), false, 0);
  76. } else if (magic == "oncg") {
  77. de = DataExtractor(buf.substr(4), true, 0);
  78. } else {
  79. errs() << "unexpected magic: " << magic << "\n";
  80. return false;
  81. }
  82. return true;
  83. }
  84. /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
  85. bool readGCDAFormat() {
  86. StringRef buf = Buffer->getBuffer();
  87. StringRef magic = buf.substr(0, 4);
  88. if (magic == "gcda") {
  89. de = DataExtractor(buf.substr(4), false, 0);
  90. } else if (magic == "adcg") {
  91. de = DataExtractor(buf.substr(4), true, 0);
  92. } else {
  93. return false;
  94. }
  95. return true;
  96. }
  97. /// readGCOVVersion - Read GCOV version.
  98. bool readGCOVVersion(GCOV::GCOVVersion &Version) {
  99. std::string str(de.getBytes(cursor, 4));
  100. if (str.size() != 4)
  101. return false;
  102. if (de.isLittleEndian())
  103. std::reverse(str.begin(), str.end());
  104. int ver = str[0] >= 'A'
  105. ? (str[0] - 'A') * 100 + (str[1] - '0') * 10 + str[2] - '0'
  106. : (str[0] - '0') * 10 + str[2] - '0';
  107. if (ver >= 90) {
  108. // PR gcov-profile/84846, r269678
  109. Version = GCOV::V900;
  110. return true;
  111. } else if (ver >= 80) {
  112. // PR gcov-profile/48463
  113. Version = GCOV::V800;
  114. return true;
  115. } else if (ver >= 48) {
  116. // r189778: the exit block moved from the last to the second.
  117. Version = GCOV::V408;
  118. return true;
  119. } else if (ver >= 47) {
  120. // r173147: split checksum into cfg checksum and line checksum.
  121. Version = GCOV::V407;
  122. return true;
  123. } else if (ver >= 34) {
  124. Version = GCOV::V304;
  125. return true;
  126. }
  127. errs() << "unexpected version: " << str << "\n";
  128. return false;
  129. }
  130. uint32_t getWord() { return de.getU32(cursor); }
  131. StringRef getString() {
  132. uint32_t len;
  133. if (!readInt(len) || len == 0)
  134. return {};
  135. return de.getBytes(cursor, len * 4).split('\0').first;
  136. }
  137. bool readInt(uint32_t &Val) {
  138. if (cursor.tell() + 4 > de.size()) {
  139. Val = 0;
  140. errs() << "unexpected end of memory buffer: " << cursor.tell() << "\n";
  141. return false;
  142. }
  143. Val = de.getU32(cursor);
  144. return true;
  145. }
  146. bool readInt64(uint64_t &Val) {
  147. uint32_t Lo, Hi;
  148. if (!readInt(Lo) || !readInt(Hi))
  149. return false;
  150. Val = ((uint64_t)Hi << 32) | Lo;
  151. return true;
  152. }
  153. bool readString(StringRef &Str) {
  154. uint32_t len;
  155. if (!readInt(len) || len == 0)
  156. return false;
  157. Str = de.getBytes(cursor, len * 4).split('\0').first;
  158. return bool(cursor);
  159. }
  160. DataExtractor de{ArrayRef<uint8_t>{}, false, 0};
  161. DataExtractor::Cursor cursor{0};
  162. private:
  163. MemoryBuffer *Buffer;
  164. };
  165. /// GCOVFile - Collects coverage information for one pair of coverage file
  166. /// (.gcno and .gcda).
  167. class GCOVFile {
  168. public:
  169. GCOVFile() = default;
  170. bool readGCNO(GCOVBuffer &Buffer);
  171. bool readGCDA(GCOVBuffer &Buffer);
  172. GCOV::GCOVVersion getVersion() const { return Version; }
  173. void print(raw_ostream &OS) const;
  174. void dump() const;
  175. std::vector<std::string> filenames;
  176. StringMap<unsigned> filenameToIdx;
  177. public:
  178. bool GCNOInitialized = false;
  179. GCOV::GCOVVersion Version;
  180. uint32_t Checksum = 0;
  181. StringRef cwd;
  182. SmallVector<std::unique_ptr<GCOVFunction>, 16> functions;
  183. std::map<uint32_t, GCOVFunction *> IdentToFunction;
  184. uint32_t RunCount = 0;
  185. uint32_t ProgramCount = 0;
  186. using iterator = pointee_iterator<
  187. SmallVectorImpl<std::unique_ptr<GCOVFunction>>::const_iterator>;
  188. iterator begin() const { return iterator(functions.begin()); }
  189. iterator end() const { return iterator(functions.end()); }
  190. };
  191. struct GCOVArc {
  192. GCOVArc(GCOVBlock &src, GCOVBlock &dst, uint32_t flags)
  193. : src(src), dst(dst), flags(flags) {}
  194. bool onTree() const;
  195. GCOVBlock &src;
  196. GCOVBlock &dst;
  197. uint32_t flags;
  198. uint64_t count = 0;
  199. uint64_t cycleCount = 0;
  200. };
  201. /// GCOVFunction - Collects function information.
  202. class GCOVFunction {
  203. public:
  204. using BlockIterator = pointee_iterator<
  205. SmallVectorImpl<std::unique_ptr<GCOVBlock>>::const_iterator>;
  206. GCOVFunction(GCOVFile &file) : file(file) {}
  207. StringRef getName(bool demangle) const;
  208. StringRef getFilename() const;
  209. uint64_t getEntryCount() const;
  210. GCOVBlock &getExitBlock() const;
  211. iterator_range<BlockIterator> blocksRange() const {
  212. return make_range(blocks.begin(), blocks.end());
  213. }
  214. uint64_t propagateCounts(const GCOVBlock &v, GCOVArc *pred);
  215. void print(raw_ostream &OS) const;
  216. void dump() const;
  217. GCOVFile &file;
  218. uint32_t ident = 0;
  219. uint32_t linenoChecksum;
  220. uint32_t cfgChecksum = 0;
  221. uint32_t startLine = 0;
  222. uint32_t startColumn = 0;
  223. uint32_t endLine = 0;
  224. uint32_t endColumn = 0;
  225. uint8_t artificial = 0;
  226. StringRef Name;
  227. mutable SmallString<0> demangled;
  228. unsigned srcIdx;
  229. SmallVector<std::unique_ptr<GCOVBlock>, 0> blocks;
  230. SmallVector<std::unique_ptr<GCOVArc>, 0> arcs, treeArcs;
  231. DenseSet<const GCOVBlock *> visited;
  232. };
  233. /// GCOVBlock - Collects block information.
  234. class GCOVBlock {
  235. public:
  236. using EdgeIterator = SmallVectorImpl<GCOVArc *>::const_iterator;
  237. using BlockVector = SmallVector<const GCOVBlock *, 1>;
  238. using BlockVectorLists = SmallVector<BlockVector, 4>;
  239. using Edges = SmallVector<GCOVArc *, 4>;
  240. GCOVBlock(uint32_t N) : number(N) {}
  241. void addLine(uint32_t N) { lines.push_back(N); }
  242. uint32_t getLastLine() const { return lines.back(); }
  243. uint64_t getCount() const { return count; }
  244. void addSrcEdge(GCOVArc *Edge) { pred.push_back(Edge); }
  245. void addDstEdge(GCOVArc *Edge) { succ.push_back(Edge); }
  246. iterator_range<EdgeIterator> srcs() const {
  247. return make_range(pred.begin(), pred.end());
  248. }
  249. iterator_range<EdgeIterator> dsts() const {
  250. return make_range(succ.begin(), succ.end());
  251. }
  252. void print(raw_ostream &OS) const;
  253. void dump() const;
  254. static uint64_t
  255. augmentOneCycle(GCOVBlock *src,
  256. std::vector<std::pair<GCOVBlock *, size_t>> &stack);
  257. static uint64_t getCyclesCount(const BlockVector &blocks);
  258. static uint64_t getLineCount(const BlockVector &Blocks);
  259. public:
  260. uint32_t number;
  261. uint64_t count = 0;
  262. SmallVector<GCOVArc *, 2> pred;
  263. SmallVector<GCOVArc *, 2> succ;
  264. SmallVector<uint32_t, 4> lines;
  265. bool traversable = false;
  266. GCOVArc *incoming = nullptr;
  267. };
  268. void gcovOneInput(const GCOV::Options &options, StringRef filename,
  269. StringRef gcno, StringRef gcda, GCOVFile &file);
  270. } // end namespace llvm
  271. #endif // LLVM_PROFILEDATA_GCOV_H