Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/tools/LineToProbesMap.java @ 18987:ac114ad31cdd
Truffle/Tools: a new framework for pluggable tools that gather Truffle execution data
- Abstract com.oracle.truffle.api.instrument.TruffleTool defines, documents, and enforces a standard "life cycle":
-- includes creation, installation, enabling/disabling, and disposing.
-- data retrieval is tool dependent, but each is encouraged to provide a default print() method for demo & debugging
- com.oracle.truffle.api.tools contains three instances, all language-agnostic:
-- LineToProbesMap: existing utility used by the debugger, adapted to the frameowrk
-- CoverageTracker: code coverage, tabulated by line
-- NodeExecCounter: raw execution counts, tabulated by node type, can be filtered by SyntaxTag
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 27 Jan 2015 20:26:41 -0800 |
parents | |
children | 3d2296dbace9 |
comparison
equal
deleted
inserted
replaced
18986:50b22daf6d53 | 18987:ac114ad31cdd |
---|---|
1 /* | |
2 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.tools; | |
26 | |
27 import java.io.*; | |
28 import java.util.*; | |
29 | |
30 import com.oracle.truffle.api.instrument.*; | |
31 import com.oracle.truffle.api.instrument.impl.*; | |
32 import com.oracle.truffle.api.source.*; | |
33 | |
34 /** | |
35 * A {@link TruffleTool} that builds a map of every {@Probe} attached to some AST, indexed | |
36 * by {@link Source} and line number. | |
37 */ | |
38 public final class LineToProbesMap extends TruffleTool { | |
39 | |
40 private static final boolean TRACE = false; | |
41 private static final PrintStream OUT = System.out; | |
42 | |
43 private static void trace(String msg) { | |
44 OUT.println("LineToProbesMap: " + msg); | |
45 } | |
46 | |
47 /** | |
48 * Map: Source line ==> probes associated with source sections starting on the line. | |
49 */ | |
50 private final Map<LineLocation, Collection<Probe>> lineToProbesMap = new HashMap<>(); | |
51 | |
52 private final ProbeListener probeListener; | |
53 | |
54 /** | |
55 * Create a map of {@link Probe}s that collects information on all probes added to subsequently | |
56 * created ASTs (once installed). | |
57 */ | |
58 public LineToProbesMap() { | |
59 this.probeListener = new LineToProbesListener(); | |
60 } | |
61 | |
62 @Override | |
63 protected boolean internalInstall() { | |
64 Probe.addProbeListener(probeListener); | |
65 return true; | |
66 } | |
67 | |
68 @Override | |
69 protected void internalReset() { | |
70 lineToProbesMap.clear(); | |
71 } | |
72 | |
73 @Override | |
74 protected void internalDispose() { | |
75 Probe.removeProbeListener(probeListener); | |
76 } | |
77 | |
78 /** | |
79 * Returns the {@link Probe}, if any, associated with a specific line of guest language code; if | |
80 * more than one, return the one with the first starting character location. | |
81 */ | |
82 public Probe findFirstProbe(LineLocation lineLocation) { | |
83 Probe probe = null; | |
84 final Collection<Probe> probes = findProbes(lineLocation); | |
85 for (Probe probesOnLine : probes) { | |
86 if (probe == null) { | |
87 probe = probesOnLine; | |
88 } else if (probesOnLine.getProbedSourceSection().getCharIndex() < probe.getProbedSourceSection().getCharIndex()) { | |
89 probe = probesOnLine; | |
90 } | |
91 } | |
92 return probe; | |
93 } | |
94 | |
95 /** | |
96 * Returns all {@link Probe}s whose associated source begins at the given {@link LineLocation}, | |
97 * an empty list if none. | |
98 */ | |
99 public Collection<Probe> findProbes(LineLocation line) { | |
100 final Collection<Probe> probes = lineToProbesMap.get(line); | |
101 if (probes == null) { | |
102 return Collections.emptyList(); | |
103 } | |
104 return Collections.unmodifiableCollection(probes); | |
105 } | |
106 | |
107 private class LineToProbesListener extends DefaultProbeListener { | |
108 | |
109 @Override | |
110 public void newProbeInserted(Probe probe) { | |
111 final SourceSection sourceSection = probe.getProbedSourceSection(); | |
112 if (sourceSection != null && !(sourceSection instanceof NullSourceSection)) { | |
113 final LineLocation lineLocation = sourceSection.getLineLocation(); | |
114 if (TRACE) { | |
115 trace("ADD " + lineLocation.getShortDescription() + " ==> " + probe.getShortDescription()); | |
116 } | |
117 Collection<Probe> probes = lineToProbesMap.get(lineLocation); | |
118 if (probes == null) { | |
119 probes = new ArrayList<>(2); | |
120 lineToProbesMap.put(lineLocation, probes); | |
121 } else { | |
122 assert !probes.contains(probe); | |
123 } | |
124 probes.add(probe); | |
125 } | |
126 } | |
127 } | |
128 } |