11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_DEBUG_DEBUG_COVERAGE_H_
61cb0ef41Sopenharmony_ci#define V8_DEBUG_DEBUG_COVERAGE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "src/debug/debug-interface.h"
121cb0ef41Sopenharmony_ci#include "src/handles/handles.h"
131cb0ef41Sopenharmony_ci#include "src/objects/objects.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cinamespace v8 {
161cb0ef41Sopenharmony_cinamespace internal {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// Forward declaration.
191cb0ef41Sopenharmony_ciclass Isolate;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cistruct CoverageBlock {
221cb0ef41Sopenharmony_ci  CoverageBlock(int s, int e, uint32_t c) : start(s), end(e), count(c) {}
231cb0ef41Sopenharmony_ci  CoverageBlock() : CoverageBlock(kNoSourcePosition, kNoSourcePosition, 0) {}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  int start;
261cb0ef41Sopenharmony_ci  int end;
271cb0ef41Sopenharmony_ci  uint32_t count;
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cistruct CoverageFunction {
311cb0ef41Sopenharmony_ci  CoverageFunction(int s, int e, uint32_t c, Handle<String> n)
321cb0ef41Sopenharmony_ci      : start(s), end(e), count(c), name(n), has_block_coverage(false) {}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  bool HasNonEmptySourceRange() const { return start < end && start >= 0; }
351cb0ef41Sopenharmony_ci  bool HasBlocks() const { return !blocks.empty(); }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  int start;
381cb0ef41Sopenharmony_ci  int end;
391cb0ef41Sopenharmony_ci  uint32_t count;
401cb0ef41Sopenharmony_ci  Handle<String> name;
411cb0ef41Sopenharmony_ci  // Blocks are sorted by start position, from outer to inner blocks.
421cb0ef41Sopenharmony_ci  std::vector<CoverageBlock> blocks;
431cb0ef41Sopenharmony_ci  bool has_block_coverage;
441cb0ef41Sopenharmony_ci};
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cistruct CoverageScript {
471cb0ef41Sopenharmony_ci  // Initialize top-level function in case it has been garbage-collected.
481cb0ef41Sopenharmony_ci  explicit CoverageScript(Handle<Script> s) : script(s) {}
491cb0ef41Sopenharmony_ci  Handle<Script> script;
501cb0ef41Sopenharmony_ci  // Functions are sorted by start position, from outer to inner function.
511cb0ef41Sopenharmony_ci  std::vector<CoverageFunction> functions;
521cb0ef41Sopenharmony_ci};
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciclass Coverage : public std::vector<CoverageScript> {
551cb0ef41Sopenharmony_ci public:
561cb0ef41Sopenharmony_ci  // Collecting precise coverage only works if the modes kPreciseCount or
571cb0ef41Sopenharmony_ci  // kPreciseBinary is selected. The invocation count is reset on collection.
581cb0ef41Sopenharmony_ci  // In case of kPreciseCount, an updated count since last collection is
591cb0ef41Sopenharmony_ci  // returned. In case of kPreciseBinary, a count of 1 is returned if a
601cb0ef41Sopenharmony_ci  // function has been executed for the first time since last collection.
611cb0ef41Sopenharmony_ci  static std::unique_ptr<Coverage> CollectPrecise(Isolate* isolate);
621cb0ef41Sopenharmony_ci  // Collecting best effort coverage always works, but may be imprecise
631cb0ef41Sopenharmony_ci  // depending on selected mode. The invocation count is not reset.
641cb0ef41Sopenharmony_ci  static std::unique_ptr<Coverage> CollectBestEffort(Isolate* isolate);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  // Select code coverage mode.
671cb0ef41Sopenharmony_ci  static void SelectMode(Isolate* isolate, debug::CoverageMode mode);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci private:
701cb0ef41Sopenharmony_ci  static std::unique_ptr<Coverage> Collect(
711cb0ef41Sopenharmony_ci      Isolate* isolate, v8::debug::CoverageMode collectionMode);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  Coverage() = default;
741cb0ef41Sopenharmony_ci};
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci}  // namespace internal
771cb0ef41Sopenharmony_ci}  // namespace v8
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci#endif  // V8_DEBUG_DEBUG_COVERAGE_H_
80