Index: runtime/vm/coverage.cc |
diff --git a/runtime/vm/coverage.cc b/runtime/vm/coverage.cc |
index a50ab73683b7c22323e8e7a38632437451341d7d..6c6278c5e53385864ebfeb68764d5abaa060dd03 100644 |
--- a/runtime/vm/coverage.cc |
+++ b/runtime/vm/coverage.cc |
@@ -41,6 +41,12 @@ static void ComputeTokenPosToLineNumberMap(const Script& script, |
} |
+static inline void PrintJSONPreamble(JSONObject* jsobj) { |
+ jsobj->AddProperty("type", "CodeCoverage"); |
+ jsobj->AddProperty("id", "coverage"); |
+} |
+ |
+ |
void CodeCoverage::CompileAndAdd(const Function& function, |
const JSONArray& hits_arr, |
const GrowableArray<intptr_t>& pos_to_line) { |
@@ -102,7 +108,9 @@ void CodeCoverage::CompileAndAdd(const Function& function, |
} |
-void CodeCoverage::PrintClass(const Class& cls, const JSONArray& jsarr) { |
+void CodeCoverage::PrintClass(const Class& cls, |
+ const JSONArray& jsarr, |
+ const Script& script_filter) { |
Isolate* isolate = Isolate::Current(); |
Array& functions = Array::Handle(cls.functions()); |
ASSERT(!functions.IsNull()); |
@@ -115,10 +123,14 @@ void CodeCoverage::PrintClass(const Class& cls, const JSONArray& jsarr) { |
while (i < functions.Length()) { |
HANDLESCOPE(isolate); |
function ^= functions.At(i); |
- JSONObject jsobj(&jsarr); |
script = function.script(); |
- ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
+ if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { |
+ i++; |
+ continue; |
+ } |
saved_url = script.url(); |
+ ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
+ JSONObject jsobj(&jsarr); |
jsobj.AddProperty("source", saved_url.ToCString()); |
jsobj.AddProperty("script", script); |
JSONArray hits_arr(&jsobj, "hits"); |
@@ -152,10 +164,14 @@ void CodeCoverage::PrintClass(const Class& cls, const JSONArray& jsarr) { |
while (i < closures.Length()) { |
HANDLESCOPE(isolate); |
function ^= closures.At(i); |
- JSONObject jsobj(&jsarr); |
script = function.script(); |
- ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
+ if (!script_filter.IsNull() && script_filter.raw() != script.raw()) { |
+ i++; |
+ continue; |
+ } |
saved_url = script.url(); |
+ ComputeTokenPosToLineNumberMap(script, &pos_to_line); |
+ JSONObject jsobj(&jsarr); |
jsobj.AddProperty("source", saved_url.ToCString()); |
jsobj.AddProperty("script", script); |
JSONArray hits_arr(&jsobj, "hits"); |
@@ -178,6 +194,86 @@ void CodeCoverage::PrintClass(const Class& cls, const JSONArray& jsarr) { |
} |
+void CodeCoverage::PrintJSONForClass(const Class& cls, |
+ JSONStream* stream) { |
+ Isolate* isolate = Isolate::Current(); |
+ JSONObject coverage(stream); |
+ PrintJSONPreamble(&coverage); |
+ { |
+ JSONArray jsarr(&coverage, "coverage"); |
+ if (cls.EnsureIsFinalized(isolate) == Error::null()) { |
+ // Only classes that have been finalized do have a meaningful list of |
+ // functions. |
+ PrintClass(cls, jsarr, Script::Handle()); |
+ } |
+ } |
+} |
+ |
+ |
+void CodeCoverage::PrintJSONForLibrary(const Library& lib, |
+ JSONStream* stream) { |
+ Isolate* isolate = Isolate::Current(); |
+ Class& cls = Class::Handle(); |
+ JSONObject coverage(stream); |
+ PrintJSONPreamble(&coverage); |
+ { |
+ JSONArray jsarr(&coverage, "coverage"); |
+ ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
+ while (it.HasNext()) { |
+ cls = it.GetNextClass(); |
+ if (cls.EnsureIsFinalized(isolate) == Error::null()) { |
+ // Only classes that have been finalized do have a meaningful list of |
+ // functions. |
+ PrintClass(cls, jsarr, Script::Handle()); |
+ } |
+ } |
+ } |
+} |
+ |
+ |
+void CodeCoverage::PrintJSONForScript(const String& script_name, |
+ JSONStream* stream) { |
+ // Unless there's a backref to the script's library we need to search through |
+ // all libraries. |
+ Isolate* isolate = Isolate::Current(); |
+ const GrowableObjectArray& libs = GrowableObjectArray::Handle( |
+ isolate, isolate->object_store()->libraries()); |
+ Library& lib = Library::Handle(); |
+ Class& cls = Class::Handle(); |
+ JSONObject coverage(stream); |
+ PrintJSONPreamble(&coverage); |
+ { |
+ JSONArray jsarr(&coverage, "coverage"); |
+ Script& script = Script::Handle(); |
+ for (int i = 0; i < libs.Length(); i++) { |
+ lib ^= libs.At(i); |
+ const Array& loaded_scripts = Array::Handle(lib.LoadedScripts()); |
+ intptr_t num_scripts = loaded_scripts.Length(); |
+ bool found = false; |
+ for (intptr_t i = 0; i < num_scripts; i++) { |
Cutch
2014/06/23 16:23:11
Factor this code into a utility method?
FindLibra
Michael Lippautz (Google)
2014/06/24 17:51:07
Done.
Added
static RawScript* Script::FindByUrl
|
+ script ^= loaded_scripts.At(i); |
+ if (script_name.CompareTo(String::Handle(script.url())) == 0) { |
+ found = true; |
+ break; |
+ } |
+ } |
+ if (!found) { |
+ continue; |
+ } |
+ ClassDictionaryIterator it(lib, ClassDictionaryIterator::kIteratePrivate); |
Cutch
2014/06/23 16:23:11
Just call PrintJSONForLibrary instead of duplicati
Michael Lippautz (Google)
2014/06/24 17:51:07
Done.
|
+ while (it.HasNext()) { |
+ cls = it.GetNextClass(); |
+ if (cls.EnsureIsFinalized(isolate) == Error::null()) { |
+ // Only classes that have been finalized do have a meaningful list of |
+ // functions. |
+ PrintClass(cls, jsarr, script); |
+ } |
+ } |
+ } |
+ } |
+} |
+ |
+ |
void CodeCoverage::Write(Isolate* isolate) { |
if (FLAG_coverage_dir == NULL) { |
return; |
@@ -228,7 +324,7 @@ void CodeCoverage::PrintJSON(Isolate* isolate, JSONStream* stream) { |
if (cls.EnsureIsFinalized(isolate) == Error::null()) { |
// Only classes that have been finalized do have a meaningful list of |
// functions. |
- PrintClass(cls, jsarr); |
+ PrintClass(cls, jsarr, Script::Handle()); |
} |
} |
} |