Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 57 additions & 84 deletions core/src/main/java/org/jruby/ext/tracepoint/TracePoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ public static void createTracePointClass(ThreadContext context, RubyClass Object

public TracePoint(Ruby runtime, RubyClass klass) {
super(runtime, klass);

this.eventName = "";
this.file = "";
this.line = -1;
this.name = "";
this.type = runtime.getNil();
this.exception = runtime.getNil();
this.returnValue = runtime.getNil();
}

@JRubyMethod(rest = true, visibility = Visibility.PRIVATE)
Expand Down Expand Up @@ -139,34 +131,33 @@ public void event(ThreadContext context, RubyEvent event, String file, int line,

int savedCallInfo = ThreadContext.resetCallInfo(context);

synchronized (this) {
inside = true;

if (type == null) type = context.fals;

IRubyObject binding;
if (event == RubyEvent.THREAD_BEGIN || event == RubyEvent.THREAD_END) {
binding = context.nil;
} else {
binding = RubyBinding.newBinding(context.runtime, context.currentBinding());
}

context.preTrace();

final IRubyObject errorInfo = context.getErrorInfo();
update(event.getName(), file, line, name, type, errorInfo, value == null ? context.nil : value, binding);

// MRI clears $! while a hook runs and restores it after (unless the hook raises)
context.setErrorInfo(context.nil);
try {
block.yieldSpecific(context, TracePoint.this);
context.setErrorInfo(errorInfo);
} finally {
update(null, null, line, null, context.nil, context.nil, context.nil, context.nil);
context.postTrace();
inside = false;
context.callInfo = savedCallInfo;
}
if (type == null) type = context.fals;

IRubyObject binding;
if (event == RubyEvent.THREAD_BEGIN || event == RubyEvent.THREAD_END) {
binding = context.nil;
} else {
binding = RubyBinding.newBinding(context.runtime, context.currentBinding());
}

context.preTrace();

final IRubyObject errorInfo = context.getErrorInfo();
// The event is kept on the thread, not on this TracePoint, so the hook can run on several threads at
// once. A hook that stops, like a debugger waiting for a command, does not hold up other threads.
Event previous = context.getTraceEvent();
context.setTraceEvent(new Event(event.getName(), file, line, name, type, errorInfo,
value == null ? context.nil : value, binding));

// MRI clears $! while a hook runs and restores it after (unless the hook raises)
context.setErrorInfo(context.nil);
try {
block.yieldSpecific(context, TracePoint.this);
context.setErrorInfo(errorInfo);
} finally {
context.setTraceEvent(previous);
context.postTrace();
context.callInfo = savedCallInfo;
}
}

Expand All @@ -188,16 +179,12 @@ public EnumSet<RubyEvent> eventSet() {

@JRubyMethod
public IRubyObject binding(ThreadContext context) {
checkInside(context);

return binding == null ? context.nil : binding;
return currentEvent(context).binding();
}

@JRubyMethod
public IRubyObject defined_class(ThreadContext context) {
checkInside(context);

return type;
return currentEvent(context).type();
}

@JRubyMethod
Expand Down Expand Up @@ -346,69 +333,62 @@ public IRubyObject enabled_p(ThreadContext context) {

@JRubyMethod
public IRubyObject event(ThreadContext context) {
checkInside(context);

return eventName == null ? context.nil : asSymbol(context, eventName);
return asSymbol(context, currentEvent(context).eventName());
}

@JRubyMethod
public IRubyObject inspect(ThreadContext context) {
return inside ? // TODO: event-specific inspect output
newString(context, "#<TracePoint:" + eventName + ">") :
Event event = context.getTraceEvent();
return event != null ? // TODO: event-specific inspect output
newString(context, "#<TracePoint:" + event.eventName() + ">") :
newString(context, "#<TracePoint:" + (enabled ? "enabled" : "disabled") + ">");
}

@JRubyMethod
public IRubyObject lineno(ThreadContext context) {
checkInside(context);

return asFixnum(context, line);
return asFixnum(context, currentEvent(context).line());
}

@JRubyMethod
public IRubyObject method_id(ThreadContext context) {
checkInside(context);

String name = currentEvent(context).name();
return name == null ? context.nil : asSymbol(context, name);
}

@JRubyMethod
public IRubyObject callee_id(ThreadContext context) {
checkInside(context);

// TODO: actually get called name, requires modifying trace handling in bindings
String name = currentEvent(context).name();
return name == null ? context.nil : asSymbol(context, name);
}

@JRubyMethod
public IRubyObject path(ThreadContext context) {
checkInside(context);

String file = currentEvent(context).file();
return file == null ? context.nil : newString(context, file);
}

@JRubyMethod
public IRubyObject raised_exception(ThreadContext context) {
checkInside(context);

return exception;
return currentEvent(context).exception();
}

@JRubyMethod
public IRubyObject return_value(ThreadContext context) {
checkInside(context);
Event event = currentEvent(context);

// FIXME: :c_return events do not pass the return value through yet, so it is always nil for them
switch (eventName) {
switch (event.eventName()) {
case "return", "b_return", "c_return" -> {}
default -> throw runtimeError(context, "not supported by this event");
}

return returnValue;
return event.returnValue();
}

@JRubyMethod
public IRubyObject self(ThreadContext context) {
IRubyObject binding = currentEvent(context).binding();
return binding.isNil() ? context.nil : ((RubyBinding)binding).getBinding().getSelf();
}

Expand All @@ -428,17 +408,6 @@ public static IRubyObject stat(ThreadContext context, IRubyObject self) {
newArray(context, asFixnum(context, stats[0]), asFixnum(context, stats[1])));
}

private void update(String eventName, String file, int line, String name, IRubyObject type, IRubyObject exception, IRubyObject returnValue, IRubyObject binding) {
this.eventName = eventName;
this.file = file;
this.line = line;
this.name = name;
this.type = type;
this.exception = exception;
this.returnValue = returnValue;
this.binding = binding;
}

private synchronized IRubyObject doToggle(ThreadContext context, RubyThread _targetThread, Block block, boolean toggle) {
if (block.isGiven()) {
boolean old = enabled;
Expand Down Expand Up @@ -474,10 +443,23 @@ public void updateEnabled(ThreadContext context, RubyThread targetThread, boolea
}
}

private void checkInside(ThreadContext context) throws RaiseException {
if (!inside) throw runtimeError(context, "access from outside");
/**
* The event of the hook running on this thread. Like CRuby, which keeps it per execution context, this does not
* check which TracePoint the hook belongs to.
*/
@CRuby(value = "get_trace_arg", file = "vm_trace.c")
private static Event currentEvent(ThreadContext context) throws RaiseException {
Event event = context.getTraceEvent();
if (event == null) throw runtimeError(context, "access from outside");
return event;
}

/**
* The data of an event, kept on the thread while a TracePoint hook runs for it.
*/
public record Event(String eventName, String file, int line, String name, IRubyObject type,
IRubyObject exception, IRubyObject returnValue, IRubyObject binding) {}

private static JavaSites.TracePointSites sites(ThreadContext context) {
return context.sites.TracePoint;
}
Expand Down Expand Up @@ -519,13 +501,4 @@ boolean covers(RubyEvent event, String file, int line, String name) {
private TracePointEventHook hook;
private volatile Target target;
private volatile boolean enabled = false;
private String eventName;
private String file;
private int line;
private String name;
private IRubyObject type;
private IRubyObject exception;
private IRubyObject returnValue;
private IRubyObject binding;
private volatile boolean inside = false;
}
13 changes: 13 additions & 0 deletions core/src/main/java/org/jruby/runtime/ThreadContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.jruby.ast.executable.RuntimeCache;
import org.jruby.exceptions.Unrescuable;
import org.jruby.ext.fiber.ThreadFiber;
import org.jruby.ext.tracepoint.TracePoint;
import org.jruby.internal.runtime.methods.DynamicMethod;
import org.jruby.ir.JIT;
import org.jruby.parser.StaticScope;
Expand Down Expand Up @@ -122,6 +123,7 @@ public static ThreadContext newContext(Ruby runtime) {

// Is this thread currently with in a function trace?
private boolean isWithinTrace;
private TracePoint.Event traceEvent;

public final TraceEventManager traceEvents;

Expand Down Expand Up @@ -1215,6 +1217,17 @@ public void setWithinTrace(boolean isWithinTrace) {
this.isWithinTrace = isWithinTrace;
}

/**
* The event of the TracePoint hook running on this thread, or null outside of a hook.
*/
public TracePoint.Event getTraceEvent() {
return traceEvent;
}

public void setTraceEvent(TracePoint.Event traceEvent) {
this.traceEvent = traceEvent;
}

/**
* Return a binding representing the current call's state
* @return the current binding
Expand Down
29 changes: 29 additions & 0 deletions spec/ruby/core/tracepoint/enable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@
trace.should_not.enabled?
end
end

it 'runs the hook on another thread while the hook is running on this thread' do
main = Thread.current
go = Queue.new
thread = Thread.new { go.pop; TracePointSpec.test }
joined = other = main_event = nil
trace = TracePoint.new(:call) do |tp|
if Thread.current == main && tp.method_id == :foo
go << true
joined = thread.join(5)
main_event = [tp.event, tp.method_id]
elsif Thread.current == thread && tp.method_id == :test
other = [tp.event, tp.method_id]
end
end

trace.enable
begin
TracePointSpec::B.new.foo
ensure
trace.disable
go << true
thread.join
end

joined.should == thread
other.should == [:call, :test]
main_event.should == [:call, :foo]
end
end

describe 'with a block' do
Expand Down
1 change: 1 addition & 0 deletions spec/tags/ruby/core/tracepoint/enable_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ debug:TracePoint#enable target_line: option traces :line events only on specifie
debug:TracePoint#enable target_line: option raises ArgumentError if :target_line value is out of target code lines range
debug:TracePoint#enable target_line: option raises ArgumentError if :target_line value is negative
debug:TracePoint#enable target_line: option accepts value that could be coerced to Integer
debug:TracePoint#enable without a block runs the hook on another thread while the hook is running on this thread
Loading