GCOV.h 9.0 KB

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