GCOV.h 9.2 KB

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