Mercurial > hg > graal-jvmci-8
annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/LineLocationToProbeCollectionMap.java @ 16856:7833417c8172
Changes to Instrumentation
author | David Piorkowski <david.piorkowski@oracle.com> |
---|---|
date | Mon, 18 Aug 2014 14:36:12 -0700 |
parents | |
children | 7bfbad29d331 |
rev | line source |
---|---|
16856
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
1 /* |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
4 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
10 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
15 * accompanied this code). |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
16 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
20 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
23 * questions. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
24 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
25 package com.oracle.truffle.api.instrument.impl; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
26 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
27 import java.util.*; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
28 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
29 import com.oracle.truffle.api.instrument.*; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
30 import com.oracle.truffle.api.source.*; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
31 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
32 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
33 * This class maintains a mapping from {@link LineLocation} to a collection of {@link Probe}s to be |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
34 * used by debugging tools. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
35 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
36 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
37 public class LineLocationToProbeCollectionMap implements ProbeListener { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
38 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
39 * Map: Source line ==> probes associated with source sections starting on the line. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
40 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
41 private final Map<LineLocation, Collection<Probe>> lineToProbesMap = new HashMap<>(); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
42 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
43 public LineLocationToProbeCollectionMap() { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
44 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
45 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
46 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
47 * Adds the current wrapper's child's line location and probe to this map. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
48 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
49 public void newProbeInserted(SourceSection source, Probe probe) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
50 final LineLocation line = source.getLineLocation(); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
51 this.addProbeToLine(line, probe); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
52 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
53 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
54 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
55 * Does nothing. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
56 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
57 public void probeTaggedAs(Probe probe, SyntaxTag tag) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
58 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
59 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
60 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
61 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
62 * Returns the {@link Probe}, if any, associated with source that starts on a specified line; if |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
63 * there are more than one, return the one with the first character location. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
64 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
65 public Probe findLineProbe(LineLocation lineLocation) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
66 Probe probe = null; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
67 final Collection<Probe> probes = getProbesAtLine(lineLocation); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
68 for (Probe probeOnLine : probes) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
69 if (probe == null) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
70 probe = probeOnLine; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
71 } else if (probeOnLine.getSourceLocation().getCharIndex() < probe.getSourceLocation().getCharIndex()) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
72 probe = probeOnLine; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
73 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
74 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
75 return probe; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
76 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
77 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
78 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
79 * Adds a probe to the given line. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
80 * <p> |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
81 * If the line already exists in the internal {@link #lineToProbesMap}, this probe will be added |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
82 * to the existing collection. If no line already exists in the internal map, then a new key is |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
83 * added along with a new collection containing the probe. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
84 * <p> |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
85 * This class requires that each added line/probe pair hasn't been previously added. However, |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
86 * attaching the same probe to a new line location is allowed. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
87 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
88 * @param line The {@link LineLocation} to attach the probe to. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
89 * @param probe The {@link Probe} to attach for that line location. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
90 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
91 private void addProbeToLine(LineLocation line, Probe probe) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
92 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
93 if (!lineToProbesMap.containsKey(line)) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
94 // Key does not exist, add new probe list |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
95 final ArrayList<Probe> newProbeList = new ArrayList<>(2); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
96 newProbeList.add(probe); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
97 lineToProbesMap.put(line, newProbeList); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
98 } else { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
99 // Probe list exists, add to existing |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
100 final Collection<Probe> existingProbeList = lineToProbesMap.get(line); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
101 assert !existingProbeList.contains(probe); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
102 existingProbeList.add(probe); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
103 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
104 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
105 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
106 /** |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
107 * Returns a collection of {@link Probe}s at the given {@link LineLocation}. If there are no |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
108 * probes at that line, an empty list is returned. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
109 * |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
110 * @param line The line to check. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
111 * @return A iterable collection of probes at the given line. |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
112 */ |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
113 private Collection<Probe> getProbesAtLine(LineLocation line) { |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
114 Collection<Probe> probeList = lineToProbesMap.get(line); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
115 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
116 if (probeList == null) |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
117 probeList = new ArrayList<>(2); |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
118 |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
119 return probeList; |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
120 } |
7833417c8172
Changes to Instrumentation
David Piorkowski <david.piorkowski@oracle.com>
parents:
diff
changeset
|
121 } |