program behavior differs when tracing function is set

T

Timur Shipilov

Hi,

I've been working on simple ruby debugger that is using
Kernel.set_trace_func method. In my work I've found that it is possible
to write a program that behave different depending on tracing function
is set or not.

Here is an example application:
at_exit {
puts 'program terminated'
}
Thread.abort_on_exception = true
$stdout.sync = true
$stderr.sync = true
begin
Thread.new {
raise Exception, 'Kill them all!!!'
}
sleep 5
rescue SystemExit
printf("Exception caught\n")
end
set_trace_func nil

The output is:
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:19:
Kill them all!!! (Exception)
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:in
`initialize'
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:in
`new'
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18
Exception caught
program terminated

But if we add code setting dummy tracing function, we will find that
sleep is not interrupted and rescue block is not called:

at_exit {
puts 'program terminated'
}
Thread.abort_on_exception = true
$stdout.sync = true
$stderr.sync = true

def trace(event, file, line, id, binding, klass)
a = 1
end
set_trace_func proc { |event, file, line, id, binding, klass|
trace(event, file, line, id, binding, klass)
}

begin
Thread.new {
raise Exception, 'Kill them all!!!'
}
sleep 5
rescue SystemExit
printf("Exception caught\n")
end
set_trace_func nil

The output is:
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:18:
Kill them all!!! (Exception)
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17:in
`initialize'
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17:in
`new'
from
C:/Work/Xored/runtime-EclipseApplication/Test/AbortOnException.rb:17
program terminated


I want somebody to comment what is the big difference?

Thanks,
Timur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top