LCOV - code coverage report
Current view: top level - src/debug - debug-coverage.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 219 221 99.1 %
Date: 2019-02-19 Functions: 26 26 100.0 %

          Line data    Source code
       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             : #include "src/debug/debug-coverage.h"
       6             : 
       7             : #include "src/ast/ast.h"
       8             : #include "src/base/hashmap.h"
       9             : #include "src/debug/debug.h"
      10             : #include "src/deoptimizer.h"
      11             : #include "src/frames-inl.h"
      12             : #include "src/isolate.h"
      13             : #include "src/objects.h"
      14             : #include "src/objects/debug-objects-inl.h"
      15             : 
      16             : namespace v8 {
      17             : namespace internal {
      18             : 
      19             : class SharedToCounterMap
      20             :     : public base::TemplateHashMapImpl<SharedFunctionInfo, uint32_t,
      21             :                                        base::KeyEqualityMatcher<Object>,
      22             :                                        base::DefaultAllocationPolicy> {
      23             :  public:
      24             :   typedef base::TemplateHashMapEntry<SharedFunctionInfo, uint32_t> Entry;
      25       15063 :   inline void Add(SharedFunctionInfo key, uint32_t count) {
      26       15063 :     Entry* entry = LookupOrInsert(key, Hash(key), []() { return 0; });
      27       15063 :     uint32_t old_count = entry->value;
      28       15063 :     if (UINT32_MAX - count < old_count) {
      29           0 :       entry->value = UINT32_MAX;
      30             :     } else {
      31       15063 :       entry->value = old_count + count;
      32             :     }
      33       15063 :   }
      34             : 
      35             :   inline uint32_t Get(SharedFunctionInfo key) {
      36       28100 :     Entry* entry = Lookup(key, Hash(key));
      37       28100 :     if (entry == nullptr) return 0;
      38       14584 :     return entry->value;
      39             :   }
      40             : 
      41             :  private:
      42             :   static uint32_t Hash(SharedFunctionInfo key) {
      43       43163 :     return static_cast<uint32_t>(key.ptr());
      44             :   }
      45             : 
      46             :   DisallowHeapAllocation no_gc;
      47             : };
      48             : 
      49             : namespace {
      50      200306 : int StartPosition(SharedFunctionInfo info) {
      51      200306 :   int start = info->function_token_position();
      52      200306 :   if (start == kNoSourcePosition) start = info->StartPosition();
      53      200306 :   return start;
      54             : }
      55             : 
      56       86103 : bool CompareSharedFunctionInfo(SharedFunctionInfo a, SharedFunctionInfo b) {
      57       86103 :   int a_start = StartPosition(a);
      58       86103 :   int b_start = StartPosition(b);
      59       86103 :   if (a_start == b_start) return a->EndPosition() > b->EndPosition();
      60       83723 :   return a_start < b_start;
      61             : }
      62             : 
      63      142132 : bool CompareCoverageBlock(const CoverageBlock& a, const CoverageBlock& b) {
      64             :   DCHECK_NE(kNoSourcePosition, a.start);
      65             :   DCHECK_NE(kNoSourcePosition, b.start);
      66      142132 :   if (a.start == b.start) return a.end > b.end;
      67      141568 :   return a.start < b.start;
      68             : }
      69             : 
      70             : void SortBlockData(std::vector<CoverageBlock>& v) {
      71             :   // Sort according to the block nesting structure.
      72       18696 :   std::sort(v.begin(), v.end(), CompareCoverageBlock);
      73             : }
      74             : 
      75       12456 : std::vector<CoverageBlock> GetSortedBlockData(SharedFunctionInfo shared) {
      76             :   DCHECK(shared->HasCoverageInfo());
      77             : 
      78             :   CoverageInfo coverage_info =
      79       24912 :       CoverageInfo::cast(shared->GetDebugInfo()->coverage_info());
      80             : 
      81             :   std::vector<CoverageBlock> result;
      82       12456 :   if (coverage_info->SlotCount() == 0) return result;
      83             : 
      84       45380 :   for (int i = 0; i < coverage_info->SlotCount(); i++) {
      85       45380 :     const int start_pos = coverage_info->StartSourcePosition(i);
      86       45380 :     const int until_pos = coverage_info->EndSourcePosition(i);
      87       45380 :     const int count = coverage_info->BlockCount(i);
      88             : 
      89             :     DCHECK_NE(kNoSourcePosition, start_pos);
      90       45380 :     result.emplace_back(start_pos, until_pos, count);
      91             :   }
      92             : 
      93             :   SortBlockData(result);
      94             : 
      95             :   return result;
      96             : }
      97             : 
      98             : // A utility class to simplify logic for performing passes over block coverage
      99             : // ranges. Provides access to the implicit tree structure of ranges (i.e. access
     100             : // to parent and sibling blocks), and supports efficient in-place editing and
     101             : // deletion. The underlying backing store is the array of CoverageBlocks stored
     102             : // on the CoverageFunction.
     103             : class CoverageBlockIterator final {
     104             :  public:
     105             :   explicit CoverageBlockIterator(CoverageFunction* function)
     106             :       : function_(function),
     107             :         ended_(false),
     108             :         delete_current_(false),
     109             :         read_index_(-1),
     110      199368 :         write_index_(-1) {
     111             :     DCHECK(std::is_sorted(function_->blocks.begin(), function_->blocks.end(),
     112             :                           CompareCoverageBlock));
     113             :   }
     114             : 
     115       99684 :   ~CoverageBlockIterator() {
     116       99684 :     Finalize();
     117             :     DCHECK(std::is_sorted(function_->blocks.begin(), function_->blocks.end(),
     118             :                           CompareCoverageBlock));
     119       99684 :   }
     120             : 
     121             :   bool HasNext() const {
     122      958544 :     return read_index_ + 1 < static_cast<int>(function_->blocks.size());
     123             :   }
     124             : 
     125      697108 :   bool Next() {
     126      389000 :     if (!HasNext()) {
     127      199344 :       if (!ended_) MaybeWriteCurrent();
     128      199344 :       ended_ = true;
     129      199344 :       return false;
     130             :     }
     131             : 
     132             :     // If a block has been deleted, subsequent iteration moves trailing blocks
     133             :     // to their updated position within the array.
     134      189656 :     MaybeWriteCurrent();
     135             : 
     136      189656 :     if (read_index_ == -1) {
     137             :       // Initialize the nesting stack with the function range.
     138             :       nesting_stack_.emplace_back(function_->start, function_->end,
     139      340548 :                                   function_->count);
     140      154688 :     } else if (!delete_current_) {
     141      118452 :       nesting_stack_.emplace_back(GetBlock());
     142             :     }
     143             : 
     144      189656 :     delete_current_ = false;
     145      189656 :     read_index_++;
     146             : 
     147             :     DCHECK(IsActive());
     148             : 
     149             :     CoverageBlock& block = GetBlock();
     150      640144 :     while (nesting_stack_.size() > 1 &&
     151      144908 :            nesting_stack_.back().end <= block.start) {
     152             :       nesting_stack_.pop_back();
     153             :     }
     154             : 
     155             :     DCHECK_IMPLIES(block.start >= function_->end,
     156             :                    block.end == kNoSourcePosition);
     157             :     DCHECK_NE(block.start, kNoSourcePosition);
     158             :     DCHECK_LE(block.end, GetParent().end);
     159             : 
     160             :     return true;
     161             :   }
     162             : 
     163             :   CoverageBlock& GetBlock() {
     164             :     DCHECK(IsActive());
     165      485284 :     return function_->blocks[read_index_];
     166             :   }
     167             : 
     168             :   CoverageBlock& GetNextBlock() {
     169             :     DCHECK(IsActive());
     170             :     DCHECK(HasNext());
     171      123660 :     return function_->blocks[read_index_ + 1];
     172             :   }
     173             : 
     174             :   CoverageBlock& GetPreviousBlock() {
     175             :     DCHECK(IsActive());
     176             :     DCHECK_GT(read_index_, 0);
     177       39140 :     return function_->blocks[read_index_ - 1];
     178             :   }
     179             : 
     180             :   CoverageBlock& GetParent() {
     181             :     DCHECK(IsActive());
     182             :     return nesting_stack_.back();
     183             :   }
     184             : 
     185       68516 :   bool HasSiblingOrChild() {
     186             :     DCHECK(IsActive());
     187      183236 :     return HasNext() && GetNextBlock().start < GetParent().end;
     188             :   }
     189             : 
     190             :   CoverageBlock& GetSiblingOrChild() {
     191             :     DCHECK(HasSiblingOrChild());
     192             :     DCHECK(IsActive());
     193             :     return GetNextBlock();
     194             :   }
     195             : 
     196             :   // A range is considered to be at top level if its parent range is the
     197             :   // function range.
     198        5744 :   bool IsTopLevel() const { return nesting_stack_.size() == 1; }
     199             : 
     200             :   void DeleteBlock() {
     201             :     DCHECK(!delete_current_);
     202             :     DCHECK(IsActive());
     203       41924 :     delete_current_ = true;
     204             :   }
     205             : 
     206             :  private:
     207      289340 :   void MaybeWriteCurrent() {
     208      578680 :     if (delete_current_) return;
     209      247416 :     if (read_index_ >= 0 && write_index_ != read_index_) {
     210       55680 :       function_->blocks[write_index_] = function_->blocks[read_index_];
     211             :     }
     212      247416 :     write_index_++;
     213             :   }
     214             : 
     215       99684 :   void Finalize() {
     216       99684 :     while (Next()) {
     217             :       // Just iterate to the end.
     218             :     }
     219       99684 :     function_->blocks.resize(write_index_);
     220       99684 :   }
     221             : 
     222             :   bool IsActive() const { return read_index_ >= 0 && !ended_; }
     223             : 
     224             :   CoverageFunction* function_;
     225             :   std::vector<CoverageBlock> nesting_stack_;
     226             :   bool ended_;
     227             :   bool delete_current_;
     228             :   int read_index_;
     229             :   int write_index_;
     230             : };
     231             : 
     232             : bool HaveSameSourceRange(const CoverageBlock& lhs, const CoverageBlock& rhs) {
     233       15516 :   return lhs.start == rhs.start && lhs.end == rhs.end;
     234             : }
     235             : 
     236       12456 : void MergeDuplicateRanges(CoverageFunction* function) {
     237             :   CoverageBlockIterator iter(function);
     238             : 
     239       49728 :   while (iter.Next() && iter.HasNext()) {
     240       15516 :     CoverageBlock& block = iter.GetBlock();
     241             :     CoverageBlock& next_block = iter.GetNextBlock();
     242             : 
     243       15516 :     if (!HaveSameSourceRange(block, next_block)) continue;
     244             : 
     245             :     DCHECK_NE(kNoSourcePosition, block.end);  // Non-singleton range.
     246           0 :     next_block.count = std::max(block.count, next_block.count);
     247             :     iter.DeleteBlock();
     248       12456 :   }
     249       12456 : }
     250             : 
     251             : // Rewrite position singletons (produced by unconditional control flow
     252             : // like return statements, and by continuation counters) into source
     253             : // ranges that end at the next sibling range or the end of the parent
     254             : // range, whichever comes first.
     255       12456 : void RewritePositionSingletonsToRanges(CoverageFunction* function) {
     256             :   CoverageBlockIterator iter(function);
     257             : 
     258       57272 :   while (iter.Next()) {
     259       44816 :     CoverageBlock& block = iter.GetBlock();
     260             :     CoverageBlock& parent = iter.GetParent();
     261             : 
     262       44816 :     if (block.start >= function->end) {
     263             :       DCHECK_EQ(block.end, kNoSourcePosition);
     264             :       iter.DeleteBlock();
     265       44816 :     } else if (block.end == kNoSourcePosition) {
     266             :       // The current block ends at the next sibling block (if it exists) or the
     267             :       // end of the parent block otherwise.
     268       19504 :       if (iter.HasSiblingOrChild()) {
     269       13760 :         block.end = iter.GetSiblingOrChild().start;
     270        5744 :       } else if (iter.IsTopLevel()) {
     271             :         // See https://crbug.com/v8/6661. Functions are special-cased because
     272             :         // we never want the closing brace to be uncovered. This is mainly to
     273             :         // avoid a noisy UI.
     274        3644 :         block.end = parent.end - 1;
     275             :       } else {
     276        2100 :         block.end = parent.end;
     277             :       }
     278             :     }
     279       12456 :   }
     280       12456 : }
     281             : 
     282       24912 : void MergeConsecutiveRanges(CoverageFunction* function) {
     283             :   CoverageBlockIterator iter(function);
     284             : 
     285       73924 :   while (iter.Next()) {
     286       49012 :     CoverageBlock& block = iter.GetBlock();
     287             : 
     288       49012 :     if (iter.HasSiblingOrChild()) {
     289             :       CoverageBlock& sibling = iter.GetSiblingOrChild();
     290       37024 :       if (sibling.start == block.end && sibling.count == block.count) {
     291             :         // Best-effort: this pass may miss mergeable siblings in the presence of
     292             :         // child blocks.
     293       23800 :         sibling.start = block.start;
     294             :         iter.DeleteBlock();
     295             :       }
     296             :     }
     297       24912 :   }
     298       24912 : }
     299             : 
     300       12456 : void MergeNestedRanges(CoverageFunction* function) {
     301             :   CoverageBlockIterator iter(function);
     302             : 
     303       34212 :   while (iter.Next()) {
     304       21756 :     CoverageBlock& block = iter.GetBlock();
     305             :     CoverageBlock& parent = iter.GetParent();
     306             : 
     307       21756 :     if (parent.count == block.count) {
     308             :       // Transformation may not be valid if sibling blocks exist with a
     309             :       // differing count.
     310             :       iter.DeleteBlock();
     311             :     }
     312       12456 :   }
     313       12456 : }
     314             : 
     315       12456 : void FilterAliasedSingletons(CoverageFunction* function) {
     316             :   CoverageBlockIterator iter(function);
     317             : 
     318       12456 :   iter.Next();  // Advance once since we reference the previous block later.
     319             : 
     320       64052 :   while (iter.Next()) {
     321       39140 :     CoverageBlock& previous_block = iter.GetPreviousBlock();
     322             :     CoverageBlock& block = iter.GetBlock();
     323             : 
     324       39140 :     bool is_singleton = block.end == kNoSourcePosition;
     325       39140 :     bool aliases_start = block.start == previous_block.start;
     326             : 
     327       39140 :     if (is_singleton && aliases_start) {
     328             :       // The previous block must have a full range since duplicate singletons
     329             :       // have already been merged.
     330             :       DCHECK_NE(previous_block.end, kNoSourcePosition);
     331             :       // Likewise, the next block must have another start position since
     332             :       // singletons are sorted to the end.
     333             :       DCHECK_IMPLIES(iter.HasNext(), iter.GetNextBlock().start != block.start);
     334             :       iter.DeleteBlock();
     335             :     }
     336       12456 :   }
     337       12456 : }
     338             : 
     339       12456 : void FilterUncoveredRanges(CoverageFunction* function) {
     340             :   CoverageBlockIterator iter(function);
     341             : 
     342       15912 :   while (iter.Next()) {
     343        3456 :     CoverageBlock& block = iter.GetBlock();
     344             :     CoverageBlock& parent = iter.GetParent();
     345        3456 :     if (block.count == 0 && parent.count == 0) iter.DeleteBlock();
     346       12456 :   }
     347       12456 : }
     348             : 
     349       12456 : void FilterEmptyRanges(CoverageFunction* function) {
     350             :   CoverageBlockIterator iter(function);
     351             : 
     352       15912 :   while (iter.Next()) {
     353        3456 :     CoverageBlock& block = iter.GetBlock();
     354        3456 :     if (block.start == block.end) iter.DeleteBlock();
     355       12456 :   }
     356       12456 : }
     357             : 
     358          36 : void ClampToBinary(CoverageFunction* function) {
     359             :   CoverageBlockIterator iter(function);
     360             : 
     361          60 :   while (iter.Next()) {
     362          24 :     CoverageBlock& block = iter.GetBlock();
     363          24 :     if (block.count > 0) block.count = 1;
     364          36 :   }
     365          36 : }
     366             : 
     367       12456 : void ResetAllBlockCounts(SharedFunctionInfo shared) {
     368             :   DCHECK(shared->HasCoverageInfo());
     369             : 
     370             :   CoverageInfo coverage_info =
     371       24912 :       CoverageInfo::cast(shared->GetDebugInfo()->coverage_info());
     372             : 
     373       57836 :   for (int i = 0; i < coverage_info->SlotCount(); i++) {
     374       45380 :     coverage_info->ResetBlockCount(i);
     375             :   }
     376       12456 : }
     377             : 
     378             : bool IsBlockMode(debug::Coverage::Mode mode) {
     379       28100 :   switch (mode) {
     380             :     case debug::Coverage::kBlockBinary:
     381             :     case debug::Coverage::kBlockCount:
     382             :       return true;
     383             :     default:
     384             :       return false;
     385             :   }
     386             : }
     387             : 
     388             : bool IsBinaryMode(debug::Coverage::Mode mode) {
     389      943477 :   switch (mode) {
     390             :     case debug::Coverage::kBlockBinary:
     391             :     case debug::Coverage::kPreciseBinary:
     392             :       return true;
     393             :     default:
     394             :       return false;
     395             :   }
     396             : }
     397             : 
     398       12456 : void CollectBlockCoverage(CoverageFunction* function, SharedFunctionInfo info,
     399             :                           debug::Coverage::Mode mode) {
     400             :   DCHECK(IsBlockMode(mode));
     401             : 
     402       12456 :   function->has_block_coverage = true;
     403       24912 :   function->blocks = GetSortedBlockData(info);
     404             : 
     405             :   // If in binary mode, only report counts of 0/1.
     406       12456 :   if (mode == debug::Coverage::kBlockBinary) ClampToBinary(function);
     407             : 
     408             :   // Remove singleton ranges with the same start position as a full range and
     409             :   // throw away their counts.
     410             :   // Singleton ranges are only intended to split existing full ranges and should
     411             :   // never expand into a full range. Consider 'if (cond) { ... } else { ... }'
     412             :   // as a problematic example; if the then-block produces a continuation
     413             :   // singleton, it would incorrectly expand into the else range.
     414             :   // For more context, see https://crbug.com/v8/8237.
     415       12456 :   FilterAliasedSingletons(function);
     416             : 
     417             :   // Rewrite all singletons (created e.g. by continuations and unconditional
     418             :   // control flow) to ranges.
     419       12456 :   RewritePositionSingletonsToRanges(function);
     420             : 
     421             :   // Merge nested and consecutive ranges with identical counts.
     422             :   // Note that it's necessary to merge duplicate ranges prior to merging nested
     423             :   // changes in order to avoid invalid transformations. See crbug.com/827530.
     424       12456 :   MergeConsecutiveRanges(function);
     425             : 
     426             :   SortBlockData(function->blocks);
     427       12456 :   MergeDuplicateRanges(function);
     428       12456 :   MergeNestedRanges(function);
     429             : 
     430       12456 :   MergeConsecutiveRanges(function);
     431             : 
     432             :   // Filter out ranges with count == 0 unless the immediate parent range has
     433             :   // a count != 0.
     434       12456 :   FilterUncoveredRanges(function);
     435             : 
     436             :   // Filter out ranges of zero length.
     437       12456 :   FilterEmptyRanges(function);
     438             : 
     439             :   // Reset all counters on the DebugInfo to zero.
     440       12456 :   ResetAllBlockCounts(info);
     441       12456 : }
     442             : }  // anonymous namespace
     443             : 
     444         367 : std::unique_ptr<Coverage> Coverage::CollectPrecise(Isolate* isolate) {
     445             :   DCHECK(!isolate->is_best_effort_code_coverage());
     446             :   std::unique_ptr<Coverage> result =
     447         367 :       Collect(isolate, isolate->code_coverage_mode());
     448        1101 :   if (!isolate->is_collecting_type_profile() &&
     449         355 :       (isolate->is_precise_binary_code_coverage() ||
     450             :        isolate->is_block_binary_code_coverage())) {
     451             :     // We do not have to hold onto feedback vectors for invocations we already
     452             :     // reported. So we can reset the list.
     453          48 :     isolate->SetFeedbackVectorsForProfilingTools(*ArrayList::New(isolate, 0));
     454             :   }
     455         367 :   return result;
     456             : }
     457             : 
     458          76 : std::unique_ptr<Coverage> Coverage::CollectBestEffort(Isolate* isolate) {
     459          76 :   return Collect(isolate, v8::debug::Coverage::kBestEffort);
     460             : }
     461             : 
     462         443 : std::unique_ptr<Coverage> Coverage::Collect(
     463         443 :     Isolate* isolate, v8::debug::Coverage::Mode collectionMode) {
     464             :   SharedToCounterMap counter_map;
     465             : 
     466             :   const bool reset_count = collectionMode != v8::debug::Coverage::kBestEffort;
     467             : 
     468         443 :   switch (isolate->code_coverage_mode()) {
     469             :     case v8::debug::Coverage::kBlockBinary:
     470             :     case v8::debug::Coverage::kBlockCount:
     471             :     case v8::debug::Coverage::kPreciseBinary:
     472             :     case v8::debug::Coverage::kPreciseCount: {
     473             :       // Feedback vectors are already listed to prevent losing them to GC.
     474             :       DCHECK(isolate->factory()
     475             :                  ->feedback_vectors_for_profiling_tools()
     476             :                  ->IsArrayList());
     477             :       Handle<ArrayList> list = Handle<ArrayList>::cast(
     478         399 :           isolate->factory()->feedback_vectors_for_profiling_tools());
     479       30136 :       for (int i = 0; i < list->Length(); i++) {
     480       29338 :         FeedbackVector vector = FeedbackVector::cast(list->Get(i));
     481       14669 :         SharedFunctionInfo shared = vector->shared_function_info();
     482             :         DCHECK(shared->IsSubjectToDebugging());
     483       14669 :         uint32_t count = static_cast<uint32_t>(vector->invocation_count());
     484       14669 :         if (reset_count) vector->clear_invocation_count();
     485       14669 :         counter_map.Add(shared, count);
     486             :       }
     487             :       break;
     488             :     }
     489             :     case v8::debug::Coverage::kBestEffort: {
     490             :       DCHECK(!isolate->factory()
     491             :                   ->feedback_vectors_for_profiling_tools()
     492             :                   ->IsArrayList());
     493             :       DCHECK_EQ(v8::debug::Coverage::kBestEffort, collectionMode);
     494          44 :       HeapIterator heap_iterator(isolate->heap());
     495      713584 :       for (HeapObject current_obj = heap_iterator.next();
     496             :            !current_obj.is_null(); current_obj = heap_iterator.next()) {
     497      713102 :         if (!current_obj->IsFeedbackVector()) continue;
     498         394 :         FeedbackVector vector = FeedbackVector::cast(current_obj);
     499         394 :         SharedFunctionInfo shared = vector->shared_function_info();
     500         394 :         if (!shared->IsSubjectToDebugging()) continue;
     501         394 :         uint32_t count = static_cast<uint32_t>(vector->invocation_count());
     502         394 :         counter_map.Add(shared, count);
     503             :       }
     504          44 :       break;
     505             :     }
     506             :   }
     507             : 
     508             :   // Iterate shared function infos of every script and build a mapping
     509             :   // between source ranges and invocation counts.
     510         443 :   std::unique_ptr<Coverage> result(new Coverage());
     511         443 :   Script::Iterator scripts(isolate);
     512       14934 :   for (Script script = scripts.Next(); !script.is_null();
     513             :        script = scripts.Next()) {
     514        8353 :     if (!script->IsUserJavaScript()) continue;
     515             : 
     516             :     // Create and add new script data.
     517             :     Handle<Script> script_handle(script, isolate);
     518        5695 :     result->emplace_back(script_handle);
     519        9840 :     std::vector<CoverageFunction>* functions = &result->back().functions;
     520             : 
     521             :     std::vector<SharedFunctionInfo> sorted;
     522             : 
     523             :     {
     524             :       // Sort functions by start position, from outer to inner functions.
     525        5695 :       SharedFunctionInfo::ScriptIterator infos(isolate, *script_handle);
     526       67590 :       for (SharedFunctionInfo info = infos.Next(); !info.is_null();
     527             :            info = infos.Next()) {
     528       28100 :         sorted.push_back(info);
     529             :       }
     530        5695 :       std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo);
     531             :     }
     532             : 
     533             :     // Stack to track nested functions, referring function by index.
     534             :     std::vector<size_t> nesting;
     535             : 
     536             :     // Use sorted list to reconstruct function nesting.
     537       39490 :     for (SharedFunctionInfo info : sorted) {
     538       28100 :       int start = StartPosition(info);
     539       28100 :       int end = info->EndPosition();
     540             :       uint32_t count = counter_map.Get(info);
     541             :       // Find the correct outer function based on start position.
     542       64506 :       while (!nesting.empty() && functions->at(nesting.back()).end <= start) {
     543             :         nesting.pop_back();
     544             :       }
     545       28100 :       if (count != 0) {
     546        2719 :         switch (collectionMode) {
     547             :           case v8::debug::Coverage::kBlockCount:
     548             :           case v8::debug::Coverage::kPreciseCount:
     549             :             break;
     550             :           case v8::debug::Coverage::kBlockBinary:
     551             :           case v8::debug::Coverage::kPreciseBinary:
     552          48 :             count = info->has_reported_binary_coverage() ? 0 : 1;
     553          48 :             info->set_has_reported_binary_coverage(true);
     554          48 :             break;
     555             :           case v8::debug::Coverage::kBestEffort:
     556             :             count = 1;
     557         505 :             break;
     558             :         }
     559             :       }
     560             : 
     561       28100 :       Handle<String> name(info->DebugName(), isolate);
     562             :       CoverageFunction function(start, end, count, name);
     563             : 
     564       28100 :       if (IsBlockMode(collectionMode) && info->HasCoverageInfo()) {
     565       12456 :         CollectBlockCoverage(&function, info, collectionMode);
     566             :       }
     567             : 
     568             :       // Only include a function range if itself or its parent function is
     569             :       // covered, or if it contains non-trivial block coverage.
     570       28100 :       bool is_covered = (count != 0);
     571             :       bool parent_is_covered =
     572       30306 :           (!nesting.empty() && functions->at(nesting.back()).count != 0);
     573             :       bool has_block_coverage = !function.blocks.empty();
     574       28100 :       if (is_covered || parent_is_covered || has_block_coverage) {
     575        8290 :         nesting.push_back(functions->size());
     576        4145 :         functions->emplace_back(function);
     577             :       }
     578             :     }
     579             : 
     580             :     // Remove entries for scripts that have no coverage.
     581        5695 :     if (functions->empty()) result->pop_back();
     582             :   }
     583         443 :   return result;
     584             : }
     585             : 
     586         588 : void Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) {
     587         350 :   switch (mode) {
     588             :     case debug::Coverage::kBestEffort:
     589             :       // Note that DevTools switches back to best-effort coverage once the
     590             :       // recording is stopped. Since we delete coverage infos at that point, any
     591             :       // following coverage recording (without reloads) will be at function
     592             :       // granularity.
     593         238 :       isolate->debug()->RemoveAllCoverageInfos();
     594         238 :       if (!isolate->is_collecting_type_profile()) {
     595             :         isolate->SetFeedbackVectorsForProfilingTools(
     596         230 :             ReadOnlyRoots(isolate).undefined_value());
     597             :       }
     598             :       break;
     599             :     case debug::Coverage::kBlockBinary:
     600             :     case debug::Coverage::kBlockCount:
     601             :     case debug::Coverage::kPreciseBinary:
     602             :     case debug::Coverage::kPreciseCount: {
     603             :       HandleScope scope(isolate);
     604             : 
     605             :       // Remove all optimized function. Optimized and inlined functions do not
     606             :       // increment invocation count.
     607         112 :       Deoptimizer::DeoptimizeAll(isolate);
     608             : 
     609             :       // Root all feedback vectors to avoid early collection.
     610         112 :       isolate->MaybeInitializeVectorListFromHeap();
     611             : 
     612         224 :       HeapIterator heap_iterator(isolate->heap());
     613     1887178 :       for (HeapObject o = heap_iterator.next(); !o.is_null();
     614             :            o = heap_iterator.next()) {
     615     1110493 :         if (IsBinaryMode(mode) && o->IsSharedFunctionInfo()) {
     616             :           // If collecting binary coverage, reset
     617             :           // SFI::has_reported_binary_coverage to avoid optimizing / inlining
     618             :           // functions before they have reported coverage.
     619       16104 :           SharedFunctionInfo shared = SharedFunctionInfo::cast(o);
     620       16104 :           shared->set_has_reported_binary_coverage(false);
     621      927373 :         } else if (o->IsFeedbackVector()) {
     622             :           // In any case, clear any collected invocation counts.
     623             :           FeedbackVector::cast(o)->clear_invocation_count();
     624             :         }
     625             :       }
     626             : 
     627             :       break;
     628             :     }
     629             :   }
     630             :   isolate->set_code_coverage_mode(mode);
     631         350 : }
     632             : 
     633             : }  // namespace internal
     634      178779 : }  // namespace v8

Generated by: LCOV version 1.10