blob: 81b178181a62c63e8b52766c4036d468e15e0bbb [file] [log] [blame]
Ben Murdoch62ed6312017-06-06 11:06:27 +01001// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_DEBUG_DEBUG_COVERAGE_H_
6#define V8_DEBUG_DEBUG_COVERAGE_H_
7
Rubin Xu7bc1b612021-02-16 09:38:50 +00008#include <memory>
Ben Murdoch62ed6312017-06-06 11:06:27 +01009#include <vector>
10
11#include "src/debug/debug-interface.h"
Rubin Xu7bc1b612021-02-16 09:38:50 +000012#include "src/handles/handles.h"
13#include "src/objects/objects.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +010014
15namespace v8 {
16namespace internal {
17
18// Forward declaration.
19class Isolate;
20
Rubin Xu7bc1b612021-02-16 09:38:50 +000021struct CoverageBlock {
22 CoverageBlock(int s, int e, uint32_t c) : start(s), end(e), count(c) {}
23 CoverageBlock() : CoverageBlock(kNoSourcePosition, kNoSourcePosition, 0) {}
24
25 int start;
26 int end;
27 uint32_t count;
28};
29
Ben Murdoch62ed6312017-06-06 11:06:27 +010030struct CoverageFunction {
31 CoverageFunction(int s, int e, uint32_t c, Handle<String> n)
Rubin Xu7bc1b612021-02-16 09:38:50 +000032 : start(s), end(e), count(c), name(n), has_block_coverage(false) {}
33
34 bool HasNonEmptySourceRange() const { return start < end && start >= 0; }
35 bool HasBlocks() const { return !blocks.empty(); }
36
Ben Murdoch62ed6312017-06-06 11:06:27 +010037 int start;
38 int end;
39 uint32_t count;
40 Handle<String> name;
Rubin Xu7bc1b612021-02-16 09:38:50 +000041 // Blocks are sorted by start position, from outer to inner blocks.
42 std::vector<CoverageBlock> blocks;
43 bool has_block_coverage;
Ben Murdoch62ed6312017-06-06 11:06:27 +010044};
45
46struct CoverageScript {
47 // Initialize top-level function in case it has been garbage-collected.
Rubin Xu7bc1b612021-02-16 09:38:50 +000048 explicit CoverageScript(Handle<Script> s) : script(s) {}
Ben Murdoch62ed6312017-06-06 11:06:27 +010049 Handle<Script> script;
50 // Functions are sorted by start position, from outer to inner function.
51 std::vector<CoverageFunction> functions;
52};
53
54class Coverage : public std::vector<CoverageScript> {
55 public:
Rubin Xu7bc1b612021-02-16 09:38:50 +000056 // Collecting precise coverage only works if the modes kPreciseCount or
57 // kPreciseBinary is selected. The invocation count is reset on collection.
58 // In case of kPreciseCount, an updated count since last collection is
59 // returned. In case of kPreciseBinary, a count of 1 is returned if a
60 // function has been executed for the first time since last collection.
61 static std::unique_ptr<Coverage> CollectPrecise(Isolate* isolate);
62 // Collecting best effort coverage always works, but may be imprecise
63 // depending on selected mode. The invocation count is not reset.
64 static std::unique_ptr<Coverage> CollectBestEffort(Isolate* isolate);
Ben Murdoch62ed6312017-06-06 11:06:27 +010065
Rubin Xu7bc1b612021-02-16 09:38:50 +000066 // Select code coverage mode.
67 static void SelectMode(Isolate* isolate, debug::CoverageMode mode);
Ben Murdoch62ed6312017-06-06 11:06:27 +010068
69 private:
Rubin Xu7bc1b612021-02-16 09:38:50 +000070 static std::unique_ptr<Coverage> Collect(
71 Isolate* isolate, v8::debug::CoverageMode collectionMode);
72
73 Coverage() = default;
Ben Murdoch62ed6312017-06-06 11:06:27 +010074};
75
76} // namespace internal
77} // namespace v8
78
79#endif // V8_DEBUG_DEBUG_COVERAGE_H_