Skip to content

Commit

Permalink
Sharing the client initialization code among both options
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Aug 17, 2024
1 parent 63d1560 commit 5927a41
Showing 1 changed file with 30 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.oracle.truffle.api.instrumentation.EventContext;
import com.oracle.truffle.api.instrumentation.ExecutionEventNode;
import com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
Expand Down Expand Up @@ -54,46 +53,42 @@ public final class ReplDebuggerInstrument extends TruffleInstrument {
*/
@Override
protected void onCreate(Env env) {
SourceSectionFilter filter = null;
ExecutionEventNodeFactory factory = null;
DebuggerMessageHandler handler = null;

if (env.getOptions().get(ENABLE_OPTION)) {
SourceSectionFilter filter =
SourceSectionFilter.newBuilder().tagIs(DebuggerTags.AlwaysHalt.class).build();
var h = new DebuggerMessageHandler();

DebuggerMessageHandler handler = new DebuggerMessageHandler();
try {
MessageEndpoint client = env.startServer(URI.create(DebugServerInfo.URI), handler);
if (client != null) {
handler.setClient(client);
Instrumenter instrumenter = env.getInstrumenter();
instrumenter.attachExecutionEventFactory(
filter,
ctx ->
ctx.getInstrumentedNode() instanceof DebugBreakpointNode
? new ReplExecutionEventNodeImpl(
false, ctx, handler, env.getLogger(ReplExecutionEventNodeImpl.class))
: null);
} else {
env.getLogger(ReplDebuggerInstrument.class)
.warning("ReplDebuggerInstrument was initialized, " + "but no client connected");
}
} catch (MessageTransport.VetoException e) {
env.getLogger(ReplDebuggerInstrument.class)
.warning(
"ReplDebuggerInstrument was initialized, "
+ "but client connection has been vetoed");
} catch (IOException e) {
throw new RuntimeException(e);
}
factory =
ctx ->
ctx.getInstrumentedNode() instanceof DebugBreakpointNode
? new ReplExecutionEventNodeImpl(
false, ctx, h, env.getLogger(ReplExecutionEventNodeImpl.class))
: null;

filter = SourceSectionFilter.newBuilder().tagIs(DebuggerTags.AlwaysHalt.class).build();
handler = h;
}
var replAtMethodName = env.getOptions().get(FN_OPTION);
if (!"".equals(replAtMethodName)) {
DebuggerMessageHandler handler = new DebuggerMessageHandler();
if (env.getOptions().get(FN_OPTION) instanceof String replMethodName
&& !replMethodName.isEmpty()) {
handler = new DebuggerMessageHandler();

factory = new AtTheEndOfMethod(handler, env);

filter =
SourceSectionFilter.newBuilder()
.tagIs(StandardTags.RootBodyTag.class)
.rootNameIs(replMethodName::equals)
.build();
}

if (handler != null && factory != null && filter != null) {
try {
MessageEndpoint client = env.startServer(URI.create(DebugServerInfo.URI), handler);
if (client != null) {
handler.setClient(client);
Instrumenter instrumenter = env.getInstrumenter();
var replAtMethod = new AtTheEndOfMethod(handler, env, instrumenter);
replAtMethod.activate(replAtMethodName);
env.getInstrumenter().attachExecutionEventFactory(filter, factory);
} else {
env.getLogger(ReplDebuggerInstrument.class)
.warning("ReplDebuggerInstrument was initialized, " + "but no client connected");
Expand Down Expand Up @@ -305,24 +300,14 @@ CallerInfo getLastScope() {
}

private final class AtTheEndOfMethod implements ExecutionEventNodeFactory {
private final Instrumenter instr;
private final DebuggerMessageHandler handler;
private final TruffleInstrument.Env env;

AtTheEndOfMethod(DebuggerMessageHandler h, TruffleInstrument.Env env, Instrumenter instr) {
this.instr = instr;
AtTheEndOfMethod(DebuggerMessageHandler h, TruffleInstrument.Env env) {
this.handler = h;
this.env = env;
}

final void activate(String methodName) {
var b = SourceSectionFilter.newBuilder();
b.tagIs(StandardTags.RootBodyTag.class);
b.rootNameIs(methodName::equals);
var anyMethod = b.build();
instr.attachExecutionEventFactory(anyMethod, this);
}

@Override
public ExecutionEventNode create(EventContext ctx) {
var log = env.getLogger(ReplExecutionEventNodeImpl.class);
Expand Down

0 comments on commit 5927a41

Please sign in to comment.