| 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" |
| 11 #include "vm/json_stream.h" | 11 #include "vm/json_stream.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 #include "vm/object_store.h" | 13 #include "vm/object_store.h" |
| 14 | 14 |
| 15 namespace dart { | 15 namespace dart { |
| 16 | 16 |
| 17 DEFINE_FLAG(charp, coverage_dir, NULL, | 17 DEFINE_FLAG(charp, coverage_dir, NULL, |
| 18 "Enable writing coverage data into specified directory."); | 18 "Enable writing coverage data into specified directory."); |
| 19 | 19 |
| 20 | 20 |
| 21 class CoverageFilterAll : public CoverageFilter { |
| 22 public: |
| 23 bool ShouldOutputCoverageFor(const Library& lib, |
| 24 const String& script_url, |
| 25 const Class& cls, |
| 26 const Function& func) const { |
| 27 return true; |
| 28 } |
| 29 }; |
| 30 |
| 31 |
| 21 // map[token_pos] -> line-number. | 32 // map[token_pos] -> line-number. |
| 22 static void ComputeTokenPosToLineNumberMap(const Script& script, | 33 static void ComputeTokenPosToLineNumberMap(const Script& script, |
| 23 GrowableArray<intptr_t>* map) { | 34 GrowableArray<intptr_t>* map) { |
| 24 const TokenStream& tkns = TokenStream::Handle(script.tokens()); | 35 const TokenStream& tkns = TokenStream::Handle(script.tokens()); |
| 25 const intptr_t len = ExternalTypedData::Handle(tkns.GetStream()).Length(); | 36 const intptr_t len = ExternalTypedData::Handle(tkns.GetStream()).Length(); |
| 26 map->SetLength(len); | 37 map->SetLength(len); |
| 27 #if defined(DEBUG) | 38 #if defined(DEBUG) |
| 28 for (intptr_t i = 0; i < len; i++) { | 39 for (intptr_t i = 0; i < len; i++) { |
| 29 (*map)[i] = -1; | 40 (*map)[i] = -1; |
| 30 } | 41 } |
| 31 #endif | 42 #endif |
| 32 TokenStream::Iterator tkit(tkns, 0, TokenStream::Iterator::kAllTokens); | 43 TokenStream::Iterator tkit(tkns, 0, TokenStream::Iterator::kAllTokens); |
| 33 intptr_t cur_line = script.line_offset() + 1; | 44 intptr_t cur_line = script.line_offset() + 1; |
| 34 while (tkit.CurrentTokenKind() != Token::kEOS) { | 45 while (tkit.CurrentTokenKind() != Token::kEOS) { |
| 35 (*map)[tkit.CurrentPosition()] = cur_line; | 46 (*map)[tkit.CurrentPosition()] = cur_line; |
| 36 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { | 47 if (tkit.CurrentTokenKind() == Token::kNEWLINE) { |
| 37 cur_line++; | 48 cur_line++; |
| 38 } | 49 } |
| 39 tkit.Advance(); | 50 tkit.Advance(); |
| 40 } | 51 } |
| 41 } | 52 } |
| 42 | 53 |
| 43 | 54 |
| 44 static inline void PrintJSONPreamble(JSONObject* jsobj) { | |
| 45 jsobj->AddProperty("type", "CodeCoverage"); | |
| 46 jsobj->AddProperty("id", "coverage"); | |
| 47 } | |
| 48 | |
| 49 | |
| 50 void CodeCoverage::CompileAndAdd(const Function& function, | 55 void CodeCoverage::CompileAndAdd(const Function& function, |
| 51 const JSONArray& hits_arr, | 56 const JSONArray& hits_arr, |
| 52 const GrowableArray<intptr_t>& pos_to_line) { | 57 const GrowableArray<intptr_t>& pos_to_line) { |
| 53 Isolate* isolate = Isolate::Current(); | 58 Isolate* isolate = Isolate::Current(); |
| 54 if (!function.HasCode()) { | 59 if (!function.HasCode()) { |
| 55 // If the function should not be compiled or if the compilation failed, | 60 // If the function should not be compiled or if the compilation failed, |
| 56 // then just skip this method. | 61 // then just skip this method. |
| 57 // TODO(iposva): Maybe we should skip synthesized methods in general too. | 62 // TODO(iposva): Maybe we should skip synthesized methods in general too. |
| 58 if (function.is_abstract() || function.IsRedirectingFactory()) { | 63 if (function.is_abstract() || function.IsRedirectingFactory()) { |
| 59 return; | 64 return; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 127 } |
| 123 } | 128 } |
| 124 // Write last hit value if needed. | 129 // Write last hit value if needed. |
| 125 if (last_line != -1) { | 130 if (last_line != -1) { |
| 126 hits_arr.AddValue(last_line); | 131 hits_arr.AddValue(last_line); |
| 127 hits_arr.AddValue(last_count); | 132 hits_arr.AddValue(last_count); |
| 128 } | 133 } |
| 129 } | 134 } |
| 130 | 135 |
| 131 | 136 |
| 132 void CodeCoverage::PrintClass(const Class& cls, | 137 void CodeCoverage::PrintClass(const Library& lib, |
| 138 const Class& cls, |
| 133 const JSONArray& jsarr, | 139 const JSONArray& jsarr, |
| 134 const Script& script_filter) { | 140 CoverageFilter* filter) { |
| 135 Isolate* isolate = Isolate::Current(); | 141 Isolate* isolate = Isolate::Current(); |
| 136 if (cls.EnsureIsFinalized(isolate) != Error::null()) { | 142 if (cls.EnsureIsFinalized(isolate) != Error::null()) { |
| 137 // Only classes that have been finalized do have a meaningful list of | 143 // Only classes that have been finalized do have a meaningful list of |
| 138 // functions. | 144 // functions. |
| 139 return; | 145 return; |
| 140 } | 146 } |
| 141 Array& functions = Array::Handle(cls.functions()); | 147 Array& functions = Array::Handle(cls.functions()); |
| 142 ASSERT(!functions.IsNull()); | 148 ASSERT(!functions.IsNull()); |
| 143 Function& function = Function::Handle(); | 149 Function& function = Function::Handle(); |
| 144 Script& script = Script::Handle(); | 150 Script& script = Script::Handle(); |
| 145 String& saved_url = String::Handle(); | 151 String& saved_url = String::Handle(); |
| 146 String& url = String::Handle(); | 152 String& url = String::Handle(); |
| 147 GrowableArray<intptr_t> pos_to_line; | 153 GrowableArray<intptr_t> pos_to_line; |
| 148 int i = 0; | 154 int i = 0; |
| 149 while (i < functions.Length()) { | 155 while (i < functions.Length()) { |
| 150 HANDLESCOPE(isolate); | 156 HANDLESCOPE(isolate); |
| 151 function ^= functions.At(i); | 157 function ^= functions.At(i); |
| 152 script = function.script(); | 158 script = function.script(); |
| 153 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | 159 saved_url = script.url(); |
| 160 if (!filter->ShouldOutputCoverageFor(lib, saved_url, cls, function)) { |
| 154 i++; | 161 i++; |
| 155 continue; | 162 continue; |
| 156 } | 163 } |
| 157 saved_url = script.url(); | |
| 158 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 164 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 159 JSONObject jsobj(&jsarr); | 165 JSONObject jsobj(&jsarr); |
| 160 jsobj.AddProperty("source", saved_url.ToCString()); | 166 jsobj.AddProperty("source", saved_url.ToCString()); |
| 161 jsobj.AddProperty("script", script); | 167 jsobj.AddProperty("script", script); |
| 162 JSONArray hits_arr(&jsobj, "hits"); | 168 JSONArray hits_arr(&jsobj, "hits"); |
| 163 | 169 |
| 164 // We stay within this loop while we are seeing functions from the same | 170 // We stay within this loop while we are seeing functions from the same |
| 165 // source URI. | 171 // source URI. |
| 166 while (i < functions.Length()) { | 172 while (i < functions.Length()) { |
| 167 function ^= functions.At(i); | 173 function ^= functions.At(i); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 184 GrowableObjectArray::Handle(cls.closures()); | 190 GrowableObjectArray::Handle(cls.closures()); |
| 185 if (!closures.IsNull()) { | 191 if (!closures.IsNull()) { |
| 186 i = 0; | 192 i = 0; |
| 187 pos_to_line.Clear(); | 193 pos_to_line.Clear(); |
| 188 // We need to keep rechecking the length of the closures array, as handling | 194 // We need to keep rechecking the length of the closures array, as handling |
| 189 // a closure potentially adds new entries to the end. | 195 // a closure potentially adds new entries to the end. |
| 190 while (i < closures.Length()) { | 196 while (i < closures.Length()) { |
| 191 HANDLESCOPE(isolate); | 197 HANDLESCOPE(isolate); |
| 192 function ^= closures.At(i); | 198 function ^= closures.At(i); |
| 193 script = function.script(); | 199 script = function.script(); |
| 194 if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { | 200 saved_url = script.url(); |
| 201 if (!filter->ShouldOutputCoverageFor(lib, saved_url, cls, function)) { |
| 195 i++; | 202 i++; |
| 196 continue; | 203 continue; |
| 197 } | 204 } |
| 198 saved_url = script.url(); | |
| 199 ComputeTokenPosToLineNumberMap(script, &pos_to_line); | 205 ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
| 200 JSONObject jsobj(&jsarr); | 206 JSONObject jsobj(&jsarr); |
| 201 jsobj.AddProperty("source", saved_url.ToCString()); | 207 jsobj.AddProperty("source", saved_url.ToCString()); |
| 202 jsobj.AddProperty("script", script); | 208 jsobj.AddProperty("script", script); |
| 203 JSONArray hits_arr(&jsobj, "hits"); | 209 JSONArray hits_arr(&jsobj, "hits"); |
| 204 | 210 |
| 205 // We stay within this loop while we are seeing functions from the same | 211 // We stay within this loop while we are seeing functions from the same |
| 206 // source URI. | 212 // source URI. |
| 207 while (i < closures.Length()) { | 213 while (i < closures.Length()) { |
| 208 function ^= closures.At(i); | 214 function ^= closures.At(i); |
| 209 script = function.script(); | 215 script = function.script(); |
| 210 url = script.url(); | 216 url = script.url(); |
| 211 if (!url.Equals(saved_url)) { | 217 if (!url.Equals(saved_url)) { |
| 212 pos_to_line.Clear(); | 218 pos_to_line.Clear(); |
| 213 break; | 219 break; |
| 214 } | 220 } |
| 215 CompileAndAdd(function, hits_arr, pos_to_line); | 221 CompileAndAdd(function, hits_arr, pos_to_line); |
| 216 i++; | 222 i++; |
| 217 } | 223 } |
| 218 } | 224 } |
| 219 } | 225 } |
| 220 } | 226 } |
| 221 | 227 |
| 222 | 228 |
| 223 void CodeCoverage::PrintJSONForClass(const Class& cls, | |
| 224 JSONStream* stream) { | |
| 225 JSONObject coverage(stream); | |
| 226 PrintJSONPreamble(&coverage); | |
| 227 { | |
| 228 JSONArray jsarr(&coverage, "coverage"); | |
| 229 PrintClass(cls, jsarr, Script::Handle()); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 | |
| 234 void CodeCoverage::PrintJSONForLibrary(const Library& lib, | |
| 235 const Script& script_filter, | |
| 236 JSONStream* stream) { | |
| 237 Class& cls = Class::Handle(); | |
| 238 JSONObject coverage(stream); | |
| 239 PrintJSONPreamble(&coverage); | |
| 240 { | |
| 241 JSONArray jsarr(&coverage, "coverage"); | |
| 242 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | |
| 243 while (it.HasNext()) { | |
| 244 cls = it.GetNextClass(); | |
| 245 ASSERT(!cls.IsNull()); | |
| 246 PrintClass(cls, jsarr, script_filter); | |
| 247 } | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 | |
| 252 void CodeCoverage::PrintJSONForScript(const Script& script, | |
| 253 JSONStream* stream) { | |
| 254 Library& lib = Library::Handle(); | |
| 255 lib = script.FindLibrary(); | |
| 256 ASSERT(!lib.IsNull()); | |
| 257 PrintJSONForLibrary(lib, script, stream); | |
| 258 } | |
| 259 | |
| 260 | |
| 261 void CodeCoverage::Write(Isolate* isolate) { | 229 void CodeCoverage::Write(Isolate* isolate) { |
| 262 if (FLAG_coverage_dir == NULL) { | 230 if (FLAG_coverage_dir == NULL) { |
| 263 return; | 231 return; |
| 264 } | 232 } |
| 265 | 233 |
| 266 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); | 234 Dart_FileOpenCallback file_open = Isolate::file_open_callback(); |
| 267 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); | 235 Dart_FileWriteCallback file_write = Isolate::file_write_callback(); |
| 268 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); | 236 Dart_FileCloseCallback file_close = Isolate::file_close_callback(); |
| 269 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { | 237 if ((file_open == NULL) || (file_write == NULL) || (file_close == NULL)) { |
| 270 return; | 238 return; |
| 271 } | 239 } |
| 272 | 240 |
| 273 JSONStream stream; | 241 JSONStream stream; |
| 274 PrintJSON(isolate, &stream); | 242 PrintJSON(isolate, &stream, NULL); |
| 275 | 243 |
| 276 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; | 244 const char* format = "%s/dart-cov-%" Pd "-%" Pd ".json"; |
| 277 intptr_t pid = OS::ProcessId(); | 245 intptr_t pid = OS::ProcessId(); |
| 278 intptr_t len = OS::SNPrint(NULL, 0, format, | 246 intptr_t len = OS::SNPrint(NULL, 0, format, |
| 279 FLAG_coverage_dir, pid, isolate->main_port()); | 247 FLAG_coverage_dir, pid, isolate->main_port()); |
| 280 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); | 248 char* filename = Isolate::Current()->current_zone()->Alloc<char>(len + 1); |
| 281 OS::SNPrint(filename, len + 1, format, | 249 OS::SNPrint(filename, len + 1, format, |
| 282 FLAG_coverage_dir, pid, isolate->main_port()); | 250 FLAG_coverage_dir, pid, isolate->main_port()); |
| 283 void* file = (*file_open)(filename, true); | 251 void* file = (*file_open)(filename, true); |
| 284 if (file == NULL) { | 252 if (file == NULL) { |
| 285 OS::Print("Failed to write coverage file: %s\n", filename); | 253 OS::Print("Failed to write coverage file: %s\n", filename); |
| 286 return; | 254 return; |
| 287 } | 255 } |
| 288 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); | 256 (*file_write)(stream.buffer()->buf(), stream.buffer()->length(), file); |
| 289 (*file_close)(file); | 257 (*file_close)(file); |
| 290 } | 258 } |
| 291 | 259 |
| 292 | 260 |
| 293 void CodeCoverage::PrintJSON(Isolate* isolate, JSONStream* stream) { | 261 void CodeCoverage::PrintJSON(Isolate* isolate, |
| 262 JSONStream* stream, |
| 263 CoverageFilter* filter) { |
| 264 CoverageFilterAll default_filter; |
| 265 if (filter == NULL) { |
| 266 filter = &default_filter; |
| 267 } |
| 294 const GrowableObjectArray& libs = GrowableObjectArray::Handle( | 268 const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
| 295 isolate, isolate->object_store()->libraries()); | 269 isolate, isolate->object_store()->libraries()); |
| 296 Library& lib = Library::Handle(); | 270 Library& lib = Library::Handle(); |
| 297 Class& cls = Class::Handle(); | 271 Class& cls = Class::Handle(); |
| 298 JSONObject coverage(stream); | 272 JSONObject coverage(stream); |
| 299 coverage.AddProperty("type", "CodeCoverage"); | 273 coverage.AddProperty("type", "CodeCoverage"); |
| 300 coverage.AddProperty("id", "coverage"); | 274 coverage.AddProperty("id", "coverage"); |
| 301 { | 275 { |
| 302 JSONArray jsarr(&coverage, "coverage"); | 276 JSONArray jsarr(&coverage, "coverage"); |
| 303 for (int i = 0; i < libs.Length(); i++) { | 277 for (int i = 0; i < libs.Length(); i++) { |
| 304 lib ^= libs.At(i); | 278 lib ^= libs.At(i); |
| 305 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); | 279 ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
| 306 while (it.HasNext()) { | 280 while (it.HasNext()) { |
| 307 cls = it.GetNextClass(); | 281 cls = it.GetNextClass(); |
| 308 ASSERT(!cls.IsNull()); | 282 ASSERT(!cls.IsNull()); |
| 309 PrintClass(cls, jsarr, Script::Handle()); | 283 PrintClass(lib, cls, jsarr, filter); |
| 310 } | 284 } |
| 311 } | 285 } |
| 312 } | 286 } |
| 313 } | 287 } |
| 314 | 288 |
| 315 | 289 |
| 316 } // namespace dart | 290 } // namespace dart |
| OLD | NEW |