Home | History | Annotate | Download | only in debug
      1 // 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 
      8 #include <vector>
      9 
     10 #include "src/debug/debug-interface.h"
     11 #include "src/objects.h"
     12 
     13 namespace v8 {
     14 namespace internal {
     15 
     16 // Forward declaration.
     17 class Isolate;
     18 
     19 struct CoverageBlock {
     20   CoverageBlock(int s, int e, uint32_t c) : start(s), end(e), count(c) {}
     21   CoverageBlock() : CoverageBlock(kNoSourcePosition, kNoSourcePosition, 0) {}
     22   int start;
     23   int end;
     24   uint32_t count;
     25 };
     26 
     27 struct CoverageFunction {
     28   CoverageFunction(int s, int e, uint32_t c, Handle<String> n)
     29       : start(s), end(e), count(c), name(n), has_block_coverage(false) {}
     30   int start;
     31   int end;
     32   uint32_t count;
     33   Handle<String> name;
     34   // Blocks are sorted by start position, from outer to inner blocks.
     35   std::vector<CoverageBlock> blocks;
     36   bool has_block_coverage;
     37 };
     38 
     39 struct CoverageScript {
     40   // Initialize top-level function in case it has been garbage-collected.
     41   explicit CoverageScript(Handle<Script> s) : script(s) {}
     42   Handle<Script> script;
     43   // Functions are sorted by start position, from outer to inner function.
     44   std::vector<CoverageFunction> functions;
     45 };
     46 
     47 class Coverage : public std::vector<CoverageScript> {
     48  public:
     49   // Collecting precise coverage only works if the modes kPreciseCount or
     50   // kPreciseBinary is selected. The invocation count is reset on collection.
     51   // In case of kPreciseCount, an updated count since last collection is
     52   // returned. In case of kPreciseBinary, a count of 1 is returned if a
     53   // function has been executed for the first time since last collection.
     54   static std::unique_ptr<Coverage> CollectPrecise(Isolate* isolate);
     55   // Collecting best effort coverage always works, but may be imprecise
     56   // depending on selected mode. The invocation count is not reset.
     57   static std::unique_ptr<Coverage> CollectBestEffort(Isolate* isolate);
     58 
     59   // Select code coverage mode.
     60   static void SelectMode(Isolate* isolate, debug::Coverage::Mode mode);
     61 
     62  private:
     63   static std::unique_ptr<Coverage> Collect(
     64       Isolate* isolate, v8::debug::Coverage::Mode collectionMode);
     65 
     66   Coverage() {}
     67 };
     68 
     69 }  // namespace internal
     70 }  // namespace v8
     71 
     72 #endif  // V8_DEBUG_DEBUG_COVERAGE_H_
     73