debug-coverage.h 2.47 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_DEBUG_DEBUG_COVERAGE_H_
#define V8_DEBUG_DEBUG_COVERAGE_H_

8
#include <memory>
9 10
#include <vector>

11
#include "src/debug/debug-interface.h"
12
#include "src/handles/handles.h"
13
#include "src/objects/objects.h"
14 15 16 17 18 19 20

namespace v8 {
namespace internal {

// Forward declaration.
class Isolate;

21 22
struct CoverageBlock {
  CoverageBlock(int s, int e, uint32_t c) : start(s), end(e), count(c) {}
23
  CoverageBlock() : CoverageBlock(kNoSourcePosition, kNoSourcePosition, 0) {}
24

25 26 27 28 29
  int start;
  int end;
  uint32_t count;
};

30 31
struct CoverageFunction {
  CoverageFunction(int s, int e, uint32_t c, Handle<String> n)
32
      : start(s), end(e), count(c), name(n), has_block_coverage(false) {}
33 34 35 36

  bool HasNonEmptySourceRange() const { return start < end && start >= 0; }
  bool HasBlocks() const { return !blocks.empty(); }

37 38 39 40
  int start;
  int end;
  uint32_t count;
  Handle<String> name;
41 42
  // Blocks are sorted by start position, from outer to inner blocks.
  std::vector<CoverageBlock> blocks;
43
  bool has_block_coverage;
44 45 46 47
};

struct CoverageScript {
  // Initialize top-level function in case it has been garbage-collected.
48
  explicit CoverageScript(Handle<Script> s) : script(s) {}
49
  Handle<Script> script;
50 51
  // Functions are sorted by start position, from outer to inner function.
  std::vector<CoverageFunction> functions;
52 53 54
};

class Coverage : public std::vector<CoverageScript> {
55
 public:
56 57 58 59 60
  // Collecting precise coverage only works if the modes kPreciseCount or
  // kPreciseBinary is selected. The invocation count is reset on collection.
  // In case of kPreciseCount, an updated count since last collection is
  // returned. In case of kPreciseBinary, a count of 1 is returned if a
  // function has been executed for the first time since last collection.
61
  static std::unique_ptr<Coverage> CollectPrecise(Isolate* isolate);
62 63
  // Collecting best effort coverage always works, but may be imprecise
  // depending on selected mode. The invocation count is not reset.
64
  static std::unique_ptr<Coverage> CollectBestEffort(Isolate* isolate);
65

66
  // Select code coverage mode.
67
  static void SelectMode(Isolate* isolate, debug::CoverageMode mode);
68 69

 private:
70
  static std::unique_ptr<Coverage> Collect(
71
      Isolate* isolate, v8::debug::CoverageMode collectionMode);
72

73
  Coverage() = default;
74 75 76 77 78 79
};

}  // namespace internal
}  // namespace v8

#endif  // V8_DEBUG_DEBUG_COVERAGE_H_