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 17616 : inline void Add(SharedFunctionInfo key, uint32_t count) {
26 17616 : Entry* entry = LookupOrInsert(key, Hash(key), []() { return 0; });
27 17616 : uint32_t old_count = entry->value;
28 17616 : if (UINT32_MAX - count < old_count) {
29 0 : entry->value = UINT32_MAX;
30 : } else {
31 17616 : entry->value = old_count + count;
32 : }
33 17616 : }
34 :
35 : inline uint32_t Get(SharedFunctionInfo key) {
36 32814 : Entry* entry = Lookup(key, Hash(key));
37 32814 : if (entry == nullptr) return 0;
38 17399 : return entry->value;
39 : }
40 :
41 : private:
42 : static uint32_t Hash(SharedFunctionInfo key) {
43 50430 : return static_cast<uint32_t>(key.ptr());
44 : }
45 :
46 : DisallowHeapAllocation no_gc;
47 : };
48 :
49 : namespace {
50 230530 : int StartPosition(SharedFunctionInfo info) {
51 230530 : int start = info->function_token_position();
52 230530 : if (start == kNoSourcePosition) start = info->StartPosition();
53 230530 : return start;
54 : }
55 :
56 98858 : bool CompareSharedFunctionInfo(SharedFunctionInfo a, SharedFunctionInfo b) {
57 98858 : int a_start = StartPosition(a);
58 98858 : int b_start = StartPosition(b);
59 98858 : if (a_start == b_start) return a->EndPosition() > b->EndPosition();
60 95808 : return a_start < b_start;
61 : }
62 :
63 171165 : bool CompareCoverageBlock(const CoverageBlock& a, const CoverageBlock& b) {
64 : DCHECK_NE(kNoSourcePosition, a.start);
65 : DCHECK_NE(kNoSourcePosition, b.start);
66 171165 : if (a.start == b.start) return a.end > b.end;
67 170525 : return a.start < b.start;
68 : }
69 :
70 : void SortBlockData(std::vector<CoverageBlock>& v) {
71 : // Sort according to the block nesting structure.
72 22475 : std::sort(v.begin(), v.end(), CompareCoverageBlock);
73 : }
74 :
75 15010 : std::vector<CoverageBlock> GetSortedBlockData(SharedFunctionInfo shared) {
76 : DCHECK(shared->HasCoverageInfo());
77 :
78 : CoverageInfo coverage_info =
79 30020 : CoverageInfo::cast(shared->GetDebugInfo()->coverage_info());
80 :
81 : std::vector<CoverageBlock> result;
82 15010 : if (coverage_info->SlotCount() == 0) return result;
83 :
84 54595 : for (int i = 0; i < coverage_info->SlotCount(); i++) {
85 54595 : const int start_pos = coverage_info->StartSourcePosition(i);
86 54595 : const int until_pos = coverage_info->EndSourcePosition(i);
87 54595 : const int count = coverage_info->BlockCount(i);
88 :
89 : DCHECK_NE(kNoSourcePosition, start_pos);
90 54595 : 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 240250 : write_index_(-1) {
111 : DCHECK(std::is_sorted(function_->blocks.begin(), function_->blocks.end(),
112 : CompareCoverageBlock));
113 : }
114 :
115 120125 : ~CoverageBlockIterator() {
116 120125 : Finalize();
117 : DCHECK(std::is_sorted(function_->blocks.begin(), function_->blocks.end(),
118 : CompareCoverageBlock));
119 120125 : }
120 :
121 : bool HasNext() const {
122 1154850 : return read_index_ + 1 < static_cast<int>(function_->blocks.size());
123 : }
124 :
125 839700 : bool Next() {
126 468645 : if (!HasNext()) {
127 240330 : if (!ended_) MaybeWriteCurrent();
128 240330 : ended_ = true;
129 240330 : 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 228315 : MaybeWriteCurrent();
135 :
136 228315 : if (read_index_ == -1) {
137 : // Initialize the nesting stack with the function range.
138 : nesting_stack_.emplace_back(function_->start, function_->end,
139 410065 : function_->count);
140 186340 : } else if (!delete_current_) {
141 142740 : nesting_stack_.emplace_back(GetBlock());
142 : }
143 :
144 228315 : delete_current_ = false;
145 228315 : read_index_++;
146 :
147 : DCHECK(IsActive());
148 :
149 : CoverageBlock& block = GetBlock();
150 771120 : while (nesting_stack_.size() > 1 &&
151 174715 : 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 584440 : return function_->blocks[read_index_];
166 : }
167 :
168 : CoverageBlock& GetNextBlock() {
169 : DCHECK(IsActive());
170 : DCHECK(HasNext());
171 149215 : return function_->blocks[read_index_ + 1];
172 : }
173 :
174 : CoverageBlock& GetPreviousBlock() {
175 : DCHECK(IsActive());
176 : DCHECK_GT(read_index_, 0);
177 47130 : return function_->blocks[read_index_ - 1];
178 : }
179 :
180 : CoverageBlock& GetParent() {
181 : DCHECK(IsActive());
182 : return nesting_stack_.back();
183 : }
184 :
185 82735 : bool HasSiblingOrChild() {
186 : DCHECK(IsActive());
187 221335 : 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 6955 : bool IsTopLevel() const { return nesting_stack_.size() == 1; }
199 :
200 : void DeleteBlock() {
201 : DCHECK(!delete_current_);
202 : DCHECK(IsActive());
203 50335 : delete_current_ = true;
204 : }
205 :
206 : private:
207 348440 : void MaybeWriteCurrent() {
208 696880 : if (delete_current_) return;
209 298105 : if (read_index_ >= 0 && write_index_ != read_index_) {
210 67740 : function_->blocks[write_index_] = function_->blocks[read_index_];
211 : }
212 298105 : write_index_++;
213 : }
214 :
215 120125 : void Finalize() {
216 120125 : while (Next()) {
217 : // Just iterate to the end.
218 : }
219 120125 : function_->blocks.resize(write_index_);
220 120125 : }
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 18580 : return lhs.start == rhs.start && lhs.end == rhs.end;
234 : }
235 :
236 15010 : void MergeDuplicateRanges(CoverageFunction* function) {
237 : CoverageBlockIterator iter(function);
238 :
239 59635 : while (iter.Next() && iter.HasNext()) {
240 18580 : CoverageBlock& block = iter.GetBlock();
241 : CoverageBlock& next_block = iter.GetNextBlock();
242 :
243 18580 : 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 15010 : }
249 15010 : }
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 15010 : void RewritePositionSingletonsToRanges(CoverageFunction* function) {
256 : CoverageBlockIterator iter(function);
257 :
258 68965 : while (iter.Next()) {
259 53955 : CoverageBlock& block = iter.GetBlock();
260 : CoverageBlock& parent = iter.GetParent();
261 :
262 53955 : if (block.start >= function->end) {
263 : DCHECK_EQ(block.end, kNoSourcePosition);
264 : iter.DeleteBlock();
265 53955 : } 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 23610 : if (iter.HasSiblingOrChild()) {
269 16655 : block.end = iter.GetSiblingOrChild().start;
270 6955 : } 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 4405 : block.end = parent.end - 1;
275 : } else {
276 2550 : block.end = parent.end;
277 : }
278 : }
279 15010 : }
280 15010 : }
281 :
282 30020 : void MergeConsecutiveRanges(CoverageFunction* function) {
283 : CoverageBlockIterator iter(function);
284 :
285 89145 : while (iter.Next()) {
286 59125 : CoverageBlock& block = iter.GetBlock();
287 :
288 59125 : if (iter.HasSiblingOrChild()) {
289 : CoverageBlock& sibling = iter.GetSiblingOrChild();
290 44680 : 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 28820 : sibling.start = block.start;
294 : iter.DeleteBlock();
295 : }
296 : }
297 30020 : }
298 30020 : }
299 :
300 15010 : void MergeNestedRanges(CoverageFunction* function) {
301 : CoverageBlockIterator iter(function);
302 :
303 41055 : while (iter.Next()) {
304 26045 : CoverageBlock& block = iter.GetBlock();
305 : CoverageBlock& parent = iter.GetParent();
306 :
307 26045 : 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 15010 : }
313 15010 : }
314 :
315 15010 : void FilterAliasedSingletons(CoverageFunction* function) {
316 : CoverageBlockIterator iter(function);
317 :
318 15010 : iter.Next(); // Advance once since we reference the previous block later.
319 :
320 77150 : while (iter.Next()) {
321 47130 : CoverageBlock& previous_block = iter.GetPreviousBlock();
322 : CoverageBlock& block = iter.GetBlock();
323 :
324 47130 : bool is_singleton = block.end == kNoSourcePosition;
325 47130 : bool aliases_start = block.start == previous_block.start;
326 :
327 47130 : 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 15010 : }
337 15010 : }
338 :
339 15010 : void FilterUncoveredRanges(CoverageFunction* function) {
340 : CoverageBlockIterator iter(function);
341 :
342 19270 : while (iter.Next()) {
343 4260 : CoverageBlock& block = iter.GetBlock();
344 : CoverageBlock& parent = iter.GetParent();
345 4260 : if (block.count == 0 && parent.count == 0) iter.DeleteBlock();
346 15010 : }
347 15010 : }
348 :
349 15010 : void FilterEmptyRanges(CoverageFunction* function) {
350 : CoverageBlockIterator iter(function);
351 :
352 19270 : while (iter.Next()) {
353 4260 : CoverageBlock& block = iter.GetBlock();
354 4260 : if (block.start == block.end) iter.DeleteBlock();
355 15010 : }
356 15010 : }
357 :
358 45 : void ClampToBinary(CoverageFunction* function) {
359 : CoverageBlockIterator iter(function);
360 :
361 75 : while (iter.Next()) {
362 30 : CoverageBlock& block = iter.GetBlock();
363 30 : if (block.count > 0) block.count = 1;
364 45 : }
365 45 : }
366 :
367 15010 : void ResetAllBlockCounts(SharedFunctionInfo shared) {
368 : DCHECK(shared->HasCoverageInfo());
369 :
370 : CoverageInfo coverage_info =
371 30020 : CoverageInfo::cast(shared->GetDebugInfo()->coverage_info());
372 :
373 69605 : for (int i = 0; i < coverage_info->SlotCount(); i++) {
374 54595 : coverage_info->ResetBlockCount(i);
375 : }
376 15010 : }
377 :
378 : bool IsBlockMode(debug::Coverage::Mode mode) {
379 32814 : 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 1090889 : switch (mode) {
390 : case debug::Coverage::kBlockBinary:
391 : case debug::Coverage::kPreciseBinary:
392 : return true;
393 : default:
394 : return false;
395 : }
396 : }
397 :
398 15010 : void CollectBlockCoverage(CoverageFunction* function, SharedFunctionInfo info,
399 : debug::Coverage::Mode mode) {
400 : DCHECK(IsBlockMode(mode));
401 :
402 15010 : function->has_block_coverage = true;
403 30020 : function->blocks = GetSortedBlockData(info);
404 :
405 : // If in binary mode, only report counts of 0/1.
406 15010 : 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 15010 : FilterAliasedSingletons(function);
416 :
417 : // Rewrite all singletons (created e.g. by continuations and unconditional
418 : // control flow) to ranges.
419 15010 : 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 15010 : MergeConsecutiveRanges(function);
425 :
426 : SortBlockData(function->blocks);
427 15010 : MergeDuplicateRanges(function);
428 15010 : MergeNestedRanges(function);
429 :
430 15010 : MergeConsecutiveRanges(function);
431 :
432 : // Filter out ranges with count == 0 unless the immediate parent range has
433 : // a count != 0.
434 15010 : FilterUncoveredRanges(function);
435 :
436 : // Filter out ranges of zero length.
437 15010 : FilterEmptyRanges(function);
438 :
439 : // Reset all counters on the DebugInfo to zero.
440 15010 : ResetAllBlockCounts(info);
441 15010 : }
442 : } // anonymous namespace
443 :
444 445 : std::unique_ptr<Coverage> Coverage::CollectPrecise(Isolate* isolate) {
445 : DCHECK(!isolate->is_best_effort_code_coverage());
446 : std::unique_ptr<Coverage> result =
447 445 : Collect(isolate, isolate->code_coverage_mode());
448 1335 : if (!isolate->is_collecting_type_profile() &&
449 430 : (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 60 : isolate->SetFeedbackVectorsForProfilingTools(*ArrayList::New(isolate, 0));
454 : }
455 445 : return result;
456 : }
457 :
458 90 : std::unique_ptr<Coverage> Coverage::CollectBestEffort(Isolate* isolate) {
459 90 : return Collect(isolate, v8::debug::Coverage::kBestEffort);
460 : }
461 :
462 535 : std::unique_ptr<Coverage> Coverage::Collect(
463 535 : Isolate* isolate, v8::debug::Coverage::Mode collectionMode) {
464 : SharedToCounterMap counter_map;
465 :
466 : const bool reset_count = collectionMode != v8::debug::Coverage::kBestEffort;
467 :
468 535 : 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 485 : isolate->factory()->feedback_vectors_for_profiling_tools());
479 35442 : for (int i = 0; i < list->Length(); i++) {
480 34472 : FeedbackVector vector = FeedbackVector::cast(list->Get(i));
481 17236 : SharedFunctionInfo shared = vector->shared_function_info();
482 : DCHECK(shared->IsSubjectToDebugging());
483 17236 : uint32_t count = static_cast<uint32_t>(vector->invocation_count());
484 17236 : if (reset_count) vector->clear_invocation_count();
485 17236 : 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 50 : HeapIterator heap_iterator(isolate->heap());
495 780064 : for (HeapObject current_obj = heap_iterator.next();
496 : !current_obj.is_null(); current_obj = heap_iterator.next()) {
497 779584 : if (!current_obj->IsFeedbackVector()) continue;
498 380 : FeedbackVector vector = FeedbackVector::cast(current_obj);
499 380 : SharedFunctionInfo shared = vector->shared_function_info();
500 380 : if (!shared->IsSubjectToDebugging()) continue;
501 380 : uint32_t count = static_cast<uint32_t>(vector->invocation_count());
502 380 : counter_map.Add(shared, count);
503 : }
504 50 : break;
505 : }
506 : }
507 :
508 : // Iterate shared function infos of every script and build a mapping
509 : // between source ranges and invocation counts.
510 535 : std::unique_ptr<Coverage> result(new Coverage());
511 535 : Script::Iterator scripts(isolate);
512 17890 : for (Script script = scripts.Next(); !script.is_null();
513 : script = scripts.Next()) {
514 10015 : if (!script->IsUserJavaScript()) continue;
515 :
516 : // Create and add new script data.
517 : Handle<Script> script_handle(script, isolate);
518 6805 : result->emplace_back(script_handle);
519 11471 : 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 6805 : SharedFunctionInfo::ScriptIterator infos(isolate, *script_handle);
526 79238 : for (SharedFunctionInfo info = infos.Next(); !info.is_null();
527 : info = infos.Next()) {
528 32814 : sorted.push_back(info);
529 : }
530 6805 : 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 46424 : for (SharedFunctionInfo info : sorted) {
538 32814 : int start = StartPosition(info);
539 32814 : int end = info->EndPosition();
540 : uint32_t count = counter_map.Get(info);
541 : // Find the correct outer function based on start position.
542 74824 : while (!nesting.empty() && functions->at(nesting.back()).end <= start) {
543 : nesting.pop_back();
544 : }
545 32814 : if (count != 0) {
546 3206 : 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 60 : count = info->has_reported_binary_coverage() ? 0 : 1;
553 60 : info->set_has_reported_binary_coverage(true);
554 60 : break;
555 : case v8::debug::Coverage::kBestEffort:
556 : count = 1;
557 526 : break;
558 : }
559 : }
560 :
561 32814 : Handle<String> name(info->DebugName(), isolate);
562 : CoverageFunction function(start, end, count, name);
563 :
564 32814 : if (IsBlockMode(collectionMode) && info->HasCoverageInfo()) {
565 15010 : 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 32814 : bool is_covered = (count != 0);
571 : bool parent_is_covered =
572 35170 : (!nesting.empty() && functions->at(nesting.back()).count != 0);
573 : bool has_block_coverage = !function.blocks.empty();
574 32814 : if (is_covered || parent_is_covered || has_block_coverage) {
575 9332 : nesting.push_back(functions->size());
576 4666 : functions->emplace_back(function);
577 : }
578 : }
579 :
580 : // Remove entries for scripts that have no coverage.
581 6805 : if (functions->empty()) result->pop_back();
582 : }
583 535 : return result;
584 : }
585 :
586 714 : void Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) {
587 424 : 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 290 : isolate->debug()->RemoveAllCoverageInfos();
594 290 : if (!isolate->is_collecting_type_profile()) {
595 : isolate->SetFeedbackVectorsForProfilingTools(
596 280 : 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 134 : Deoptimizer::DeoptimizeAll(isolate);
608 :
609 : // Root all feedback vectors to avoid early collection.
610 134 : isolate->MaybeInitializeVectorListFromHeap();
611 :
612 268 : HeapIterator heap_iterator(isolate->heap());
613 2182046 : for (HeapObject o = heap_iterator.next(); !o.is_null();
614 : o = heap_iterator.next()) {
615 1298489 : 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 20160 : SharedFunctionInfo shared = SharedFunctionInfo::cast(o);
620 20160 : shared->set_has_reported_binary_coverage(false);
621 1070729 : } 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 424 : }
632 :
633 : } // namespace internal
634 183867 : } // namespace v8
|