| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/coverage.h" | 5 #include "vm/coverage.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 | 8 |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/isolate.h" | 10 #include "vm/isolate.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 (*map)[tkit.CurrentPosition()] = cur_line; | 46 (*map)[tkit.CurrentPosition()] = cur_line; |
| 47 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { | 47 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { |
| 48 cur_line++; | 48 cur_line++; |
| 49 } | 49 } |
| 50 tkit.Advance(); | 50 tkit.Advance(); |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 | 54 |
| 55 void CodeCoverage::CompileAndAdd(const Function& function, | 55 void CodeCoverage::CompileAndAdd(const Function& function, |
| 56 const JSONArray& hits_arr, | 56 const JSONArray& hits_or_sites, |
| 57 const GrowableArray<intptr_t>& pos_to_line) { | 57 const GrowableArray<intptr_t>& pos_to_line, |
| 58 bool as_call_sites) { |
| 58 Isolate* isolate = Isolate::Current(); | 59 Isolate* isolate = Isolate::Current(); |
| 59 if (!function.HasCode()) { | 60 if (!function.HasCode()) { |
| 60 // If the function should not be compiled or if the compilation failed, | 61 // If the function should not be compiled or if the compilation failed, |
| 61 // then just skip this method. | 62 // then just skip this method. |
| 62 // TODO(iposva): Maybe we should skip synthesized methods in general too. | 63 // TODO(iposva): Maybe we should skip synthesized methods in general too. |
| 63 if (function.is_abstract() || function.IsRedirectingFactory()) { | 64 if (function.is_abstract() || function.IsRedirectingFactory()) { |
| 64 return; | 65 return; |
| 65 } | 66 } |
| 66 if (function.IsNonImplicitClosureFunction() && | 67 if (function.IsNonImplicitClosureFunction() && |
| 67 (function.context_scope() == ContextScope::null())) { | 68 (function.context_scope() == ContextScope::null())) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 #if defined(DEBUG) | 107 #if defined(DEBUG) |
| 107 const Script& script = Script::Handle(function.script()); | 108 const Script& script = Script::Handle(function.script()); |
| 108 intptr_t test_line = -1; | 109 intptr_t test_line = -1; |
| 109 script.GetTokenLocation(token_pos, &test_line, NULL); | 110 script.GetTokenLocation(token_pos, &test_line, NULL); |
| 110 ASSERT(test_line == line); | 111 ASSERT(test_line == line); |
| 111 #endif | 112 #endif |
| 112 // Merge hit data where possible. | 113 // Merge hit data where possible. |
| 113 if (last_line == line) { | 114 if (last_line == line) { |
| 114 last_count += ic_data->AggregateCount(); | 115 last_count += ic_data->AggregateCount(); |
| 115 } else { | 116 } else { |
| 116 if (last_line != -1) { | 117 if ((last_line != -1) && !as_call_sites) { |
| 117 hits_arr.AddValue(last_line); | 118 hits_or_sites.AddValue(last_line); |
| 118 hits_arr.AddValue(last_count); | 119 hits_or_sites.AddValue(last_count); |
| 119 } | 120 } |
| 120 last_count = ic_data->AggregateCount(); | 121 last_count = ic_data->AggregateCount(); |
| 121 last_line = line; | 122 last_line = line; |
| 122 } | 123 } |
| 124 if (as_call_sites) { |
| 125 bool is_static_call = iter.Kind() == RawPcDescriptors::kUnoptStaticCall; |
| 126 ic_data->PrintToJSONArray(hits_or_sites, token_pos, is_static_call); |
| 127 } |
| 123 } | 128 } |
| 124 } | 129 } |
| 125 // Write last hit value if needed. | 130 // Write last hit value if needed. |
| 126 if (last_line != -1) { | 131 if ((last_line != -1) && !as_call_sites) { |
| 127 hits_arr.AddValue(last_line); | 132 hits_or_sites.AddValue(last_line); |
| 128 hits_arr.AddValue(last_count); | 133 hits_or_sites.AddValue(last_count); |
| 129 } | 134 } |
| 130 } | 135 } |
| 131 | 136 |
| 132 | 137 |
| 133 void CodeCoverage::PrintClass(const Library& lib, | 138 void CodeCoverage::PrintClass(const Library& lib, |
| 134 const Class& cls, | 139 const Class& cls, |
| 135 const JSONArray& jsarr, | 140 const JSONArray& jsarr, |
| 136 CoverageFilter* filter) { | 141 CoverageFilter* filter, |
| 142 bool as_call_sites) { |
| 137 Isolate* isolate = Isolate::Current(); | 143 Isolate* isolate = Isolate::Current(); |
| 138 if (cls.EnsureIsFinalized(isolate) != Error::null()) { | 144 if (cls.EnsureIsFinalized(isolate) != Error::null()) { |
| 139 // Only classes that have been finalized do have a meaningful list of | 145 // Only classes that have been finalized do have a meaningful list of |
| 140 // functions. | 146 // functions. |
| 141 return; | 147 return; |
| 142 } | 148 } |
| 143 Array& functions = Array::Handle(cls.functions()); | 149 Array& functions = Array::Handle(cls.functions()); |
| 144 ASSERT(!functions.IsNull()); | 150 ASSERT(!functions.IsNull()); |
| 145 Function& function = Function::Handle(); | 151 Function& function = Function::Handle(); |
| 146 Script& script = Script::Handle(); | 152 Script& script = Script::Handle(); |
| 147 String& saved_url = String::Handle(); | 153 String& saved_url = String::Handle(); |
| 148 String& url = String::Handle(); | 154 String& url = String::Handle(); |
| 149 GrowableArray<intptr_t> pos_to_line; | 155 GrowableArray<intptr_t> pos_to_line; |
| 150 int i = 0; | 156 int i = 0; |
| 151 while (i < functions.Length()) { | 157 while (i < functions.Length()) { |
| 152 HANDLESCOPE(isolate); | 158 HANDLESCOPE(isolate); |
| 153 function ^= functions.At(i); | 159 function ^= functions.At(i); |
| 154 script = function.script(); | 160 script = function.script(); |
| 155 saved_url = script.url(); | 161 saved_url = script.url(); |
| 156 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { | 162 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { |
| 157 i++; | 163 i++; |
| 158 continue; | 164 continue; |
| 159 } | 165 } |
| 160 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 166 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 161 JSONObject jsobj(&jsarr); | 167 JSONObject jsobj(&jsarr); |
| 162 jsobj.AddProperty("source", saved_url.ToCString()); | 168 jsobj.AddProperty("source", saved_url.ToCString()); |
| 163 jsobj.AddProperty("script", script); | 169 jsobj.AddProperty("script", script); |
| 164 JSONArray hits_arr(&jsobj, "hits"); | 170 JSONArray hits_or_sites(&jsobj, as_call_sites ? "callSites" : "hits"); |
| 165 | 171 |
| 166 // We stay within this loop while we are seeing functions from the same | 172 // We stay within this loop while we are seeing functions from the same |
| 167 // source URI. | 173 // source URI. |
| 168 while (i < functions.Length()) { | 174 while (i < functions.Length()) { |
| 169 function ^= functions.At(i); | 175 function ^= functions.At(i); |
| 170 script = function.script(); | 176 script = function.script(); |
| 171 url = script.url(); | 177 url = script.url(); |
| 172 if (!url.Equals(saved_url)) { | 178 if (!url.Equals(saved_url)) { |
| 173 pos_to_line.Clear(); | 179 pos_to_line.Clear(); |
| 174 break; | 180 break; |
| 175 } | 181 } |
| 176 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { | 182 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { |
| 177 i++; | 183 i++; |
| 178 continue; | 184 continue; |
| 179 } | 185 } |
| 180 CompileAndAdd(function, hits_arr, pos_to_line); | 186 CompileAndAdd(function, hits_or_sites, pos_to_line, as_call_sites); |
| 181 if (function.HasImplicitClosureFunction()) { | 187 if (function.HasImplicitClosureFunction()) { |
| 182 function = function.ImplicitClosureFunction(); | 188 function = function.ImplicitClosureFunction(); |
| 183 CompileAndAdd(function, hits_arr, pos_to_line); | 189 CompileAndAdd(function, hits_or_sites, pos_to_line, as_call_sites); |
| 184 } | 190 } |
| 185 i++; | 191 i++; |
| 186 } | 192 } |
| 187 } | 193 } |
| 188 | 194 |
| 189 GrowableObjectArray& closures = | 195 GrowableObjectArray& closures = |
| 190 GrowableObjectArray::Handle(cls.closures()); | 196 GrowableObjectArray::Handle(cls.closures()); |
| 191 if (!closures.IsNull()) { | 197 if (!closures.IsNull()) { |
| 192 i = 0; | 198 i = 0; |
| 193 pos_to_line.Clear(); | 199 pos_to_line.Clear(); |
| 194 // We need to keep rechecking the length of the closures array, as handling | 200 // We need to keep rechecking the length of the closures array, as handling |
| 195 // a closure potentially adds new entries to the end. | 201 // a closure potentially adds new entries to the end. |
| 196 while (i < closures.Length()) { | 202 while (i < closures.Length()) { |
| 197 HANDLESCOPE(isolate); | 203 HANDLESCOPE(isolate); |
| 198 function ^= closures.At(i); | 204 function ^= closures.At(i); |
| 199 script = function.script(); | 205 script = function.script(); |
| 200 saved_url = script.url(); | 206 saved_url = script.url(); |
| 201 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { | 207 if (!filter->ShouldOutputCoverageFor(lib, script, cls, function)) { |
| 202 i++; | 208 i++; |
| 203 continue; | 209 continue; |
| 204 } | 210 } |
| 205 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 211 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 206 JSONObject jsobj(&jsarr); | 212 JSONObject jsobj(&jsarr); |
| 207 jsobj.AddProperty("source", saved_url.ToCString()); | 213 jsobj.AddProperty("source", saved_url.ToCString()); |
| 208 jsobj.AddProperty("script", script); | 214 jsobj.AddProperty("script", script); |
| 209 JSONArray hits_arr(&jsobj, "hits"); | 215 JSONArray hits_or_sites(&jsobj, as_call_sites ? "callSites" : "hits"); |
| 210 | 216 |
| 211 // We stay within this loop while we are seeing functions from the same | 217 // We stay within this loop while we are seeing functions from the same |
| 212 // source URI. | 218 // source URI. |
| 213 while (i < closures.Length()) { | 219 while (i < closures.Length()) { |
| 214 function ^= closures.At(i); | 220 function ^= closures.At(i); |
| 215 script = function.script(); | 221 script = function.script(); |
| 216 url = script.url(); | 222 url = script.url(); |
| 217 if (!url.Equals(saved_url)) { | 223 if (!url.Equals(saved_url)) { |
| 218 pos_to_line.Clear(); | 224 pos_to_line.Clear(); |
| 219 break; | 225 break; |
| 220 } | 226 } |
| 221 CompileAndAdd(function, hits_arr, pos_to_line); | 227 CompileAndAdd(function, hits_or_sites, pos_to_line, as_call_sites); |
| 222 i++; | 228 i++; |
| 223 } | 229 } |
| 224 } | 230 } |
| 225 } | 231 } |
| 226 } | 232 } |
| 227 | 233 |
| 228 | 234 |
| 229 void CodeCoverage::Write(Isolate* isolate) { | 235 void CodeCoverage::Write(Isolate* isolate) { |
| 230 if (FLAG_coverage_dir == NULL) { | 236 if (FLAG_coverage_dir == NULL) { |
| 231 return; | 237 return; |
| 232 } | 238 } |
| 233 | 239 |
| 234 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); | 240 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
| 235 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); | 241 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
| 236 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); | 242 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
| 237 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { | 243 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { |
| 238 return; | 244 return; |
| 239 } | 245 } |
| 240 | 246 |
| 241 JSONStream stream; | 247 JSONStream stream; |
| 242 PrintJSON(isolate, &stream, NULL); | 248 PrintJSON(isolate, &stream, NULL, false); |
| 243 | 249 |
| 244 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; | 250 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; |
| 245 intptr_t pid = OS::ProcessId(); | 251 intptr_t pid = OS::ProcessId(); |
| 246 intptr_t len = OS::SNPrint(NULL, 0, format, | 252 intptr_t len = OS::SNPrint(NULL, 0, format, |
| 247 FLAG_coverage_dir, pid, isolate->main_port()); | 253 FLAG_coverage_dir, pid, isolate->main_port()); |
| 248 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); | 254 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 249 OS::SNPrint(filename, len + 1, format, | 255 OS::SNPrint(filename, len + 1, format, |
| 250 FLAG_coverage_dir, pid, isolate->main_port()); | 256 FLAG_coverage_dir, pid, isolate->main_port()); |
| 251 void* file = (*file_open)(filename, true); | 257 void* file = (*file_open)(filename, true); |
| 252 if (file == NULL) { | 258 if (file == NULL) { |
| 253 OS::Print("Failed to write coverage file: %s\n", filename); | 259 OS::Print("Failed to write coverage file: %s\n", filename); |
| 254 return; | 260 return; |
| 255 } | 261 } |
| 256 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); | 262 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); |
| 257 (*file_close)(file); | 263 (*file_close)(file); |
| 258 } | 264 } |
| 259 | 265 |
| 260 | 266 |
| 261 void CodeCoverage::PrintJSON(Isolate* isolate, | 267 void CodeCoverage::PrintJSON(Isolate* isolate, |
| 262 JSONStream* stream, | 268 JSONStream* stream, |
| 263 CoverageFilter* filter) { | 269 CoverageFilter* filter, |
| 270 bool as_call_sites) { |
| 264 CoverageFilterAll default_filter; | 271 CoverageFilterAll default_filter; |
| 265 if (filter == NULL) { | 272 if (filter == NULL) { |
| 266 filter = &default_filter; | 273 filter = &default_filter; |
| 267 } | 274 } |
| 268 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | 275 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
| 269 isolate, isolate->object_store()->libraries()); | 276 isolate, isolate->object_store()->libraries()); |
| 270 Library& lib = Library::Handle(); | 277 Library& lib = Library::Handle(); |
| 271 Class& cls = Class::Handle(); | 278 Class& cls = Class::Handle(); |
| 272 JSONObject coverage(stream); | 279 JSONObject coverage(stream); |
| 273 coverage.AddProperty("type", "CodeCoverage"); | 280 coverage.AddProperty("type", "CodeCoverage"); |
| 274 { | 281 { |
| 275 JSONArray jsarr(&coverage, "coverage"); | 282 JSONArray jsarr(&coverage, "coverage"); |
| 276 for (int i = 0; i < libs.Length(); i++) { | 283 for (int i = 0; i < libs.Length(); i++) { |
| 277 lib ^= libs.At(i); | 284 lib ^= libs.At(i); |
| 278 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | 285 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
| 279 while (it.HasNext()) { | 286 while (it.HasNext()) { |
| 280 cls = it.GetNextClass(); | 287 cls = it.GetNextClass(); |
| 281 ASSERT(!cls.IsNull()); | 288 ASSERT(!cls.IsNull()); |
| 282 PrintClass(lib, cls, jsarr, filter); | 289 PrintClass(lib, cls, jsarr, filter, as_call_sites); |
| 283 } | 290 } |
| 284 } | 291 } |
| 285 } | 292 } |
| 286 } | 293 } |
| 287 | 294 |
| 288 | 295 |
| 289 } // namespace dart | 296 } // namespace dart |
| OLD | NEW |