changeset 22221:c92d6117696f

Truffle/Instrumentation: modify behavior of CoverageTracker and LineToProbesMap so that they will work on ASTs created before installation (as long as correct probes are in place)
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Sat, 19 Sep 2015 13:24:47 -0700
parents 20380d1d41f2
children 69156ed8192b
files truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java
diffstat 3 files changed, 83 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java	Thu Sep 17 21:23:57 2015 -0700
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java	Sat Sep 19 13:24:47 2015 -0700
@@ -32,6 +32,7 @@
 import java.util.Map;
 
 import com.oracle.truffle.api.instrument.InstrumentationTool;
+import com.oracle.truffle.api.instrument.Instrumenter;
 import com.oracle.truffle.api.instrument.Probe;
 import com.oracle.truffle.api.instrument.ProbeListener;
 import com.oracle.truffle.api.instrument.impl.DefaultProbeListener;
@@ -69,7 +70,11 @@
 
     @Override
     protected boolean internalInstall() {
-        getInstrumenter().addProbeListener(probeListener);
+        final Instrumenter instrumenter = getInstrumenter();
+        for (Probe probe : instrumenter.findProbesTaggedAs(null)) {
+            addMapEntry(probe);
+        }
+        instrumenter.addProbeListener(probeListener);
         return true;
     }
 
@@ -116,21 +121,25 @@
 
         @Override
         public void newProbeInserted(Probe probe) {
-            final SourceSection sourceSection = probe.getProbedSourceSection();
-            if (sourceSection != null && sourceSection.getSource() != null) {
-                final LineLocation lineLocation = sourceSection.getLineLocation();
-                if (TRACE) {
-                    trace("ADD " + lineLocation.getShortDescription() + " ==> " + probe.getShortDescription());
-                }
-                Collection<Probe> probes = lineToProbesMap.get(lineLocation);
-                if (probes == null) {
-                    probes = new ArrayList<>(2);
-                    lineToProbesMap.put(lineLocation, probes);
-                } else {
-                    assert !probes.contains(probe);
-                }
-                probes.add(probe);
+            addMapEntry(probe);
+        }
+    }
+
+    private void addMapEntry(Probe probe) {
+        final SourceSection sourceSection = probe.getProbedSourceSection();
+        if (sourceSection != null && sourceSection.getSource() != null) {
+            final LineLocation lineLocation = sourceSection.getLineLocation();
+            if (TRACE) {
+                trace("ADD " + lineLocation.getShortDescription() + " ==> " + probe.getShortDescription());
             }
+            Collection<Probe> probes = lineToProbesMap.get(lineLocation);
+            if (probes == null) {
+                probes = new ArrayList<>(2);
+                lineToProbesMap.put(lineLocation, probes);
+            } else {
+                assert !probes.contains(probe);
+            }
+            probes.add(probe);
         }
     }
 }
--- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java	Thu Sep 17 21:23:57 2015 -0700
+++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java	Sat Sep 19 13:24:47 2015 -0700
@@ -35,6 +35,7 @@
 
 import com.oracle.truffle.api.instrument.Instrument;
 import com.oracle.truffle.api.instrument.InstrumentationTool;
+import com.oracle.truffle.api.instrument.Instrumenter;
 import com.oracle.truffle.api.instrument.Probe;
 import com.oracle.truffle.api.instrument.ProbeListener;
 import com.oracle.truffle.api.instrument.SimpleInstrumentListener;
@@ -116,7 +117,11 @@
 
     @Override
     protected boolean internalInstall() {
-        getInstrumenter().addProbeListener(probeListener);
+        final Instrumenter instrumenter = getInstrumenter();
+        for (Probe probe : instrumenter.findProbesTaggedAs(countingTag)) {
+            addCoverageCounter(probe);
+        }
+        instrumenter.addProbeListener(probeListener);
         return true;
     }
 
@@ -274,34 +279,37 @@
         @Override
         public void probeTaggedAs(Probe probe, SyntaxTag tag, Object tagValue) {
             if (countingTag == tag) {
-
-                final SourceSection srcSection = probe.getProbedSourceSection();
-                if (srcSection == null) {
-                    // TODO (mlvdv) report this?
-                    return;
-                }
-                // Get the source line where the
-                final LineLocation lineLocation = srcSection.getLineLocation();
-                CoverageRecord record = coverageMap.get(lineLocation);
-                if (record != null) {
-                    // Another node starts on same line; count only the first (textually)
-                    if (srcSection.getCharIndex() > record.srcSection.getCharIndex()) {
-                        // Existing record, corresponds to code earlier on line
-                        return;
-                    } else {
-                        // Existing record, corresponds to code at a later position; replace it
-                        record.instrument.dispose();
-                    }
-                }
-
-                final CoverageRecord coverage = new CoverageRecord(srcSection);
-                final Instrument instrument = Instrument.create(coverage, CoverageTracker.class.getSimpleName());
-                coverage.instrument = instrument;
-                instruments.add(instrument);
-                probe.attach(instrument);
-                coverageMap.put(lineLocation, coverage);
+                addCoverageCounter(probe);
             }
         }
     }
 
+    private void addCoverageCounter(Probe probe) {
+        final SourceSection srcSection = probe.getProbedSourceSection();
+        if (srcSection == null) {
+            // TODO (mlvdv) report this?
+            return;
+        }
+        // Get the source line where the
+        final LineLocation lineLocation = srcSection.getLineLocation();
+        CoverageRecord record = coverageMap.get(lineLocation);
+        if (record != null) {
+            // Another node starts on same line; count only the first (textually)
+            if (srcSection.getCharIndex() > record.srcSection.getCharIndex()) {
+                // Existing record, corresponds to code earlier on line
+                return;
+            } else {
+                // Existing record, corresponds to code at a later position; replace it
+                record.instrument.dispose();
+            }
+        }
+
+        final CoverageRecord coverage = new CoverageRecord(srcSection);
+        final Instrument instrument = Instrument.create(coverage, CoverageTracker.class.getSimpleName());
+        coverage.instrument = instrument;
+        instruments.add(instrument);
+        probe.attach(instrument);
+        coverageMap.put(lineLocation, coverage);
+    }
+
 }
--- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java	Thu Sep 17 21:23:57 2015 -0700
+++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/LineToProbesMap.java	Sat Sep 19 13:24:47 2015 -0700
@@ -32,6 +32,7 @@
 import java.util.Map;
 
 import com.oracle.truffle.api.instrument.InstrumentationTool;
+import com.oracle.truffle.api.instrument.Instrumenter;
 import com.oracle.truffle.api.instrument.Probe;
 import com.oracle.truffle.api.instrument.ProbeListener;
 import com.oracle.truffle.api.instrument.impl.DefaultProbeListener;
@@ -69,7 +70,11 @@
 
     @Override
     protected boolean internalInstall() {
-        getInstrumenter().addProbeListener(probeListener);
+        final Instrumenter instrumenter = getInstrumenter();
+        for (Probe probe : instrumenter.findProbesTaggedAs(null)) {
+            addMapEntry(probe);
+        }
+        instrumenter.addProbeListener(probeListener);
         return true;
     }
 
@@ -116,21 +121,25 @@
 
         @Override
         public void newProbeInserted(Probe probe) {
-            final SourceSection sourceSection = probe.getProbedSourceSection();
-            if (sourceSection != null && sourceSection.getSource() != null) {
-                final LineLocation lineLocation = sourceSection.getLineLocation();
-                if (TRACE) {
-                    trace("ADD " + lineLocation.getShortDescription() + " ==> " + probe.getShortDescription());
-                }
-                Collection<Probe> probes = lineToProbesMap.get(lineLocation);
-                if (probes == null) {
-                    probes = new ArrayList<>(2);
-                    lineToProbesMap.put(lineLocation, probes);
-                } else {
-                    assert !probes.contains(probe);
-                }
-                probes.add(probe);
+            addMapEntry(probe);
+        }
+    }
+
+    private void addMapEntry(Probe probe) {
+        final SourceSection sourceSection = probe.getProbedSourceSection();
+        if (sourceSection != null && sourceSection.getSource() != null) {
+            final LineLocation lineLocation = sourceSection.getLineLocation();
+            if (TRACE) {
+                trace("ADD " + lineLocation.getShortDescription() + " ==> " + probe.getShortDescription());
             }
+            Collection<Probe> probes = lineToProbesMap.get(lineLocation);
+            if (probes == null) {
+                probes = new ArrayList<>(2);
+                lineToProbesMap.put(lineLocation, probes);
+            } else {
+                assert !probes.contains(probe);
+            }
+            probes.add(probe);
         }
     }
 }