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