1999-01-19 23:59:39 -05: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 07:13:05 -05:00
|
|
|
class Tracer
|
1999-01-19 23:59:39 -05:00
|
|
|
@RCS_ID='-$Id: tracer.rb,v 1.8 1998/05/19 03:42:49 keiju Exp keiju $-'
|
|
|
|
|
2000-10-11 02:29:16 -04:00
|
|
|
@stdout = STDOUT
|
2001-10-29 00:07:26 -05:00
|
|
|
@verbose = false
|
1999-01-19 23:59:39 -05:00
|
|
|
class << self
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_accessor :verbose
|
1999-01-19 23:59:39 -05:00
|
|
|
alias verbose? verbose
|
2006-07-20 13:36:36 -04:00
|
|
|
attr_accessor :stdout
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
EVENT_SYMBOL = {
|
|
|
|
"line" => "-",
|
|
|
|
"call" => ">",
|
|
|
|
"return" => "<",
|
|
|
|
"class" => "C",
|
2000-12-21 22:22:25 -05:00
|
|
|
"end" => "E",
|
|
|
|
"c-call" => ">",
|
|
|
|
"c-return" => "<",
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def initialize
|
|
|
|
@threads = Hash.new
|
|
|
|
if defined? Thread.main
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.main.object_id] = 0
|
1999-01-19 23:59:39 -05:00
|
|
|
else
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.current.object_id] = 0
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
@get_line_procs = {}
|
|
|
|
|
|
|
|
@filters = []
|
|
|
|
end
|
|
|
|
|
2000-10-11 02:29:16 -04:00
|
|
|
def stdout
|
|
|
|
Tracer.stdout
|
|
|
|
end
|
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
def on
|
2000-06-28 04:31:35 -04:00
|
|
|
if block_given?
|
1999-01-19 23:59:39 -05:00
|
|
|
on
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
off
|
|
|
|
end
|
|
|
|
else
|
2003-06-16 03:14:50 -04:00
|
|
|
set_trace_func method(:trace_func).to_proc
|
2000-10-11 02:29:16 -04:00
|
|
|
stdout.print "Trace on\n" if Tracer.verbose?
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def off
|
|
|
|
set_trace_func nil
|
2000-10-11 02:29:16 -04:00
|
|
|
stdout.print "Trace off\n" if Tracer.verbose?
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05: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 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_line(file, line)
|
1999-01-19 23:59:39 -05:00
|
|
|
if p = @get_line_procs[file]
|
2002-06-28 10:42:46 -04:00
|
|
|
return p.call(line)
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
2001-12-21 04:23:28 -05:00
|
|
|
unless list = SCRIPT_LINES__[file]
|
1999-01-19 23:59:39 -05:00
|
|
|
begin
|
|
|
|
f = open(file)
|
|
|
|
begin
|
2001-12-21 04:23:28 -05:00
|
|
|
SCRIPT_LINES__[file] = list = f.readlines
|
1999-01-19 23:59:39 -05:00
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
rescue
|
2001-12-21 04:23:28 -05:00
|
|
|
SCRIPT_LINES__[file] = list = []
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
end
|
2004-10-20 02:53:42 -04:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if l = list[line - 1]
|
|
|
|
l
|
|
|
|
else
|
|
|
|
"-\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_thread_no
|
2002-11-07 14:18:16 -05:00
|
|
|
if no = @threads[Thread.current.object_id]
|
1999-01-19 23:59:39 -05:00
|
|
|
no
|
|
|
|
else
|
2002-11-07 14:18:16 -05:00
|
|
|
@threads[Thread.current.object_id] = @threads.size
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2002-10-02 12:45:35 -04:00
|
|
|
def trace_func(event, file, line, id, binding, klass, *)
|
2006-02-17 10:40:32 -05:00
|
|
|
return if file == __FILE__
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
for p in @filters
|
2000-12-21 22:22:25 -05:00
|
|
|
return unless p.call event, file, line, id, binding, klass
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-03-19 09:15:07 -05:00
|
|
|
saved_crit = Thread.critical
|
2000-07-10 00:49:24 -04:00
|
|
|
Thread.critical = true
|
2000-12-21 22:22:25 -05: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 09:15:07 -05:00
|
|
|
Thread.critical = saved_crit
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Single = new
|
|
|
|
def Tracer.on
|
2000-06-28 04:31:35 -04:00
|
|
|
if block_given?
|
1999-01-19 23:59:39 -05:00
|
|
|
Single.on{yield}
|
|
|
|
else
|
|
|
|
Single.on
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def Tracer.off
|
|
|
|
Single.off
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def Tracer.set_get_line_procs(file_name, p = proc)
|
|
|
|
Single.set_get_line_procs(file_name, p)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
def Tracer.add_filter(p = proc)
|
|
|
|
Single.add_filter(p)
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
|
|
|
|
2001-10-29 00:07:26 -05:00
|
|
|
SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
|
1999-10-15 04:52:18 -04:00
|
|
|
|
2006-02-17 10:40:32 -05:00
|
|
|
if $0 == __FILE__
|
|
|
|
# direct call
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2006-02-17 10:40:32 -05:00
|
|
|
$0 = ARGV[0]
|
|
|
|
ARGV.shift
|
|
|
|
Tracer.on
|
|
|
|
require $0
|
2006-02-18 10:38:56 -05:00
|
|
|
elsif caller(0).size == 1
|
2006-02-17 10:40:32 -05:00
|
|
|
Tracer.on
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|