OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/debug/debug-coverage.h" | 5 #include "src/debug/debug-coverage.h" |
6 | 6 |
7 #include "src/base/hashmap.h" | 7 #include "src/base/hashmap.h" |
8 #include "src/deoptimizer.h" | 8 #include "src/deoptimizer.h" |
9 #include "src/frames-inl.h" | 9 #include "src/frames-inl.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 // Iterate shared function infos of every script and build a mapping | 115 // Iterate shared function infos of every script and build a mapping |
116 // between source ranges and invocation counts. | 116 // between source ranges and invocation counts. |
117 Coverage* result = new Coverage(); | 117 Coverage* result = new Coverage(); |
118 Script::Iterator scripts(isolate); | 118 Script::Iterator scripts(isolate); |
119 while (Script* script = scripts.Next()) { | 119 while (Script* script = scripts.Next()) { |
120 if (!script->IsUserJavaScript()) continue; | 120 if (!script->IsUserJavaScript()) continue; |
121 | 121 |
122 // Create and add new script data. | 122 // Create and add new script data. |
123 Handle<Script> script_handle(script, isolate); | 123 Handle<Script> script_handle(script, isolate); |
124 result->emplace_back(script_handle); | 124 result->emplace_back(isolate, script_handle); |
125 std::vector<CoverageFunction>* functions = &result->back().functions; | 125 std::vector<CoverageFunction>* functions = &result->back().functions; |
126 | 126 |
127 std::vector<SharedFunctionInfo*> sorted; | 127 std::vector<SharedFunctionInfo*> sorted; |
128 | 128 |
129 { | 129 { |
130 // Sort functions by start position, from outer to inner functions. | 130 // Sort functions by start position, from outer to inner functions. |
131 SharedFunctionInfo::ScriptIterator infos(script_handle); | 131 SharedFunctionInfo::ScriptIterator infos(script_handle); |
132 while (SharedFunctionInfo* info = infos.Next()) { | 132 while (SharedFunctionInfo* info = infos.Next()) { |
133 sorted.push_back(info); | 133 sorted.push_back(info); |
134 } | 134 } |
135 std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo); | 135 std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo); |
136 } | 136 } |
137 | 137 |
138 // Stack to track nested functions, referring function by index. | |
139 std::vector<size_t> nesting; | |
140 | |
141 // Use sorted list to reconstruct function nesting. | 138 // Use sorted list to reconstruct function nesting. |
142 for (SharedFunctionInfo* info : sorted) { | 139 for (SharedFunctionInfo* info : sorted) { |
143 int start = StartPosition(info); | 140 int start = StartPosition(info); |
144 int end = info->end_position(); | 141 int end = info->end_position(); |
145 uint32_t count = counter_map.Get(info); | 142 uint32_t count = counter_map.Get(info); |
146 // Find the correct outer function based on start position. | |
147 while (!nesting.empty() && functions->at(nesting.back()).end <= start) { | |
148 nesting.pop_back(); | |
149 } | |
150 if (count != 0) { | 143 if (count != 0) { |
151 switch (collectionMode) { | 144 switch (collectionMode) { |
152 case v8::debug::Coverage::kPreciseCount: | 145 case v8::debug::Coverage::kPreciseCount: |
153 break; | 146 break; |
154 case v8::debug::Coverage::kPreciseBinary: | 147 case v8::debug::Coverage::kPreciseBinary: |
155 count = info->has_reported_binary_coverage() ? 0 : 1; | 148 count = info->has_reported_binary_coverage() ? 0 : 1; |
156 info->set_has_reported_binary_coverage(true); | 149 info->set_has_reported_binary_coverage(true); |
157 break; | 150 break; |
158 case v8::debug::Coverage::kBestEffort: | 151 case v8::debug::Coverage::kBestEffort: |
159 count = 1; | 152 count = 1; |
160 break; | 153 break; |
161 } | 154 } |
162 } else if (nesting.empty() || functions->at(nesting.back()).count == 0) { | |
163 // Only include a function range if it has a non-0 count, or | |
164 // if it is directly nested inside a function with non-0 count. | |
165 continue; | |
166 } | 155 } |
167 Handle<String> name(info->DebugName(), isolate); | 156 Handle<String> name(info->DebugName(), isolate); |
168 nesting.push_back(functions->size()); | |
169 functions->emplace_back(start, end, count, name); | 157 functions->emplace_back(start, end, count, name); |
170 } | 158 } |
171 | |
172 // Remove entries for scripts that have no coverage. | |
173 if (functions->empty()) result->pop_back(); | |
174 } | 159 } |
175 return result; | 160 return result; |
176 } | 161 } |
177 | 162 |
178 void Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) { | 163 void Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) { |
179 switch (mode) { | 164 switch (mode) { |
180 case debug::Coverage::kBestEffort: | 165 case debug::Coverage::kBestEffort: |
181 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); | 166 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); |
182 break; | 167 break; |
183 case debug::Coverage::kPreciseBinary: | 168 case debug::Coverage::kPreciseBinary: |
(...skipping 25 matching lines...) Expand all Loading... |
209 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); | 194 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); |
210 isolate->SetCodeCoverageList(*list); | 195 isolate->SetCodeCoverageList(*list); |
211 break; | 196 break; |
212 } | 197 } |
213 } | 198 } |
214 isolate->set_code_coverage_mode(mode); | 199 isolate->set_code_coverage_mode(mode); |
215 } | 200 } |
216 | 201 |
217 } // namespace internal | 202 } // namespace internal |
218 } // namespace v8 | 203 } // namespace v8 |
OLD | NEW |