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
|
2002-11-07 22:18:16 +03:00
|
|
|
@threads[Thread.main.object_id] = 0
|
1999-01-20 07:59:39 +03:00
|
|
|
else
|
2002-11-07 22:18:16 +03:00
|
|
|
@threads[Thread.current.object_id] = 0
|
1999-01-20 07:59:39 +03:00
|
|
|
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
|
2003-06-16 11:14:50 +04:00
|
|
|
set_trace_func method(:trace_func).to_proc
|
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]
|
2002-06-28 18:42:46 +04:00
|
|
|
return p.call(line)
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
|
|
|
|
2001-12-21 12:23:28 +03:00
|
|
|
unless list = SCRIPT_LINES__[file]
|
1999-01-20 07:59:39 +03:00
|
|
|
begin
|
|
|
|
f = open(file)
|
|
|
|
begin
|
2001-12-21 12:23:28 +03:00
|
|
|
SCRIPT_LINES__[file] = list = f.readlines
|
1999-01-20 07:59:39 +03:00
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
rescue
|
2001-12-21 12:23:28 +03:00
|
|
|
SCRIPT_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
|
2002-11-07 22:18:16 +03:00
|
|
|
if no = @threads[Thread.current.object_id]
|
1999-01-20 07:59:39 +03:00
|
|
|
no
|
|
|
|
else
|
2002-11-07 22:18:16 +03:00
|
|
|
@threads[Thread.current.object_id] = @threads.size
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
|
2002-10-02 20:45:35 +04: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
|
|
|
|
2003-03-19 17:15:07 +03:00
|
|
|
saved_crit = Thread.critical
|
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))
|
2003-03-19 17:15:07 +03:00
|
|
|
Thread.critical = saved_crit
|
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
|