comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/debug/LineToProbesMap.java @ 22003:5bc7f7b867ab

Making debugger always on for each TruffleVM execution. Introducing EventConsumer to process such debugger events. Requesting each RootNode to be associated with a TruffleLanguage, so debugger can find out proper context for each Node where executions gets suspended.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Sat, 18 Jul 2015 18:03:36 +0200
parents
children dc83cc1f94f2 3aad794eec0e
comparison
equal deleted inserted replaced
22002:324997830dc9 22003:5bc7f7b867ab
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.debug;
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.DefaultProbeListener;
32 import com.oracle.truffle.api.source.*;
33
34 /**
35 * An {@link InstrumentationTool} that builds a map of every {@link Probe} attached to some AST,
36 * indexed by {@link Source} and line number.
37 */
38 final class LineToProbesMap extends InstrumentationTool {
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.getSource() != null) {
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 }