1999-01-20 07:59:39 +03:00
|
|
|
#
|
|
|
|
# tracer.rb -
|
|
|
|
# $Release Version: 0.2$
|
|
|
|
# $Revision: 1.8 $
|
|
|
|
# $Date: 1998/05/19 03:42:49 $
|
|
|
|
# by Keiju ISHITSUKA(Nippon Rational Inc.)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# tracer main class
|
|
|
|
#
|
1998-01-16 15:13:05 +03:00
|
|
|
class Tracer
|
1999-01-20 07:59:39 +03:00
|
|
|
@RCS_ID='-$Id: tracer.rb,v 1.8 1998/05/19 03:42:49 keiju Exp keiju $-'
|
|
|
|
|
2000-10-11 10:29:16 +04:00
|
|
|
@stdout = STDOUT
|
2001-10-29 08:07:26 +03:00
|
|
|
@verbose = false
|
1999-01-20 07:59:39 +03:00
|
|
|
class << self
|
2000-07-10 08:49:24 +04:00
|
|
|
attr :verbose, true
|
1999-01-20 07:59:39 +03:00
|
|
|
alias verbose? verbose
|
2000-10-11 10:29:16 +04:00
|
|
|
attr :stdout, true
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
MY_FILE_NAME = caller(0)[0].scan(/^(.*):[0-9]+$/)[0][0]
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
EVENT_SYMBOL = {
|
|
|
|
"line" => "-",
|
|
|
|
"call" => ">",
|
|
|
|
"return" => "<",
|
|
|
|
"class" => "C",
|
2000-12-22 06:22:25 +03:00
|
|
|
"end" => "E",
|
|
|
|
"c-call" => ">",
|
|
|
|
"c-return" => "<",
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def initialize
|
|
|
|
@threads = Hash.new
|
|
|
|
if defined? Thread.main
|
|
|
|
@threads[Thread.main.id] = 0
|
|
|
|
else
|
|
|
|
@threads[Thread.current.id] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
@get_line_procs = {}
|
|
|
|
|
|
|
|
@filters = []
|
|
|
|
end
|
|
|
|
|
2000-10-11 10:29:16 +04:00
|
|
|
def stdout
|
|
|
|
Tracer.stdout
|
|
|
|
end
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
def on
|
2000-06-28 12:31:35 +04:00
|
|
|
if block_given?
|
1999-01-20 07:59:39 +03:00
|
|
|
on
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
off
|
|
|
|
end
|
|
|
|
else
|
2000-12-22 06:22:25 +03:00
|
|
|
set_trace_func proc{|event, file, line, id, binding, klass, *rest|
|
|
|
|
trace_func event, file, line, id, binding, klass
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-10-11 10:29:16 +04:00
|
|
|
stdout.print "Trace on\n" if Tracer.verbose?
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def off
|
|
|
|
set_trace_func nil
|
2000-10-11 10:29:16 +04:00
|
|
|
stdout.print "Trace off\n" if Tracer.verbose?
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
def add_filter(p = proc)
|
|
|
|
@filters.push p
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_get_line_procs(file, p = proc)
|
|
|
|
@get_line_procs[file] = p
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_line(file, line)
|
1999-01-20 07:59:39 +03:00
|
|
|
if p = @get_line_procs[file]
|
|
|
|
return p.call line
|
|
|
|
end
|
|
|
|
|
1999-10-15 12:52:18 +04:00
|
|
|
unless list = LINES__[file]
|
1999-01-20 07:59:39 +03:00
|
|
|
begin
|
|
|
|
f = open(file)
|
|
|
|
begin
|
1999-10-15 12:52:18 +04:00
|
|
|
LINES__[file] = list = f.readlines
|
1999-01-20 07:59:39 +03:00
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
rescue
|
1999-10-15 12:52:18 +04:00
|
|
|
LINES__[file] = list = []
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
if l = list[line - 1]
|
|
|
|
l
|
|
|
|
else
|
|
|
|
"-\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_thread_no
|
|
|
|
if no = @threads[Thread.current.id]
|
|
|
|
no
|
|
|
|
else
|
|
|
|
@threads[Thread.current.id] = @threads.size
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2000-12-22 06:22:25 +03:00
|
|
|
def trace_func(event, file, line, id, binding, klass)
|
1999-01-20 07:59:39 +03:00
|
|
|
return if file == MY_FILE_NAME
|
|
|
|
|
|
|
|
for p in @filters
|
2000-12-22 06:22:25 +03:00
|
|
|
return unless p.call event, file, line, id, binding, klass
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-10 08:49:24 +04:00
|
|
|
Thread.critical = true
|
2000-12-22 06:22:25 +03:00
|
|
|
stdout.printf("#%d:%s:%d:%s:%s: %s",
|
|
|
|
get_thread_no,
|
|
|
|
file,
|
|
|
|
line,
|
|
|
|
klass || '',
|
|
|
|
EVENT_SYMBOL[event],
|
|
|
|
get_line(file, line))
|
2000-07-10 08:49:24 +04:00
|
|
|
Thread.critical = false
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
Single = new
|
|
|
|
def Tracer.on
|
2000-06-28 12:31:35 +04:00
|
|
|
if block_given?
|
1999-01-20 07:59:39 +03:00
|
|
|
Single.on{yield}
|
|
|
|
else
|
|
|
|
Single.on
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def Tracer.off
|
|
|
|
Single.off
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def Tracer.set_get_line_procs(file_name, p = proc)
|
|
|
|
Single.set_get_line_procs(file_name, p)
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
def Tracer.add_filter(p = proc)
|
|
|
|
Single.add_filter(p)
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
2001-10-29 08:07:26 +03:00
|
|
|
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
|
1999-10-15 12:52:18 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if caller(0).size == 1
|
|
|
|
if $0 == Tracer::MY_FILE_NAME
|
|
|
|
# direct call
|
|
|
|
|
|
|
|
$0 = ARGV[0]
|
|
|
|
ARGV.shift
|
|
|
|
Tracer.on
|
|
|
|
require $0
|
|
|
|
else
|
|
|
|
Tracer.on
|
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|