Method: TracePoint.new

Defined in:
vm_trace.c

.new(*args) ⇒ Object

TracePoint.new(*events) { |obj| block } -> obj

Returns a new TracePoint object, not enabled by default.

Next, in order to activate the trace, you must use TracePoint.enable

trace = TracePoint.new(:call) do |tp|

p [tp.lineno, tp.defined_class, tp.method_id, tp.event]

end #=> #<TracePoint:disabled>

trace.enable #=> false

puts “Hello, TracePoint!” # … # [48, IRB::Notifier::AbstractNotifier, :printf, :call] # …

When you want to deactivate the trace, you must use TracePoint.disable

trace.disable

See TracePoint@Events for possible events and more information.

A block must be given, otherwise a ThreadError is raised.

If the trace method isn’t included in the given events filter, a RuntimeError is raised.

TracePoint.trace(:line) do |tp|

p tp.raised_exception

end #=> RuntimeError: ‘raised_exception’ not supported by this event

If the trace method is called outside block, a RuntimeError is raised.

TracePoint.trace(:line) do |tp|
  $tp = tp
end
$tp.line #=> access from outside (RuntimeError)

Access from other threads is also forbidden.



1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
# File 'vm_trace.c', line 1234

static VALUE
tracepoint_new_s(int argc, VALUE *argv, VALUE self)
{
    rb_event_flag_t events = 0;
    int i;

    if (argc > 0) {
	for (i=0; i<argc; i++) {
	    events |= symbol2event_flag(argv[i]);
	}
    }
    else {
	events = RUBY_EVENT_TRACEPOINT_ALL;
    }

    if (!rb_block_given_p()) {
	rb_raise(rb_eThreadError, "must be called with a block");
    }

    return tracepoint_new(self, 0, events, 0, 0, rb_block_proc());
}